<?php
ob_start();
/* 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('../classes/cls_utils.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);
}

if (isset($_SESSION["sess_adminname"]) and $_SESSION["sess_adminname"] != "") {
    $adminname = $_SESSION["sess_adminname"];
} else {
    header("Location:login.php");
    exit;
}


include "../includes/htmltop.php";

$artistlist = makeArtistList();
$ddlArtist = $_POST["ddlArtist"];
$image_flag = 0;
if (isset($_POST['update'])) {


    /* check whether gift coupon with the same name exists in the database */

    $select_coupon = "SELECT * FROM " . $tableprefix . "products WHERE LOWER(product_name) = " . GetSQLValueString(strtolower($_POST['txtGiftCouponName']), "text")
            . " AND product_id  != " . $_GET['edit_product_id'];

    $result_coupon = mysql_query($select_coupon) or die(mysql_error());

    if (mysql_num_rows($result_coupon) > 0) {

        $message = CNAME_EXIST;
    } else {

        $couponfiletype = $_FILES['txtCouponImage']['type'];
        $couponfilename = $_FILES['txtCouponImage']['name'];
        $coupontempname = $_FILES['txtCouponImage']['tmp_name'];

        if($_POST['product_name_old']!=$_POST['txtGiftCouponName']){
         //Generate Alias name for product
         $aliasname = Utils::generateAlias($tableprefix . "products", $_POST['txtGiftCouponName']);
        }
        
        //','txtCouponImage','txtCouponAmount','ddlArtist'

        $update_gift_coupon = "UPDATE " . $tableprefix . "products
						  	   SET product_name = " . GetSQLValueString($_POST['txtGiftCouponName'], "text") . ",
							       product_code = " . GetSQLValueString($_POST['txtGiftCouponCode'], "text") . ",
						  	       product_description = " . GetSQLValueString($_POST['txtCouponDescription'], "text") . ",
						               product_price = " . GetSQLValueString($_POST['txtCouponAmount'], "text") . ",";
        if($aliasname!=NULL)$update_gift_coupon .=" alias = '".$aliasname."',";
        $update_gift_coupon .=" product_artist_id = '".$ddlArtist."'";                                                
							       

        $update_gift_coupon .= " WHERE product_id = '" . $_GET['edit_product_id'] . "'";
        mysql_query($update_gift_coupon) or die(mysql_error());
        $update_product_options = "UPDATE " . $tableprefix . "product_options
                                                SET product_stock = " . GetSQLValueString($_POST['txtCouponStock'], "text") . ",
                                                    reorder_level = " . GetSQLValueString($_POST['txtStockReorder'], "text");

        $update_gift_coupon_master = "UPDATE " . $tableprefix . "giftcoupon_master
									  SET ngift_coupon_amount = " . GetSQLValueString($_POST['txtCouponAmount'], "text") .
                " WHERE ngift_product_id = " . $_GET['edit_product_id'];

        mysql_query($update_gift_coupon_master) or die(mysql_error());
        $message = GC_UPDATED;

        if ($_FILES['txtCouponImage']['name'][0] != "") {
            if (!isValidWebImageType($couponfiletype, $couponfilename, $coupontempname)) {

                $message_image = " * ".INVALID_IMAGE."<br>";

                $image_flag = 1;
            } else {


                $imagewidth_height_type_array = explode(":", ImageTypeBanner($_FILES['txtCouponImage']['tmp_name']));
                $imagetype = $imagewidth_height_type_array[0];

                $assignedname_small = "gift_coupon_small" . time() . "." . $imagetype;

                $assignedname_large = "gift_coupon_large" . time() . "." . $imagetype;

                if (move_uploaded_file($_FILES['txtCouponImage']['tmp_name'], "../products/" . $assignedname_large)) {

                    chmod("../products/$assignedname_large", 0777);

                    /* taken from the general site settings table */

                    $gift_image_size = '400,400';
                    $gift_size = split(',', $gift_image_size);

                    resizeImg("../products/" . $assignedname_large, 489, 328, false, 100, 0, "_thumb");

                    @copy("../products/$assignedname_large", "../products/$assignedname_small");

                    resizeImg("../products/" . $assignedname_small, 387, 260, false, 100, 0, "_thumb");

                    $update_product_options .= ",image_small = '" . $assignedname_small . "',
                                                                     image_big = '" . $assignedname_large . "'";

                    unlink('../products/' . $_POST['txtCouponBuffer']);
                }
            }
        }
        $update_product_options .= "  WHERE product_id = '" . $_GET['edit_product_id'] . "'";
        mysql_query($update_product_options) or die(mysql_error());

         //Scalabity Modifications for Gift Certificates

            $objDefaultOptionVo                                     =    new stdClass();
            $objOptionVo                                            =    Products::getProduct_Default_Option_Values($_GET['edit_product_id']);

            $objDefaultOptionVo->default_image_small                =   $objOptionVo->image_small;
           // $objDefaultOptionVo->default_stock                      =   $objOptionVo->product_stock;
            $objDefaultOptionVo->default_additional_price           =   $objOptionVo->additional_price;
            $objDefaultOptionVo->default_shipping_weight            =   $objOptionVo->product_shipping_weight;
            $objDefaultOptionVo->default_discount                   =   $objOptionVo->discount;
            $objDefaultOptionVo->default_discount_description       =   $objOptionVo->discount_description;
            $objDefaultOptionVo->default_option_id                  =   $objOptionVo->product_option_id;
            $objDefaultOptionVo->product_id                         =   $_GET['edit_product_id'];
            // Save default option values to product table
            $productinfoUpdate                                      =   Products::updateProduct_Default_Option_Value($objDefaultOptionVo);


        if ($image_flag == 0) {
            header("Location:gift_coupon.php?" . $_GET['qstring'] . "&txtSearch=" . $_GET['txtSearch'] . "&msg=".GC_UPDATED);
        }
    }
}
?>
<body topmargin="0">
    <script>
        function checkAddCouponForm()
        {

            //var form_name = new Array('txtGiftCouponName','ddlArtist');		
           // var display_name = new Array('<?php echo GC_NAME;?>','<?php echo SELLER;?>');
           // var count = 2;	
           if(document.frmAddGiftCoupon.txtCouponBuffer.value == ''){
        	   var form_name = new Array('txtGiftCouponName','txtGiftCouponCode','txtCouponImage','txtCouponAmount','ddlArtist');		
               var display_name = new Array('<?php echo GC_NAME;?>','<?php echo GC_CODE;?>','<?php echo GC_IMAGE;?>','<?php echo GC_AMOUNT;?>','<?php echo SELLER;?>');
               var count = 5;	
           }
           else {
				var form_name = new Array('txtGiftCouponName','txtGiftCouponCode','txtCouponAmount','ddlArtist');		
		        var display_name = new Array('<?php echo GC_NAME;?>','<?php echo GC_CODE;?>','<?php echo GC_AMOUNT;?>','<?php echo SELLER;?>');
		        var count = 4;	
           }
            if(check_fields('frmAddGiftCoupon',form_name,display_name,count))	
            {            	 
  	
                if(!check_numeric(trimSpaces(document.frmAddGiftCoupon.txtCouponAmount.value)))
                {
	 
                	alert("<?php echo PLS_NUMBER;?>");
                    document.frmAddGiftCoupon.txtCouponAmount.focus();
                    return false;		 
	 
                }
   
            }
            else
            {
  
                return false;
  
            }
   
        }

    </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 EDIT_GC;?></div>
                <div align="center" class="text_information grey_tr"><?php echo $message ?></div>
                <br>

                <div align="left">									

                    <form name="frmAddGiftCoupon" action="#" method="POST" enctype="multipart/form-data" onSubmit="return checkAddCouponForm();">

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

                            <tr><td colspan="3">&nbsp;</td></tr>
                            <tr><td colspan="3" class="message" align="left"><?php echo $message_image; ?></td></tr>
                            <tr><td colspan="3" align="right"><span class="required">*</span>&nbsp;<?php echo FIELDS_ARE_MANDATORY;?></td></tr>

                            <?php
                            $select_gift_coupon_details = "SELECT prd.product_name,prd.product_description,prd.product_price,prd.product_code,
										  prd.product_artist_id,po.image_small,po.product_stock,po.reorder_level
								   FROM " . $tableprefix . "products prd
                                                                   INNER JOIN " . $tableprefix . "product_options po ON po.product_id = prd.product_id AND po.default_option = 'YES'
								   WHERE prd.product_id = " . $_GET['edit_product_id'];

                            $result_gift_coupon_details = mysql_query($select_gift_coupon_details) or die(mysql_error());
                            $gift_coupon_row = mysql_fetch_assoc($result_gift_coupon_details);
                            $ddlArtist = $gift_coupon_row['product_artist_id'];
                            ?>	

                            <tr><td width="26%" align="left" valign=top><?php echo GC_NAME;?>&nbsp;<span class="required">*</span></td><td width="2%" >&nbsp;</td>
                                <td width="72%" align="left" valign=top><input class="textarea2" type="text" name="txtGiftCouponName" value="<?php echo $gift_coupon_row['product_name'] ?>" size="25" maxlength="150"></td>
                            </tr>
                            <tr>
                                <td align="left" valign=top><?php echo GC_CODE;?> &nbsp;<span class="required">*</span></td>
                                <td >&nbsp;</td>
                                <td align="left" valign=top><input name="txtGiftCouponCode" type="text" class="textarea2" id="txtGiftCouponCode" value="<?php echo $gift_coupon_row['product_code'] ?>" size="25" maxlength="150"></td>
                            </tr>
                            <tr>
                                <td align="left" valign=top><?php echo DESCRIPTION;?>&nbsp;</td>
                                <td >&nbsp;</td>
                                <td valign=top align="left">
                                    <textarea name="txtCouponDescription" rows="8" cols="30" class="textarea2"><?php echo $gift_coupon_row['product_description'] ?></textarea>
                                </td>
                            </tr>
                            <tr><td align="left" valign=top><?php echo CERT_IMAGE;?>&nbsp;<span class="required">*</span></td><td >&nbsp;</td>
                                <td valign=top align="left">
                                    <input type="file" name="txtCouponImage">
                                    &nbsp;<b><?php echo FILE_TYPES;?></b>: <?php echo JPG_GIF_PNG;?> &nbsp; <b><?php echo SIZE;?></b>: 500 x 350&nbsp;<img src="../products/<?php echo $gift_coupon_row['image_small'] ?>" height="50" width="100">
                                    <input type="hidden" name="txtCouponBuffer" value="<?php echo $gift_coupon_row['image_small'] ?>">
                                </td>
                            </tr>

                            <tr><td align="left" valign=top><?php echo CERT_PRICE;?>&nbsp;<span class="required">*</span></td><td >&nbsp;</td>
                                <td valign=top align="left"><input type="text" class="textarea2" name="txtCouponAmount" value="<?php echo $gift_coupon_row['product_price'] ?>">&nbsp; <?php echo $adminCurrency['currency_code']; ?> </td>
                            </tr>

                            <tr><td align="left" valign=top><?php echo STOCK_AVAILABLE;?>&nbsp;<span class="required">*</span></td>
                                <td>&nbsp;</td>
                                <td valign="top" align="left">
                                    <select name="txtCouponStock" class="textarea2" id="txtCouponStock">
                                        <?php
                                        for ($i = 1; $i <= 50; $i++) {

                                            if ($gift_coupon_row['product_stock'] == $i) {
                                                $stock_selected = "selected";
                                            } else {
                                                $stock_selected = "";
                                            }
                                            ?>										
                                            <option value="<?php echo $i ?>" <?php echo $stock_selected ?>><?php echo $i ?></option>								

                                            <?php
                                        }
                                        ?>									
                                    </select>			
                                </td>
                            </tr>
                            <tr><td align="left" valign=top><?php echo STOCK_REORDER_LEVEL;?>&nbsp;<span class="required">*</span></td>
                                <td>&nbsp;</td>
                                <td valign="top" align="left">
                                    <select name="txtStockReorder" class="textarea2" id="txtStockReorder">
                                        <?php
                                        for ($i = 1; $i <= 50; $i++) {

                                            if ($gift_coupon_row['reorder_level'] == $i) {
                                                $stock_reorder = "selected";
                                            } else {
                                                $stock_reorder = "";
                                            }
                                            ?>										
                                            <option value="<?php echo $i ?>" <?php echo $stock_reorder ?>><?php echo $i ?></option>								

                                            <?php
                                        }
                                        ?>									
                                    </select>
                                    <b><?php echo STOCK_REORDR_MSG;?></b>			
                                </td>
                            </tr>
                            <tr>
                                <td align="left" valign=top><?php echo SELLER;?>&nbsp;<span class="required">*</span></td><td >&nbsp;</td>
                                <td align="left"><?php echo makeDropDownList("ddlArtist", $artistlist, $ddlArtist, false, 'textbox', $properties, $behaviors) ?></td>
                            </tr>
                            <tr>
                                <td colspan="3" align="center">
                                    <input type="hidden" name="product_name_old" value="<?php echo $gift_coupon_row['product_name']?>">
                                    <input type="button" class="button" name="back" value="&nbsp;<?php echo BACK;?>&nbsp;" onClick="window.location='gift_coupon.php'">
									<input type="submit" value="<?php echo UPDATE;?>" name="update" class="button">
                                </td>
                            </tr>
                            <tr><td colspan="3">&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";
        ob_end_flush();
        ?>