<?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 (get_magic_quotes_gpc()) {
    $_POST = array_map('stripslashes_deep', $_POST);
    $_GET = array_map('stripslashes_deep', $_GET);
    $_COOKIE = array_map('stripslashes_deep', $_COOKIE);
}

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

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($_GET["catid"]) and $_GET["catid"] != "") {
    $catid = $_GET["catid"];
} else if (isset($_POST["catid"]) and $_POST["catid"] != "") {
    $catid = $_POST["catid"];
}



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['btnFeaturedSelected'])){
            $error=0;
            $productlist=$_POST["chkproducts"];
            $totaldisplayed=$_POST["totaldisplayed"];

			/* update all the list as non featured in current page*/
            $listedproductids=$_POST['listedproductids'];
            $sql = "update ".$tableprefix."products set featured='N' 
					WHERE product_id in($listedproductids)
					 AND featured != 'P'";
			$result = mysql_query($sql);

            /* featured items in other pages*/
            $qry="select * from  ".$tableprefix."products  WHERE featured='Y'  ";
            $excqry=mysql_query($qry);
			$totalnumberoffeatureditem=mysql_num_rows($excqry);

            $allowedfeaturedproduct_inthispage=6-$totalnumberoffeatureditem;
            if(count($productlist) > $allowedfeaturedproduct_inthispage){
                $featuredids=$_POST['featuredids'];
                //make already fetured as featured
                $sql = "update ".$tableprefix."products set featured='Y' WHERE product_id in($featuredids)";
			    $result = mysql_query($sql);
                $message .= "* You can add maximum of six items as featured! <br>";
                $error=1;
            }else{
			        if(isNotNull($productlist)){

				    $plist = implode(",",$productlist );
					$sql = "update ".$tableprefix."products set featured='Y' WHERE product_id in($plist)";
					$result = mysql_query($sql);
					}

			}







			/*$location=$_SESSION["gbackurl"]."&error=$error";
			header("location:$location");
			exit;*/

}

if($_GET['error']=="1"){
  $message .= "* You can add maximum of six items as featured! <br>";
}else if($_GET['error']=="0"){
  $message .= "* Selected item(s) successfully updated to featured! <br>";
}


if(isset($_POST['gotoPage']))
{

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


$qryopt="";
if($txtSearch != ""){
   if($cmbSearchType == "artistname"){
            $qryopt .= "  AND  s.artist_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  p.date_added like '" . addslashes($dt) . "%'";
   }else if($cmbSearchType == "productname"){
            $qryopt .= "  AND  p.product_name like '" . addslashes($txtSearch) . "%'";
   }
}
$sql = " SELECT p.*, s.artist_name FROM ".$tableprefix."products p INNER JOIN ".$tableprefix."artists s ON p.product_artist_id = s.artist_id 
		 WHERE 1=1 " . $qryopt . "   ORDER BY p.product_name  ";

	if(!(isset($pagenum)))
	{
		$pagenum = 1;
	} 
	
	$limit = getSettingsValue('npagination_limit');
	
	$page = isset($_GET['page'])? trim($_GET['page']):1;

	
	$page = $_GET['page'];
	
	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 language="JavaScript" type="text/JavaScript">
function clickSearch()
{
	document.frmCatalog.submit();
}
function confirmFeatured()
{

	/*var checked = false;

	for(var i=0; i < document.frmCatalog1.chkproducts[].length; i++)
	{
		if(document.frmCatalog1.chkproducts[i].checked)		
		{
			checked = true;
		}
		
	}
	
	if(checked == false)
	{
		alert('Please select atleast one product');
	}
	else
	{*/	

		if(confirm("Are you sure you want to change the selected records as featured/non-featured?") ){
			return true;
		}
		return false;
	//}	
}


function makeFeatured(val)
{

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

		return confirm('Are You Sure You Want To Make The Product Featured !!')
	
	}
	else if(val == 'Y')
	{
	
		return confirm('Are You Sure You Want To Make The Product Non Featured !!')
	
	}	





}



</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">Featured Listing</div>
					<div align="center" class="text_information grey_tr"><?php echo $message?></div>
            		<br>
											
					<div align="left">																			

					<form name="frmCatalog" action="<?php echo $_SERVER["PHP_SELF"]?>" method="POST" enctype="multipart/form-data">
					<input type="hidden" name="catid" value="<?php echo htmlentities($catid);?>" >
					
					<table width="100%" border="0" cellpadding="0" cellspacing="0" class="manage_top">

					<tr>
                                            <td width="100%" align="left" valign="middle" class="">Search ::
							<select name="cmbSearchType" class="textbox">
								<option value="productname"  <?php if($cmbSearchType == "productname" || $cmbSearchType == ""){ echo("selected"); } ?>>Product Name</option>
								<option value="artistname"  <?php if($cmbSearchType == "artistname"){ echo("selected"); } ?>>Seller Name</option>
								<option value="dateadded"  <?php if($cmbSearchType == "dateadded"){ echo("selected"); } ?>>Date Added (mm/dd/yyyy)</option>
							</select> &nbsp;
							<input type="text" name="txtSearch" size="20" maxlength="50" value="<?php echo(htmlentities($txtSearch)); ?>"  onKeyPress="if(window.event.keyCode == '13'){ return false; }" class="textbox">
							&nbsp;
							<a href="javascript:clickSearch();" class="search_button"><?php echo BTN_SEARCH; ?></a>&nbsp;
						</td>
					</tr>
					
				</table>
				
				</form>
				
				
				
				<form action="<?php echo $_SERVER["PHP_SELF"]?>" method="post" name="frmCatalog1">
				
				<table border="0" width="100%" class="manage_table" cellpadding="0" cellspacing="0"> 
				
				<tr>
					<th width="4%">&nbsp;</th>
					<th width="21%">Product Name</th>
					<th width="18%">Product Code</th>
					<th width="15%" >Seller</th>
					<th width="21%">Added on(mm/dd/yyyy)</th>
					<th width="21%" >Featured</th>
				</tr>
<?php
				
				$listedproductids="";
				$listcounter=0;
				$featuredids="0,";
				
			if(mysql_num_rows($result) > 0 )
			{
				$i = 1;
			
				while($row = mysql_fetch_array($rs))
				{
					$link = "<a href='viewdetailproduct.php?productid=".$row["product_id"]."' class='links'>";

					$fetured="No";
					$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'>Featured Request Pending</span>";						
					}
					$check = "<input class='checkbox' type='checkbox' name='chkproducts[]' value='" . $row["product_id"] . "' class='smalllink' $checkboxselected> ";
?>
					<tr valign=top>
						<td valign="top"><?php echo $check; ?></td>
						<td valign="top" align='left'><?php echo $link .htmlentities($row["product_name"])."</a>";?></td>
						<td valign="top" align='left'><?php echo $link .htmlentities($row["product_code"])."</a>";?></td>
						<td valign="top" align='left'><?php echo $link .htmlentities($row["artist_name"])."</a>";?></td>
						<td valign="top"><?php echo $link .dateFormat($row["date_added"], "Y-m-d", "m/d/Y")."</a>";?></td>
						<td valign="top" align='left'><a href="featureditem.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>&nbsp;</td>
					</tr>
<?php
					$i++;
					$listcounter++;
				}
				
			}
			else
			{
			
?>

				<tr>
						<td align="center" valign="middle" colspan="6"  class="text_information">No Records Found !!</td>
				</tr>	
										
<?php			
				
			}
			
			$featuredids=substr($featuredids,0,-1 );
?>
		   <input type=hidden name=totaldisplayed value="<?php echo $listcounter?>">
		   <input type=hidden name=featuredids value="<?php echo $featuredids?>">


																																		
<?php
if(isNotNull($check))
{
			  $listedproductids=substr($listedproductids,0,-1 );
?>
			<tr>
				<input type="hidden" name="listedproductids" value="<?php echo $listedproductids?>">
				<td colspan="6" align="center">
					<input type="submit" class="button" name="btnFeaturedSelected" value="Make Selected item featured/non-featured" onClick="return confirmFeatured();" >
				</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 align="center" colspan="6">
												<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:1px; ">
																<?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.frmCatalog1.submit();" class="go_button_2"><?php echo GO; ?></a>															
														</td>
														<td width="41%" align="center"><?php echo $next?></td>
													</tr>
												</table>												
										     </td>
										</tr>
										</table>
										
					  </form>
										
																	

						</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";
?>