<?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";
include_once('../includes/S3.php');

$adminCurrency = GetAdminCurrency();

if (isset($_SESSION["sess_adminname"]) and $_SESSION["sess_adminname"] != "") {
    $adminname = $_SESSION["sess_adminname"];
} 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"];
}

$txtSearch = trim($txtSearch);



if (isset($_POST['gotoPage'])) {
    if (isset($_POST["txtSearch"]) and $_POST["txtSearch"] != "") {
        $txtSearch = $_POST["txtSearch"];
        $cmbSearchType = $_POST["cmbSearchType"];
    }
    header("Location:" . $_SERVER['PHP_SELF'] . "?page=" . $_POST['gotoPage'] . "&cmbSearchType=" . $cmbSearchType . "&txtSearch=" . $txtSearch);
}


if (isset($_POST['txtProductId'])) {
    $ids = $_POST['txtProductId'];
    if (empty($ids)) {
        $message .= SEL_PROD_TO_DELETE."<br>";
    }

    if ($message == '') {
        $productid_format = substr($_POST['txtProductId'], 0, strlen($_POST['txtProductId']) - 1);

        $select_product_images = "SELECT p.image_small,p.image_big
							  FROM " . $tableprefix . "product_options p WHERE p.product_id IN(" . $productid_format . ")";

        $result_product_images = mysql_query($select_product_images) or die(mysql_error());


        if (mysql_num_rows($result_product_images) > 0) {
            while ($product_images_row = mysql_fetch_array($result_product_images)) {

                $imagesmall_thumb = $product_images_row['image_small'];

                $imagelarge_thumb = $product_images_row['image_big'];

                @unlink("../products/" . $imagesmall_thumb);
                @unlink("../products/" . $imagelarge_thumb);
                deleteCDNImagesall($imagesmall_thumb,$imagelarge_thumb);


                /* -------Category count update patch------------ */
            }
        }
        $sql_options = "DELETE FROM " . $tableprefix . "product_options WHERE product_id IN ($productid_format) ";
        $del_options = mysql_query($sql_options);
        $select_product_images_more = "SELECT im.vimage_name
							  				FROM " . $tableprefix . "product_view_images im  WHERE im.nimage_productid IN(" . $productid_format . ")";
        $rs_product_image_more = mysql_query($select_product_images_more) or die(mysql_error());

        if (mysql_num_rows($rs_product_image_more) > 0) {

            while ($prows = mysql_fetch_array($rs_product_image_more)) {
                $moreimage = $prows['vimage_name'];
                $moreimage_thumb = "thumb_" . $prows['vimage_name'];

                @unlink("../products/" . $moreimage);
                @unlink("../products/" . $moreimage_thumb);
                deleteCDNImagesMore($moreimage);

            }
        }
        $sql_product_images_more = "DELETE FROM " . $tableprefix . "product_view_images WHERE nimage_productid IN ($productid_format) ";
        $del_product_images_more = mysql_query($sql_product_images_more);

        $delete_products = "DELETE FROM " . $tableprefix . "products WHERE product_id IN(" . $productid_format . ")";

        mysql_query($delete_products) or die(mysql_error());
        
        $delete_cart_products = "DELETE FROM " . $tableprefix . "cart WHERE product_id IN(" . $productid_format . ")";
        
        mysql_query($delete_cart_products) or die(mysql_error());

        $message = PRO_DELETED;

        header("Location:listProducts.php?page=1");
    } // end message empty check
}

$select_seller = "SELECT artist_id,artist_name,storename FROM " . $tableprefix . "artists WHERE 1";
$result_seller = mysql_query($select_seller) or die(mysql_error());

$qryopt = "";

if ($txtSearch != "") {
    if ($cmbSearchType == "product") {
        $qryopt .= "  AND  prd.product_name like '" . addslashes($txtSearch) . "%'";
    } else if ($cmbSearchType == "dateadded") {
        $dt = $txtSearch;
        $arrdt = explode("/", $dt);
        $day = $arrdt[1];
        $month = $arrdt[0];
        $year = $arrdt[2];
        $dt = $year . "-" . $month . "-" . $day;
        $qryopt .= "  AND  prd.date_added like '" . addslashes($dt) . "%'";
    } else if ($cmbSearchType == "category") {
        $qryopt .= "  AND  cat.category_name like '" . addslashes($txtSearch) . "%'";
    } else if ($cmbSearchType == "price") {
        $qryopt .= "  AND  (prd.product_price + prd.default_additional_price) = " . addslashes($txtSearch);
    }
}

if (isset($_POST['txtSeller'])) {
    if ($_POST['txtSeller'] != "") {
        $qryopt .= " AND prd.product_artist_id = " . $_POST['txtSeller'];
    }
}

// Filter Gift Certificates in product listing <M>
//$qryopt .= " AND prd.product_id NOT IN(SELECT GROUP_CONCAT(DISTINCT ngift_product_id ORDER BY ngift_product_id DESC SEPARATOR ', ') FROM " . $tableprefix . "giftcoupon_master)";

/*
$sql = "SELECT prd.product_id,prd.product_name,
    (prd.product_price+po.additional_price) as product_price,
    prd.date_added,cat.category_name,payment_status,
        (SELECT COUNT(pi.product_impression_id) 
              FROM " . $tableprefix . "product_impression pi
             WHERE pi.product_id = prd.product_id) AS impression_count		
                FROM " . $tableprefix . "products prd
                INNER JOIN " . $tableprefix . "product_options po ON po.product_id = prd.product_id AND po.default_option = 'YES'
                INNER JOIN " . $tableprefix . "categories cat ON prd.product_category = cat.category_id
		WHERE  1=1 " . $qryopt . ' ORDER BY product_id DESC';
*/
$sql = "SELECT prd.product_id,prd.product_name,prd.publish_status,
    (prd.product_price+prd.default_additional_price) as product_price,
    prd.date_added,cat.category_name,payment_status,
        (SELECT COUNT(pi.product_impression_id)
              FROM " . $tableprefix . "product_impression pi
             WHERE pi.product_id = prd.product_id) AS impression_count
                FROM " . $tableprefix . "products prd
                INNER JOIN " . $tableprefix . "categories cat ON prd.product_category = cat.category_id
		WHERE  1=1 " . $qryopt . " AND cat.gift_certificate_flag !='Y' ORDER BY product_id DESC";

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

$limit = getSettingsValue('npagination_limit');

$page = isset($_GET['page']) ? trim($_GET['page']) : 1;


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);
if ($_GET["message"] && !$message)
    $message = $_GET["message"];

include "../includes/htmltop.php";
?>
<link rel="stylesheet" href="<?php echo SITE_URL; ?>/style/jquery-ui.css">
<script src="<?php echo SITE_URL; ?>/js/jquery-ui.js"></script>

<script>
      $(document).ready(function () {
        $("#datepicker").datepicker({
                dateFormat: 'mm/dd/yy',
                maxDate: '0'
       
         });
      
    $("#cmbSearchType").on("change",function(){
        if(this.value=='dateadded'){
             $("#textSelector").hide();
             $("#dateSelector").show();
             $("#txtSearch").attr("disabled","disabled");
             $("#datepicker").removeAttr("disabled");
            
        }else{
             $("#textSelector").show();
             $("#dateSelector").hide();
             $("#datepicker").attr("disabled","disabled");
             $("#txtSearch").removeAttr("disabled");
             
        }
        
        
    });
});
    function clickSearch()
    {
        document.frmMyProducts.submit();
    }

    function addValues(order_value)
    {

        var checkbox_values = "";

        for(i=1;i<=document.frmMyProducts1.checkCount.value;i++)
        {

            var checkbox_name = "Product_"+i;

            if(document.getElementById(checkbox_name).checked)
            {
                checkbox_values += document.getElementById(checkbox_name).value + ',';
            }

            document.frmMyProducts2.txtProductId.value = checkbox_values;

        }
    }

    function checkAllOrders()
    {

        var checkbox_values = "";

        if(document.frmMyProducts1.checkAllBox.checked)
        {

            for(i=1;i<=document.frmMyProducts1.checkCount.value;i++)
            {

                var checkbox_name = "Product_"+i;
                document.getElementById(checkbox_name).checked = true;
                checkbox_values += document.getElementById(checkbox_name).value + ',';

            }

            document.frmMyProducts2.txtProductId.value = checkbox_values;

        }
        else
        {
            for(i=1;i<=document.frmMyProducts1.checkCount.value;i++)
            {

                var checkbox_name = "Product_"+i;
                document.getElementById(checkbox_name).checked = false;

            }

            document.frmMyProducts2.txtProductId.value = "";
        }

    }

    function deleteProducts()
    {
        if(document.frmMyProducts2.txtProductId.value == "")
        {
            return confirm("<?php echo SEL_DEL;?>");
        }
        else
        {

            var status =  confirm('<?php echo DELETE_PRODUCTS;?>');

            if(status == true)
            {
                document.frmMyProducts2.submit();
            }
        }

    }
</script>
<body>
<center>
    <div align="left" id="layout">
        <!--header area start -->
<?php
include_once("../includes/topheader_admin.php")
?>
        <!--header area end -->
        <div align="left" class="main_body">
            <!--main body start -->
<?php
include_once("../includes/admintopmenu.php")
?>
            <div align="left" class="content_area">
                <!--content area start -->
                <div align="left" class="page_title"><?php echo PRODUCTS;?></div>
                <div align="center" class="text_information_success grey_tr"><?php echo $message ?></div>
                <br>
                <div align="left">
                    <form action="<?php echo $_SERVER["PHP_SELF"] ?>" method="post" name="frmMyProducts">
                        <table width="100%" border="0" cellpadding="0" cellspacing="0" class="manage_top">
                            <tr>
                                <td align="left" valign="middle" colspan="2" class=""><?php echo SEARCH;?>
                                   
                                     <?php
                                $dateblock = "none";
                                if($cmbSearchType!= "dateadded"){
                                           $txtBlock = "display";
                                           $dateblock = "none";
                                           $txtSearch1 = $txtSearch; 
                                           $disabled1   = "";
                                           $disabled2   = "disabled=disabled";
                                 } else{
                                           $txtBlock = "none";
                                           $dateblock = "display";
                                           $txtSearch2 = $txtSearch;
                                           $disabled1   = "disabled=disabled";
                                           $disabled2   = "";
                                }
                                                           
                             ?>
                                    <select name="cmbSearchType" id="cmbSearchType" class="textarea">
                                        <option value="product"  <?php
            if ($cmbSearchType == "product" || $cmbSearchType == "") {
                echo("selected");
            }
?>><?php echo PRODUCT_NAME;?></option>
                                        <option value="category"  <?php
            if ($cmbSearchType == "category") {
                echo("selected");
            }
?>><?php echo CATEGORY;?></option>
                                        <option value="price"  <?php
            if ($cmbSearchType == "price") {
                echo("selected");
            }
?>><?php echo PRICE;?></option>
                                        <option value="dateadded"  <?php
                                                if ($cmbSearchType == "dateadded") {
                                                    echo("selected");
                                                }
?>><?php echo ADDED_DATE;?><!--(<?php // echo MM."/".DD."/".YYYY;?>)--></option>
                                    </select> &nbsp;
                                    
                                     <span id="textSelector" style="display:<?php echo $txtBlock; ?>"  >
                                        <input type="text" <?php echo $disabled1; ?> name="txtSearch" id="txtSearch" size="20" maxlength="50" value="<?php echo(htmlentities($txtSearch1)); ?>"  onKeyPress="if(window.event.keyCode == '13'){ return false; }"  class="textarea">
                                   </span>
                                    <span id="dateSelector" style="display:<?php echo $dateblock; ?>" >
                                        <input type="text" <?php echo $disabled2; ?> name="txtSearch" id="datepicker" size="20" maxlength="50" value="<?php echo(htmlentities($txtSearch2)); ?>"   class="textarea">
                                    </span>
                                   &nbsp;<a href="javascript:clickSearch();" class="search_button"><?php echo BTN_SEARCH; ?></a>&nbsp;
                                </td>
                                <td width="20%" align="right"><a href="catalog.php"  class="add_new_button"><?php echo BTN_ADD_NEW_PRODUCT; ?></a></td>
                            </tr>
                            <tr>
                                <td width="8%" colspan="3">&nbsp;</td>
                            </tr>
                            <tr>
                                <td><?php echo SELLERS;?></td>
                                <td width="70%">
                                    <select name="txtSeller" class="textarea" onChange="Javascript:document.frmMyProducts.submit();">
                                        <option value="">--<?php echo ALL_SELLERS;?>--</option>
                                        <?php
                                        if (mysql_num_rows($result_seller) > 0) {
                                            while ($seller_row = mysql_fetch_array($result_seller)) {

                                                if ($seller_row['artist_id'] == $_POST['txtSeller']) {
                                                    $select_seller = "selected";
                                                } else {
                                                    $select_seller = "";
                                                }
                                                ?>
                                                <option value="<?php echo $seller_row['artist_id'] ?>" <?php echo $select_seller ?>><?php echo $seller_row['artist_name'] ?>&nbsp; (<?php echo $seller_row['storename'] ?> ) </option>
                                                <?php
                                            }
                                        }
                                        ?>
                                    </select>
                                </td>
                            </tr>
                        </table>
                    </form>
                </div>
                <div align="left">
                    <table border="0" width="100%" cellpadding="0" cellspacing="0" class="manage_table">
                        <form action="" method="post" name="frmMyProducts1">
                            <input type="hidden" value="<?php echo $cmbSearchType; ?>" name="cmbSearchType">
                            <input type="hidden" value="<?php echo $txtSearch; ?>" name="txtSearch">
                            <tr class="form_header">
                                <th width="3%">#</th>
                                <th width="18%" align="left"><?php echo PRODUCT_NAME;?></th>
                                <th width="13%" align="left"><?php echo PRICE;?></th>
                                <th width="14%" align="left"><?php echo CATEGORY;?></th>
                                <th width="10%" align="left"><?php echo DATE_ADDED;?></th>
                                <th width="10%" align="left"><?php echo IMP_COUNT?></th>
                                <th width="10%" align="left"><?php echo PUBLISH_STATUS?></th>
                                <th width="10%" align="left"><?php echo LISTING_FEE;?></th>
                                <th width="20%" colspan="2" align="left"><input type="checkbox" name="checkAllBox" value="1" onClick="checkAllOrders();"><?php echo SEL_OR_DESELECT;?>
                                    <a onClick="deleteProducts();" style="cursor:pointer" title="<?php echo TITLE_DELETE_SELECTED; ?>"><img border="0" src="../images/btn_del.gif"></a></th>
                            </tr>
                            <?php
                            if (mysql_num_rows($result) > 0) {
                                $i = 1;
                                while ($row = mysql_fetch_array($rs)) {
                                    $link = "<a class=links href='editproduct.php?productid=" . $row["product_id"] . "&page=" . $_GET['page'] . "&from=list'>";
                                    switch (htmlentities($row["payment_status"])){
                                    	case 'PENDING':$st2=strtoupper(PENDING);
                                    	break;                        
                                    	case 'COMPLETED':$st2=strtoupper(COMPLETED);
                                    	break;
                                    	}
                                    ?>
                                    <tr>
                                        <td><?php echo (($page * $limit) + $i - $limit) ?></td>
                                        <td align="left"><?php echo $link . stripslashes($row["product_name"]) . "</a>"; ?></td>
                                        <td><?php echo $adminCurrency['currency_symbol']; ?>  <?php echo $link . htmlentities(number_format($row["product_price"], 2, ".", "")) . "</a>"; ?></td>
                                        <td><?php echo $link .$row["category_name"] . "</a>"; ?></td>
                                        <td align=""><?php echo $link . dateFormat($row["date_added"], "Y-m-d", "m/d/Y") . "</a>"; ?></td>
                                        <td><?php echo $link . htmlentities($row["impression_count"]) . "</a>"; ?></td>
<?php $pstatus = ($row["publish_status"]=='Y')?TEXT_PUBLISHED:TEXT_NOT_PUBLISHED;?>
                                    <td align="left"><?php echo $link.$pstatus."</a>";?></td>                                        
                                        <td><?php echo $link . $st2. "</a>"; ?></td>
                                        <td>
                                            <?php
                                            $product_checkbox = "Product_" . $i;
                                            ?>
                                            <input type="checkbox" name="<?php echo $product_checkbox ?>" id="<?php echo $product_checkbox ?>" value="<?php echo $row['product_id'] ?>" onClick="addValues('<?php echo $row['product_id'] ?>');">
                                        </td>


                                    </tr>
                                    <?php
                                    $i++;
                                }
                                ?>
                                <input type="hidden" name="checkCount" value="<?php echo $i - 1 ?>">
                                <?php
                            } else {
                                ?>
                                <tr>
                                    <td align="center" valign="middle" colspan="7"  class="text_information"><?php echo NO_PRODUCT_FOUND_TEXT;?> !!</td>
                                </tr>
    <?php
}

if ($page > 1) {
    $pageprev = $page - 1;

    $previous_page = "<a href=" . $_SERVER['PHP_SELF'] . "?page=$pageprev&cmbSearchType=$cmbSearchType&txtSearch=$txtSearch alt='Previous' border='0' class='small_gray_button'>".TEXT_PREV."</a>";
}
if ($page < $numofpages) {
    $pagenext = ($page + 1);

    $next = "<a href=" . $_SERVER['PHP_SELF'] . "?page=$pagenext&cmbSearchType=$cmbSearchType&txtSearch=$txtSearch alt='Next' border='0' class='small_gray_button'>".TEXT_NEXT."</a>";
}
if ($numofpages) {
    ?>
                                <tr>
                                    <td align="center" colspan="8">
										<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 PAGE."(".S.")";?> <?php echo $page ?>/<?php echo $numofpages ?>&nbsp;&nbsp;
													<?php echo GO_TO;?>
												</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.frmMyProducts1.submit();" class="go_button_2"><?php echo GO; ?></a>
												</td>
												<td width="41%" align="center"><?php echo $next ?></td>
											</tr>
										</table>
									</td>
                                </tr>
                                            <?php
                                        }
                                        ?>
                        </form>
                        <tr>
                            <td colspan="7">
                                <form name="frmMyProducts2" method="post" action="#">
                                    <input type="hidden" name="txtProductId">
                                </form>
                            </td>
                        </tr>
                    </table>
                </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";
?>