<? 

ob_start();

session_start();

require_once("../dbconnect.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()

{

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

{

	 $subid=$_POST['subcat'];

}

else

{

	$subid=0;

}

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

{

	$subsubid=$_POST['subsubcat'];

}

else

{

	$subsubid=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['unit']!="" && $_POST['price']!="") 

	{

		$mixval11=array();

		for($i=0;$i<$_POST['firstcnt'];$i++)

		{

			$mixval11[$i]=$_POST['unit'][$i]."-".$_POST['price'][$i];	

		}

		foreach($mixval11 as $mxval11)

		$resquan11=implode(',', $mixval11);

     }*/

	 

	 

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

$material1= $_POST['min']; 

foreach($material1 as $valuem)

{

if($valuem!="")

{

if($avail5){

$avail5.=",".$valuem;

}else{

$avail5=$valuem;

}

}



}

}

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

$color1= $_POST['max']; 

foreach($color1 as $valuec)

{

if($valuec!="")

{

if($colored){

$colored.=",".$valuec;

}else{

$colored=$valuec;

}

}



}

}

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($_POST['color']!="") 

	{

		$mixva=array();

		for($i=0;$i<$_POST['secondcnt'];$i++)

		{

			$mixva[$i]=$_POST['color'][$i];	

		}

		foreach($mixva as $mxva)

		$resquan=implode(',', $mixva);

    }

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

    {

		$cat= $_POST['relatedprods']; 

		foreach($cat as $value)

		$catstr=implode(',',$cat);

    }

$strf=date('Y-m-d');

	 

	  echo  $sql= "INSERT INTO products(catid,subcat_id,subsubcatid,itemno,pname,unitsprice,colors,description,discount,relatedprods,mini,maxi,status,addedon,weight,sizes,image1,image2,image3,image4,image5,Print)VALUES('".$catid."','".$_POST['subcat']."','".$_POST['subsubcat']."','".$_POST['itemno']."','".$pname."','".$priced."','".$resquan."','".$pdes."','".$_POST['discount']."','".$catstr."','".$avail5."','".$colored."',1,'".$strf."','".$_POST['weight']."','".$sizea."','".$_FILES['prodimg1']['name']."','".$_FILES['prodimg2']['name']."','".$_FILES['prodimg3']['name']."','".$_FILES['prodimg4']['name']."','".$_FILES['prodimg5']['name']."','".$_POST['checkbox']."')";

		

		

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

	

	    $lastid=mysql_insert_id();



if($_FILES['prodimg1']['name']!="") {



$ftmp = $_FILES['prodimg1']['tmp_name'];

            $oname = $_FILES['prodimg1']['name'];

            $fname = '../images/waste/'.$_FILES['prodimg1']['name'];

			$userfile_type = $_FILES['prodimg1']['type'];

            

            $sizes = getimagesize($ftmp);

            $width	=	$sizes[0];

            $height	=	$sizes[1];

    

            $extenssion = strstr($oname, ".");

        

            $prod_img		= $fname;	

            move_uploaded_file($ftmp, $prod_img);

			

			createthumb($fname, "../images/products/".$lastid."-1.jpg", 350,350,false, true, false);

            

			



unlink('../images/waste/'.$_FILES['prodimg1']['name']);



}

if($_FILES['prodimg2']['name']!="") {



$ftmp = $_FILES['prodimg2']['tmp_name'];

            $oname = $_FILES['prodimg2']['name'];

            $fname = '../images/waste/'.$_FILES['prodimg2']['name'];

			$userfile_type = $_FILES['prodimg2']['type'];

            

            $sizes = getimagesize($ftmp);

            $width	=	$sizes[0];

            $height	=	$sizes[1];

    

            $extenssion = strstr($oname, ".");

        

            $prod_img		= $fname;	

            move_uploaded_file($ftmp, $prod_img);

			

			createthumb($fname, "../images/products/".$lastid."-2.jpg", 350,350,false, true, false);

            

			



unlink('../images/waste/'.$_FILES['prodimg2']['name']);



}

if($_FILES['prodimg3']['name']!="") {



$ftmp = $_FILES['prodimg3']['tmp_name'];

            $oname = $_FILES['prodimg3']['name'];

            $fname = '../images/waste/'.$_FILES['prodimg3']['name'];

			$userfile_type = $_FILES['prodimg3']['type'];

            

            $sizes = getimagesize($ftmp);

            $width	=	$sizes[0];

            $height	=	$sizes[1];

    

            $extenssion = strstr($oname, ".");

        

            $prod_img		= $fname;	

            move_uploaded_file($ftmp, $prod_img);

			

			createthumb($fname, "../images/products/".$lastid."-3.jpg", 350,350,false, true, false);

            

			



unlink('../images/waste/'.$_FILES['prodimg3']['name']);



}


if($_FILES['prodimg4']['name']!="") {



$ftmp = $_FILES['prodimg4']['tmp_name'];

            $oname = $_FILES['prodimg4']['name'];

            $fname = '../images/waste/'.$_FILES['prodimg4']['name'];

			$userfile_type = $_FILES['prodimg4']['type'];

            

            $sizes = getimagesize($ftmp);

            $width	=	$sizes[0];

            $height	=	$sizes[1];

    

            $extenssion = strstr($oname, ".");

        

            $prod_img		= $fname;	

            move_uploaded_file($ftmp, $prod_img);

			

			createthumb($fname, "../images/products/".$lastid."-4.jpg", 350,350,false, true, false);

            

			



unlink('../images/waste/'.$_FILES['prodimg4']['name']);

}


if($_FILES['prodimg5']['name']!="") {



$ftmp = $_FILES['prodimg5']['tmp_name'];

            $oname = $_FILES['prodimg5']['name'];

            $fname = '../images/waste/'.$_FILES['prodimg5']['name'];

			$userfile_type = $_FILES['prodimg5']['type'];

            

            $sizes = getimagesize($ftmp);

            $width	=	$sizes[0];

            $height	=	$sizes[1];

    

            $extenssion = strstr($oname, ".");

        

            $prod_img		= $fname;	

            move_uploaded_file($ftmp, $prod_img);

			

			createthumb($fname, "../images/products/".$lastid."-5.jpg", 350,350,false, true, false);

            

			



unlink('../images/waste/'.$_FILES['prodimg5']['name']);



}

/*

if($subsubid==0)

{

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

}else {

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

}	 
*/


     if($_POST['subsub']!="")
     {
    header("Location:lastsubsub.php?padd=padd&catid=".$_POST['catid']."&subcat=".$_POST['subcat']."&subsub=".$_POST['subsub']);  
     }
     elseif($_POST['subcat']!="" || $_POST['subsub']!="")
     {
     header("Location:subsubcategory.php?padd=padd&catid=".$_POST['catid']."&subcat=".$_POST['subcat']);
     }
     else
     {
      header('Location: addsubcategory.php?padd=padd&catid='.$_POST['catid']); 
     }     
		      

}



	/*

    Edit Product

*/



function editProduct()

{



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

{

	 $subid=$_POST['subcat'];

}

else

{

	$subid=0;

}

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

{

	$subsubid=$_POST['subsubcat'];

}

else

{

	$subsubid=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['min']!="") {

$material1= $_POST['min']; 

foreach($material1 as $valuem)

{

if($valuem!="")

{

if($avail5){

$avail5.=",".$valuem;

}else{

$avail5=$valuem;

}

}



}

}

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

$color1= $_POST['max']; 

foreach($color1 as $valuec)

{

if($valuec!="")

{

if($colored){

$colored.=",".$valuec;

}else{

$colored=$valuec;

}

}



}

}



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($_POST['color']!="") 

	{

		$mixva=array();

		for($i=0;$i<count($_POST['color']);$i++)

		{

			$mixva[$i]=$_POST['color'][$i];	

		}

		foreach($mixva as $mxva)

		$resquan=implode(',', $mixva);

    }

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

    {

		$cat= $_POST['relatedprods']; 

		foreach($cat as $value)

		$catstr=implode(',',$cat);

    }

	 

	 $sql= "Update products SET itemno='".$_POST['itemno']."',pname='".$pname."',

	   unitsprice='".$priced."',

	   description='".$pdes."',

	   colors = '".$resquan."',

	   relatedprods = '".$catstr."',

	   mini='".$avail5."',

	    maxi='".$colored."',

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

		 sizes = '".$sizea."',		
         
		 Print='".$_POST['checkbox']."'
		 
	  where pid=".$_POST['prodid'];

		

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

	

	    $lastid=$_POST['prodid'];

	

	if($_FILES['prodimg1']['name']!="") {



$ftmp = $_FILES['prodimg1']['tmp_name'];

            $oname = $_FILES['prodimg1']['name'];

            $fname = '../images/waste/'.$_FILES['prodimg1']['name'];

			$userfile_type = $_FILES['prodimg1']['type'];

            

            $sizes = getimagesize($ftmp);

            $width	=	$sizes[0];

            $height	=	$sizes[1];

    

            $extenssion = strstr($oname, ".");

        

            $prod_img		= $fname;	

            move_uploaded_file($ftmp, $prod_img);

			

			createthumb($fname, "../images/products/".$_POST['prodid']."-1.jpg", 350,350,false, true, false);

            $sql= mysql_query("Update products SET image1='".$_FILES['prodimg1']['name']."'  where pid=".$_POST['prodid']);

			



unlink('../images/waste/'.$_FILES['prodimg1']['name']);



}

if($_FILES['prodimg2']['name']!="") {



$ftmp = $_FILES['prodimg2']['tmp_name'];

            $oname = $_FILES['prodimg2']['name'];

            $fname = '../images/waste/'.$_FILES['prodimg2']['name'];

			$userfile_type = $_FILES['prodimg2']['type'];

            

            $sizes = getimagesize($ftmp);

            $width	=	$sizes[0];

            $height	=	$sizes[1];

    

            $extenssion = strstr($oname, ".");

        

            $prod_img		= $fname;	

            move_uploaded_file($ftmp, $prod_img);

			

			createthumb($fname, "../images/products/".$_POST['prodid']."-2.jpg", 350,350,false, true, false);

            $sql= mysql_query("Update products SET image2='".$_FILES['prodimg2']['name']."'  where pid=".$_POST['prodid']);

			



unlink('../images/waste/'.$_FILES['prodimg2']['name']);



}

if($_FILES['prodimg3']['name']!="") {



$ftmp = $_FILES['prodimg3']['tmp_name'];

            $oname = $_FILES['prodimg3']['name'];

            $fname = '../images/waste/'.$_FILES['prodimg3']['name'];

			$userfile_type = $_FILES['prodimg3']['type'];

            

            $sizes = getimagesize($ftmp);

            $width	=	$sizes[0];

            $height	=	$sizes[1];

    

            $extenssion = strstr($oname, ".");

        

            $prod_img		= $fname;	

            move_uploaded_file($ftmp, $prod_img);

			

			createthumb($fname, "../images/products/".$_POST['prodid']."-3.jpg", 350,350,false, true, false);

            

			$sql= mysql_query("Update products SET image3='".$_FILES['prodimg3']['name']."'  where pid=".$_POST['prodid']);



unlink('../images/waste/'.$_FILES['prodimg3']['name']);



}

if($_FILES['prodimg4']['name']!="") {



$ftmp = $_FILES['prodimg4']['tmp_name'];

            $oname = $_FILES['prodimg4']['name'];

            $fname = '../images/waste/'.$_FILES['prodimg4']['name'];

			$userfile_type = $_FILES['prodimg4']['type'];

            

            $sizes = getimagesize($ftmp);

            $width	=	$sizes[0];

            $height	=	$sizes[1];

    

            $extenssion = strstr($oname, ".");

        

            $prod_img		= $fname;	

            move_uploaded_file($ftmp, $prod_img);

			

			createthumb($fname, "../images/products/".$_POST['prodid']."-4.jpg", 350,350,false, true, false);

            

			$sql= mysql_query("Update products SET image3='".$_FILES['prodimg4']['name']."'  where pid=".$_POST['prodid']);



unlink('../images/waste/'.$_FILES['prodimg4']['name']);



}

if($_FILES['prodimg5']['name']!="") {



$ftmp = $_FILES['prodimg5']['tmp_name'];

            $oname = $_FILES['prodimg5']['name'];

            $fname = '../images/waste/'.$_FILES['prodimg5']['name'];

			$userfile_type = $_FILES['prodimg5']['type'];

            

            $sizes = getimagesize($ftmp);

            $width	=	$sizes[0];

            $height	=	$sizes[1];

    

            $extenssion = strstr($oname, ".");

        

            $prod_img		= $fname;	

            move_uploaded_file($ftmp, $prod_img);

			

			createthumb($fname, "../images/products/".$_POST['prodid']."-5.jpg", 350,350,false, true, false);

            

			$sql= mysql_query("Update products SET image5='".$_FILES['prodimg5']['name']."'  where pid=".$_POST['prodid']);



unlink('../images/waste/'.$_FILES['prodimg5']['name']);



}




if($subsubid==0)

{

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

}else
{
	if($_POST['subsub']!="")
	 {
    header("Location:lastsubsub.php?pdel=pdel&catid=".$_POST['catid']."&subcat=".$_POST['subcat']."&subsub=".$_POST['subsub']);   
	 }
	 elseif($_POST['subcat']!="" || $_POST['subsub']!="")
	 {
	 header("Location:subsubcategory.php?pdel=pdel&catid=".$_POST['catid']."&subcat=".$_POST['subcat']); 
	 }
	 else
	 {
	  header('Location: addsubcategory.php?pdel=pdel&catid='.$_POST['catid']);  
	 }	
	 }     







}

	/*

    Delte Product

*/



function deleteProduct()

{





  $myFile1 = "../images/products/".$_POST['prodid'].".jpg";

	unlink($myFile1);



	$myFile2 = "../images/products/preview/".$_POST['prodid'].".jpg";

	unlink($myFile2);





 $sql ="DELETE FROM products  WHERE  pid=".$_POST['prodid'];

			mysql_query($sql);

			

			

   if($_POST['subsub']!="")
     {
    header("Location:lastsubsub.php?pdel=pdel&catid=".$_POST['catid']."&subcat=".$_POST['subcat']."&subsub=".$_POST['subsub']);  
     }
     elseif($_POST['subcat']!="" || $_POST['subsub']!="")
     {
     header("Location:subsubcategory.php?pdel=pdel&catid=".$_POST['catid']."&subcat=".$_POST['subcat']);
     }
     else
     {
      header('Location: addsubcategory.php?pdel=pdel&catid='.$_POST['catid']); 
     }   
			



}



/*

    Status Product

*/



 

 function statusProduct()

{

$pid=$_GET['prodid'];	

$catid=$_GET['catid'];	

$subcat=$_GET['subcat'];	

$subsub=$_GET['subsub'];

$status=$_GET['status'];


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

	        $result = mysql_query($sql) or die('Cannot add category' . mysql_error());	
		
   if($_GET['subsub']!="")
	 {
    header("Location:lastsubsub.php?catid=".$_GET['catid']."&subcat=".$_GET['subcat']."&subsub=".$_GET['subsub']);   
	 }
	 elseif($_GET['subcat']!="" || $_GET['subsub']!="")
	 {
	 header("Location:subsubcategory.php?catid=".$_GET['catid']."&subcat=".$_GET['subcat']); 
	 }
	 else
	 {
	  header('Location: addsubcategory.php?&catid='.$_GET['catid']);  
	 }
}



/*

    Mata tag Product

*/



 function metatagproduct()

{

	

$pid=$_POST['prodid'];	

$catid=$_POST['catid'];	

 $subcat=$_POST['subcat'];	



$subsub=$_POST['subsub'];





	get_magic_quotes_gpc();

	

	if(!get_magic_quotes_gpc())

	{

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

	}

	else

	{

	$mtitle=$_POST['mtitle'];

	}





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

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







$sql="UPDATE products SET mtitle='".$mtitle."',mkeywords='".$mkeywords."',mdescription='".$mdescription."' where pid=".$pid; 



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



     if($_POST['subsub']!="")
     {
    header("Location:lastsubsub.php?catid=".$_POST['catid']."&subcat=".$_POST['subcat']."&subsub=".$_POST['subsub']);  
     }
     elseif($_POST['subcat']!="" || $_POST['subsub']!="")
     {
     header("Location:subsubcategory.php?catid=".$_POST['catid']."&subcat=".$_POST['subcat']);
     }
     else
     {
      header('Location: addsubcategory.php?catid='.$_POST['catid']); 
     }
				



	

	

	

}

	

 //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']."&catid=".$_POST['catid']."&subcat=".$_POST['subcatid']); 



}

///





?>

