<?php ob_start(); ?>
<?php  session_start(); ?>
<SCRIPT LANGUAGE="javascript">
    <!--
    function OnChange()
    {

        var pro = document.getElementById('pro_id').value;

        if(pro==2){
            document.getElementById('pro_type').style.display="block";
            document.getElementById('pro_type2').style.display="none";
        } else if(pro==3 ){
            document.getElementById('pro_type').style.display="none";
            document.getElementById('pro_type2').style.display="block";
        }else{
            document.getElementById('pro_id').value='';
            document.getElementById('pro_type').style.display="none";
            document.getElementById('pro_type2').style.display="none";
        }
        return true;
    }
    //-->

</SCRIPT>

<?php




require_once 'func.php';

require_once 'resize-class.php';
require_once 'auth.php';



ob_start();



require_once 'htmlhead.php';

?>



<h3>Upload New Card</h3>



<?php



#exec('chmod -R 0777 ' . $card_dir);

$_width = 1425;

$_height = 883;



$_t_width = 191;

$_t_height = 112;



$cat = isset($_GET['cat']) ? (int)$_GET['cat'] : null;

$product_filter = isset($_GET['product']) ? (int)$_GET['product'] : null;
$type_filter = isset($_GET['type']) ? (int)$_GET['type'] : null;



$qry = 'select * from category';

$category_res = mysql_query($qry) or die(mysql_error());


$qry_pro = 'select * from products';

$products_res = mysql_query($qry_pro) or die(mysql_error());

$qry_type = 'select * from product_types where product_id=2';
$products_type = mysql_query($qry_type) or die(mysql_error());

$qry_type2 = 'select * from product_types where product_id=3';
$products_type2 = mysql_query($qry_type2) or die(mysql_error());
?>

<span class="note">&nbsp;</span>

<form action="upload.php" method="post" enctype="multipart/form-data">

    <input type="hidden" name="act" value="upload" />
    <span  style="width:600px; float:left" >
        <input type="file" name="card" />

        Category: <select name="cat">

            <option value="">all</option>

            <?php

            while (($row=mysql_fetch_assoc($category_res))!==false) {

                echo '<option value="' . $row['category_'] . '" ' . ($cat == $row['category_'] ? 'selected' : '') . '>' . htmlentities($row['name'], ENT_QUOTES, 'UTF-8' ) . '</option>';

            }

            ?>

        </select>
        Product: <select name="pro" id="pro_id" onchange='OnChange();' >

            <option value="1">all</option>

            <?php

            while (($row=mysql_fetch_assoc($products_res))!==false) { ?>

            <option value="<?php echo $row['Product_Id']; ?>"><?php echo $row['Product_Name']; ?></option>

                <?php }

            ?>

        </select></span>
    <span id="pro_type" style="display:none; width:200px; float:left" >
        Product Type: <select name="pro_type_clen">

            <option value="">all</option>

            <?php

            while (($row=mysql_fetch_assoc($products_type))!==false) {

                echo '<option value="' . $row['type_id'] . '" ' . ($cat == $row['type_id'] ? 'selected' : '') . '>' . htmlentities($row['type_name'], ENT_QUOTES, 'UTF-8' ) . '</option>';

            }

            ?>

        </select></span>
    <span id="pro_type2" style="display:none; width:200px; float:left" >
        Product Type: <select name="pro_type_flyer">

            <option value="">all</option>

            <?php

            while (($row=mysql_fetch_assoc($products_type2))!==false) {

                echo '<option value="' . $row['type_id'] . '" ' . ($cat == $row['type_id'] ? 'selected' : '') . '>' . htmlentities($row['type_name'], ENT_QUOTES, 'UTF-8' ) . '</option>';

            }

            ?>

        </select></span>
    <input type="submit" value="Upload" />

</form>

<?php



$act = isset($_REQUEST['act']) ? $_POST['act'] : null;


if ($act == 'upload') {
    
    $_cat = isset($_POST['cat']) ? $_POST['cat'] : null;

    $card = isset($_FILES['card']) ? $_FILES['card'] : null;
    $size_for="Business Cards";
    if(!empty($_POST['pro']) && $_POST['pro']=='2') { // clender
        if(!empty($_POST['pro_type_clen']) && $_POST['pro_type_clen']=='5') { //wall
            $_width='1024';
            $_height='700';

            $size_for="Canlender Wall";
        }
        if(!empty($_POST['pro_type_clen']) && $_POST['pro_type_clen']=='6') { //poster
            $_width='3341';
            $_height='5272';
            $size_for="Canlender Poster";
        }
    }
    if(!empty($_POST['pro']) && $_POST['pro']=='3') { // flyer
        if(!empty($_POST['pro_type_flyer']) && $_POST['pro_type_flyer']=='1') { //flyer 1
            $_width='1717';
            $_height='1339';
            $size_for="Flyer One";
        }
        if(!empty($_POST['pro_type_flyer']) && $_POST['pro_type_flyer']=='2') { //flyer 2
            $_width='2625';
            $_height='1717';
            $size_for="Flyer Two";
        }
        if(!empty($_POST['pro_type_flyer']) && $_POST['pro_type_flyer']=='3') { //flyer 3
            $_width='2626';
            $_height='3370';
            $size_for="Flyer Three";
        }
        if(!empty($_POST['pro_type_flyer']) && $_POST['pro_type_flyer']=='4') { //flyer 4
            $_width='1197';
            $_height='2556';
            $size_for="Flyer Four";
        }
    }
    if(!empty($_POST['pro']) && $_POST['pro']=='4') {
        $_width='2625';
        $_height='3375';
        $size_for="Letterhead Stationery";

    }
    if(!empty($_POST['pro']) && $_POST['pro']=='5') {
        $_width='2625';
        $_height='3375';
        $size_for="Folding Brochures";

    }

    if(!empty($_POST['pro']) && $_POST['pro']=='2') {

        $product = isset($_POST['pro']) ? $_POST['pro'] : null;
        $product_type = isset($_POST['pro_type_clen']) ? $_POST['pro_type_clen'] : null;

    } else if(!empty($_POST['pro']) && $_POST['pro']=='3') {
        $product = isset($_POST['pro']) ? $_POST['pro'] : null;
        $product_type = isset($_POST['pro_type_flyer']) ? $_POST['pro_type_flyer'] : null;
    } else {

        $product = isset($_POST['pro']) ? $_POST['pro'] : null;
        $product_type = null;

    }

    if ($card && $card['tmp_name'] && !$card['error']) {

        $data = getimagesize ($card['tmp_name']);

        if (!empty($data)) {

            $width = $data[0];

            $height = $data[1];

            if ($width < $_width || $height < $_height) {

                echo '<span class="error">Size for '.$size_for.' should be ' . $_width . 'x' . $_height . '. Given: ' . $width . 'x' . $height . '</span>';

            } else {

                $fileParts=explode('.',$card['name']);

                $ext = strtolower(end($fileParts));

                $filename = substr(md5(time()), 0, 7) . '.' . $ext;

                move_uploaded_file($card['tmp_name'], $card_dir . 'full/' . $filename);
                //move_uploaded_file($card['tmp_name'],  'images/' . $filename);
//                echo $_FILES["card"]["tmp_name"];
//                 move_uploaded_file($_FILES["card"]["tmp_name"],
//                'images/' . $filename);

                #print_r(array($width, $height));

                if ($width > $_width || $height > $_height) {

                 //   createthumb($card_dir . 'full/' . $filename, $card_dir . 'full/' . $filename, $_width, $_height);
                 $target_path = $card_dir . 'full';
                $resizeObj = new resize($target_path."/".$filename);
                $resizeObj -> resizeImage($_width,$_height,'auto');
                $resizeObj -> saveImage($target_path."/".$filename,100);
                }

          
           
            
                $_cat = (int)$_cat;

                $_cat = !$_cat ? 'null' : $_cat;

                $qry = 'insert into card (adddate, filename, category_,ProductId,ProductType) values (\'' . date('Y-m-d H:i:s') . '\', \'' . $filename . '\', \'' . mysql_escape_string($_cat) . '\', \'' . mysql_escape_string($product) . '\', \'' . mysql_escape_string($product_type) . '\')';

                mysql_query($qry) or die(mysql_error());

                $id = mysql_insert_id();

                if (!$id) {

                    echo '<span class="error">Can not add file</span>';

                } else {

                  //  createthumb($card_dir . 'full/' . $filename, $card_dir . 'thumbnail/' . $filename, $_t_width, $_t_height);

                                   $target_path = $card_dir . 'full';
                                   $save_path = $card_dir . 'thumbnail';
                $resizeObj = new resize($target_path."/".$filename);
                $resizeObj -> resizeImage($_t_width,$_t_height,'auto');
                $resizeObj -> saveImage($save_path."/".$filename,100);
                    // ***** !!!!for dev only!!!! ******** {{{

                    #chmod($card_dir . 'full/' . $filename, 0777);

                    #chmod($card_dir . 'thumbnail/' . $filename, 0777);

                    // }}} ***** !!!!for dev only!!!! ******** {{{

                }

            }

        } else {

            echo '<span class="error">File not valid</span>';

        }

#        echo "<pre>";

#        print_r($data);

    } else {

        echo '<span class="error">File not uploaded</span>';

    }



} elseif ($act == 'manage') {
    
 $action = isset($_POST['action']) ? $_POST['action'] : null;
   

    if ($action == 'Delete') {
        
       $cid = isset($_POST['cid']) ? $_POST['cid'] : array();

        if ($cid) {

            $del = array();

            foreach ($cid as $id) {

                if (is_numeric($id)) {

                    $del[] = $id;

                }

            }

            $qry = 'select * from card where card_ in (' . implode(',', $del) . ')';

            $res = mysql_query($qry) or die(mysql_error());

            while (($row=mysql_fetch_assoc($res))!==false) {

                if (!empty($row['filename']) && is_file($card_dir . 'full/' . $row['filename']))

                    unlink($card_dir . 'full/' . $row['filename']);

                if (!empty($row['filename']) && is_file($card_dir . 'thumbnail/' . $row['filename']))

                    unlink($card_dir . 'thumbnail/' . $row['filename']);

            }

            $qry = 'delete from card where card_ in (' . implode(',', $del) . ')';

            @mysql_query($qry);

            header('Location: ' . $self_url . 'upload.php' );
        }

    } elseif ($action == 'Update') {

        $cid = isset($_POST['cid']) ? $_POST['cid'] : array();

        $_cat = isset($_POST['cat']) ? $_POST['cat'] : null;

        if ($_cat && $cid) {

            $upd = array();

            foreach ($cid as $id) {

                if (is_numeric($id)) {

                    $upd[] = $id;

                }

            }

            $qry = 'update card set category_ = ' . mysql_escape_string($_cat) . ' where card_ in (' . implode(', ', $upd) . ')';

            @mysql_query($qry);

        }

    }

    header('Location: ' . $self_url . 'upload.php' . (!empty($cat) ? '?cat=' . $cat : ''));

    ob_clean();

    die;

} elseif ($act == 'catedit') {

    $action = isset($_POST['action']) ? $_POST['action'] : null;

    if ($action == 'Save') {

        $cat_name = isset($_POST['cat_name']) ? $_POST['cat_name'] : null;

        $cat_check = isset($_POST['cat_check']) ? $_POST['cat_check'] : array();

        if (!empty($cat_check)) {

            foreach ($cat_check as $k => $v) {

                unset($cat_name[$k]);

            }

            $del = array();

            foreach ($cat_check as $k => $v) {

                if (is_numeric($k)) {

                    $del[] = $k;

                }

            }

            $qry = 'delete from category where category_ in (' . implode(', ', $del) . ')';

            @mysql_query($qry);

        }

        if (!empty($cat_name)) {

            foreach ($cat_name as $k => $v) {

                $qry = 'update category set name = "' . mysql_escape_string($v) . '" where category_ = ' . mysql_escape_string($k);

                @mysql_query($qry);

            }

        }

    } elseif ($action == 'add') {

        $cat_name = isset($_POST['cat_name']) ? $_POST['cat_name'] : null;

        $qry = 'insert into category (name) values ("' . mysql_escape_string($cat_name) . '")';

        @mysql_query($qry);

    }

    header('Location: ' . $self_url . 'upload.php' . (!empty($cat) ? '?cat=' . $cat : ''));

    ob_clean();

    die;

}



?>

<hr />
 <h3>Search by category</h3>
<form action="upload.php" method="get">

    
   
    Category Filter: <select name="cat">

        <option value="">All</option>

        <?php

        @mysql_data_seek($category_res, 0);

        while (($row=mysql_fetch_assoc($category_res))!==false) {

            echo '<option value="' . $row['category_'] . '" ' . ($cat == $row['category_'] ? 'selected' : '') . '>' . htmlentities($row['name'], ENT_QUOTES, 'UTF-8' ) . '</option>';

        }

        ?>

    </select>

    <input value="filter" type="submit" />

    <input value="reset" type="button" onclick="window.location='<?php echo $self_url; ?>upload.php'" />

</form>
<hr />
<h3>Search by Product</h3>

<form action="upload.php" method="get">



    Product Filter: <select name="product">

        <option value="">All</option>

        <?php
        $qry_pro = 'select * from products';

        $products_res = mysql_query($qry_pro) or die(mysql_error());

        $qry_type = 'select * from product_types where product_id=2';
        $products_type = mysql_query($qry_type) or die(mysql_error());

        $qry_type2 = 'select * from product_types where product_id=3';
        $products_type2 = mysql_query($qry_type2) or die(mysql_error());

        while (($row=mysql_fetch_assoc($products_res))!==false) {

            echo '<option value="' . $row['Product_Id'] . '" ' . ($product_filter == $row['Product_Id'] ? 'selected' : '') . '>' . htmlentities($row['Product_Name'], ENT_QUOTES, 'UTF-8' ) . '</option>';

        }

        ?>

    </select>

    <input value="filter" type="submit" />

    <input value="reset" type="button" onclick="window.location='<?php echo $self_url; ?>upload.php'" />

</form>
<hr />
<h3>Search by Product Type</h3>
<form action="upload.php" method="get">



    Product Type Filter: <select name="type">

        <option value="">All</option>

        <?php


        $qry_type = 'select * from product_types';
        $products_type = mysql_query($qry_type) or die(mysql_error());

        while (($row=mysql_fetch_assoc($products_type))!==false) {

            echo '<option value="' . $row['type_id'] . '" ' . ($type_filter == $row['type_id'] ? 'selected' : '') . '>' . htmlentities($row['type_name'], ENT_QUOTES, 'UTF-8' ) . '</option>';

        }

        ?>

    </select>

    <input value="filter" type="submit" />

    <input value="reset" type="button" onclick="window.location='<?php echo $self_url; ?>upload.php'" />

</form>
<hr />
<h3>Manage Background Images</h3>
<table>

    <tr>

        <td>

            <form action="upload.php" method="post">

                <input type="hidden" name="act" value="manage" />

                Delete Selected Image: <input type="submit" value="Delete" name="action" /><br/>
           

                Change category: <select name="cat">

                    <option value="">Select</option>

                    <?php



                    @mysql_data_seek($category_res, 0);

                    while (($row=mysql_fetch_assoc($category_res))!==false) {

                        echo '<option value="' . $row['category_'] . '" ' . ($cat == $row['category_'] ? 'selected' : '') . '>' . htmlentities($row['name'], ENT_QUOTES, 'UTF-8' ) . '</option>';

                    }

                    ?>

                </select> <input type="submit" value="Update" name="action" />
            
        </td>

    </tr>

    <tr>

        <td>

            <?php

            if(empty($cat) && !empty($product_filter)) {
                $where = ($product_filter !== null && is_numeric($product_filter)) ? ' where c.ProductID= ' . mysql_escape_string($product_filter) : '';
            } else if(empty($cat) && !empty($type_filter)) {
                $where = ($type_filter !== null && is_numeric($type_filter)) ? ' where c.ProductType= ' . mysql_escape_string($type_filter) : '';
            } else if(!empty($cat)){

                $where = ($cat !== null && is_numeric($cat)) ? ' where c.category_ = ' . mysql_escape_string($cat) : '';
            } 
            $qry = 'select c.*, ct.name as cname from card as c left join category as ct on (ct.category_ = c.category_)' . $where;

            $res = mysql_query($qry) or die(mysql_error());
            ?> 
            <?php
            while (($row=mysql_fetch_assoc($res))!==false) {
                $sql_pro="select * from products where Product_Id=".$row['ProductId'];
                $res_pro = mysql_query($sql_pro) or die(mysql_error());
                $rs_pro=mysql_fetch_array($res_pro);

                if(!empty($rs_pro['Product_Name'])) {
                    $product_name=" | " . $rs_pro['Product_Name'];
                } else {
                    $product_name='';
                }

                $sql_type="select * from product_types where type_id=".$row['ProductType'];
                $res_type = mysql_query($sql_type) or die(mysql_error());
                $rs_type=mysql_fetch_array($res_type);
                if(!empty($rs_type['type_name'])) {
                    $type_name="| " . $rs_type['type_name'];
                } else {
                    $type_name='';
                }
                $image_url = '';?>
            <?php 
             /*   echo "<div class='card_block' style='    border-bottom: 1px solid #CCCCCC;    float: left;    margin: 10px;    padding-bottom: 10px;    width: 200px; height:auto;'>

                		<div style='width:191px;height:170px'><a href=" . $image_url . "images/full/" . $row['filename'] . " target='_blank'><img src=" . $image_url . "images/thumbnail/" . $row['filename'] .' /></a></div><br />

                	  	<label for="id_' . $row['card_'] . '">select:</label> <input id="id_' . $row['card_'] . '" type="checkbox" name="cid[]" value="' . $row['card_'] . '" /> <span class="added">added: ' . $row['adddate'] . '</span><br />

                	  	<span  style="font-size:13px;">' . htmlentities($row['cname'], ENT_QUOTES, 'UTF-8' ) .' '.$product_name .' '.$type_name. '</span>

                	  </div>';  */
					  
					          echo "<div class='card_block' style='    border-bottom: 1px solid #CCCCCC;    float: left;    margin: 10px;    padding-bottom: 10px;    width: 200px; height:auto;'>

                		<div style='width:191px;height:170px'><a href=" . $image_url . "images/full/" . $row['filename'] . " target='_blank'><img src=" . $image_url . "images/thumbnail/" . $row['filename'] .' /></a></div><br />

                	  	<label for="id_' . $row['card_'] . '">select:</label> <input id="id_' . $row['card_'] . '" type="checkbox" name="cid[]" value="' . $row['card_'] . '" /> <span class="added">added: ' . $row['adddate'] . '</span><br />

                	  	<span  style="font-size:13px;">' . htmlentities($row['cname'], ENT_QUOTES, 'UTF-8' ) .' '.$product_name .' '.$type_name. '</span>

                	  </div>';

            }



?>




</form>

        </td>

    </tr>

</table>


<!--<h3>Manage Categories</h3>

<form action="<?php // echo $self_url; ?>upload.php" method="post">

    <input type="hidden" name="act" value="catedit" />

    <table>

        <tr>

            <td>

                <input type="submit" name="action" value="Update" />

            </td>

            <td>

		Delete

            </td>

        </tr>

        <?php


//
//        @mysql_data_seek($category_res, 0);
//
//        $cnt = 0;
//
//        while (($row=mysql_fetch_assoc($category_res))!==false) {
//
//            $cnt++;

    ?>

        <tr>

            <td><input name="cat_name[<?php // echo $row['category_']; ?>]" value="<?php

                //    echo htmlentities($row['name'], ENT_QUOTES, 'UTF-8' );

    ?>" />

            </td>

            <td>

                <input type="checkbox" name="cat_check[<?php // echo $row['category_']; ?>]" />

            </td>

        </tr>

            <?php

     //   }

       // if ($cnt > 5) {

    ?>

        <tr>

            <td>

                <input type="submit" name="action" value="Update" />

            </td>

            <td>

		Delete

            </td>

        </tr>

    <?php // }; ?>

    </table>

</form>





Add Category:<br />

<form action="<?php echo $self_url; ?>upload.php" method="post">

    <input type="hidden" name="act" value="catedit" />

    <input type="hidden" name="action" value="add" />

    <input type="text" value="" name="cat_name" /><br />

    <input type="submit" value="Save" />

</form>-->


<?php require_once 'htmlfoot.php'; ?>

<?php



$data = ob_get_contents();

ob_clean();



echo $data;

?>