<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
// +----------------------------------------------------------------------+
// | PHP version 4/5                                                      |
// +----------------------------------------------------------------------+
// | This source file is a part of iScripts Multicart                     |
// +----------------------------------------------------------------------+
// | Authors: Santhosh Thomas Alexander<santhosh.thomas@armiasystems.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
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"];
}

$txtCategoryName = trim($_POST["txtCategoryName"]);
$txtProductName = $_POST["txtProductName"];
$txtDescription = $_POST["txtDescription"];
$txtPrice = $_POST["txtPrice"];
$ddlArtist =$_POST["ddlArtist"];
$productlist = $_POST["chkproducts"];


$artistlist = makeArtistList();

$productListingFee = getSettingsValue('product_listing_fee');

if(CheckGiftCertificateCategory($_GET['catid'])=='Y') {
    $sql="SELECT p.product_id,p.product_name,(p.product_price + po.additional_price) AS product_price,po.image_small,po.image_big,po.product_option_id,po.product_stock,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."giftcoupon_master gm oN gm.ngift_product_id=p.product_id
                      LEFT 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.vapproved='Y' AND p.deleted ='N'";
    if($productListingFee > 0) {
        $sql .= " AND p.payment_status='COMPLETED' ";
    }
    $sql .= "AND s.vapproved='Y' AND s.plan_expired='N' ";
}

else {
    $sql="SELECT p.product_id,p.product_name,(p.product_price + po.additional_price) AS product_price,po.image_small,po.image_big,po.product_option_id,po.product_stock,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'
                      LEFT 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.vapproved='Y' AND p.deleted ='N' " ;
    if($productListingFee > 0) {
        $sql .= " AND p.payment_status='COMPLETED' ";
    }
    $sql .= "
                AND s.vapproved='Y' AND s.plan_expired='N' ";
}

$qrystring= " ";

if($txtSearch != "" || $catid!='') { // check for searching

    $qrystring.=" AND ";

    if(!empty($catid)) { // check category Id is empty or not

        $qrystring.=" p.product_category =".GetSQLValueString($catid,"text")." AND ";

    } //

    if($txtSearch!='') {
        $qrystring .= ' ( p.product_name like "%' . addslashes($txtSearch) . '%" || p.product_description like "%' . addslashes($txtSearch) . '%") AND  ';
    }

} // end if search 

$query1=rtrim($qrystring, " AND");	

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 s.artist_name DESC ";
	}
	else if($_REQUEST['sel_order_by']=="vendor asc"){
		$order = "GROUP BY p.product_id ORDER BY s.artist_name ASC";
	}
	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 ASC";
	}
	else {
		$order = "GROUP BY p.product_id ";
	}
}
else {
	$order = "GROUP BY p.product_id ";
}

$query=$sql.$query1.$order;

if(!(isset($pagenum))) {
    $pagenum = 1;
} 

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

$totalrows = mysql_num_rows($result);

$limit=12;
//$limit=3;

$sql_qr=dopaging($query,'',$limit);	

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

$prodimagedir = "products/";

if($totalrows > 0 ) {
    $i=1;
    $cells =4;
    $count_row = 1;

    while($row = mysql_fetch_array($result)) {
       // echo "<pre>";print_r($row);echo "</pre>";
        // 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 = $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 = $prodimagedir."noimage.jpg";
                }
                else {
                    $imageurl = $site_url."/".$prodimagedir.$product_image;
                }
            }
        
             $imageurl = "<img class='product_img' src='".$imageurl."' border='0' >";
        
        if(checkProductStock($row["product_id"]) > 0) {
            $productinstock = true;
        }
        else {
            $productinstock = false;
        }
        

        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,".","");        
        ?>
<div class="products" <?php if($count_row==4) {?> style="margin-right:0px;" <?php } ?>>
    <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"];?>" title="<?php echo $prd_name; ?>">
                        <?php
                        if(strlen($prd_name)>27) {
                            echo substr(ucwords(strtolower($prd_name)),0,27)."...";
                        } else {
                            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:0;margin-right:18px; float:right;"></div>
     <?php
    if ($enable_seller_friendly == "Y") {
                                    $sellerLink = (!empty($row['storename'])) ? SITE_URL . "/stores/" . $row['storename'] : SITE_URL . "/store/" . $row['artist_name'];

                                    } else {
                                   $sellerLink = (!empty($row['storename'])) ? SITE_URL."/showportfolio.php?storename=" . $row['storename'] : SITE_URL."/showportfolio.php?artistid=" . $row['artist_id'];

                                    }
                                    ?>
<div class="artist_name"><a href="<?php echo $sellerLink; ?>" title="<?php echo $row['artist_name'];?>"><b><?php echo $row['artist_name'];?></b></a></div>
 
    
    <div width="64%" align="left" class="txt_align" valign="middle"> <?php $productRating = ProductRatingStar($row["product_id"]); ?></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"><span class="out_stock">Out of Stock</span></div>
                <?php

            } 
            ?>
    <div class="clear"></div>
</div>
        <?php
        if($count_row ==$cells) {

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

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

}
?>