<?php

ob_start();

require_once("../dbconnect.php");
//image code//
require_once("imagecrop.php");
//


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

switch ($action) {
	
    case 'add' :
        add();
        break;
		
    case 'edit' :
        edit();
        break;
		
		
	case 'delete' :
      deleteProduct();
        break;
		
  		
   
		
		default :
        // if action is not defined or unknown
        // move to main category page
        header('Location: gallery.php');
		
		}
		
		
		
	/*
    Add Product
*/
function add()
{

	
   $sql= "INSERT INTO images(name) 
              VALUES ('".$_FILES['file']['name']."')";
        $result = mysql_query($sql) or die('Cannot add category' . mysql_error());
	
	  $lastid=mysql_insert_id();
	
	if($_FILES['file']['name']!="")
	{
			$ftmp = $_FILES['file']['tmp_name'];
            $oname = $_FILES['file']['name'];
            $fname = '../gallery/waste/'.$_FILES['file']['name'];
			$userfile_type = $_FILES['file']['type'];
            
            $sizes = getimagesize($ftmp);
            $width	=	$sizes[0];
            $height	=	$sizes[1];
    
            $extenssion = strstr($oname, ".");
        
            $prod_img		= $fname;	
            move_uploaded_file($ftmp, $prod_img);
			
			createthumb($fname, "../gallery/".$lastid.".gif", 160,141,false, true, false);
            
		createthumb($fname, "../gallery/big/".$lastid.".gif", $width,$height,false, true, false);
			
			
	}

	 header("Location: gallery.php");  
	
}

	/*
    Edit Product
*/

function edit()
{

    
	 $pid=$_POST['i_id'];
	 
         if($_FILES['file']['name']!="")
       {	 
	 
	 

   
	     $sql="UPDATE images SET name='".$_FILES['file']['name']."'
			 WHERE i_id=".$pid;			
	        $result = mysql_query($sql) or die('Cannot add category' . mysql_error());	
            $lastid=$pid;	
	
	
	$ftmp = $_FILES['file']['tmp_name'];
            $oname = $_FILES['file']['name'];
            $fname = '../gallery/waste/'.$_FILES['file']['name'];
			$userfile_type = $_FILES['file']['type'];
            
            $sizes = getimagesize($ftmp);
            $width	=	$sizes[0];
            $height	=	$sizes[1];
    
            $extenssion = strstr($oname, ".");
        
            $prod_img= $fname;	
            move_uploaded_file($ftmp, $prod_img);
			
			createthumb($fname, "../gallery/".$lastid.".gif", 160,141,false, true, false);
            
			createthumb($fname, "../gallery/big/".$lastid.".gif", 300,300,false, true, false);
		
	}
	 
	  header("Location: gallery.php");  
}
	/*
    Delte Product
*/

function deleteProduct()
{

  $myFile1 = "../gallery/".$_POST['i_id'].".gif";
	unlink($myFile1);

	$myFile2 = "../gallery/big/".$_POST['i_id'].".gif";
	unlink($myFile2);


 $sql ="DELETE FROM images  WHERE  i_id=".$_POST['i_id'];
			mysql_query($sql);
			
	/*		
	if($_POST['k']!="")
{

header("Location: searchproduct.php?keyword=".$_POST['k']."&pdel=".pdel."");  		
	
}
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']);  
	 }	
	 
}	 
	 */
	 
			
	  header('Location: gallery.php');  

}

/*
    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['k']!="")
{

header("Location: searchproduct.php?keyword=".$_GET['k']);  		
	
}
else
{	


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







	 if($mtitle=="")
                { 
		 
    header("Location: searchproduct.php?keyword=".$_POST['k']);  
				}
				else
				{
				
		 header("Location:lastsubsub.php?seopedit=seopedit&prodid=".$_POST['prodid']."&catid=".$_POST['catid']."&subcat=".$_POST['subcat']."&subsub=".$_POST['subsub']);   
		 
		  header("Location: searchproduct.php?keyword=".$_POST['k']."&prodid=".$_POST['prodid']."&seopedit=seopedit&suc=".suc);  
				
				}




		
	
}
else
{





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



}	
	
	
	
}
	
 //Selected Id//
function seld()
{
  $selid=$_POST['selid'];
  $catid=$_POST['catid'];
  $subcat=$_POST['subcat'];
  $subsub=$_POST['subsub'];
  
  $pid=$_POST['pid'];
   

echo $querys="UPDATE products SET deselect='".$selid."' where pid=".$pid;
$results=mysql_query($querys);
//header("Location: addsubcategory.php?catid=".$catid."&subcat=".$subcat."");   

	 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=".$catid);   
	  }




}
///
 





?>
