<?php
ob_start();
session_start();
require_once("../dbconnect.php");
ini_set('memory_limit', '512M');
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,100);
            $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' :
        addCategory();
        break;
		
    case 'edit' :
        editCategory();
        break;
		
    case 'delete' :
	     deleteCategory();
         break;
		
	
    case 'status' :
	     statusCategory();
         break;
		 
    case 'metatag' :
	     metatagCategory();
         break;	 
		 
	case 'seld' :
	     seld();
         break;	
		 
	case 'user_status' :
	      user_statusupdate();
		  break;
	case 'user_statuss' :
	      user_status();
		  break;
	
		default :
        // if action is not defined or unknown
        // move to main category page
        header('Location: addcategory.php');
		
		}
		
		
/*
    Add a category
*/
function addCategory()
{
    
	
	get_magic_quotes_gpc();
	
	if(!get_magic_quotes_gpc())
	{
	$categoryname=addslashes($_POST['category']);
	}
	else
	{
	$categoryname=$_POST['category'];
	}
	
	 $categoryname;
	 $laxman=stripslashes($categoryname);
	
	
	
	
 $query = "SELECT categoryname from tblcategories where categoryname like '".$categoryname."'";

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

	if(mysql_num_rows($user_userid) > 0 ) 
	{ 

		
	header("Location: addcategory.php?addcat=addcat&error=ture&category=".$laxman);   

	} 
	
	else
	
	{	
	  $sql   = "INSERT INTO tblcategories(categoryname,description,status) 
              VALUES ('".$categoryname."','".$_POST['description']."',1)";
    $result = mysql_query($sql) or die('Cannot add category' . mysql_error());
 $image_id=mysql_insert_id();
 
    $ftmp = $_FILES['image']['tmp_name'];
            $oname = $_FILES['image']['name'];
            $fname = '../images/waste/'.$_FILES['image']['name'];
			$userfile_type = $_FILES['image']['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/category/".$image_id.".jpg", 200,250,false, true, false);
			
		
		
	$myFile = "../images/waste/".$_FILES['image']['name'];
		unlink($myFile);
header('Location: addcategory.php?sadd=sadd');   
	
	
	}           
}

/*
   Edit category
*/



function editCategory()
{
 get_magic_quotes_gpc();
	
	if(!get_magic_quotes_gpc())
	{
	$categoryname=addslashes($_POST['category']);
	}
	else
	{
	$categoryname=$_POST['category'];
	}
	
	 $categoryname;
	 
	 $catid=$_POST['catid'];
	 
	 $laxman=stripslashes($categoryname);
 
 
 
  $sql ="UPDATE tblcategories SET categoryname='".$categoryname."',description='".$_POST['description']."' where category_id=".$catid; 
    $result = mysql_query($sql) or die('Cannot add category' . mysql_error());
 
 if($_FILES['image']['name']!="") {
 $myFile1 = "../images/category/".catid;
		unlink($myFile1);
		
 $ftmp = $_FILES['image']['tmp_name'];
            $oname = $_FILES['image']['name'];
            $fname = '../images/waste/'.$_FILES['image']['name'];
			$userfile_type = $_FILES['image']['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/category/".$catid.".jpg", 200,250,false, true, false);
			
		
		
	$myFile = "../images/waste/".$_FILES['image']['name'];
		unlink($myFile);
 }
 header('Location: addcategory.php?maincatid='.$_GET['maincatid'].'&edit=edit&suc=suc&catid='.$catid);   
 
}		


/*
   Delete Category
*/


function deleteCategory()
{

  $catid=$_POST['catid'];


	// delete the products
	 $sql = "DELETE FROM products
			WHERE  catid=".$catid;
			mysql_query($sql);
			
	
	
			
		
				// delete the subcategory
	 $sql2 = "DELETE FROM tblsubcategories 
			WHERE  categoryid=".$catid;
			mysql_query($sql2);			
			
			
			// delete the category

      $sql3 = "DELETE FROM tblcategories WHERE  category_id=".$catid;
	      mysql_query($sql3);	

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


 header('Location: addcategory.php?maincatid='.$_GET['maincatid'].'&cdel=cdel');  


 }


/*
    Edit Status
*/
function statusCategory()
{
	
$catid=$_GET['catid'];

 $status=$_GET['status'];

 $sql ="UPDATE tblcategories SET status='".$status."' where category_id=".$catid; 
    $result = mysql_query($sql) or die('Cannot add category' . mysql_error());
 
header('Location: addcategory.php?maincatid='.$_GET['maincatid']);   
 
}		

// updae userstatus

function user_statusupdate()
{
	$user_id = $_POST['user_id'];
	$status=$_POST['status'];
	$update_status = "update register set Status=".$status.", discount ='".$_POST['discount']."', amounttype='".$_POST['type']."' where user_id=".$user_id;
	mysql_query($update_status);
	
	
	$a1= mysql_query("select * from  register where user_id=".$user_id);
 $s=mysql_fetch_array($a1);
 if($_POST['type']==0)
 {
 $hi="%";
 }
 else
 {
  $hi="$";
 }
	
	$email_from = "sirbox26@yahoo.com"; 
$email_subject = "Discount Amount";
$message.="Dear ".ucfirst($s['Fname'])." ".$s['Lname']."\n";
$message.="You got ".$_POST['discount']."".$hi." Discount to purchase products\n\n";


$message.='Sirbox.com';

 $email_message = $message; 

$email_to =$to;  
$headers = "From: ".$from;
mail($email_to,$email_subject,$email_message,$headers); 
	
	
	header('Location: customers.php');   
	
}
function user_status()
{
	$user_id = $_GET['user_id'];
	$status=$_GET['status'];
	 $update_status = "update register set Status=".$status." where user_id=".$user_id;
	
	mysql_query($update_status);
	
	
	header('Location: customers.php');  
}
 
//end of update userstatus
/*
    Add Meta tag
*/
function metatagCategory()
{
	
    $catid=$_POST['catid'];

	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 tblcategories SET mtitle='".$mtitle."',mkeywords='".$mkeywords."',mdescription='".$mdescription."' where category_id=".$catid; 
    $result = mysql_query($sql) or die('Cannot add category' . mysql_error());
    
  
  if($mtitle=="")
  {
  header('Location:addcategory.php?maincatid='.$_GET['maincatid']); 
  }
  else
  {
	 header('Location: addcategory.php?maincatid='.$_GET['maincatid'].'&seoedit=seoedit&suc=suc&catid='.$catid);  
  }
	
	        
}

//Selected Id//
function seld()
{

 $catid=$_POST['catid'];
 $selid=$_POST['selid'];

$querys="UPDATE tblcategories SET deselect='".$selid."' where category_id=".$catid;
$results=mysql_query($querys);
header("Location: addcategory.php?maincatid=".$_GET['maincatid']);   


}
///

?>