<?php

include "includes/config.php";
include "includes/session.php";  
include "includes/functions.php";
include_once "classes/cls_products.php";

if (get_magic_quotes_gpc()) {
    $_POST = array_map('stripslashes_deep', $_POST);
    $_GET = array_map('stripslashes_deep', $_GET);
    $_COOKIE = array_map('stripslashes_deep', $_COOKIE);
}

$currency_attributes = displayCurrencyType();
// setting pagination limit From Admin settings

$limit=getPaginationLimit();

if($limit==0)
{
	$limit=10;
}	
//$limit=3;

$page=$_GET['page'];
if(empty($page))
	{
		$page = 1;
	}
//End pagination settings
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"];
}

$giftCategoryFlag=CheckGiftCertificateCategory($catid);
if(!isset($catid) && $catid =="" && $txtSearch=="")
{
        $showproductlisting="false";
        
}
if((isset($catid) && $catid!="")  ||  $txtSearch!="" || $giftCategoryFlag=='Y')
{
        $showproductlisting="true";
        
} 
/*display the active template*/
$active_template = displayTemplate();
include "includes/htmltop.php";
/* Top File Name */
include_once ("includes/".$active_template[0]); 
// Check Category is a Gift Certificate Category 


$sql_chk_category = " SELECT * FROM " . $tableprefix . "categories ORDER BY category_name ";
$cntCattotalrows = mysql_num_rows(mysql_query($sql_chk_category));

?> 
<script language="javascript" type="text/JavaScript"><!--
function popupWindow(url) {
  window.open(url,'popupWindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,copyhistory=no,width=100,height=100,screenX=150,screenY=150,top=150,left=150')
}
function clickSearch()
{
	document.frmCatalog.submit();
}
function confirmDelete(){
	if(confirm('<?php echo CONFIRM_DELETE_RECORDS_TEXT;?>') ){
		return true;
	}
	return false;
}
//-->
</script>
<!--mainbody area start -->
<div class="featuredproduct_list main_body">
    <div class="container">
        <form name="frmCatalog" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"], ENT_QUOTES, 'UTF-8') ?>" method="POST" enctype="multipart/form-data">
            <div class="row">
                <section class="sec_marg">
                    <div class="col-sm-12 col-lg-6 col-md-6 ">
                        <div class="brdcrumb_container">
                            <?php echo stripslashes(getCategoryLinkNew("categorydetail.php", GetSQLValueString($catid, "text"))); ?>
                        </div>
                    </div>
                        <?php if((isset($catid) && $catid!="")){
                            $limit = 20;
                            $sql_category = " SELECT * FROM " . $tableprefix . "categories WHERE parent_id LIKE " . GetSQLValueString($catid, "text") . " ORDER BY category_name ";
                            $sql_qr = dopaging($sql_category, '', $limit);
                            $totalrows = mysql_num_rows(mysql_query($sql_category));
                            //echo $sql_category;
                            $result_category = mysql_query($sql_qr) or die(mysql_error());
                             if (mysql_num_rows($result_category) <= 0) {
                            ?>
                    <div class="col-sm-12 col-lg-6 col-md-6 ">
           
                        <!--- Sort/search by --->
                        <div class="category_searchdiv">
                            <div class="prdct_search_container pull-right">
                                <?php if (($_REQUEST['catid'] != "" || $txtSearch != '')) { ?>
                                    &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>
                                        
                    <?php } ?>
                            </div>
                                
                        </div>
                        <!--- Sort by --->
                    </div>
                    <?php }} ?>
                    <div class="clear"></div>
                </section>
                <div class="clear"></div>
                <section class="sec_marg">
                    <?php if ($_REQUEST['catid'] == '' && $txtSearch == '') { ?>							  
                        <?php include_once("includes/categorylist_catalog.php"); ?>
                    <?php }//end if?>			
                </section>
                <section class="sec_marg">
<?php/*
// $limit=3;   
$limit = 20;
$sql_category = " SELECT * FROM " . $tableprefix . "categories WHERE parent_id LIKE " . GetSQLValueString($catid, "text") . " ORDER BY category_name ";
$sql_qr = dopaging($sql_category, '', $limit);
$totalrows = mysql_num_rows(mysql_query($sql_category));
//echo $sql_category;
$result_category = mysql_query($sql_qr) or die(mysql_error());
*/

?>			
                            
                    <!--center content area start-->		 
                    <input type="hidden" name="catid" value="<?php echo htmlentities($catid); ?>" >
                    <div align="left" id="item_details">
                        <!--items display area start -->
                            
                        <?php
                        if (!isset($catid) || $catid == "") {
                            $catid = 0;
                        }

                        //die(mysql_num_rows($result_category));
                        $showproducts = false;
                        if($cntCattotalrows<=1){?>
                                <div class="col-sm-12 col-lg-12 col-md-12">
                                <div class="noproducts" >No Categories Found.</div>
                                </div>
                        <?php }
                        if (mysql_num_rows($result_category) > 0) {

                            $rowcnt1 = 0; 
                            ?> 
                            <div class="category_listings_new">
                                <!--new design-->
                                    
                                <?php
                                //Category Search
                                while ($row_category = mysql_fetch_array($result_category)) {


                                    $sql_route_catalog = "SELECT route,category_id from " . $tableprefix . "categories cat  where cat.route REGEXP '^" . $row_category['category_id'] . ",|,^" . $row_category['category_id'] . "|," . $row_category['category_id'] . ",|," . $row_category['category_id'] . "$|^" . $row_category['category_id'] . "$'  ";
                                    $rs_route_catalog = mysql_query($sql_route_catalog) or die(mysql_error());

                                    $current_catlist_catalog = array();
                                    if (mysql_num_rows($rs_route_catalog) > 0) {
                                        while ($List_catalog = mysql_fetch_array($rs_route_catalog)) {
                                            $current_catlist_catalog[] = $List_catalog['category_id'];
                                        }
                                    }

                                    if (count($current_catlist_catalog) > 0) {
                                        $categoryString_catalog = @implode("','", $current_catlist_catalog);
                                    } else {
                                        $categoryString_catalog = $current_catlist_catalog;
                                    }

                                    $count_product_catalog = 0;
                                    $productListingFee = getSettingsValue('product_listing_fee');
                                    $whereCondition = "";
                                    $sqlcountProduct    =   "SELECT count(p.product_id) AS pcount FROM " . $tableprefix . "products p INNER JOIN " . $tableprefix . "artists a ON p.product_artist_id = a.artist_id where p.vapproved = 'Y' ";
                                    $whereCondition.=" AND p.deleted='N' AND a.plan_expired ='N' ";  // check deleted status
                                    $whereCondition.=" AND p.publish_status='Y' ";  // check published status
                                    $whereCondition.=" AND p.seller_vacation_mode='N' ";  // check seller vacation ode settings
                                    $whereCondition.=" AND p.vSellerStatus='Y' ";  // check seller approved or plan expiry status
                                    if ($productListingFee > 0) {
                                            $whereCondition .= " AND p.payment_status='COMPLETED' ";  // Check seller payment completed
                                        }

                                     $whereCondition.=  " AND p.product_category IN  ('".$categoryString_catalog."') ";

                                     $sql_catalog   =   $sqlcountProduct . $whereCondition;
	

                                    /*$sql_catalog = "SELECT count(p.product_id) AS pcount
							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 . "categories cat ON  p.product_category=cat.category_id
							INNER JOIN " . $tableprefix . "artists ar ON ar.artist_id =p.product_artist_id 
							WHERE ar.vapproved ='Y' AND ar.deleted='N' AND ar.plan_expired='N' AND p.publish_status='Y AND p.seller_vacation_mode='N'
							AND p.product_category IN  ('" . $categoryString_catalog . "')
							AND p.vapproved = 'Y' ";
                                    if ($productListingFee > 0) {
                                        $sql_catalog .= " AND p.payment_status='COMPLETED' ";
                                    }*/
                                    $sql_catalog .= " AND  p.deleted !='Y'  ";
                                    $result33 = mysql_query($sql_catalog);
                                    //echo $sql_catalog;
                                    if (mysql_num_rows($result33) != 0) {
                                        $rw_catalog = mysql_fetch_array($result33);
                                        $count_product_catalog = $rw_catalog['pcount'];
                                        //	print_r($rw_catalog);//exit();
                                    }


                                    if ($row_category["gift_certificate_flag"] == "Y") {

                                          $sqlcountGiftProduct    =   "SELECT  COUNT(p.product_id) AS gcount FROM " . $tableprefix . "products p INNER JOIN " . $tableprefix . "artists a ON p.product_artist_id = a.artist_id
                                                             INNER JOIN ".$tableprefix."giftcoupon_master gm oN gm.ngift_product_id=p.product_id  where p.vapproved = 'Y' ";
                                        $whereCondition.=" AND p.deleted='N' AND a.plan_expired ='N'";  // check deleted status
                                        $whereCondition.=" AND p.publish_status='Y' ";  // check published status
                                        $whereCondition.=" AND p.seller_vacation_mode='N' ";  // check seller vacation ode settings
                                        $whereCondition.=" AND p.vSellerStatus='Y' ";  // check seller approved or plan expiry status
                                        if ($productListingFee > 0) {
                                                $whereCondition .= " AND p.payment_status='COMPLETED' ";  // Check seller payment completed
                                            }
                                        $whereCondition.=  " AND  gm.vgift_delete_status = 'N' AND p.product_category IN  ('".$row_category['category_id']."') ";

                                        $product_count_sql_catalog     = $sqlcountGiftProduct.$whereCondition;

                                       /* $product_count_sql_catalog = "SELECT COUNT(p.product_id) AS gcount
                                                                                                                      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
                                                                                                                      INNER JOIN " . $tableprefix . "giftcoupon_master gm oN gm.ngift_product_id=p.product_id
                                                                                                                      AND p.product_category IN (" . $row_category['category_id'] . ")
                                                                                                                      AND p.vapproved = 'Y' AND s.vapproved='Y' AND gm.vgift_delete_status = 'N' AND  p.deleted !='Y'";

                                        if ($productListingFee > 0) {
                                            $product_count_sql_catalog .= " AND p.payment_status='COMPLETED' ";
                                        }*/

                                        //echo $product_count_sql;
                                        $product_count_reslt_catalog = mysql_query($product_count_sql_catalog) or die(mysql_error());
                                        if (mysql_num_rows($product_count_reslt_catalog) > 0) {
                                            $count_product_catalog = 0;
                                            while ($product_counts_catalog = mysql_fetch_object($product_count_reslt)) {
                                                // 	print_r($product_count_reslt); exit();
                                                $count_product_catalog = $product_counts_catalog->gcount;
                                            }
                                        }
                                    }
                                    if ($rowcnt1 % 2 == '0') {
                                        $rowcnt1 = '0';
                                    }//end if

                                    if (strlen($row_category["category_name"]) > 30)
                                        $category_name = substr($row_category["category_name"], 0, 30) . "...";
                                    else
                                        $category_name = $row_category["category_name"];
$prodimagedir = 'categories/avatar/';
$avatar = $row_category["avatar"];  
                           
                               
                                if ($avatar == "") { 
                                    $avatarImage = $site_url."/".$prodimagedir . "noimage.jpg";   
                                 } else { 
                                     if(ENABLE_AMAZON_S3_IMAGES=="Y"){ 
                                        $avatarImage = AMAZON_S3_DOMAIN_URL.$prodimagedir.$avatar ;
                                     }else{
                                         $avatarImage = 'categories/avatar/' . $row_category["avatar"];
                                     }
                                       
                                }
                           ?>		
                                    <div class="col-sm-12 col-lg-3 col-md-3">
                                    <div class="cat_box">	
                                        <a class="product_anchor" href="categorydetail.php?catid=<?php echo htmlentities($row_category["category_id"]) ?>">
                                            <div class="imgblock"><img src="<?php echo $avatarImage; ?>" /></div>
                                        </a>
                                        <h5><a href="categorydetail.php?catid=<?php echo htmlentities($row_category["category_id"]) ?>"><?php echo stripslashes($category_name); ?>&nbsp;(<?php echo $count_product_catalog; ?>)</a></h5>
                                    </div>
                                    </div>
                                    <?php
                                    $rowcnt1++;
                                }//end while			Category Search
                                ?>
                                
                                <!--new design-->
                                <div class="clear"></div>
                            </div>
                                <?php
                            }//end if
                            else {$showproducts = true;?>                                

                        <?php } ?>
                </section>
                        <?php if ($totalrows > $limit) {
                            ?>
                    <div  class="pagination"> <?php echo rightpaging(); ?></div>
                        <?php } ?>
                <div class="clear"></div>
                    
                <div class="clear"></div>
                    
                <section class="sec_marg">
                        <?php
                        if ($showproducts) {
                            if ($showproductlisting == "true") { // display products
                                include_once("includes/productlist.php");
                            } else {
                                
                            }
                        } // end if show products
                        ?>
                    <div class="clear"></div>
                </section>
            </div>								
            <!--items display area end -->
        </form>
    </div>
</div>
</div>
<?php
include "includes/".$active_template[2];	/* Bottom File Name */
?>																		