<?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 (C) 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";

if (isset($_SESSION["sess_adminname"]) and $_SESSION["sess_adminname"] != "") {
    $adminname = $_SESSION["sess_adminname"];
} else {
    header("Location:login.php");
    exit;
}
if (isset($_REQUEST['stat']) && $_REQUEST['stat'] == 'Y') {
    $message = PRO_IMAGES_SAVED."!.";
}

$maximagesizestringinPHP = ini_get("upload_max_filesize");
$maximagesizeinPHP = $maximagesizestringinPHP * (1024 * 1024);

$sql_artists = "select artist_id,artist_name from " . $tableprefix . "artists where vapproved='Y' AND deleted='N' AND plan_expired ='N' ";
$rs_id = mysql_query($sql_artists) or die(mysql_error());
$numrows = mysql_num_rows($rs_id);


if (isset($_POST['btnSave']) && $_POST['btnSave'] != '') { // check button for save 
    $message = "";

    if (!isNotNull($_FILES['txtUploadZip']['name'])) {
        $message .= "* ".UPLOAD_CSV."<br>";
    }
    $rdPublishStatus = $_POST['rdPublishStatus'];

    (isset($_POST['rdPublishStatus'])) ? $rdPublishStatus = $_POST['rdPublishStatus'] : $rdPublishStatus = 'Y';
    $sellerid = $_POST['seller'];
    if($rdPublishStatus=="N"){
        //published time in 24_hour_format
        $publishDate  = date("Y-m-d H:i:s", strtotime($_POST['publishdate'].$_POST['publishtime']));
        //Get seller time zone
        $arrSellerTimezone = DbHelper::getResult('artists', "seller_time_zone", "artist_id =".$sellerid);        
        $sellertimezone = $arrSellerTimezone[0]->seller_time_zone;
    }else{
        $publishDate   = "";
        $sellertimezone = "";
    }    
    $uploadfile = ReplaceArrayImage($_FILES['txtUploadZip']['name']);
    $types = array('csv');
    preg_match('/\.([a-zA-Z]+?)$/', $uploadfile, $matches);
    if (!in_array(strtolower($matches[1]), $types)) { // check file format is valid or not 
        $message .= "* ".MSG_INVALID_CSV. " ! <br>";
    } // end format check 

    if (!empty($_FILES['txtUploadZip']['name'])) { // moving files to folder
                        $filename = $_FILES['txtUploadZip']['name'];
                        $file_extn = end(explode(".",$filename));
                        $filename = "csv_".rand().$file_extn;
			$tempFile=$_FILES['txtUploadZip']['tmp_name'];
			$destFolder="../csv/".$filename;

        if ($_FILES['txtUploadZip']['size'] > $maximagesizeinPHP) {
            $message.= "* ".SIZE_EXCEEDS.$maximagesizestringinPHP.".<br>";
        }

        if (is_file($destFolder) && file_exists($destFolder)) {
            $message .= "* ".CSV_ALREADY_EXIST. "<br>";
        } // end if file exists check 
 //echo $message;
        if ($message == '') {
			
            $file = move_uploaded_file($tempFile, $destFolder);
           
            if ($file) { //echo "wwww";
				//header("Location:index.php");
                header("Location:bulkupload_check.php?file=$filename&sellerid=$sellerid&publishstatus=$rdPublishStatus&publishtime=". urlencode($publishDate)."&sellertimezone=".  urlencode($sellertimezone));
               // exit();
            }
        }
    } // end if 
} // end if button check 

include "../includes/htmltop.php";
?>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<link href="<?php echo SITE_URL; ?>/style/jquery.timepicker.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="<?php echo SITE_URL; ?>/style/dateselector.css" type="text/css" />
<link href="http://www.jqueryscript.net/css/jquerysctipttop.css" rel="stylesheet" type="text/css">		

<script language="JavaScript" type="text/JavaScript" src="<?php echo SITE_URL?>/js/jquery.timepicker.js"></script>
<script language="JavaScript" type="text/JavaScript" src="<?php echo SITE_URL?>/js/dateselector.js"></script>
<script language="JavaScript" type="text/JavaScript" src="<?php echo SITE_URL?>/js/dateselector.min.js"></script>

<script language="JavaScript" type="text/JavaScript">
$(document).ready(function() {
    var sellerid    = $('#cmbSeller').val();
    getSellerVacationmode(sellerid);


     $("#cmbSeller").change(function (){
            var sellerid    = $(this).val();
            getSellerVacationmode(sellerid);
      });

$("#btnSave").click(function (){
            if($('#txtUploadZip').val()=="")
            {
                 alert('<?php echo SELECT_BULKUPLOAD_FILE;?>');
                 return false;
            }
              if($('#txtUploadZip').val()!='')
            {
             if(!isValidCSV($('#txtUploadZip').val()))
             {
                alert('<?php echo SELECT_BULKUPLOAD_FILE_INVALID;?>');
                return false;
             }
            }
            else{
             return true;
            }
      });


        $('.rdPublishStatus').click(function(){
                if($('input[name=rdPublishStatus]:checked').val()=="N"){
                    $("#publish_time").show();
                }else if($('input[name=rdPublishStatus]:checked').val()=="Y"){
                    $("#publish_time").hide();
                }
        });

        $("#publishtime").timepicker();
       // $("#publishdate" ).dateSelector();
        var dateToday = new Date(Date.now());
         $("#publishdate").datepicker({
            // dateFormat: 'mm-dd-yy',
             minDate: dateToday
         }).datepicker('setDate', dateToday)
    
 });



    function getSellerVacationmode(sellerid)
    {
       
        var action = 'vacationmodesetting';
        var dataString = {"action":action,"sellerId":sellerid};
                $.ajax({

                    url		:"<?php echo SITE_URL?>/utils.php",

                    type		:"POST",

                    data		:dataString,

                    dataType        : "json",
                       success: function(data) {
                                  var flag =data.flag;
                                 
                                  if(flag=='Y')
                                  {
                                       $("#jQPublish_Y").hide();
                                       $("#publish_time").show();
                                       $("#jQPublishBtn_N").prop('checked', true);
                                  }else{
                                        $("#jQPublish_Y").show();
                                       $("#publish_time").hide();
                                        $("#jQPublishBtn_Y").prop('checked', true);
                                       $("#jQPublishBtn_N").prop('checked', false);
                                  }
                              return false;
                          }
                });
    }
    function clickSearch()
    {

        document.frmProductBulkImages.submit();	
 
    }

    function blockImages(val)
    {


        if(val == 'N')
        {

            return confirm('<?php echo DISABLE_BULKIMG;?>')
	
        }
        else if(val == 'Y')
        {
	
            return confirm('<?php echo ENABLE_BULKIMG;?>')
	
        }	
	

    }

    function deleteImages()
    {
			
        return confirm('<?php echo DELETE_BULKIMG;?>')
    }

</script>

<body>

<center>

    <div align="right" id="layout">




        <!--header area start -->
        <?php
        include_once("../includes/topheader_admin.php")
        ?>

        <!--header area end -->




        <div align="right" class="main_body">
            <!--main body start -->



            <?php
            include_once("../includes/admintopmenu.php")
            ?>		


            <div align="right" class="content_area">

                <!--content area start -->

                <div align="left" class="page_title"><?php echo BULK_PRO_UPLOAD;?></div>
                <div align="center" class="text_information grey_tr"><?php echo $message ?></div>
                <br>

                <div align="right">		
                    <table>
                        <tr><td height="35"><?php //include_once("../includes/imagemenu.php") ?></td>
                        </tr></table>																	
                    <form action="" method="post" enctype="multipart/form-data" name="frmbulkupload">

                        <table width="98%" border="0" cellpadding="0" cellspacing="0" class="manage_table">

                            <tr>
                                <td colspan="3" align="left"><?php echo MAX_UPLOAD_SIZE;?> : <b><?php echo $maximagesizestringinPHP; ?></b></td>
                            </tr>
                            <tr>
                                <td height="33" colspan="3" align="left"><h4><?php echo MSG_DWNLD_CSV.JUST_DWNLD?> <a href="../includes/download.php?f=../csv/sample.csv" class="csvlink"><?php echo SAMPLE_CSV;?>.</a> <?php echo KEEP_HEADER_SAME;?>. </h4></td>
                            </tr>


                            <tr>
                                <td height="33" colspan="3" align="left"><b><i><?php echo NOTE;?> : </i></b>  <?php echo MSG_NO_BULK_UPLD;?>.</td>
                            </tr>
                            
                             <tr>
                                <td height="20" colspan="3" align="left"><b><i><?php echo NOTE;?> : </i></b>  <?php echo MSG_SMAILL_IMAGE_SIZE;?>.</td>
                            </tr>
                             <tr>
                                <td height="20" colspan="3" align="left"><b><i><?php echo NOTE;?> : </i></b>  <?php echo MSG_BIG_IMAGE_SIZE;?>.</td>
                            </tr>
                            <tr>
                            <td width="33%" height="34" align="right"><?php echo SELECT_SELLER;?></td>
                             <td width="1%">&nbsp;</td>
                                <td width="66%" align="left"><select class="textarea2" name="seller" id="cmbSeller">
                                        <?php
                                        if ($numrows > 0) {
                                            while ($rr = mysql_fetch_array($rs_id)) {
                                                ?>
                                                <option value="<?php echo $rr['artist_id']; ?>"><?php echo $rr['artist_name']; ?></option>
                                                <?php
                                            } // end while
                                        } //end if 
                                        ?>
                                    </select></td>
                               
								
                            </tr>
                      <?php if(getSettingsValue('enable_product_scheduled_publishing')=='Y')   { ?>
                            <tr>
                                <td align="right"><?php echo TEXT_PUBLISH_STATUS; ?>&nbsp;</td>
                                <td>:</td>
                                <td align='left'>
                                    <span id="jQPublish_Y">
                                    <input type="radio" id="jQPublishBtn_Y" name="rdPublishStatus" class="rdPublishStatus" value="Y" <?php if ($rdPublishStatus  == "Y") {   echo "checked"; } elseif($rdPublishStatus==NULL){echo "checked";}?>><?php echo TEXT_PUBLISH_NOW;?>
                                    </span>
                                     <span id="jQPublish_N"><input type="radio" id="jQPublishBtn_N" name="rdPublishStatus" class="rdPublishStatus" value="N" <?php if ($rdPublishStatus  == "N") {   echo "checked"; }?>><?php echo TEXT_PUBLISH_LATER;?></span>
                                    <span class="help"><a href="#" title="<?php echo PUBLISH_TOOLTIP; ?>"><img src="../images/question_mark.jpg" border="0" height="15"></a></span>
                                </td>
                            </tr>
                            <tr id="publish_time" style="display: none;">
                                <td align="right"><?php echo TEXT_PUBLISH_DATE; ?>&nbsp;</td>
                                <td>:</td>
                                <td align='left'><table><tr><td><input type="text" id="publishdate" name="publishdate"></td>
                                            <td><?php echo TEXT_PUBLISH_TIME; ?></td>
                                            <td><input type="text" id="publishtime" name="publishtime" style="width: 54px;"></td>
                                            <td><span class="help"><a href="#" title="<?php echo TEXT_PUBLISH_TIME_TOOLTIP; ?>"><img src="../images/question_mark.jpg" border="0" height="15"></a></span></td>
                                        </tr>                                                                        
                                    </table>                                                                        
                                </td>
                            </tr>
                       <?php } ?>
                            <tr>
                            	<td align="right"><?php echo UPLOAD_CSV_FILE;?> </td>
								 <td>&nbsp;</td>
                                <td align="left"><input name="txtUploadZip" type="file" id="txtUploadZip"></td>
                            </tr>
                            <tr>
                                <td>&nbsp;</td>
                                <td>&nbsp;</td>
                                <td>&nbsp;</td>
                            </tr>
                            <tr>
                                <td>&nbsp;</td>
                                <td>&nbsp;</td>
                                <td>&nbsp;</td>
                            </tr>
                            <tr>
                            <td>&nbsp;</td>
                             <td align="left"><input type="submit" name="btnSave" id="btnSave" value="<?php echo NEXT;?>" class="button"></td>                                
                                <td>&nbsp;</td>
                               
                            </tr>
                        </table>

                    </form>


                </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";
        ?>
        <script language="javascript">

            function valiadtion()

            {
                if(document.frmbulkupload.txtBulkUpload.value=="")
                {
		
                    return confirm("<?php echo PLS_UPLOAD_FILE;?>");
                    document.frmbulkupload.txtBulkUpload.focus();
                    return false;
		
                }
                if(document.frmbulkupload.seller.value=="")
                {
		
                	return confirm("<?php echo PLS_SEL_SELLER;?>");
                    document.frmbulkupload.seller.focus();
                    return false;
		
                }
		
		
                else
                {
		
                    return true;
		
                }	

            }


   
</script>