<?php 

ob_start();

session_start();

require_once("../dbconnect.php");
require_once("img_upload.php");

ini_set('memory_limit', '512M');

//For Add Card Image

function createthumb($name, $newname, $new_w, $new_h, $border=false, $transparency=true, $base64=false) {

    if(file_exists($newname))

        @unlink($newname);

    if(!file_exists($name))

        return false;

    $arr = split("\.",$name);

    $ext = $arr[count($arr)-1];



    if($ext=="jpeg" || $ext=="jpg"){

        $img = @imagecreatefromjpeg($name);

    } elseif($ext=="png"){

        $img = @imagecreatefrompng($name);

    } elseif($ext=="gif") {

        $img = @imagecreatefromgif($name);

    }

    if(!$img)

        return false;

    $old_x = imageSX($img);

    $old_y = imageSY($img);

	$thumb_w = $new_w;

    $thumb_h = $new_h;

  

    $thumb_w = ($thumb_w<1) ? 1 : $thumb_w;

    $thumb_h = ($thumb_h<1) ? 1 : $thumb_h;

    $new_img = ImageCreateTrueColor($thumb_w, $thumb_h);

   

    if($transparency) {

        if($ext=="png") {

            imagealphablending($new_img, false);

            $colorTransparent = imagecolorallocatealpha($new_img, 0, 0, 0, 127);

            imagefill($new_img, 0, 0, $colorTransparent);

            imagesavealpha($new_img, true);

        } elseif($ext=="gif") {

            $trnprt_indx = imagecolortransparent($img);

            if ($trnprt_indx >= 0) {

                //its transparent

                $trnprt_color = imagecolorsforindex($img, $trnprt_indx);

                $trnprt_indx = imagecolorallocate($new_img, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);

                imagefill($new_img, 0, 0, $trnprt_indx);

                imagecolortransparent($new_img, $trnprt_indx);

            }

        }

    } else {

        Imagefill($new_img, 0, 0, imagecolorallocate($new_img, 255, 255, 255));

    }

   

    imagecopyresampled($new_img, $img, 0,0,0,0, $thumb_w, $thumb_h, $old_x, $old_y);

    if($border) {

        $black = imagecolorallocate($new_img, 0, 0, 0);

        imagerectangle($new_img,0,0, $thumb_w, $thumb_h, $black);

    }

    if($base64) {

        ob_start();

        imagepng($new_img);

        $img = ob_get_contents();

        ob_end_clean();

        $return = base64_encode($img);

    } else {

        if($ext=="jpeg" || $ext=="jpg"){

            imagejpeg($new_img, $newname);

            $return = true;

        } elseif($ext=="png"){

            imagepng($new_img, $newname);

            $return = true;

        } elseif($ext=="gif") {

            imagegif($new_img, $newname);

            $return = true;

        }

    }

    imagedestroy($new_img);

    imagedestroy($img);

    return $return;

}





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



switch ($action) {

	

    case 'add' :

        addProduct();

        break;

		

    case 'edit' :

        editProduct();

        break;

		

		

	case 'delete' :

      deleteProduct();

        break;

		

    case 'status' :

	   statusProduct();

        break;	

		

	case 'metatag' :

	     metatagproduct();

         break;	

		 

	case 'seld' :

	     seld();

         break;	

		

		

		default :

        // if action is not defined or unknown

        // move to main category page

        header('Location: maincategory.php');

		

		}

		

		

		

	/*

    Add Product

*/

function addProduct()

{
  $image_path = $_SERVER['DOCUMENT_ROOT']."/zeaplocks/"; 

	if($_POST['subcat']!="")

{

	 $subid=$_POST['subcat'];

}

else

{

	$subid=0;

}

if($_POST['subsubcat']!="")

{

	$subsubid=$_POST['subsubcat'];

}

else

{

	$subid=0;

}



/*if($_POST['subsubsubcat']!="")

{

	$subsubsubid=$_POST['subsubsubcat'];

}

else

{

	$subsubsubid=0;

}
*/
  get_magic_quotes_gpc();

	

	if(!get_magic_quotes_gpc())

	{

	$pname=addslashes($_POST['pname']);

	}

	else

	{

	$pname=$_POST['pname'];

	}

	  $catid=$_POST['catid'];

	   $subcatid=$_POST['subcat'];

	  $pname=addslashes($_POST['pname']);

	 $pdes=addslashes($_POST['description']);

	

	

if($_POST['price']!="") {

$price1= $_POST['price']; 

foreach($price1 as $price)

{

if($price!="")

{

if($priced){

$priced.=",".$price;

}else{

$priced=$price;

}

}



}

}

$strf=date('Y-m-d');
$image = $_FILES["file"]["name"];
// image upload code in zeaplocks folder
if($_FILES["file"]["name"]!=""){
	$err = "";
	$img_upload = new img_upload($image_path,"file");
	$img_upload->setThumb("th-",350,350);
	$fileName = $img_upload->process($err);
	
}	 

	  $sql= "INSERT INTO products1(categeryid, subcetid,itemno,pdname,size,description,weight,price,status,qtycase,qty,image)VALUES('".$_POST['catid']."','".$_POST['subcat']."','".$_POST['itemno']."','".$_POST['pdname']."','".$_POST['size']."','".$_POST['description']."','".$_POST['weight']."','".$_POST['price']."',1,'".$_POST['case']."','".$_POST['qty']."','".$fileName."')";

		

		

		  $result = mysql_query($sql);

	

	    $lastid=mysql_insert_id();

}

header("Location:subcategory1.php?padd=padd&categiry_id=".$_POST['catid']."&subcat=".$_POST['subcat'].""); 

	 



     


	/*

    Edit Product

*/



function editProduct()

{

$image_path = $_SERVER['DOCUMENT_ROOT']."/zeaplocks/"; 
$image = $_FILES["file"]["name"];
// image upload code in zeaplocks folder
if($_FILES["file"]["name"]!=""){
	$err = "";
	$img_upload = new img_upload($image_path,"file");
	$img_upload->setThumb("th-",350,350);
	$fileName = $img_upload->process($err);
	
}
	if($_POST['subcat']!="")

{

	 $subid=$_POST['subcat'];

}



  get_magic_quotes_gpc();

	

	if(!get_magic_quotes_gpc())

	{

	$pname=addslashes($_POST['pdname']);

	}

	else

	{

	$pname=$_POST['pdname'];

	}

	  $catid=$_POST['catid'];

	   $subcatid=$_POST['subcat'];
	   
       $prodid=$_POST['pdid'];

	  $pname=addslashes($_POST['pdname']);

	 $pdes=addslashes($_POST['description']);

	

  if($_POST['price']!="") {

$price1= $_POST['price']; 

foreach($price1 as $price)

{

if($price!="")

{

if($priced){

$priced.=",".$price;

}else{

$priced=$price;

}

}



}

}

if($_POST['size']!="") {

$size1= $_POST['size']; 

foreach($size1 as $size)

{

if($size!="")

{

if($sizea){

$sizea.=",".$size;

}else{

$sizea=$size;

}

}



}

}

	
	 
	if($image!=""){	
	 $sql= "Update products1 SET itemno='".$_POST['itemno']."',pdname='".$_POST['pdname']."',

	   size='".$_POST['size']."',

	   description='".$_POST['description']."',

	   weight = '".$_POST['weight']."',

	   price = '".$_POST['price']."',

	   qtycase='".$_POST['case']."',

	    qty='".$_POST['qty']."' ,
	    image='".$fileName."' 
		
	  where pdid=".$prodid;

	}
	else{
		$sql= "Update products1 SET itemno='".$_POST['itemno']."',pdname='".$_POST['pdname']."',

	   size='".$_POST['size']."',

	   description='".$_POST['description']."',

	   weight = '".$_POST['weight']."',

	   price = '".$_POST['price']."',

	   qtycase='".$_POST['case']."',

	    qty='".$_POST['qty']."' 
	    
		
	  where pdid=".$prodid;
	}	

		  $result = mysql_query($sql) or die('Cannot ' . mysql_error());

	

	    $lastid=$_POST['prodid'];
       
	  header("Location: subcategory1.php?pedit=pedit&categiry_id=".$_POST['catid']."&subcat=".$_POST['subcat']."");  
	 	
	     






}


	/*

    Delte Product

*/



function deleteProduct()

{





  $myFile1 = "../images/products/".$prodid.".jpg";

	unlink($myFile1);



	$myFile2 = "../images/products/preview/".$prodid.".jpg";

	unlink($myFile2);





 $sql ="DELETE FROM products1  WHERE  pdid=".$prodid;

			mysql_query($sql);

header("Location: subcategory1.php?pdel=pdel&categiry_id=".$_POST['catid']."&subcat=".$_POST['subcat']."");
}




/*

    Status Product

*/



 

 function statusProduct()

{

$pid=$_POST['pdid'];	

$catid=$_POST['catid'];	

$subcat=$_POST['subcat'];	

//$subsub=$_GET['subsub'];

$status=$_POST['status'];


$sql="UPDATE products1 SET status='".$status."' WHERE pdid=".$pid;		

	        $result = mysql_query($sql);	
		
 if($_GET['sub']!="")
	 
	 {
	  header('Location: addsubcategory1.php?&categiry_id='.$catid.'&subcat='.$subcat);  
	 }


}

 //Selected Id//

/*function seld()

{

  $selid=$_POST['selid'];

  $catid=$_POST['catid'];

  $subcat=$_POST['subcat'];

  $subsub=$_POST['subsub'];

  

  $pid=$_POST['pid'];

   



$querys="UPDATE products SET deselect='".$selid."' where pid=".$pid;

$results=mysql_query($querys);

//header("Location: addsubcategory.php?catid=".$catid."&subcat=".$subcat."");   



	 

	 header("Location:subsubcategory.php?maincatid=".$_GET['maincatid']."&categiry_id=".$catid."&subcat=".$_POST['subcatid'].""); 



}
*/
///





?>

