<?php
include('../helpers/init.php');
if(isset($_POST['addNews']))
{
    $category = $_POST['category'];
    $title = $_POST['name'];
    $name = str_replace("'","",$_POST['name']);
	 $name = str_replace('"','\"',$name);
	 $slug = slug($name);
    $content = $_POST['content']; 
    $error = "";
    
    $sql = "insert into products(category,title,slug,news) values('$category','$title','$slug','$content')";
		if(query($sql)){
			header('Location:../pages/add-products.php?&result=success');
		}else{
			$error = error();
			header('Location:../pages/add-products.php?&result=failer');
		}
	
}

//-----------------------Add Product End Here ---------------

if(isset($_POST['updateContent'])){
	$id=$_POST['id'];
    $cont = $_POST['content'];
    
	 	$sql = "update products set news='$cont' where id=$id";
		if(query($sql)){
			header('Location:../pages/update-product.php?id='.$id.'&result=success');
		}else{
			$error =  error();
			header('Location:../pages/update-product.php?id='.$id.'&result=failer');
		}
	/*end of */
	}

//----------- Update Action Ends Here --------------



if(isset($_GET['action']) && $_GET['action']=="deletePro")
{
	$id=$_GET['id'];
    $clients = getWhere("pgallery","product",$id);
	if(num_rows($clients)){
		$error = " Product Cannot be deleted because ".num_rows($clients)." gallery images are added in this product.";
		header("Location:../pages/manage-products.php?result=failer&message=".$error);
	}
    else
    {
		$sql = "delete from products where id=$id";
		if(query($sql))
        {
			header("Location:../pages/manage-products.php?result=dsuccess");
		}
            else{
			$error =  error();
			header("Location:../pages/manage-products.php?result=failer&message=".$error);
		  }
    }

}
//-------------------Product Delete Ends Here ---------------

if(isset($_POST['addGalleryImage'])){
    
	$client = $_POST['clid'];
    $error = "";    
	$image="iip_".rand(111,999)."_".basename($_FILES['file']['name']);
    $fileinfo = @getimagesize($_FILES["file"]["tmp_name"]);
        $width = $fileinfo[0];
        $height = $fileinfo[1];
    
	if($_FILES['file']['size']>350000){
			header('Location:../pages/product-gallery.php?id='.$client.'&result=picfailer');
	}
    
    if($width != "870" || $height != "387"){
			header('Location:../pages/product-gallery.php?id='.$client.'&result=picfaild');
	}
    else{
		if (do_upload($image,"project-gallery")){
		$sql = "insert into pgallery(product,picture) values('$client','$image')";
		if(query($sql)){
			header('Location:../pages/product-gallery.php?id='.$client.'&result=success');
		}else{
			$error = error();
			header('Location:../pages/product-gallery.php?id='.$client.'&result=failer');
		}
		}
	}/*end else for size validation*/
	
	
}

//-----------------------Add Image Edns Here ---------------


if(isset($_GET['action']) && $_GET['action']=="deleteGalleryImage"){
	$id=$_GET['id'];
	$data = getWhere("pgallery","id",$id);
    foreach($data as $row){
        $client= $row['product']; 
		$image= $row['picture'];
        $banner = "../uploads/project-gallery/".$image;
        
		$sql = "delete from pgallery where id=$id";
        
		if(query($sql)){  
            unlink($banner);
			header('Location:../pages/product-gallery.php?id='.$client.'&result=vsuccess');
		}else{
			header('Location:../pages/product-gallery.php?id='.$client.'&result=failer');
		}
	}
}

?>