<? session_start();
ob_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' :
        addsubCategory();
        break;
		
    case 'edit' :
        editsubCategory();
        break;
		
    case 'delete' :
	     deleteCategory();
         break;
		 
    case 'status' :
	      statussubCategory();
          break;	
		  
	case 'metatag' :
	     metatagsubCategory();
         break;	 
		 
	 case 'seld' :
	     seld();
         break;	
		
		
		default :
        // if action is not defined or unknown
        // move to main category page
        header('Location: addcategory.php');
		
		}
		
	/*
    Add a subcategory
*/
function addsubCategory()
{
    
		get_magic_quotes_gpc();
	
	if(!get_magic_quotes_gpc())
	{
	$subcategoryname=addslashes($_POST['subcategory']);
	}
	else
	{
	$subcategoryname=$_POST['subcategory'];
	}
	
	 $subcategoryname;
	 $laxman=stripslashes($subcategoryname);
	
	
       $query = "SELECT subsubcat_name from tblsubsubcategories where subsubcat_name like '".$subcategoryname."' and 
        categoryid like ".$_GET['catid']." and subcatid=".$_GET['subcat']."";

	$user_userid = mysql_query($query) or die("Failed in query_checkemail: " . mysql_error()); 

	if(mysql_num_rows($user_userid) > 0 ) 
	{ 

		
		
		header("Location: addsubsubcategory.php?subsubcat=subsubcat&error=ture&catid=".$_GET['catid']."&subcat=".$_GET['subcat']."&subcategory=".$laxman);   

	} 
	
	else{	
	
   $sql   = "INSERT INTO tblsubsubcategories(categoryid,subcatid,subsubcat_name,ssimage,desc1,subsubstatus) 
              VALUES ('".$_GET['catid']."','".$_GET['subcat']."','".$subcategoryname."','".$_FILES['ssimage']['name']."','".$_POST['desc']."',1)";
    $result = mysql_query($sql) or die('Cannot add category' . mysql_error());
	
	$lastid=mysql_insert_id();
	
	if($_FILES['ssimage']['name']!="")
  {
	  
	 
	ini_set('memory_limit', '512M');
  
    $ftmp = $_FILES['ssimage']['tmp_name'];
            $oname = $_FILES['ssimage']['name'];
            $fname = '../images/subsubwaste/'.$_FILES['ssimage']['name'];
			$userfile_type = $_FILES['ssimage']['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/subsubthumb/".$lastid.".jpg", 130,130,false, true, false);
			
		
		
		//End of Second Image
	}
   
   
 header('Location: addsubsubcategory.php?sadd=sadd&catid='.$_GET['catid'].'&subcat='.$_GET['subcat'].'&subcatid='.$_GET['subcat']);   
 
	
	}           
}

		
	/*
    Editsubcategory
*/
function editsubCategory()
{

 get_magic_quotes_gpc();
	
	if(!get_magic_quotes_gpc())
	{
	$subcategoryname=addslashes($_POST['subcategory']);
	}
	else
	{
	$subcategoryname=$_POST['subcategory'];
	}
	
	 $subcategoryname;
	 $laxman=stripslashes($subcategoryname);
	 
	$catid=$_POST['catid'];
	$subcat=$_POST['subcat'];
	$subsubcat=$_POST['subsubcat'];
 
 
$sql ="UPDATE tblsubsubcategories SET subsubcat_name='".$subcategoryname."',desc1='".$_POST['desc']."' where subsubcatid=".$subsubcat; 
   $result = mysql_query($sql) or die('Cannot add category' . mysql_error());
   
  if($_FILES['ssimage']['name']!="")
  {
	  
	 
	ini_set('memory_limit', '512M');
  
    $ftmp = $_FILES['ssimage']['tmp_name'];
            $oname = $_FILES['ssimage']['name'];
            $fname = '../images/subsubwaste/'.$_FILES['ssimage']['name'];
			$userfile_type = $_FILES['ssimage']['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/subsubthumb/".$subsubcat.".jpg", 130,130,false, true, false);
			
			$sql1 ="UPDATE tblsubsubcategories SET ssimage='".$_FILES['ssimage']['name']."'  where subsubcatid=".$subsubcat; 
   $result1 = mysql_query($sql1) or die('Cannot add category' . mysql_error());
		
		//End of Second Image
	}
 
 
header("Location: addsubsubcategory.php?edit=edit&suc=suc&catid=".$catid."&subcat=".$subcat."&subsubcatid=".$subsubcat."");   
 
}	
	
function deleteCategory()
{
$catid=$_POST['catid'];
$subcat=$_POST['subcat'];
$subsubcat=$_POST['subsubcat'];

	// delete the products
	 $sql = "DELETE FROM products
			WHERE   subsubcatid=".$subsubcat;
			mysql_query($sql);
			
		
			
		
				// delete the subcategory
	 $sql2 = "DELETE FROM tblsubsubcategories 
			WHERE  subsubcatid=".$subsubcat;
			mysql_query($sql2);			
			


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


 header('Location: addsubsubcategory.php?cdel=cdel&catid='.$catid.'&subcat='.$subcat.'&subsubcatid='.$subsubcat.'');  
}
 
 function statussubCategory()
{
	
$catid=$_GET['catid'];	
$subcat=$_GET['subcat'];	
$status=$_GET['status'];
$subsubcat=$_GET['subsubcat'];

   echo $sql ="UPDATE tblsubsubcategories SET subsubstatus=".$status." where subsubcatid=".$subsubcat; 
    $result = mysql_query($sql) or die('Cannot add category' . mysql_error());
 
header("Location: addsubsubcategory.php?catid=".$catid."&subcat=".$subcat."&subsubcatid=".$subsubcat."");   
 
}	
 
 

/*
    Add Meta tag
*/
function metatagsubCategory()
{
	
    $catid=$_POST['catid'];
	$subcat=$_POST['subcat'];
	$subsubcat=$_POST['subsubcat'];
	

	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 tblsubsubcategories SET subsubmtitle='".$mtitle."',subsubmkeywords='".$mkeywords."',subsubmdescription='".$mdescription."' where subsubcatid=".$subsubcat; 
     $result = mysql_query($sql) or die('Cannot add category' . mysql_error());
    
  
  if($mtitle=="")
  {
  header("Location:addsubsubcategory.php?catid=".$catid."&subcat=".$subcat."&subsubcat=".$subsubcat.""); 
  }
  else
  {
	 header("Location:addsubsubcategory.php?seoedit=seoedit&suc=suc&catid=".$catid."&subcat=".$subcat."&subsubcat=".$subsubcat."");  
  }
	
	        
}

//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: addsubsubcategory.php?catid=".$catid."&subcat=".$subcat."&subsubcat=".$subsubcat."");   


}
///

?>