<?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
$currency_attributes = displayCurrencyType();
/*----------Multicart 2.0 Upgradation Starts------------*/


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

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

	if($enable_seller_friendly == "Y")
{
	$select_artist_id = "SELECT artist_id,artist_name FROM ".$tableprefix."artists WHERE artist_name = ".GetSQLValueString($artistid,"text");

	$result_artist_id = mysql_query($select_artist_id) or die(mysql_error());

	if(mysql_num_rows($result_artist_id) > 0)
	{

		$artist_row = mysql_fetch_assoc($result_artist_id);
		$artistid = $artist_row['artist_id'];
		$artist_name = $artist_row['artist_name'];


	}
}

else
{

	$select_artist_id = "SELECT artist_id,artist_name FROM ".$tableprefix."artists WHERE artist_id = ".GetSQLValueString($artistid,"text");

	$result_artist_id = mysql_query($select_artist_id) or die(mysql_error());

	if(mysql_num_rows($result_artist_id) > 0)
	{

		$artist_row = mysql_fetch_assoc($result_artist_id);
		$artistid = $artist_row['artist_id'];
		$artist_name = $artist_row['artist_name'];


	}

}


	if($catid!='')
	{

				 $sql_seller_route_cat="SELECT route,category_id from ".$tableprefix."categories cat  where cat.route REGEXP '^".$catid.",|,^".$rowcategories['category_id']."|,".$catid.",|,".$catid."$|^".$catid."$'  ";
				 $rs_seller_route_cat=mysql_query($sql_seller_route_cat) or die(mysql_error());

				 if(mysql_num_rows($rs_seller_route_cat)>0)
				 {
						while($rows_seller_categores=mysql_fetch_array($rs_seller_route_cat))
						{

							$seller_products_category[]=$rows_seller_categores['category_id'];

						}
				 }


				 if(count($seller_products_category)>0)
				 {
						 $seller_products_category_list	= @implode("','",$seller_products_category);

					     $current_product_category		= @end($seller_products_category);
				 }


	 }
// show the friendly url  based on config settings
if($enable_seller_friendly == "Y")
{
        // if (function_exists('session_unregister'))
	  //(isset($_SESSION['SESS_seller_name']))?session_unregister('SESS_seller_name'):session_register('SESS_seller_name');
	$_SESSION['SESS_seller_name'] = $artist_name;
}
/*----------Multicart 2.0 Upgradation Ends------------*/
	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"];
	}
	$cmbSearchType==($_GET["cmbSearchType"] != "")?$_GET["cmbSearchType"]:$_POST["cmbSearchType"];
	$qryopt="";
	if(($catid!=''))
	{
		$qryopt .= " AND p.product_category IN  ('".$seller_products_category_list."') ";
	}
	if($txtSearch != ""){
	  $qryopt = "  AND  (p.product_name like '%" . addslashes($txtSearch) . "%' || p.product_description like '%" . addslashes($txtSearch) . "%') ";
	}
        $productListingFee = getSettingsValue('product_listing_fee');
	if(CheckGiftCertificateCategory($_GET['catid'])=='Y')
	{
		$sql="SELECT p.product_id,p.product_name,po.product_option_id,po.image_small,po.image_big,po.product_stock,(p.product_price + po.additional_price) AS product_price,po.discount,s.artist_name,s.storename,cat.category_name,
                    (SELECT pm.vimage_name FROM ".$tableprefix."product_view_images pm where pm.nimage_productid=p.product_id LIMIT 0,1)AS smallimage, AVG(r.nrating_id) as rating
                FROM ".$tableprefix."products p
		INNER JOIN ".$tableprefix."product_options po ON po.product_id = p.product_id AND po.default_option='YES'
                INNER JOIN ".$tableprefix."giftcoupon_master gm oN gm.ngift_product_id=p.product_id
		INNER JOIN ".$tableprefix."artists s on p.product_artist_id=s.artist_id
                INNER JOIN ".$tableprefix."categories cat on p.product_category = cat.category_id LEFT JOIN ".$tableprefix."record_ratings r ON p.product_id = r.nrseller_id
		Where p.vapproved = 'Y' AND s.vapproved='Y' AND gm.vgift_delete_status = 'N' AND  p.deleted !='Y'";
if($productListingFee > 0){
                 $sql .= " AND p.payment_status='COMPLETED' ";
            }
            $sql .= " AND s.plan_expired='N' ";
	}
	else
	{
                $sql = "SELECT p.product_id,p.product_name,po.product_option_id,po.image_small,po.image_big,po.product_stock,(p.product_price + po.additional_price) AS product_price,po.discount,s.artist_name,s.storename,
                    (SELECT pm.vimage_name FROM ".$tableprefix."product_view_images pm where pm.nimage_productid=p.product_id LIMIT 0,1)AS smallimage, AVG(r.nrating_id) as rating
                FROM ".$tableprefix."products p
                INNER JOIN ".$tableprefix."product_options po ON po.product_id = p.product_id AND po.default_option='YES'
                INNER JOIN ".$tableprefix."artists s ON p.product_artist_id = s.artist_id LEFT JOIN ".$tableprefix."record_ratings r ON p.product_id = r.nrseller_id
                WHERE p.product_artist_id='".$artistid."' AND  p.vapproved = 'Y' AND s.vapproved='Y'
                    AND s.plan_expired='N'
";
if($productListingFee > 0){
                 $sql .= " AND p.payment_status='COMPLETED' ";
            }

            if(isset($_REQUEST['sel_order_by']))
            {
            	//die("Here I'm");
            	if($_REQUEST['sel_order_by']=="rating desc"){
            		$order = "GROUP BY p.product_id ORDER BY AVG(r.nrating_id) DESC ";
            	}
            	else if($_REQUEST['sel_order_by']=="vendor desc"){
            		$order = "GROUP BY p.product_id ORDER BY p.product_artist_id DESC ";
            	}
            	else if($_REQUEST['sel_order_by']=="vendor asc"){
            		$order = "GROUP BY p.product_id ORDER BY p.product_artist_id ";
            	}
            	else if($_REQUEST['sel_order_by']=="price desc"){
            		$order = "GROUP BY p.product_id ORDER BY product_price DESC ";
            	}
            	else if($_REQUEST['sel_order_by']=="price asc"){
            		$order = "GROUP BY p.product_id ORDER BY product_price ";
            	}
            	else {
            		$order = "GROUP BY p.product_id  ORDER BY p.product_name ";
            	}
            }
            else {
            	$order = "GROUP BY p.product_id  ORDER BY p.product_name ";
            }

$sql .= " AND p.deleted !='Y' ".$qryopt .$order;
	}


	if(!(isset($pagenum)))
	{
		$pagenum = 1;
	}
	$result=mysql_query($sql) or die(mysql_error());
	$totalrows = mysql_num_rows($result);
        if($txtNumCols>0 && $txtNumRows>0)
            {
                $limit = $txtNumCols*$txtNumRows;
            }
        else
            {
                $limit=getPaginationLimit();
        }
	//$limit=getPaginationLimit();
	if($limit==0)
	{
		$limit=12;
	}
	$sql_qr=dopaging($sql,'',$limit);

	$result=mysql_query($sql_qr) or die(mysql_error());

	$prodimagedir = "products/";



?>
<form name="frmCatalog" action="<?php echo SITE_URL?>/showportfolio.php?artistid=<?php echo $artistid?>" method="POST" enctype="multipart/form-data">
			<div align="left" class="prdct_search_container">
				<input type="hidden" name="catid" value="<?php echo $catid;?>">
					<div align="right">
					<span align="left" id="items_top_area_ttl_3" class="left"><?php echo PRODUCTS_TEXT;?></span>
					 &nbsp;<span align="right" id="items_top_area_ttl_4" class="f_right_marg"><?php echo SORT_BY_TEXT;?></span>
					 &nbsp;<select onchange="clickSearch()" class="topsearch_fld f_right_marg" name="sel_order_by" id="sel_order_by">
					<option value=""><?php echo SELECT_ORDER_TEXT;?></option>
					<option value="price asc" <?php if($_REQUEST['sel_order_by']=="price asc"){ echo "selected";}?>><?php echo PRICE_ORDER_LOW_HIGH_TEXT;?></option>
					<option value="price desc" <?php if($_REQUEST['sel_order_by']=="price desc"){ echo "selected";}?>><?php echo PRICE_ORDER_HIGH_LOW_TEXT;?></option>
					<!-- <option value="vendor asc" <?php if($_REQUEST['sel_order_by']=="vendor asc"){ echo "selected";}?>><?php echo VENDOR_ORDER_ASC_TEXT;?></option>
					<option value="vendor desc" <?php if($_REQUEST['sel_order_by']=="vendor desc"){ echo "selected";}?>><?php echo VENDOR_ORDER_DESC_TEXT;?></option> -->
					<option value="rating desc" <?php if($_REQUEST['sel_order_by']=="rating desc"){ echo "selected";}?>><?php echo RATING_ORDER_HIGH_LOW_TEXT;?></option>
					</select>
							&nbsp;<input type="text" class="textarea" name="txtSearch" maxlength="50" value="<?php echo(($txtSearch)); ?>"   size="40">
							<input type="button" name="btnSearch" id="btnSearch" onClick="javascript:clickSearch();" class="button" value="<?php echo SEARCH_BUTTON;?>">

				</div>


			</div>

<?php
if(mysql_num_rows($result) > 0 )
	{

	?>

<div class="brdcrumbnbtn_container">

<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tr><td>
<div class="brdcrumb_container">
<?php if($_GET['catid']=='') { echo ALL_PRODUCTS_TEXT;} else {   echo stripslashes(getSellerProfileLink("showportfolio.php",$artistid,GetSQLValueString($current_product_category,"text"))); } ?>
 </div> </td>
</tr>
</table>
</div>

<?php

	if($_GET['catid']!='')
	{
		$subCount	= checkSubCategoryCount($_GET['catid'])		;

     }

?>


<?php

                if($txtNumCols>0)
                    {
                        $cells = $txtNumCols;
                        $count_row = 1;
                }
                else
                    {
                        $cells =4;
                        $count_row = 1;
                }

?>
			<div class='featuredproduct_list'>

<?php
	while($row = mysql_fetch_array($result))
		{

                // Image Selection Modification // Done By Asha
            $imageurl='';
            if($row["image_small"] !="") {
                $product_image = $row["image_small"];

            }  // end if product image null check
            elseif($row["image_big"] != "") {
                $product_image = $row["image_big"];
            }
            else
                {
                     $product_image=$row["smallimage"];
            }

       if($product_image=="") {
                $imageurl = SITE_URL."/".$prodimagedir."noimage.jpg";
            }
            else {
                if(strstr($product_image, "http://")) {
                    $imageurl = $product_image;
                }
                elseif(!is_file($prodimagedir.$product_image) && !file_exists($prodimagedir.$product_image)) {
                            // $imageurl = $prodimagedir.$product_image;
                            $imageurl = SITE_URL."/".$prodimagedir."noimage.jpg";

                }
                else {
                    $imageurl = SITE_URL."/".$prodimagedir.$product_image;
                }
            }

             $imageurl = "<img class='product_img' src='".$imageurl."' border='0' >";

          // End Image Display Modification

			if(checkProductStock($row["product_id"]) > 0)
				{
				$productinstock = true;
			}
			else
			{
				$productinstock = false;
			}

			if(!empty($product_image))
			{ // Rename Image Thumb;
				$imagename=$product_image;
		    }  // end thumb creation

			if(strlen($row["product_name"])>30)
			{
							  $prd_name = stripslashes(substr($row["product_name"],0,30));
			}
			else
			{
				 $prd_name = stripslashes($row["product_name"]);

		     }

			$price=$row['product_price'];

					$discountstr="";

				$currency_attributes = displayCurrencyType();
				$conversionRate	= ConvertedCurrencyRate($currency_attributes[2]);
				$converted_price = $price * $conversionRate;
				$discountprice=0;
				if($row["discount"] !="0"){
					$discountprice=$converted_price- ($converted_price*$row["discount"])/100;
					$discountprice =number_format($discountprice,2,".","");
					$discountstr="<br>discount(".$row["discount"]."%)";

			      }


		        $display_product_price = number_format($converted_price,2,".","");
  $appendStyle = NULL;
if($count_row==$cells) {
    switch($cells){
        case '4':
            $appendStyle = 'style="margin-right:0px;"';
            break;
        case '3':
            $appendStyle = 'style="margin-right:250px;"';
            break;
        case '2':
            $appendStyle = 'style="margin-right:300px;"';
            break;
        case '1':
            $appendStyle = 'style="margin-right:500px;"';
            break;
    }
}
?>
<!--Display the product details-->

<div class="products" <?php if($count_row==$cells) { echo $appendStyle; } ?> >
<a href="<?php echo SITE_URL?>/productdetails.php?productid=<?php echo $row["product_id"]?>"><?php echo $imageurl?></a>

<h5><a href="<?php echo SITE_URL?>/productdetails.php?productid=<?php echo $row["product_id"]?>"><?php echo ucwords(strtolower($prd_name)); ?></a></h5>

<div class="product_price"><p> <?php if($discountprice!=0) { ?> <?php echo  $currency_attributes[0] ?> <?php echo $discountprice;?> (<strike><?php echo  $currency_attributes[0] ?> <?php echo $display_product_price;?> </strike>) <?php }  else { ?> <?php echo  $currency_attributes[0] ?> <?php echo $display_product_price;?> <?php } ?> </p></div>
 <?php $productRating = ProductRatingStar($row["product_id"]); ?>
<div class="rating_<?php echo $productRating; ?>" id="rating_<?php echo $productRating; ?>" style="margin-top:3px;margin-bottom:5px;margin-left:20px;margin-right:50px;"></div>



<?php
if($productinstock)
{
?>

<div class="adtocart_tbn">
<a class="adtocart" href="<?php echo SITE_URL?>/homepage_addtocart.php?productid=<?php echo $row["product_id"]?>&product_option_id=<?php echo $row["product_option_id"]?>"><?php echo ADD_TO_CART_TEXT; ?></a>
<a class="more_btn" href="<?php echo SITE_URL?>/productdetails.php?productid=<?php echo $row["product_id"]?>"><?php echo MORE_TEXT; ?></a>
</div>

<?php }else
	{

	?>
	<div class="adtocart_tbn" align="left"><img src="<?php echo SITE_URL?>/images/home_outfstock.gif"  alt="Out of Stock" border="0"/></div>
	<?php

	}
	?>

<div class="clear"></div>
</div>

<?php

	if($count_row ==$cells) {

			$count_row = 1;
	}
		else {
		$count_row++;
	 }

	}

?>
</div>
<?php
}
else
{

 ?>
 <div class="noproducts" ><?php echo NO_PRODUCT_FOUND_TEXT;?></div>

 <?php }  // end else
 ?>

<input type="hidden" value="<?php echo $featured;?>" name="featured">

<div class="clear"></div>
</div>

<?php if($totalrows>$limit)
{?>
<div  class="pagination"> <?php echo rightpaging();?></div>
<?php  } ?>

</form>

