<?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_artistid"]) and $_SESSION["sess_artistid"]!= ""){
	$artistid = $_SESSION["sess_artistid"];
}else{
	header("Location:login.php");
	exit;
}



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"];
}


/*---------Patch-----------*/


if(isset($_GET['msg']))
{
	$message = $_GET['msg'];
}

/*---------Patch-----------*/

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

	$select_product_id = "SELECT gm.ngift_product_id,prd.product_category 
						  FROM ".$tableprefix."giftcoupon_master gm,".$tableprefix."products prd
						  WHERE gm.ngift_product_id = prd.product_id
						   AND gm.ngift_id = ".GetSQLValueString($_GET['del_id'],"text");
	$result_product_id = mysql_query($select_product_id) or die(mysql_error());
	$product_row_id = mysql_fetch_assoc($result_product_id);

}

if($_GET['delete_permanent'] == 'N')
{
	switch($_GET['delete_status'])
	{
	
		CASE 'N':
			$update_category_count = "UPDATE ".$tableprefix."categories SET count = count - 1
									  WHERE category_id = ".$product_row_id['product_category'];
			mysql_query($update_category_count) or die(mysql_error());

			$delete_gift_product = "UPDATE ".$tableprefix."products SET vapproved = 'Y' WHERE product_id = ".$product_row_id['ngift_product_id'];
			mysql_query($delete_gift_product) or die(mysql_error());
		
			header("Location:gift_coupon_artist.php?page=".$_GET['page']."&txtSearch=".$_GET['txtSearch']."&cmbSearchType=".$cmbSearchType."&msg=".MSG_GIFT_CERT_DISABLE);
			
		break;
		CASE 'Y':
			$delete_gift_product = "UPDATE ".$tableprefix."products SET vapproved = 'N' WHERE product_id = ".$product_row_id['ngift_product_id'];
			mysql_query($delete_gift_product) or die(mysql_error());

			$update_category_count = "UPDATE ".$tableprefix."categories SET count = count + 1
									  WHERE category_id = ".$product_row_id['product_category'];
			mysql_query($update_category_count) or die(mysql_error());			
			
			header("Location:gift_coupon_artist.php?page=".$_GET['page']."&txtSearch=".$_GET['txtSearch']."&cmbSearchType=".$cmbSearchType ."&msg=".MSG_GIFT_CERT_ENABLE);
			
		break;	
	
	}
	
}	
else if($_GET['delete_permanent'] == 'Y')
{

	$select_gift_coupon = "SELECT po.image_small,p.product_category
						   FROM ".$tableprefix."products p
                                                   INNER JOIN ".$tableprefix."product_options po ON po.product_id = p.product_id AND po.default_option = 'YES'
						   WHERE p.product_id = ".$product_row_id['ngift_product_id'];
						   
	$result_gift_coupon = mysql_query($select_gift_coupon) or die(mysql_error());
	
	if(mysql_num_rows($result_gift_coupon) > 0)
	{
		while($coupon_images_row = mysql_fetch_array($result_gift_coupon))	
		{
			unlink("../products/".$coupon_images_row['image_small']);
			
			$gift_category = $coupon_images_row['product_category'];
		}
	}
	
	
	$delete_coupon = "DELETE FROM ".$tableprefix."giftcoupon_master WHERE ngift_id = ".$_GET['del_id'];
	mysql_query($delete_coupon) or die(mysql_error());
	
	$delete_product_option = "DELETE FROM ".$tableprefix."product_options WHERE product_id = ".$product_row_id['ngift_product_id'];
	mysql_query($delete_product_option) or die(mysql_error());

        $delete_product = "DELETE FROM ".$tableprefix."products WHERE product_id = ".$product_row_id['ngift_product_id'];
	mysql_query($delete_product) or die(mysql_error());
	
	$update_category_count = "UPDATE ".$tableprefix."categories SET count = count - 1
							   WHERE category_id = ".$gift_category;
				   
	$result_category_count = mysql_query($update_category_count) or die(mysql_error());
	
	header('Location:gift_coupon_artist.php?msg='.MSG_GIFT_CERT_DELETE);

}

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

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


$qryopt="";

$txtSearch = addslashes($txtSearch);
if($txtSearch != "")
{
   if($cmbSearchType == "couponname")
   {
            $qryopt .= "  AND pr.product_name  like '" .$txtSearch. "%'";
   }
   if($cmbSearchType == "couponamount")
   {
   		$qryopt .= "  AND pr.product_price = '".$txtSearch."'";
   }
}
  $sql = "SELECT gcm.*,po.image_small,pr.product_name,pr.product_price,pr.vapproved as vapproved,
                CASE WHEN gcm.vgift_delete_status = 'N' THEN 'Yes' ELSE 'No' END AS vgift_deleted,
		CASE WHEN pr.vapproved  = 'N' THEN 'Not Approved' ELSE 'Approved' END AS approval_status
		FROM ".$tableprefix."giftcoupon_master gcm
                INNER JOIN ".$tableprefix."products pr ON pr.product_id = gcm.ngift_product_id
                INNER JOIN ".$tableprefix."product_options po ON po.product_id = pr.product_id AND po.default_option = 'YES'
		WHERE pr.product_artist_id ='".addslashes($artistid)."'"
		 . $qryopt . " GROUP BY gcm.ngift_product_id ORDER BY ngift_id DESC ";
		 
	 
	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()
{

  var form_name = new Array('txtSearch');		
  var display_name = new Array('Search Keyword');
  var count = 1;	

  if(check_fields('frmGiftCoupon',form_name,display_name,count))	
  {
  	 document.frmGiftCoupon.submit();	
  }
  else
  {
  		return false;
  }
     
}


function blockCertificate(val)
{
	if(val == 'N')
	{

		return confirm("<?php echo QTN_GIFT_CERT_ENABLE; ?>")
	
	}
	else if(val == 'Y')
	{
	
		return confirm("<?php echo QTN_GIFT_CERT_DISABLE;?>")
	
	}	

}

function deleteCoupon()
{
	return confirm("<?php echo QTN_GIFT_CERT_DELETE;?>")
}

</script>
		<body>
		<center>
		<div align="left" id="layout">
				<!--header area start -->
<?php
	include_once("../includes/topheader_seller.php")
?>
				<!--header area end -->
				<div align="left" class="main_body">
				<!--main body start -->
<?php
		include_once("../includes/artisttopmenu.php")
?>		
					<div align="left" class="content_area">
					<!--content area start -->
					<div align="left" class="page_title"><?php echo TEXT_GIFT_CERT; ?></div>
					<div align="center" class="text_information grey_tr"><?php echo $message?></div>
            		<br>
											
					<div align="left">		
					<form action="" method="get" name="frmGiftCoupon">
						<table width="100%" border="0" cellpadding="0" cellspacing="0" class="manage_top">
							<tr>
								<td width="80%" align="left" valign="baseline" class='toplinksCopy'><?php echo TEXT_SEARCH ; ?>::
									<select name="cmbSearchType" class="textarea">
									<option value="couponname"  <?php if($cmbSearchType == "couponname" || $cmbSearchType == ""){ echo("selected"); } ?>><?php echo TEXT_GIFT_CERT_NAME; ?></option>
									<option value="couponamount"  <?php if($cmbSearchType == "couponamount" || $cmbSearchType == ""){ echo("selected"); } ?>><?php echo TEXT_AMT; ?></option>
									</select> &nbsp;
									<input type="text" name="txtSearch" size="20" maxlength="50" value="<?php echo stripslashes($txtSearch); ?>" class="textarea">
									&nbsp;
									<a style="cursor:pointer" onClick="return clickSearch();" class="go_button"><?php echo BTN_SEARCH; ?></a>
								</td>
								<td align="right"><a href="gift_coupon_add_artist.php" class="add_new_button"><?php echo BTN_ADD_NEW; ?></a></td>
							</tr>
						</table>
							
				</form>
				</div>
				<div align="left">					
				<form action="" method="post" name="frmGiftCoupon1">		
				  <table width="100%" border="0" cellpadding="0" cellspacing="0" class="manage_table">
                    <tr class="form_header">
                      <th width="3%" align="left">#</th>
                      <th width="19%" align="left"><?php echo TEXT_GIFT_CERT_NAME; ?></th>
                      <th width="23%" align="left"><?php echo TEXT_GIFT_CERT_IMG; ?></th>
                      <th width="11%" align="left"><?php echo TEXT_AMT; ?></th>
                      <th width="20%" align="left"><?php echo TEXT_ACTIVE; ?></th>
                      <th width="24%" align="left"><?php echo TEXT_ACTION; ?></th>
                    </tr>
                    <?php
if(mysql_num_rows($result) > 0 )
{
	$i = 1;
	
	while($row = mysql_fetch_array($rs))
	{
		$st=$row["approval_status"];
		if($st=='Approved')
			$apst=TEXT_APPRD;
		else if($st=='Not Approved')
			$apst=TEXT_NOT_APPRD;
			
																													
?>
                    <tr>
                      <td><?php echo $i?></td>
                      <td align="left"><?php echo $link .($row["product_name"])."</a>";?></td>
                      <td align="left"><img height="30" width="100" src="../products/<?php echo $row["image_small"]?>"></td>
                      <td align="left"><?php echo $adminCurrency['currency_symbol'];?> &nbsp; <?php echo number_format($row['product_price'],2,".","");?></td>
                      <td align="left"><?php //echo htmlentities($row["approval_status"]);?><?php echo $apst;?></td>
                      <td width="24%" align="left">
                          <a href="gift_coupon_edit_artist.php?edit_id=<?php echo $row["ngift_id"]?>&edit_product_id=<?php echo $row['ngift_product_id']?>&qstring=<?php echo $_SERVER['QUERY_STRING']?>"><img src="../images/btn_edit.gif" border="0" title="<?php echo TITLE_EDIT; ?>"></a>&nbsp;/&nbsp;<a href="gift_coupon_artist.php?del_id=<?php echo $row["ngift_id"]?>&delete_status=<?php echo $row['vgift_delete_status']?>&delete_permanent=Y" style="cursor:pointer" onClick="return deleteCoupon();"><img src="../images/btn_del.gif" border="0" title="<?php echo TITLE_DELETE; ?>"></a>&nbsp;/&nbsp;
                          <?php

	
	if($row['vapproved'] == "Y")
	{

?>
                              <a href="gift_coupon_artist.php?del_id=<?php echo $row["ngift_id"]?>&delete_status=<?php echo $row['vapproved']?>&delete_permanent=N&page=<?php echo $page?>&txtSearch=<?php echo $txtSearch?>&cmbSearchType=<?php echo $cmbSearchType?>" style="cursor:pointer" onClick="return blockCertificate('<?php echo $row['vapproved']?>')" class="disable_button"><?php echo LINK_DISABLE; ?></a>
                              <?php

	}
	else
	{

?>
                              <a href="gift_coupon_artist.php?del_id=<?php echo $row["ngift_id"]?>&delete_status=<?php echo $row['vapproved']?>&delete_permanent=N&page=<?php echo $page?>&txtSearch=<?php echo $txtSearch?>&cmbSearchType=<?php echo $cmbSearchType?>" style="cursor:pointer" onClick="return blockCertificate('<?php echo $row['vapproved']?>')" class="enable_button"><?php echo LINK_ENABLE; ?></a>
                              <?php
	}

?>
 
                      </td>
                    </tr>
                    <?php
	$i++;
	
	}
											
}
else
{
										
?>
                    <tr>
                      <td align="center" valign="middle" colspan="6"  class="text_information"><?php echo TEXT_NO_RECORD; ?></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'>prev</a>";
								
							}
																											
																				
							if($page < $numofpages)
							{
								$pagenext = ($page + 1);
								
								$next = "<a href=".$_SERVER['PHP_SELF']."?page=$pagenext alt='Next' border='0' class='small_gray_button'>next</a>";
								
							}	


if($page < $numofpages || $page > 1)
{
?>
                    <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">
									 <?php echo TEXT_PAGES;?> <?php echo $page ?>/<?php echo $numofpages?>&nbsp;&nbsp;<?php echo TEXT_GOTO;?>
								</td>
								<td width="8%" align="right">
									<select class="textarea2" 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.frmGiftCoupon1.submit();" class="go_button_2">Go</a>
								</td>
								<td width="41%" align="center"><?php echo $next?></td>
							</tr>
						</table>
                     </td>
                    </tr>
					<?php
					}
					?>
                  </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";
?>
