<?php
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: addcategory1.php?maincatid='.$_GET['maincatid']);

		

		}

		

	/*

    Add a subcategory

*/

function addsubCategory()

{

    

		get_magic_quotes_gpc();

	

	if(!get_magic_quotes_gpc())

	{

	$subcot_name=addslashes($_POST['subcategory']);

	}

	else

	{

	$subcot_name=$_POST['subcategory'];

	}

	

	 $subcot_name;

	 $laxman=stripslashes($subcot_name);

	

	

       $query = "SELECT subcot_name from tblsubcategories1 where subcot_name like '".$subcot_name."' and 

        categiryid like ".$_GET['categiry_id'];



	$user_userid = mysql_query($query); 



	if(mysql_num_rows($user_userid) > 0 ) 

	{ 



		

		

		header("Location: addsubcategory1.php?subcat=subcat&error=ture&categiry_id=".$_GET['categiry_id']."&subcategory=".$laxman);} 
else{	
     $sql   = "INSERT INTO tblsubcategories1(categiryid,subcot_name,description,status) 

              VALUES ('".$_GET['catid']."','".$subcot_name."','".$_POST['description']."',1)";

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

	

	$lastid=mysql_insert_id();

	

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

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

            $fname = '../images/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, "../images/subimg/".$lastid.".jpg", 130,130,false, true, false);

            

			//createthumb($fname, "../subimg/big/".$lastid.".gif", 90,40,false, true, false);

			

	

	

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

   

 header('Location: addsubcategory1.php?sadd=sadd&categiry_id='.$_GET['catid']);   

 

	

	}           

}



		

	/*

    Editsubcategory

*/

function editsubCategory()

{



 get_magic_quotes_gpc();

	

	if(!get_magic_quotes_gpc())

	{

	$subcot_name=addslashes($_POST['subcategory']);

	}

	else

	{

	$subcot_name=$_POST['subcategory'];

	}

	

	 $subcot_name;

	 $laxman=stripslashes($subcot_name);

	 

	$catid=$_POST['catid'];

	$subcat=$_POST['subcat'];

 

 



      if($_FILES['file']['name']=="")

       {	 

 

echo "UPDATE tblsubcategories1 SET subcot_name='".$subcot_name."',description='".$_POST['description']."' where subcot_id=".$subcat;

$sql ="UPDATE tblsubcategories1 SET subcot_name='".$subcot_name."',

description='".$_POST['description']."' where subcot_id=".$subcat; 

   $result = mysql_query($sql);

   

   }

   else

   {

   

   

   	$myFile2 = "../images/subimg/".$subcat.".jpg";

	unlink($myFile2);

echo "UPDATE tblsubcategories1 SET subcot_name='".$subcot_name."',description='".$_POST['subcatdes']."'where subcot_id=".$subcat;





 $sql ="UPDATE tblsubcategories1 SET 

subcot_name='".$subcot_name."',

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

 where subcot_id=".$subcat; 

            $result = mysql_query($sql);

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

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

            $fname = '../images/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, "../images/subimg/".$subcat.".jpg", 130,130,false, true, false);

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

			//createthumb($fname, "../subimg/big/".$lastid.".gif", 90,40,false, true, false);   

   

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

	

function deleteCategory()

{

$catid=$_POST['categiry_id'];

$subcat=$_POST['subcat'];





	// delete the products

	 $sql = "DELETE FROM products1

			WHERE   subcatid=".$subcat;

			mysql_query($sql);

			

		

			

		

				// delete the subcategory
				//echo "DELETE FROM tblsubcategories1 WHERE  subcot_id=".$subcat;exit;

	 $sql2 = "DELETE FROM tblsubcategories1 

			WHERE  subcot_id=".$subcat;

			mysql_query($sql2);			

			





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





 header('Location: addsubcategory1.php?cdel=cdel&categiry_id='.$_POST['catid'].'&subcat='.$subcat);  

}

 

 function statussubCategory()

{

	

$catid=$_GET['categiry_id'];	

$subcat=$_GET['subcat'];	

$status=$_GET['status'];





   $sql ="UPDATE tblsubcategories1 SET status='".$status."' where subcot_id=".$subcat; 

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

 

header("Location: addsubcategory1.php?catid=".$catid);   

 

}	

 

 



/*

    Add Meta tag

*/

function metatagsubCategory()

{

	

    $catid=$_POST['catid'];

	$subcat=$_POST['subcat'];

	



	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 tblsubcategories SET mtitle='".$mtitle."',mkeywords='".$mkeywords."',mdescription='".$mdescription."' where subcat_id=".$subcat; 

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

    

  

  if($mtitle=="")

  {

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

  }

  else

  {

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

  }

	

	        

}



//Selected Id//

function seld()

{

  $selid=$_POST['selid'];

  $catid=$_POST['catid'];

   $subcat=$_POST['subcat'];



 $querys="UPDATE tblsubcategories SET deselect='".$selid."' where subcat_id=".$subcat;

$results=mysql_query($querys);

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





}

///

 



	







?>