<?php
include "db_connect.php";
$create_time=date('Y-m-d H:i:s');
if(isset($_POST['submit']) && $_POST['submit']=='Submit') {
$categoryName = !empty($_POST['categoryName'])?$_POST['categoryName']:'';
$categorySlug = !empty($_POST['categorySlug'])?$_POST['categorySlug']:'';
$mainDescription = !empty($_POST['mainDescription'])?$_POST['mainDescription']:'';
$otherDescription = !empty($_POST['otherDescription'])?$_POST['otherDescription']:'';
$titleTag = !empty($_POST['titleTag'])?$_POST['titleTag']:'';
$metaKeyword = !empty($_POST['metaKeyword'])?$_POST['metaKeyword']:'';
$metaDescription = !empty($_POST['metaDescription'])?$_POST['metaDescription']:'';
$categoryName=check_textinput($con,$categoryName);
$categorySlug=check_input($con,$categorySlug);
$mainDescription=check_input($con,$mainDescription);
$otherDescription=check_input($con,$otherDescription);
$titleTag=check_input($con,$titleTag);
$metaKeyword=check_input($con,$metaKeyword);
$metaDescription=check_input($con,$metaDescription);
$insert_stmt = $con->prepare('INSERT INTO `spc_category` SET `category_name`=?, `category_slug`=?,`status` = "1",`create_date_time`=?');
$insert_stmt->bind_param("sss",$categoryName,$categorySlug,$create_time);
$insert_stmt->execute();
$lastInsertId=mysqli_insert_id($con);
$query_desc = mysqli_query($con, "UPDATE `spc_category` SET `description`='".$mainDescription."',`title_tag`='".$titleTag."',`meta_keyword`='".$metaKeyword."',`meta_description`='".$metaDescription."' WHERE `category_id`='".$lastInsertId."'") or die(mysqli_error($con));
$NameFile = $_FILES['image']['name'];
if (isset($NameFile) && !empty($NameFile)) {
$extension = strtolower(pathinfo($NameFile, PATHINFO_EXTENSION));
if ($extension != "jpg" && $extension != "jpeg" && $extension != "png" && $extension != "gif") {
echo '<script type="text/javascript">';
echo 'alert("Only jpg, jpeg, png and gif files are allowed");';
echo 'window.location.href = "add-images.php";';
echo '</script>';
exit();
}
$galleryImg = "Category Images";
//$fileName = $imgName . "." . $extension;
$fileName = $NameFile;
$fpath = ".." . DIRECTORY_SEPARATOR . $galleryImg . DIRECTORY_SEPARATOR . $fileName;
if (!file_exists(".." . DIRECTORY_SEPARATOR . $galleryImg) && !is_dir(".." . DIRECTORY_SEPARATOR . $blogImg)) {
mkdir(".." . DIRECTORY_SEPARATOR . $galleryImg);
}
if (move_uploaded_file($_FILES["image"]["tmp_name"], $fpath)) {
$img_stmt = $con->prepare('UPDATE `spc_category` SET `image` = ? WHERE `category_id` = ? ');
$img_stmt->bind_param('ss', $fileName, $lastInsertId);
$img_stmt->execute();
$img_stmt->store_result();
}
}
/*
for($a=0;$a<count($_POST['vSizeId'])&&($_POST['quantity']);$a++){
$value1 =check_input($con,base64_decode($_POST['vSizeId'][$a]));
$value2=check_input($con,$_POST['quantity'][$a]);
$insertSCid = $con->prepare('INSERT INTO `hda_product_variation` SET `product_id` =?, `size_id` = ?, `variation_quantity` = ?, `create_date_time` = ? ');
$insertSCid->bind_param('ssss',$lastInsertId,$value1,$value2,$create_time);
$insertSCid->execute();
} */
/*if(isset($_POST['relatedProduct']) && $_POST['relatedProduct']!=''){
$relatedProduct=$_POST['relatedProduct'];
foreach($relatedProduct as $rlid){
$rlid=base64_decode($rlid);
$rlid=check_input($con,$rlid);
$insert_rl = $con->prepare('INSERT INTO `spc_rl_product` SET `product_id`=?,`rproduct_id`=?,`create_date_time`=?');
$insert_rl->bind_param("sss",$lastInsertId,$rlid,$create_time);
$insert_rl->execute();
}
}*/
if($insert_stmt) {
$_SESSION['msg'] = 'data_uploaded';
header("location: view-category.php");
} else {
header("location: view-category.php");exit;
}
}
if(isset($_POST['submit']) && $_POST['submit']=='Save Changes') {
$id =check_input($con,base64_decode($_POST['id']));
$id1 =check_input($con,$_POST['id']);
$categorySlug = !empty($_POST['categorySlug'])?$_POST['categorySlug']:'';
$categorySlug=check_input($con,$categorySlug);
$fetchPrSlug=mysqli_query($con,"SELECT `category_slug` FROM `spc_category` WHERE `category_id`='".$id."'") or die(mysqli_error($con));
$rowPrSlug=mysqli_fetch_array($fetchPrSlug);
$presentSlug=$rowPrSlug['category_slug'];
if($presentSlug!=$categorySlug) {
$result2=mysqli_query($con,"SELECT `category_slug` FROM `spc_category` WHERE `category_slug`='".$categorySlug."'") or die(mysqli_error($con));
$count=mysqli_num_rows($result2);
if($count!=0) {
echo '<script type="text/javascript">';
echo 'alert("Category Slug already Present. Please enter another Category Slug");';
echo 'window.location.href = "view-category.php";';
echo '</script>';
exit();
}
}
$categoryName = !empty($_POST['categoryName'])?$_POST['categoryName']:'';
$categorySlug = !empty($_POST['categorySlug'])?$_POST['categorySlug']:'';
$mainDescription = !empty($_POST['mainDescription'])?$_POST['mainDescription']:'';
$otherDescription = !empty($_POST['otherDescription'])?$_POST['otherDescription']:'';
$titleTag = !empty($_POST['titleTag'])?$_POST['titleTag']:'';
$metaKeyword = !empty($_POST['metaKeyword'])?$_POST['metaKeyword']:'';
$metaDescription = !empty($_POST['metaDescription'])?$_POST['metaDescription']:'';
$categoryName=check_textinput($con,$categoryName);
$categorySlug=check_input($con,$categorySlug);
$mainDescription=check_input($con,$mainDescription);
$otherDescription=check_input($con,$otherDescription);
$titleTag=check_input($con,$titleTag);
$metaKeyword=check_input($con,$metaKeyword);
$metaDescription=check_input($con,$metaDescription);
$update_stmt = $con->prepare('UPDATE `spc_category` SET `category_slug`=? WHERE `category_id` = ?');
$update_stmt->bind_param("ss",$productSlug,$id);
$update_stmt->execute();
$query_desc = mysqli_query($con, "UPDATE `spc_category` SET `category_name`='".$categoryName."',`description`='".$mainDescription."',`title_tag`='".$titleTag."',`meta_keyword`='".$metaKeyword."',`meta_description`='".$metaDescription."' WHERE `category_id`='".$id."'") or die(mysqli_error($con));
$NameFile = $_FILES['image']['name'];
if (isset($NameFile) && !empty($NameFile)) {
$extension = strtolower(pathinfo($NameFile, PATHINFO_EXTENSION));
if ($extension != "jpg" && $extension != "jpeg" && $extension != "png" && $extension != "gif") {
echo '<script type="text/javascript">';
echo 'alert("Only jpg, jpeg, png and gif files are allowed");';
echo 'window.location.href = "view-category.php";';
echo '</script>';
exit();
}
}
$galleryImg = "Category Images";
if (isset($_POST['existImage']) && $_POST['existImage'] == '') {
if (isset($_POST['removedImage']) && $_POST['removedImage'] != '') {
$rimg = "../" . $galleryImg . "/" . $_POST['removedImage'];
if (file_exists($rimg)) {
unlink($rimg);
$nimg = "";
$uimg_stmt = $con->prepare('UPDATE `spc_category` SET `image` = ? WHERE `category_id` = ? ');
$uimg_stmt->bind_param("ss", $nimg, $id);
$uimg_stmt->execute();
$uimg_stmt->store_result();
}
}
}
if (isset($NameFile) && !empty($NameFile)) {
if (!file_exists(".." . DIRECTORY_SEPARATOR . $galleryImg) && !is_dir(".." . DIRECTORY_SEPARATOR . $blogImg)) {
mkdir(".." . DIRECTORY_SEPARATOR . $galleryImg);
}
//$fileName = $imgName . "." . $extension;
$fileName = $NameFile;
$fpath = ".." . DIRECTORY_SEPARATOR . $galleryImg . DIRECTORY_SEPARATOR . $fileName;
if (move_uploaded_file($_FILES["image"]["tmp_name"], $fpath)) {
$nimg_stmt = $con->prepare('UPDATE `spc_category` SET `image` = ? WHERE `category_id` = ? ');
$nimg_stmt->bind_param('ss', $fileName, $id);
$nimg_stmt->execute();
$nimg_stmt->store_result();
/*
$resizeObj = new ImageResizeService($fpath);
$resizeObj->resizeImage(730, 380, 'auto');
$resizeObj->saveImage($fpath, 100);
*/
}
}
$ext = pathinfo($row_pslug['image'], PATHINFO_EXTENSION);
$presentImg = basename($row_pslug['image'], "." . $ext);
$oldImg = ".." . DIRECTORY_SEPARATOR . $galleryImg . DIRECTORY_SEPARATOR . $presentImg . "." . $ext;
$newImg = ".." . DIRECTORY_SEPARATOR . $galleryImg . DIRECTORY_SEPARATOR . $imgName . "." . $ext;
if (($presentImg != $imgName) && ($NameFile == '') && (file_exists($oldImg))) {
if (!(rename($oldImg, $newImg))) {
echo '<script type="text/javascript">';
echo 'alert("An error occurred during Rename Image.");';
echo 'window.location.href = "view-gallery.php";';
echo '</script>';
exit();
}
$rName = $imgName . "." . $ext;
$rnimg_stmt = $con->prepare('UPDATE `spc_category` SET `image` = ? WHERE `category_id` = ? ');
$rnimg_stmt->bind_param('ss', $rName, $id);
$rnimg_stmt->execute();
$rnimg_stmt->store_result();
}
//Variation
/*
$deleteDMapId = explode(',', $_POST['deleteDMapId'][0]);
if(isset($_POST['deleteDMapId'][0]) && $_POST['deleteDMapId'][0]!=''){
$countdeleteDMapId = count($deleteDMapId);
for ($k = 0; $k < $countdeleteDMapId; $k++) {
$delteVLId=mysqli_query($con,"DELETE FROM `hda_product_variation` WHERE `default_map_id`='".check_input($con,$deleteDMapId[$k])."' AND `product_id`='".$id."' ") or die(mysqli_error($con));
}
}
$cvCount = count($_POST['vSizeId']);
for ($a = 0; $a < $cvCount; $a++) {
if ($_POST['vSizeId'][$a] != '') {
$value1 =check_input($con,base64_decode($_POST['vSizeId'][$a]));
$value2=check_input($con,$_POST['quantity'][$a]);
if (isset($_POST['oldDmapId'][$a]) && $_POST['oldDmapId'][$a] != '') {
$updateSCid=mysqli_query($con,"UPDATE `hda_product_variation` SET `size_id`='".$value1."',`variation_quantity`='".$value2."' WHERE `product_id`='".$id."' AND `default_map_id`='".check_input($con,$_POST['oldDmapId'][$a])."' ") or die(mysqli_error($con));
} else {
$insertSCid = $con->prepare('INSERT INTO `dhe_product_variation` SET `product_id` =?, `size_id` = ?, `variation_quantity` = ?, `create_date_time` = ? ');
$insertSCid->bind_param('ssss',$id,$value1,$value2,$create_time);
$insertSCid->execute();
}
}
}
*/
//exit;
//Related Product update
/* if(isset($_POST['relatedProduct']) && $_POST['relatedProduct']!=''){
$rpIds=$_POST['relatedProduct'];
if (!is_array($rpIds)) {
$rpIds = array($rpIds);
}
$rlproducts=array();
$fetchRlpr=mysqli_query($con,"SELECT `rproduct_id` FROM `spc_rl_product` WHERE `product_id`='".$id."' ")or die(mysqli_error($con));
while($selectedRlpr=mysqli_fetch_array($fetchRlpr)) {
$rlproducts[]=base64_encode($selectedRlpr['rproduct_id']);
}
$newRlProduct = array_diff($rpIds, $rlproducts);
if ($newRlProduct != "" && $newRlProduct != null && is_array($newRlProduct)) {
foreach ($newRlProduct as $nrpId) {
$nrpId=base64_decode($nrpId);
$nrpId=check_input($con,$nrpId);
$insert_rp = $con->prepare('INSERT INTO `spc_rl_product` SET `product_id`=?,`rproduct_id`=?,`create_date_time`=?');
$insert_rp->bind_param("sss",$id,$nrpId,$create_time);
$insert_rp->execute();
}
}
$deleteRp = array_diff($rlproducts, $rpIds);
if ($deleteRp != "" && $deleteRp != null && is_array($deleteRp)) {
foreach ($deleteRp as $drpId) {
$drpId=base64_decode($drpId);
$drpId=check_input($con,$drpId);
$dltrp_stmt = $con->prepare('DELETE FROM `spc_rl_product` WHERE `product_id` = ? AND `rproduct_id` = ? ');
$dltrp_stmt->bind_param('ss', $id, $drpId);
$result = $dltrp_stmt->execute();
}
}
} else {
$dltrlpr_stmt = $con->prepare('DELETE FROM `spc_rl_product` WHERE `product_id` = ? ');
$dltrlpr_stmt->bind_param('s', $id);
$result = $dltrlpr_stmt->execute();
} */
if($update_stmt) {
$_SESSION['msg'] = 'data_updated';
header("location: view-category.php");
} else {
header("location: view-category.php");exit;
}
}
if($_SERVER["REQUEST_METHOD"]== "GET" && isset($_GET['action']) && $_GET['action']=='status'){
$id=base64_decode($_REQUEST['id']);
$sts_stmt = $con->prepare('SELECT `status` FROM `spc_category` WHERE `category_id` = ?');
$sts_stmt->bind_param('s', $id);
$sts_result = $sts_stmt->execute();
$sts_stmt->store_result();
$sts_stmt->bind_result($cstatus);
$sts_stmt->fetch();
if($cstatus=='1'){
$status=0;
$stmt = $con->prepare('UPDATE `spc_category` SET `status` = ? WHERE `category_id` = ? ');
$stmt->bind_param('ss', $status, $id);
$result = $stmt->execute();
}
if($cstatus=='0'){
$status=1;
$stmt = $con->prepare('UPDATE `spc_category` SET `status` = ? WHERE `category_id` = ? ');
$stmt->bind_param('ss', $status, $id);
$result = $stmt->execute();
}
if($stmt) {
$_SESSION['msg'] = 'status_changed';
header('location: view-category.php');exit;
} else {
header("location: view-category.php");exit;
}
}
if($_SERVER["REQUEST_METHOD"]== "GET" && isset($_GET['action']) && $_GET['action']=='delete'){
$id=check_input($con,base64_decode($_REQUEST['id']));
$delete_stmt = $con->prepare('DELETE FROM `spc_category` WHERE `category_id` = ? ');
$delete_stmt->bind_param('s', $id);
$delete_stmt->execute();
if($delete_stmt) {
$_SESSION['msg'] = 'delete_data';
header('location: view-category.php');exit;
} else {
header("location: view-category.php");exit;
}
}
?>