<?php 
ob_start();
session_start();
require_once("../dbconnect.php");
ini_set('memory_limit', '512M');
//For Add Card Image
function watermark($source_file,$destination_file) {
	$main_img 		= $source_file; // main big photo / picture
	$watermark_img	= "../images/123.gif"; // use GIF or PNG, JPEG has no tranparency support
	$padding 		= 3; // distance to border in pixels for watermark image
	$opacity		= 100;	// image opacity for transparent watermark
	
	$watermark 	= imagecreatefromgif($watermark_img); // create watermark
	$image 		= imagecreatefromjpeg($main_img); // create main graphic
	
	//if(!$image || !$watermark) die("Error: main image or watermark could not be loaded!");
	$watermark_size 	= getimagesize($watermark_img);
	$watermark_width 	= $watermark_size[0];  
	$watermark_height 	= $watermark_size[1];  
	
	$image_size 	= getimagesize($main_img);  
	$dest_x 		= $image_size[0] - $watermark_width - $padding;  
	$dest_y 		= $image_size[1] - $watermark_height - $padding;
	imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 5, $watermark_width, $watermark_height, $opacity);
	imagejpeg($image,$destination_file);  
}

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" || $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"  || $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 'special' :

	   specialProduct();

        break;
		
		 case 'index' :

	   indexProduct();

        break;	
		
		
		 case 'top' :

	   topProduct();

        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()

{

$strf=date('Y-m-d');
$format="%Y-%m-%d %H:%M:%S";
$strf=strftime($format);

if($_POST['mec_specifi']!="")
{
	$mec_specifi=$_POST['mec_specifi'];
	foreach($mec_specifi as $value1)
	$mec_specifi123=implode(',',$mec_specifi);
}

echo $sql="INSERT INTO  `mec_product` (`mec_catid` ,`mec_subcatid` , `mec_subsubid`, `mec_brd_cat`, `mec_brd_sub`, `mec_brd_subsub`, `mec_itemno` ,`mec_pname` ,`mec_specifi` ,`mec_price` ,`mec_price2` ,`mec_price3` ,`mec_pricenme1` ,`mec_pricenme2` ,`mec_pricenme3` ,`mec_price4` ,`mec_pricenme4` ,`mec_qty` ,`mec_description` ,`mec_weight` ,`mec_image1` ,`mec_image2` ,`mec_status`)
VALUES ('".$_POST['mec_catid']."',  '".$_POST['mec_subcatid']."', '".$_POST['mec_subsubid']."', '".$_SESSION['mec_brdid']."', '".$_SESSION['mec_subbrdid']."', '".$_POST['mec_brd_subsub']."', '".$_POST['mec_itemno']."',  '".$_POST['mec_pname']."', '".$mec_specifi123."',  '".$_POST['mec_price']."',  '".$_POST['mec_price2']."',  '".$_POST['mec_price3']."',  '".$_POST['mec_pricenme1']."',  '".$_POST['mec_pricenme2']."',  '".$_POST['mec_pricenme3']."',  '".$_POST['mec_price4']."',  '".$_POST['mec_pricenme4']."',  '".$_POST['mec_qty']."',  '".$_POST['mec_description']."',  '".$_POST['mec_weight']."',  '".$_FILES['prodimg1']['name']."',  '".$_FILES['prodimg2']['name']."',  '1')";

		  $result = mysql_query($sql) or die('Cannot ' . mysql_error());
	    $lastid=mysql_insert_id();
for($img=1;$img<=5;$img++)
	{
if($_FILES['prodimg'.$img]['name']!="") {
$ftmp = $_FILES['prodimg'.$img]['tmp_name'];

            $oname = $_FILES['prodimg'.$img]['name'];

            $fname = '../products/waste/'.$_FILES['prodimg'.$img]['name'];

			$userfile_type = $_FILES['prodimg'.$img]['type'];
            $sizes = getimagesize($ftmp);
            $width	=	$sizes[0];
            $height	=	$sizes[1];
            $extenssion = strstr($oname, ".");
            $prod_img		= $fname;	
move_uploaded_file($ftmp, $prod_img);
 
		  createthumb($fname, "../products/thumb/".$lastid."_$img.jpg",367,200, false, true, false);
		  createthumb($fname, "../products/details/".$lastid."_$img.jpg",600,600, false, true, false);
 $thumb="../products/thumb/".$lastid."_$img.jpg";
  $big="../products/big/".$lastid."_$img.jpg";	
   $large="../products/large/".$lastid."_$img.jpg";	
    $detail="../products/detail/".$lastid."_$img.jpg";	
 $image2="../products/detail/".$lastid."_$img.jpg";		
 watermark($image2,$image2);
 watermark($thumb,$thumb);
 watermark($big,$big);
 watermark($large,$large);
 watermark($detail,$detail);
unlink('../products/waste/'.$_FILES['prodimg1']['name']);
}
}
header("location: products.php?mec_catid=".$_POST['mec_catid']."&mec_subcatid=".$_POST['mec_subcatid']."&mec_subsubid=".$_POST['mec_subsubid']."");
}
	/*

    Edit Product

*/
function editProduct()

{
  get_magic_quotes_gpc();
	if(!get_magic_quotes_gpc())

{

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

	}

	else

	{

	$pname=$_POST['pname'];

	}


if($_POST['mec_specifi']!="")
{
	$mec_specifi=$_POST['mec_specifi'];
	foreach($mec_specifi as $value1)
	$mec_specifi123=implode(',',$mec_specifi);
}
	 

 $sql= "Update mec_product SET 
	 mec_itemno='".$_POST['mec_itemno']."',
      mec_pname='".$_POST['mec_pname']."',
	  mec_specifi='".$mec_specifi123."',
	 mec_price='".$_POST['mec_price']."',
	 	 mec_price2='".$_POST['mec_price2']."',
		 	 mec_price3='".$_POST['mec_price3']."',
			 	 mec_pricenme1='".$_POST['mec_pricenme1']."',
				 	 mec_pricenme2='".$_POST['mec_pricenme2']."',
					 	 mec_pricenme3='".$_POST['mec_pricenme3']."',
						 mec_price4='".$_POST['mec_price4']."',
						 mec_pricenme4='".$_POST['mec_pricenme4']."',
	   mec_qty='".$_POST['mec_qty']."',
	   mec_description='".addslashes($_POST['mec_description'])."',
	   mec_weight='".$_POST['mec_weight']."',
		mec_image1='".$_FILES['prodimg1']['name']."',
		mec_image2='".$_FILES['prodimg2']['name']."'		 
		 where mec_pid=".$_POST['mec_pid'];  
		  $result = mysql_query($sql) or die('Cannot ' . mysql_error());

	    $lastid=$_POST['mec_pid'];
//echo "<pre>"; print_r($_POST); echo "</pre>";'".$mec_specifi123."'
		//echo $_POST['p_id'];
		//echo $_POST['pages'];
		//exit ;
		echo  $upval1=count($_POST['anwar']);
		
		 
for($vv1=1;$vv1<=$upval1;$vv1++)
{
	
	/* echo "UPDATE `pgprice` SET 
`pages` = '".$_POST['pages'.$vv1]."',
`pprice` = '".$_POST['pprice'.$vv1]."' WHERE `r_id` =".$_POST['r_id'.$vv1];
*/


$pagesixe1=$_POST['pages'.$vv1];
$e_uquery1="UPDATE `pgprice` SET 
`pages` = '".addslashes($pagesixe1)."',
`pprice` = '".addslashes($_POST['pprice'.$vv1])."',
`test` = '".addslashes($_POST['test'.$vv1])."',
`weight` = '".addslashes($_POST['weight'.$vv1])."'

 WHERE `r_id` =".$_POST['r_id'.$vv1];
   
mysql_query($e_uquery1);
} 
  $v_count1=count($_POST['pages']);	
for($mm1=0;$mm1<$v_count1;$mm1++)
{
	$pagesize=$_POST['pages'][$mm1];
 $sam="INSERT INTO `pgprice` (`pi_d`, `pages`, `pprice` ,`test`, `weight`) VALUES (".$lastid.", '".addslashes($pagesize)."', '".addslashes($_POST['pprice'] [$mm1])."', '".addslashes($_POST['test'] [$mm1])."', '".addslashes($_POST['weight'] [$mm1])."')";
mysql_query($sam);
}
	
	
		
for($img=1;$img<=5;$img++)
	{
	

	if($_FILES['prodimg'.$img]['name']!="") {


		$ftmp = $_FILES['prodimg'.$img]['tmp_name'];



		$oname = $_FILES['prodimg'.$img]['name'];

		$fname = '../products/waste/'.$_FILES['prodimg'.$img]['name'];

		$userfile_type = $_FILES['prodimg'.$img]['type'];


		$sizes = getimagesize($ftmp);



		$width	=	$sizes[0];



		$height	=	$sizes[1];



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



		$prod_img		= $fname;	



		move_uploaded_file($ftmp, $prod_img);
		  //createthumb($fname, "../products/thumb/".$lastid."_$img.jpg",540,270, false, true, false);
 			createthumb($fname, "../products/thumb/".$lastid."_$img.jpg",367,200, false, true, false);
		  createthumb($fname, "../products/details/".$lastid."_$img.jpg",600,600, false, true, false);
		 $thumb="../products/thumb/".$lastid."_$img.jpg";
  $big="../products/big/".$lastid."_$img.jpg";	
   $large="../products/large/".$lastid."_$img.jpg";	
    $detail="../products/detail/".$lastid."_$img.jpg";
	$image2="../products/detail/".$lastid."_$img.jpg";		
 watermark($image2,$image2);
 watermark($thumb,$thumb);
 watermark($big,$big);
 watermark($large,$large);
 watermark($detail,$detail);
		
				
unlink('../products/waste/'.$_FILES['prodimg1']['name']);


	}
	}

header("location: products.php?mec_catid=".$_POST['mec_catid']."&mec_subcatid=".$_POST['mec_subcatid']."&mec_subsubid=".$_POST['mec_subsubid']."");
}

	/*

    Delte Product

*/



function deleteProduct()

{

$pid=$_GET['mec_pid'];	

$catid=$_GET['mec_catid'];	

$subcat=$_GET['mec_subcatid'];	



  $myFile1 = "../products/".$pid.".jpg";

	unlink($myFile1);



	$myFile2 = "../products/preview/".$pid.".jpg";

	unlink($myFile2);





 $sql ="DELETE FROM mec_product  WHERE  mec_pid=".$pid; 

			mysql_query($sql);


	 header("Location:products.php?mec_catid=".$_GET['mec_catid']."&mec_subcatid=".$_GET['mec_subcatid']."&mec_subsubid=".$_GET['mec_subsubid']); 


}



/*

    Status Product

*/



 

 function statusProduct()

{

$pid=$_GET['mec_pid'];	

$catid=$_GET['catid'];	

$subcat=$_GET['subcat'];	

$subsub=$_GET['subsub'];

$status=$_GET['mec_status'];











  $sql="UPDATE mec_product SET mec_status='".$status."' WHERE mec_pid='".$pid."'";		

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

	 header("Location:products.php?mec_catid=".$_GET['mec_catid']."&mec_subcatid=".$_GET['mec_subcatid']."&mec_subsubid=".$_GET['mec_subsubid']); 


}



function specialProduct()

{

$pid=$_GET['prodid'];	

$catid=$_GET['catid'];	

$subcat=$_GET['subcat'];	

$subsub=$_GET['subsub'];

$special=$_GET['special'];


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

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





if($_GET['subcat']=="0"){
	
	 header("Location:products.php?maincatid=".$_GET['maincatid']."&catid=".$_GET['catid']."&subcat=".$_GET['subcat']."&mec_subsubid=".$_GET['mec_subsubid']); 
	}else{

	 header("Location:products.php?maincatid=".$_GET['maincatid']."&catid=".$_GET['catid']."&subcat=".$_GET['subcat']."&mec_subsubid=".$_GET['mec_subsubid']); 
}


}
function indexProduct()

{

$pid=$_GET['prodid'];	

$catid=$_GET['catid'];	

$subcat=$_GET['subcat'];	

$subsub=$_GET['subsub'];

$special=$_GET['index'];


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

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





if($_GET['subcat']=="0"){
	
	 header("Location:products.php?maincatid=".$_GET['maincatid']."&catid=".$_GET['catid']."&subcat=".$_GET['subcat']."&mec_subsubid=".$_GET['mec_subsubid']); 
	}else{





	 header("Location:products.php?maincatid=".$_GET['maincatid']."&catid=".$_GET['catid']."&subcat=".$_GET['subcat']."&mec_subsubid=".$_GET['mec_subsubid']); 
}

 

}


function topProduct()

{

$pid=$_GET['prodid'];	

$catid=$_GET['catid'];	

$subcat=$_GET['subcat'];	

$subsub=$_GET['subsub'];

$top=$_GET['top'];


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

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





if($_GET['subcat']=="0"){
	
	 header("Location:products.php?maincatid=".$_GET['maincatid']."&catid=".$_GET['catid']."&subcat=".$_GET['subcat']."&mec_subsubid=".$_GET['mec_subsubid']); 
	}else{





	 header("Location:products.php?maincatid=".$_GET['maincatid']."&catid=".$_GET['catid']."&subcat=".$_GET['subcat']."&mec_subsubid=".$_GET['mec_subsubid']); 
}

 

}


/*
topProduct
    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']);

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







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



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





		       if($mtitle=="")

                { 

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

		         }

				else

                 {

					

     header("Location: products.php?maincatid=".$_GET['maincatid']."&seopedit=seopedit&suc=suc&prodid=".$_POST['prodid']."&catid=".$_POST['catid']."&subcat=".$_POST['subcat']."&mec_subsubid=".$_POST['mec_subsubid']);  	

				 }

				



	

	

	

}

	

 //Selected Id//

function seld()

{

  $selid=$_POST['selid'];

  $catid=$_POST['catid'];

  $subcat=$_POST['subcat'];

  $subsub=$_POST['subsub'];

  

  $pid=$_GET['pid'];

   



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

$results=mysql_query($querys);

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



	 

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



}

///
if($_GET['del'])
{
	//echo "DELETE FROM pgprice WHERE r_id=".$_GET['del'];
//exit ;
	$sql ="DELETE FROM pgprice WHERE r_id=".$_GET['del'];
			mysql_query($sql);
 
if($_GET['subcat']=="0"){
	
 header("Location:products.php?maincatid=".$_GET['maincatid']."&catid=".$_GET['catid']."&subcat=".$_GET['subcat']."&mec_subsubid=".$_GET['mec_subsubid']); 
	}else{





	 header("Location:addsubcategory.php?maincatid=".$_GET['maincatid']."&catid=".$_GET['catid']."&subcat=".$_GET['subcat']."&mec_subsubid=".$_GET['mec_subsubid']); 
}

}



?>
