<?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.                                                       |
// +----------------------------------------------------------------------+
$currency_selected = "";


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

    //activateCurrencyType($_POST['currency_type']);
    $currency_type = $_POST['currency_type'];

    $_SESSION['SESS_currency_type'] = $currency_type;

    echo "<script type='text/javascript'> window.location='" . $_SERVER['HTTP_REFERER'] . "';</script>";
} else {

    if (!isset($_SESSION['SESS_currency_type']) OR $_SESSION['SESS_currency_type'] == "") {

        $select_currency_id = "SELECT ncurrency_id FROM " . $tableprefix . "currency_master WHERE vactive = 'Y'";
        $result_currency_id = mysql_query($select_currency_id) or die(mysql_error());
        $currency_rowid = mysql_fetch_assoc($result_currency_id);
        $_SESSION['SESS_currency_type'] = $currency_rowid['ncurrency_id'];
        $currency_type = $currency_rowid['ncurrency_id'];
    }
}


/* currency section ends */
?>
<!-- categories-->

<!--categories listing start -->
<div class="category_listings_new">

<?php
$limit = getPaginationLimit();

$limit = 20;

$productListingFee = getSettingsValue('product_listing_fee');

$sql = "SELECT DISTINCT p.product_category,cat.route FROM " . $tableprefix . "products p LEFT JOIN " . $tableprefix . "artists a ON a.artist_id = p.product_artist_id
                    INNER JOIN " . $tableprefix . "categories cat ON cat.category_id =p.product_category 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
}

$sql_product_categories_catalog = $sql . $whereCondition;
/* $sql_product_categories_catalog="SELECT DISTINCT p.product_category,cat.route
  FROM ".$tableprefix."products p
  INNER JOIN ".$tableprefix."categories cat ON cat.category_id =p.product_category
  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.vapproved = 'Y' ";
  if($productListingFee > 0){
  $sql_product_categories_catalog .= " AND p.payment_status='COMPLETED' ";
  }
  $sql_product_categories_catalog .= " AND  p.deleted ='N'  "; */

//echo $sql_product_categories_catalog;
$rs_product_categories_catalog = mysql_query($sql_product_categories_catalog) or die(mysql_error());

if (mysql_num_rows($rs_product_categories_catalog) > 0) {
    while ($productcatalogObj = mysql_fetch_object($rs_product_categories_catalog)) {

        if ($productcatalogObj->route != '') {
            $exp_category_catalog = explode(",", $productcatalogObj->route);

            $primary_category_catalog = $exp_category_catalog[0];
        } else {
            $primary_category_catalog = $productcatalogObj->product_category;
        }

        $productcatalogcategories[] = $primary_category_catalog;
    }
}

if (count($productcatalogcategories) > 0) {
    $current_product_catalog_categories = @implode("','", $productcatalogcategories);
}

//echo $current_product_catalog_categories;

$sql_catalog = " SELECT c.avatar,c.category_id,c.category_name,c.gift_certificate_flag,c.count FROM " . $tableprefix . "categories c
 		WHERE c.category_id IN ('" . $current_product_catalog_categories . "') AND c.parent_id='0' ORDER BY c.category_name ";
$resultcatalogcategories = mysql_query($sql_catalog) or die(mysql_error());

$parentcatalogCat[] = array();

if (mysql_num_rows($resultcatalogcategories) > 0) {
    while ($rowcatalogcategories = mysql_fetch_array($resultcatalogcategories)) {

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

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

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

        $whereCondition = "";
        $sqlcountProduct = "SELECT count(*) AS pcount FROM " . $tableprefix . "products p INNER JOIN " . $tableprefix . "artists a ON a.artist_id = p.product_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 . "') ";


        $sql_product = $sqlcountProduct . $whereCondition;


        $result33 = mysql_query($sql_product);

        if (mysql_num_rows($result33) != 0) {
            $rw_catalog = mysql_fetch_array($result33);
            $count_product = $rw_catalog['pcount'];
        }

        $whereCondition = "";
        if ($rowcatalogcategories["gift_certificate_flag"] == "Y") {
            $sqlcountGiftProduct = "SELECT  COUNT(p.product_id) AS count FROM " . $tableprefix . "products p
                                                             INNER JOIN " . $tableprefix . "giftcoupon_master gm oN gm.ngift_product_id=p.product_id  where p.vapproved = 'Y' ";
            $whereCondition.=" AND p.deleted='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  ('" . $rowcatalogcategories['category_id'] . "') ";
            $product_count_sql = $sqlcountGiftProduct . $whereCondition;
            /* $product_count_sql = "SELECT COUNT(p.product_id) AS count
              FROM ".$tableprefix."products p 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 (".$rowcatalogcategories['category_id'].")
              AND p.vapproved = 'Y' AND p.deleted='N' AND s.vapproved='Y' AND s.is_vacation_mode = 'N' ";

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

            //echo $product_count_sql;
            $product_count_reslt = mysql_query($product_count_sql)or die(mysql_error());
            if (mysql_num_rows($product_count_reslt) > 0) {
                $count_product = 0;
                while ($product_counts = mysql_fetch_object($product_count_reslt)) {
                    $count_product = $product_counts->count;
                }
            }
        }

        //checking colounm count
        if ($rowcnt % 2 == '0') {
            $rowcnt = '0';
        }//end if
        ?>						

            <?php
            /*   $avatarImage = 'categories/avatar/noimage.jpg';
              if(is_file('categories/avatar/' . $rowcatalogcategories["avatar"]) && file_exists('categories/avatar/' . $rowcatalogcategories["avatar"]))
              {
              $avatarImage = 'categories/avatar/' . $rowcatalogcategories["avatar"];
              }

             */


            $prodimagedir = 'categories/avatar/';
            $avatar = $rowcatalogcategories["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 = $site_url . "/" . $prodimagedir . $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($rowcatalogcategories["category_id"]) ?>" title="<?php echo (stripslashes($rowcatalogcategories["category_name"])); ?>">
                        <div class="imgblock"><img src="<?php echo $avatarImage; ?>" /></div>
                    </a>
                    <div class="clear"></div>
                    <h5><a href="categorydetail.php?catid=<?php echo htmlentities($rowcatalogcategories["category_id"]) ?>" title="<?php echo (stripslashes($rowcatalogcategories["category_name"])); ?>"><?php echo ($rowcatalogcategories["category_name"]); ?>&nbsp;(<?php echo htmlentities($count_product); ?>)</a></h5>
                </div>   
            </div>                                     

            <?php
            $rowcnt++;
        }//end while
    }//end if
    ?>
    <div class="clear"></div>
</div>


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

<!--categories listing end -->





