<?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;
}

if($_POST['postback1'] == "CS"){//for changing status
        $id=$_POST['pid'];
     $changeto=$_POST['changeto'];
     $sqlcs = "UPDATE ".$tableprefix."products SET vapproved = '$changeto' where product_id=$id";
        //echo $sqlcs;
       mysql_query($sqlcs);
		
}

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($_POST['gotoPage']))
{

	header("Location:".$_SERVER['PHP_SELF']."?page=".$_POST['gotoPage']);
}


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 Products are deleted Successfully !!";  
	
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 = " . addslashes($txtSearch);
   }
}

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

$sql = "SELECT prd.product_id,prd.product_name,prd.product_price,prd.vapproved,prd.date_added,cat.category_name
		FROM ".$tableprefix."products prd,".$tableprefix."categories cat 
		WHERE prd.product_category = cat.category_id ".$qryopt;
		 
		
	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.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 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;
		
		alert(status);
        if(status == "N"){
                changeto = "De Acivate";
        }
		 if(status == "Y")
		 {
                changeto = "Approve";
        }
        if(confirm("Are you sure you want to "+ changeto +" this product?")){
                frm.postback1.value="CS";
                frm.pid.value=id;
                frm.changeto.value=status;
                frm.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">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="">Search ::
								<select name="cmbSearchType" class="textbox">
									<option value="product"  <?php if($cmbSearchType == "product" || $cmbSearchType == ""){ echo("selected"); } ?>>Product Name</option>
									<option value="category"  <?php if($cmbSearchType == "category"){ echo("selected"); } ?>>Category</option>
									<option value="price"  <?php if($cmbSearchType == "price"){ echo("selected"); } ?>>Price</option>
									<option value="dateadded"  <?php if($cmbSearchType == "dateadded"){ echo("selected"); } ?>>Added Date(mm/dd/yyyy)</option>
								</select> &nbsp;
								<input type="text" name="txtSearch"  class="textbox" 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="8%">&nbsp;</td>
						</tr>
						<tr>
							<td>Sellers ::</td>
							<td width="70%">
								<select name="txtSeller" onChange="Javascript:document.frmMyProducts.submit();">
								<option value="">--All Sellers--</option>
<?php
if(mysql_num_rows($result_seller) > 0)
{
	while($seller_row = mysql_fetch_array($result_seller))
	{
	
		if($seller_row['artist_id'] == $_POST['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>
							<th width="4%">#</th>
							<th width="16%">Product Name</th>
							<th width="15%">Price</th>
							<th width="13%">Category</th>
							<th width="16%">Date Added</th>
							<th width="16%">Status</th>
							<th colspan="2"><strong>
					      </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 )
{
	$i = 1;
	while($row = mysql_fetch_array($rs))
	{
		$link = "<a class=links href='editproduct.php?productid=".$row["product_id"]."&page=".$_GET['page']."'>";
			
		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 $i?></td>
					<td align="left"><?php echo $link .stripslashes($row["product_name"])."</a>";?></td>
					<td><?php echo $link .htmlentities($row["product_price"])."</a>";?></td>
					<td><?php echo $link .htmlentities($row["category_name"])."</a>";?></td>
					<td align=""><?php echo $link .dateFormat($row["date_added"], "Y-m-d", "m/d/Y")."</a>";?></td>
					<td align="">&nbsp;<?php echo $link1 .$status."</a>";?>
					</td>
												
				    <td width="9%" align="">&nbsp;</td>
				</tr>
<?php
	$i++;
	}
?>	
	
	<input type="hidden" name="checkCount" value="<?php echo $i-1?>">
<?php	

}
else
{
										
?>					
							<tr>
									<td align="center" valign="middle" colspan="8"  class="text_information">No Products Found !!</td>
							</tr>					
<?php										
}

if($page > 1)
{
	$pageprev = $page-1;
	
	$previous_page = "<a href=".$_SERVER['PHP_SELF']."?page=$pageprev alt='Previous' border='0' class='small_gray_button'>".TEXT_PREV."</a>";
	
}
																				
													
if($page < $numofpages)
{
	$pagenext = ($page + 1);
	
	$next = "<a href=".$_SERVER['PHP_SELF']."?page=$pagenext alt='Next' border='0' class='small_gray_button'>".TEXT_NEXT."</a>";
	
}	



?>

								
										<tr>
											<td class="page_nos"  align="center" colspan="8">
											
											<table width="100%" border="0" cellspacing="10" cellpadding="0">
												<tr align="left" valign="middle">
													<td width="21%" align="right"><?php echo $previous_page?></td>
													<td width="23%" align="right">
														Page(s) <?php echo $page ?>/<?php echo $numofpages?>&nbsp;&nbsp;Goto
													</td>
													<td width="8%" align="right">
														<select name="gotoPage" style="padding:4px 5px; margin-bottom:; ">
  <?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>
													</td>
													<td width="7%">
														<a onClick="Javascript:document.frmMyProducts1.submit();" class="go_button_2"><?php echo GO; ?></a>
													</td>
													<td width="41%" align="center"><?php echo $next?></td>
												</tr>
											</table>
										   </td>
										</tr>
										</form>
										<tr>
										<td colspan="8">
										<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";
?>