<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
// +----------------------------------------------------------------------+
// | PHP version 4/5                                                      |
// +----------------------------------------------------------------------+
// | This source file is a part of iScripts Multicart                     |
// +----------------------------------------------------------------------+
// | Authors: Programmer<programmer@armia.com>        		              |
// +----------------------------------------------------------------------+
// | Copyrights Armia Systems, Inc and iScripts.com � 2005                |
// | All rights reserved                                                  |
// +----------------------------------------------------------------------+
// | This script may not be distributed, sold, given away for free to     |
// | third party, or used as a part of any internet services such as      |
// | webdesign etc.                                                       |
// +----------------------------------------------------------------------+
?>
<?php
include "../includes/config.php";
include "../includes/session.php";
include "../includes/functions.php";



if(isset( $_SESSION["sess_adminname"]) and $_SESSION["sess_adminname"]!= ""){
	$adminname = $_SESSION["sess_adminname"];
}else{
	header("Location:login.php");
	exit;
}


$txtSeller = $_REQUEST['txtSeller'];
$txtSearch = $_REQUEST['txtSearch'];
$cmbSearchType = $_REQUEST['cmbSearchType'];

if($_POST['postback1'] == "CS"){//for changing status
        $id=$_POST['pid'];
     $changeto=$_POST['changeto'];
     $sqlcs = "UPDATE ".$tableprefix."products SET vapproved = '$changeto' where product_id=$id";
       mysql_query($sqlcs);
	   
// Update category count during approve or 	reject   
	   if($changeto=='Y')
	   {
	   $update_category_count = "UPDATE ".$tableprefix."categories SET count = count + 1 
									  	  WHERE category_id IN (SELECT product_category FROM ".$tableprefix."products where product_id=$id)";
	    
		}
		else
		{
		 $update_category_count = "UPDATE ".$tableprefix."categories SET count = count - 1 
									  	  WHERE category_id IN (SELECT product_category FROM ".$tableprefix."products where product_id=$id)";
		}
				mysql_query($update_category_count) or die(mysql_error());
		
}

if(isset($_GET["txtSearch"]) and $_GET["txtSearch"]!=""){
	$txtSearch = $_GET["txtSearch"];
	$cmbSearchType = $_GET["cmbSearchType"];
}else if(isset($_POST["txtSearch"]) and $_POST["txtSearch"]!=""){
	$txtSearch = $_POST["txtSearch"];
	$cmbSearchType = $_POST["cmbSearchType"];
}

if(isset($_GET['featured_id']))
{

	if($_GET['featured_status'] == "Y")
	{
		$update_product_featured = "UPDATE ".$tableprefix."products 
		                               SET featured = 'N' 
									 WHERE product_id = ".$_GET['featured_id'];			
	}
	else
	{
		$update_product_featured = "UPDATE ".$tableprefix."products SET featured = 'Y' WHERE product_id = ".$_GET['featured_id'];			
	}
	mysql_query($update_product_featured) or die(mysql_error());

}

if(isset($_POST['gotoPage']))
{
	$txtSearch     = $_POST['searchbox'];
	$cmbSearchType = $_POST['searchboxtype'];
	$txtSeller     = $_POST['seller'];
	header("Location:".$_SERVER['PHP_SELF']."?page=".$_POST['gotoPage']."&txtSearch=".$txtSearch."&cmbSearchType=".$cmbSearchType."&txtSeller=".$txtSeller);
}


if(isset($_POST['txtProductId']))
{
		
	$productid_format = substr($_POST['txtProductId'],0,strlen($_POST['txtProductId'])-1);
			
	$select_product_images = "SELECT product_image_small,product_image_big,product_category 
							    FROM ".$tableprefix."products 
							   WHERE product_id 
							      IN(".$productid_format.")";							  
							  
	$result_product_images = mysql_query($select_product_images) or die(mysql_error());
	
	if(mysql_num_rows($result_product_images) > 0)
	{
		while($product_images_row = mysql_fetch_array($result_product_images))
		{
			@unlink("../products/".$product_images_row['product_image_small']);
			@unlink("../products/".$product_images_row['product_image_big']);
			
/*-------Category count update patch------------*/			
			
			
			$selecting_category_route = "SELECT route FROM ".$tableprefix."categories 
										 WHERE category_id = ".$product_images_row['product_category'];
			$result_category_route = mysql_query($selecting_category_route) or die(mysql_error());
									
			if(mysql_num_rows($result_category_route) > 0)
			{
			
				$category_route_row = mysql_fetch_assoc($result_category_route);
				
				$update_category_count = "UPDATE ".$tableprefix."categories SET count = count - 1 
									  	  WHERE category_id IN (".$category_route_row['route'].")";
				  
				mysql_query($update_category_count) or die(mysql_error());
				
			}							 
			
/*-------Category count update patch------------*/					
			
			
		}
	}	
	
	$delete_products = "DELETE FROM ".$tableprefix."products WHERE product_id IN(".$productid_format.")";
	
	mysql_query($delete_products) or die(mysql_error());				
	
	$message = SELECTED_PRO_DELETED." !!";  
	
	header("Location:listProducts.php?page=1");	

}

$select_seller = "SELECT artist_id,artist_name FROM ".$tableprefix."artists WHERE 1";
$result_seller = mysql_query($select_seller) or die(mysql_error());

$qryopt="";

if($txtSearch != "")
{
   if($cmbSearchType == "product")
   {
   		$qryopt .= "  AND  prd.product_name like '" . addslashes($txtSearch) . "%'";
   }
   else if($cmbSearchType == "dateadded")
   {
   		$dt = $txtSearch;
		$arrdt = explode("/",$dt);
		$day = $arrdt[1];
		$month = $arrdt[0];
		$year = $arrdt[2];
		$dt = $year."-". $month ."-".$day;
		$qryopt .= "  AND  prd.date_added like '" . addslashes($dt) . "%'";
   }
   else if($cmbSearchType == "category")
   {
    	$qryopt .= "  AND  cat.category_name like '" . addslashes($txtSearch) . "%'";
   }
   else if($cmbSearchType == "price")
   {
   		$qryopt .= "  AND  (prd.product_price+po.additional_price) = " . addslashes($txtSearch);
   }
}

if(isset($_REQUEST['txtSeller']))
{
	if($_REQUEST['txtSeller'] != "")
	{
		$qryopt .= " AND prd.product_artist_id = ".$_REQUEST['txtSeller'];
	}
	
}

	 $sql = "SELECT prd.product_id,prd.product_name,(prd.product_price+po.additional_price) AS product_price,prd.vapproved,prd.featured,prd.date_added,cat.category_name,po.discount,prd.payment_status 
		FROM ".$tableprefix."products prd
                LEFT JOIN ".$tableprefix."product_options po ON po.product_id = prd.product_id AND po.default_option = 'YES'
                INNER JOIN ".$tableprefix."categories cat ON prd.product_category = cat.category_id
		WHERE  1=1 ".$qryopt." order by prd.date_added  DESC";
		 
		
	if(!(isset($pagenum)))
	{
		$pagenum = 1;
	} 
	
	$limit = getSettingsValue('npagination_limit');
	
	
	$page = isset($_GET['page'])? trim($_GET['page']):1;

	
	if(empty($page))
	{
		$page = 1;
	}
	
	$result=mysql_query($sql) or die(mysql_error());						
	
	$totalrows = mysql_num_rows($result);
	
	$limitvalue = ($page - 1) * $limit;
	
	$query_string = " LIMIT $limitvalue, $limit";
	
	$sql = $sql.$query_string;
	 
	$rs = mysql_query($sql) or die(mysql_error());

	$numofpages = ceil($totalrows / $limit);

include "../includes/htmltop.php";

?>

<script>
function clickSearch()
{
	document.frmMyProducts1.searchbox.value = document.frmMyProducts.txtSearch.value;
	document.frmMyProducts.submit();
}

function addValues(order_value)
{
		
	var checkbox_values = "";	
	
	for(i=1;i<=document.frmMyProducts1.checkCount.value;i++)
	{
	
		var checkbox_name = "Product_"+i;
			
		if(document.getElementById(checkbox_name).checked)
		{
			checkbox_values += document.getElementById(checkbox_name).value + ',';
		}
				
		document.frmMyProducts2.txtProductId.value = checkbox_values;
		
			
	}		
}

function makeFeatured(val)
{

			
	if(val == 'N' || val == 'P')
	{

		return confirm('<?php echo ARE_YOU_SURE_PRO_FEATURED;?> !!')
	
	}
	else if(val == 'Y')
	{
	
		return confirm('<?php echo ARE_YOU_SURE_PRO_NON_FEATURED;?> !!')
	
	}	


}

function checkAllOrders()
{

	var checkbox_values = "";	
	
	if(document.frmMyProducts1.checkAllBox.checked)
	{
				
		for(i=1;i<=document.frmMyProducts1.checkCount.value;i++)
		{		
			var checkbox_name = "Product_"+i;
			document.getElementById(checkbox_name).checked = true;	
			checkbox_values += document.getElementById(checkbox_name).value + ',';					
		}
		
		document.frmMyProducts2.txtProductId.value = checkbox_values;

	}
	else
	{
		for(i=1;i<=document.frmMyProducts1.checkCount.value;i++)
		{
		
			var checkbox_name = "Product_"+i;
			document.getElementById(checkbox_name).checked = false;	
			
		}
		
		document.frmMyProducts2.txtProductId.value = "";
	}


}
function ChangeStatus(id,status){

        var frm = document.frmMyProducts1;
		
        if(status == "N"){
                changeto = "De Acivate";
        }
		 if(status == "Y")
		 {
                changeto = "<?php echo APPROVE;?>";
        }
        if(confirm("<?php echo ARE_YOU_SURE_YOU_WANT_TO;?> "+ changeto +" <?php echo THIS_PRODUCT;?>?")){
                frm.postback1.value="CS";
                frm.pid.value=id;
                frm.changeto.value=status;
                frm.submit();
        }

}

function setValForSearchType(val)
{
	document.frmMyProducts1.searchboxtype.value = val;
}

function setValForSellers(val)
{
	document.frmMyProducts1.seller.value = val;
	document.frmMyProducts.submit();
}


</script>
		
		
		
		<body>
		
		<center>
		
		<div align="left" id="layout">
		
		
				
				
				<!--header area start -->
<?php

	include_once("../includes/topheader_admin.php")

?>
				
				<!--header area end -->
				
				
				
				
				<div align="left" class="main_body">
				<!--main body start -->
				
				
				
<?php
		include_once("../includes/admintopmenu.php")
?>		
																					
								
					<div align="left" class="content_area">
					
					<!--content area start -->
					<div align="left" class="page_title"><?php echo APPROVE_PRODUCTS;?></div>
					<div align="center" class="text_information grey_tr"><?php echo $message?></div>
            		<br>
											
					<div align="left">		
					
					<form action="<?php echo $_SERVER["PHP_SELF"]?>" method="post" name="frmMyProducts">
																										
					<table width="100%" border="0" cellpadding="0" cellspacing="0" class="manage_top">
						<tr>
                                                    <td align="left" valign="middle" colspan="2" class=""><?php echo SEARCH;?> ::
								<select name="cmbSearchType" class="textarea" onChange="javascript:setValForSearchType(this.value);">
								<option value=""><?php echo ALL_PRODUCTS; ?></option>
									<option value="product"  <?php if($cmbSearchType == "product" || $cmbSearchType == ""){ echo("selected"); } ?>><?php echo PRODUCT_NAME;?></option>
									<option value="category"  <?php if($cmbSearchType == "category"){ echo("selected"); } ?>><?php echo CATEGORY;?></option>
									<option value="price"  <?php if($cmbSearchType == "price"){ echo("selected"); } ?>><?php echo PRICE;?></option>
									<option value="dateadded"  <?php if($cmbSearchType == "dateadded"){ echo("selected"); } ?>><?php echo ADDED_DATE;?>(<?php echo MM."/".DD."/".YYYY;?>)</option>
								</select> &nbsp;
								<input type="text" name="txtSearch"  class="textarea" size="20" maxlength="50" value="<?php echo(htmlentities($txtSearch)); ?>">
								&nbsp;<a href="javascript:clickSearch();" class="search_button"><?php echo BTN_SEARCH; ?></a>&nbsp;
							</td>
						</tr>
						<tr>
							<td width="7%">&nbsp;</td>
						</tr>
						<tr>
							<td><?php echo SELLERS;?> ::</td>
							<td width="93%">
								<select class="textarea" name="txtSeller" onChange="javascript:setValForSellers(this.value);">
								<option value="">--<?php echo ALL_SELLERS;?>--</option>
<?php
if(mysql_num_rows($result_seller) > 0)
{
	while($seller_row = mysql_fetch_array($result_seller))
	{
	
		if($seller_row['artist_id'] == $_REQUEST['txtSeller'])
		{
			$select_seller = "selected";
		}
		else
		{
			$select_seller = "";
		}
?>
									<option value="<?php echo $seller_row['artist_id']?>" <?php echo $select_seller?>><?php echo $seller_row['artist_name']?></option>			
<?php
	}
}
?>															
								</select>
							</td>
						</tr>
					</table>
																															

					</form>
					
					</div>
						
					<div align="left">					
					
																																				
					<table border="0" width="100%" cellpadding="0" cellspacing="0" class="manage_table">
					<form action="" method="post" name="frmMyProducts1">
						<tr class="form_header">
							<th width="3%">#</th>
							<th width="19%" align="left"><strong><?php echo PRODUCT_NAME;?></strong></th>
							<th width="9%" align="left"><strong><?php echo PRICE;?></strong></th>
							<th width="9%" align="left"><?php echo DISCOUNT;?>(%)</th>
							<th width="15%" align="left"><strong><?php echo CATEGORY;?></strong></th>
							<th width="16%" align="left"><strong><?php echo DATE_ADDED;?></strong></th>
                                                        <th width="10%" align="left"><?php echo LISTING_FEE;?></th>
							<th width="13%" align="left"><strong><?php echo STATUS;?></strong></th>
							<th colspan="2" align="left"><strong><?php echo FEATURED;?>
			              </strong></th>
						</tr>
					<input name="postback1" type="hidden" id="postback1">
					<input name="pid" type="hidden" id="pid">
					<input name="changeto" type="hidden" id="changeto">	
						
<?php
if(mysql_num_rows($result) > 0 )
{
	$j = 1;
	while($row = mysql_fetch_array($rs))
	{
		$link = "<a class=links href='editproduct.php?productid=".$row["product_id"]."&page=".$_GET['page']."'>";
		$fetured=NO1;
					$listedproductids .=$row["product_id"].",";
					$checkboxselected="";
					if($row["featured"]=="Y")
					{
					  $featuredids=$featuredids.$row["product_id"].",";
					  $fetured="<font color=#E87E4C>".YES."</font>";
					  $checkboxselected="checked";
					}
					else if($row["featured"]=="P")
					{
						$fetured="<span class='text_information'>".FEATURE_REQ_PEND."</span>";						
					}
                                        switch (htmlentities($row["payment_status"])){
                                    	case 'PENDING':$st2=strtoupper(PENDING);
                                    	break;
                                    	case 'COMPLETED':$st2=strtoupper(COMPLETED);
                                    	break;
                                    	}
			
		if($row['vapproved']=="N")
		{ // check if product approved or not 
		
			$status=NOT_APPROVED;
		} // end if 
		
		if($row['vapproved']=="Y")
		{ // check if product approved or not 
		
			$status=APPROVED;
		} // end if 
						
		if($row["vapproved"] == "Y")
			
			{
				//$statusimg = "cross.gif";
				$changestatuslink = "ChangeStatus(".$row["product_id"].",'N');";
		   }
			else
				{
					//$statusimg = "tick.gif";
				$changestatuslink = "ChangeStatus(".$row["product_id"].",'Y');";
			}
															
	 $link1 = "<a style = 'cursor:pointer' href=\"#\" class='smalllink' onclick=\"$changestatuslink\">";

				
?>
				<tr> 
					<td><?php echo (($page*$limit)+$j-$limit)?></td>
					<td align="left"><?php echo stripslashes($row["product_name"]);?></td>
					<td><?php echo $adminCurrency['currency_symbol'];?> <?php echo htmlentities($row["product_price"]);?></td>
					<td><?php echo htmlentities($row["discount"]);?></td>
					<td><?php echo ($row["category_name"]);?></td>
					<td align=""><?php echo dateFormat($row["date_added"], "Y-m-d", "m/d/Y");?></td>
                                        <td><?php echo $link . $st2. "</a>"; ?></td>
					<td align="">&nbsp;<?php echo $link1 .$status."</a>";?></td>
				    <td colspan="2">&nbsp;<a href="product_approve.php?featured_id=<?php echo $row["product_id"]?>&featured_status=<?php echo $row["featured"]?>" onClick="return makeFeatured('<?php echo $row["featured"]?>')"><?php echo $fetured ?></a></td>
				</tr>
<?php
	$j++;
	}
?>	
	
	<input type="hidden" name="checkCount" value="<?php echo $j-1?>">
<?php	

}
else
{
										
?>					
							<tr>
									<td align="center" valign="middle" colspan="9"  class="text_information"><?php echo NO_PRODUCT_FOUND_TEXT;?> !!</td>
							</tr>					
<?php										
}

if($page > 1)
{
	$pageprev = $page-1;
	
	$previous_page = "<a href=".$_SERVER['PHP_SELF']."?txtSearch=".$txtSearch."&txtSeller=".$txtSeller."&cmbSearchType=".$cmbSearchType."&rghtMenu=".$_GET["rghtMenu"]."&page=$pageprev alt='Previous' border='0'><img src='../images/previous_btn_admin.gif' alt='Next' border='0'/></a>";
	
}
																				
													
if($page < $numofpages)
{
	$pagenext = ($page + 1);
	
	$next = "<a href=".$_SERVER['PHP_SELF']."?txtSearch=".$txtSearch."&txtSeller=".$txtSeller."&cmbSearchType=".$cmbSearchType."&rghtMenu=".$_GET["rghtMenu"]."&page=$pagenext alt='Next' border='0'><img src='../images/next_btn_admin.gif' alt='Next' border='0'/></a>";
	
}	



?>

								
										<tr>
										  <td class="page_nos"  align="center" colspan="2">&nbsp;</td>
										  <td align="right" valign="middle" class="page_nos" colspan="6">&nbsp;</td>
										  <td width="10%" align="left" valign="middle" class="btn_gotbl">&nbsp;</td>
					  </tr>							  <?php
							  if($page < $numofpages || $page > 1)
							  {
							  ?>
										<tr>
											<td class="page_nos"  align="center" colspan="2"><?php echo $previous_page?>&nbsp;<?php echo $next?></td>
											<td align="right" valign="middle" class="page_nos" colspan="4"><?php echo PAGE."(".S.")";?><?php echo $page ?>/<?php echo $numofpages;?>&nbsp;&nbsp;<?php echo GO_TO;?></td>
											<td colspan="3" align="center" valign="middle" class="page_nos"><span class="btn_gotbl">
											  <select class="textarea2" name="gotoPage">
                                                <?php
									for($i=1;$i<=$numofpages;$i++)
									{		
										
										
										if($i == $_GET['page'])
										{
											$page_selected = "selected";
										}
										else
										{
											$page_selected = "";
										}
										
										
?>
                                                <option value="<?php echo $i?>" <?php echo $page_selected?>><?php echo $i?></option>
                                                <?php
									}
									
?>
                                              </select>
                                              <a onClick="Javascript:document.frmMyProducts1.submit();" style="cursor:pointer"><img src="../images/go_btn.gif" alt=""  /></a> </span>										  </td>
										</tr><?php
										}
										?>
										<input type="hidden" name="searchbox" id="searchbox" value="<?php echo $_REQUEST['txtSearch']; ?>">
										<input type="hidden" name="searchboxtype" id="searchboxtype" value="<?php echo $_REQUEST['cmbSearchType']; ?>">
										<input type="hidden" name="seller" id="seller" value="<?php echo $_REQUEST['txtSeller']; ?>">
					  </form>
										<tr>
										<td colspan="9">
										<form name="frmMyProducts2" method="post" action="#">
										<input type="hidden" name="txtProductId">
										</form>
										</td>
										</tr>
					  </table>
								
					  </div>
						
						
				<!--content area end-->
								
				</div>
				
								
					<div align="left" class="clear_float"><img src="<?php echo $imagefolder;?>/clear.gif" alt="" width="1" height="1" /></div>
				<!--main body end -->
				</div>
				
				
<?php
	include "../includes/footer.php";
?>