<?php
include "db_connect.php";
$create_time=date('Y-m-d H:i:s');
if(isset($_POST['submit']) && $_POST['submit']=='Add Product') {
$productName = !empty($_POST['productName'])?$_POST['productName']:'';
$productSlug = !empty($_POST['productSlug'])?$_POST['productSlug']:'';
$productCode = !empty($_POST['productCode'])?$_POST['productCode']:'';
$categoryId = !empty($_POST['categoryId'])?$_POST['categoryId']:'';
$medID = !empty($_POST['medID'])?$_POST['medID']:'';
$brandId = !empty($_POST['brandId'])?$_POST['brandId']:'';
$modelId = !empty($_POST['modelId'])?$_POST['modelId']:'';
$weeklyDeal = !empty($_POST['weeklyDeal'])?$_POST['weeklyDeal']:'';
$hotProduct = !empty($_POST['hotProduct'])?$_POST['hotProduct']:'';
$newArrivals = !empty($_POST['newArrivals'])?$_POST['newArrivals']:'';
$specialOffer = !empty($_POST['specialOffer'])?$_POST['specialOffer']:'';
$comingSoon = !empty($_POST['comingSoon'])?$_POST['comingSoon']:'';
$recommendedYou = !empty($_POST['recommendedYou'])?$_POST['recommendedYou']:'';
$originalPrice = !empty($_POST['originalPrice'])?$_POST['originalPrice']:'';
$productPrice = !empty($_POST['productPrice'])?$_POST['productPrice']:'';
$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']:'';
echo $categoryId;
$productName=check_textinput($con,$productName);
$productSlug=check_input($con,$productSlug);
$productCode=check_input($con,$productCode);
$categoryId =check_input($con,base64_decode($categoryId));
$medID =check_input($con,$medID);
$brandId =check_input($con,base64_decode($brandId));
$modelId =check_input($con,base64_decode($modelId));
$weeklyDeal=check_input($con,$weeklyDeal);
$hotProduct=check_input($con,$hotProduct);
$newArrivals=check_input($con,$newArrivals);
$specialOffer=check_input($con,$specialOffer);
$comingSoon=check_input($con,$comingSoon);
$recommendedYou=check_input($con,$recommendedYou);
$originalPrice=check_input($con,$originalPrice);
$productPrice=check_input($con,$productPrice);
$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);
$query_desc = mysqli_query($con, "INSERT into `spc_product` SET `product_name`='".$productName."',`product_slug`='".$productSlug."',`category_id`='".$categoryId."',`med_id`='".$medID."',`description`='".$mainDescription."',`other_description`='".$otherDescription."',`title_tag`='".$titleTag."',`meta_keyword`='".$metaKeyword."',`meta_description`='".$metaDescription."'") or die(mysqli_error($con));
$lastInsertId=mysqli_insert_id($con);
/*
$query_desc = mysqli_query($con, "UPDATE `spc_product` SET `product_name`='".$productName."',`description`='".$mainDescription."',`other_description`='".$otherDescription."',`title_tag`='".$titleTag."',`meta_keyword`='".$metaKeyword."',`meta_description`='".$metaDescription."' WHERE `product_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-product.php";';
echo '</script>';
exit();
}
$galleryImg = "Product 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_product` SET `image` = ? WHERE `product_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-product.php");
} else {
header("location: view-product.php");exit;
}
}
if(isset($_POST['submit']) && $_POST['submit']=='Save Changes') {
$id =check_input($con,base64_decode($_POST['id']));
$id1 =check_input($con,$_POST['id']);
$productSlug = !empty($_POST['productSlug'])?$_POST['productSlug']:'';
$productSlug=check_input($con,$productSlug);
$fetchPrSlug=mysqli_query($con,"SELECT `product_slug` FROM `spc_product` WHERE `product_id`='".$id."'") or die(mysqli_error($con));
$rowPrSlug=mysqli_fetch_array($fetchPrSlug);
$presentSlug=$rowPrSlug['product_slug'];
if($presentSlug!=$productSlug) {
$result2=mysqli_query($con,"SELECT `product_slug` FROM `spc_product` WHERE `product_slug`='".$productSlug."'") or die(mysqli_error($con));
$count=mysqli_num_rows($result2);
if($count!=0) {
echo '<script type="text/javascript">';
echo 'alert("Product Slug already Present. Please enter another Product Slug");';
echo 'window.location.href = "view-product.php";';
echo '</script>';
exit();
}
}
$productName = !empty($_POST['productName'])?$_POST['productName']:'';
$productCode = !empty($_POST['productCode'])?$_POST['productCode']:'';
$categoryId = !empty($_POST['categoryId'])?$_POST['categoryId']:'';
$medID = !empty($_POST['medId'])?$_POST['medId']:'';
$brandId = !empty($_POST['brandId'])?$_POST['brandId']:'';
$modelId = !empty($_POST['modelId'])?$_POST['modelId']:'';
$weeklyDeal = !empty($_POST['weeklyDeal'])?$_POST['weeklyDeal']:'';
$hotProduct = !empty($_POST['hotProduct'])?$_POST['hotProduct']:'';
$newArrivals = !empty($_POST['newArrivals'])?$_POST['newArrivals']:'';
$specialOffer = !empty($_POST['specialOffer'])?$_POST['specialOffer']:'';
$comingSoon = !empty($_POST['comingSoon'])?$_POST['comingSoon']:'';
$recommendedYou = !empty($_POST['recommendedYou'])?$_POST['recommendedYou']:'';
$originalPrice = !empty($_POST['originalPrice'])?$_POST['originalPrice']:'';
$productPrice = !empty($_POST['productPrice'])?$_POST['productPrice']:'';
$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']:'';
$productName=check_textinput($con,$productName);
$productCode=check_input($con,$productCode);
$categoryId =check_input($con,base64_decode($categoryId));
$medID=check_input($con,$medID);
$brandId =check_input($con,base64_decode($brandId));
$modelId =check_input($con,base64_decode($modelId));
$weeklyDeal=check_input($con,$weeklyDeal);
$hotProduct=check_input($con,$hotProduct);
$newArrivals=check_input($con,$newArrivals);
$specialOffer=check_input($con,$specialOffer);
$comingSoon=check_input($con,$comingSoon);
$recommendedYou=check_input($con,$recommendedYou);
$originalPrice=check_input($con,$originalPrice);
$productPrice=check_input($con,$productPrice);
$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);
$query_desc = mysqli_query($con, "UPDATE `spc_product` SET `product_name`='".$productName."',`description`='".$mainDescription."',`other_description`='".$otherDescription."',`title_tag`='".$titleTag."',`meta_keyword`='".$metaKeyword."',`meta_description`='".$metaDescription."' WHERE `product_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-product.php";';
echo '</script>';
exit();
}
}
$galleryImg = "Product 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_product` SET `image` = ? WHERE `product_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_product` SET `image` = ? WHERE `product_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-product.php";';
echo '</script>';
exit();
}
$rName = $imgName . "." . $ext;
$rnimg_stmt = $con->prepare('UPDATE `spc_product` SET `image` = ? WHERE `product_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-product.php");
} else {
header("location: view-product.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_product` WHERE `product_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_product` SET `status` = ? WHERE `product_id` = ? ');
$stmt->bind_param('ss', $status, $id);
$result = $stmt->execute();
}
if($cstatus=='0'){
$status=1;
$stmt = $con->prepare('UPDATE `spc_product` SET `status` = ? WHERE `product_id` = ? ');
$stmt->bind_param('ss', $status, $id);
$result = $stmt->execute();
}
if($stmt) {
$_SESSION['msg'] = 'status_changed';
header('location: view-product.php');exit;
} else {
header("location: view-product.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_product` WHERE `product_id` = ? ');
$delete_stmt->bind_param('s', $id);
$delete_stmt->execute();
if($delete_stmt) {
$_SESSION['msg'] = 'delete_data';
header('location: view-product.php');exit;
} else {
header("location: view-product.php");exit;
}
}
?>