<?php
require_once '../../library/config.php';
require_once '../library/functions.php';
checkUser();
$action = isset($_GET['action']) ? $_GET['action'] : '';
switch ($action) {
case 'addProduct' :
addProduct();
break;
case 'modifyProduct' :
modifyProduct();
break;
case 'deleteProduct' :
deleteProduct();
break;
case 'deleteImage' :
deleteImage();
break;
case 'addOffert' :
addOffert();
break;
case 'modifyOffer' :
modifyOffer();
break;
default :
// if action is not defined or unknown
// move to main product page
header('Location: index.php');
}function addProduct()
{
$catId = $_POST['cboCategory'];
$sku = $_POST['txtSku'];
$name = $_POST['txtName'];
$description = $_POST['mtxDescription'];
$direction = $_POST['txtDirection'];
$benefits = $_POST['txtBenefites'];
$size1 = $_POST['txtSize1'];
$coverage1 = $_POST['txtCoverage1'];
$price1 = str_replace(',', '', (double)$_POST['txtPrice1']);
$qty1 = (int)$_POST['txtQty1'];
$size2 = $_POST['txtSize2'];
$coverage2 = $_POST['txtCoverage2'];
$price2 = str_replace(',', '', (double)$_POST['txtPrice2']);
$qty2 = (int)$_POST['txtQty2'];
$size3 = $_POST['txtSize3'];
$coverage3 = $_POST['txtCoverage3'];
$price3 = str_replace(',', '', (double)$_POST['txtPrice3']);
$qty3 = (int)$_POST['txtQty3'];
if(!empty($_POST['txtSize4'])) {
$size4 = $_POST['txtSize4'];
} else {
$size4 = '';
}
if(!empty($_POST['txtCoverage4'])) {
$coverage4 = $_POST['txtCoverage4'];
} else {
$coverage4 = '';
}
if(!empty($_POST['txtPrice4'])) {
$price4 = str_replace(',', '', (double)$_POST['txtPrice4']);
} else {
$price4 = '';
}
if(!empty($_POST['txtQty4'])) {
$qty4 = (int)$_POST['txtQty4'];
} else {
$qty4 = '';
}
if(!empty($_POST['txtSize5'])) {
$size5 = $_POST['txtSize5'];
} else {
$size5 = '';
}
if(!empty($_POST['txtCoverage5'])) {
$coverage5 = $_POST['txtCoverage5'];
} else {
$coverage5 = '';
}
if(!empty($_POST['txtPrice5'])) {
$price5 = str_replace(',', '', (double)$_POST['txtPrice5']);
} else {
$price5 = '';
}
if(!empty($_POST['txtQty5'])) {
$qty5 = (int)$_POST['txtQty5'];
} else {
$qty5 = '';
}
$shipWithIn = $_POST['txtShipWithin'];
$images = uploadProductImage('fleImage', SRV_ROOT . 'images/product/');
$mainImage = $images['image'];
$thumbnail = $images['thumbnail'];
$sql = "INSERT INTO tbl_product (cat_id, pd_sku, pd_name, pd_description, pd_direction, pd_benefits, pd_size1, pd_coverage1, pd_price1, pd_qty1, pd_size2, pd_coverage2, pd_price2, pd_qty2, pd_size3, pd_coverage3, pd_price3, pd_qty3, pd_size4, pd_coverage4, pd_price4, pd_qty4, pd_size5, pd_coverage5, pd_price5, pd_qty5, pd_shipwithin, pd_image, pd_thumbnail, pd_date)
VALUES ('$catId', '$sku', '$name', '$description', '$direction', '$benefits', '$size1', '$coverage1', $price1, '$qty1', '$size2', '$coverage2', $price2, '$qty2', '$size3', '$coverage3', $price3, '$qty3', '$size4', '$coverage4', $price4, '$qty4','$size5', '$coverage5', $price5, '$qty5','$shipWithIn', '$mainImage', '$thumbnail', NOW())";
$result = dbQuery($sql);
header("Location: index.php");
}
/*
Upload an image and return the uploaded image name
*/
function uploadProductImage($inputName, $uploadDir)
{
$image = $_FILES[$inputName];
$imagePath = '';
$thumbnailPath = '';
// if a file is given
if (trim($image['tmp_name']) != '') {
$ext = substr(strrchr($image['name'], "."), 1); //$extensions[$image['type']];
// generate a random new file name to avoid name conflict
$imagePath = md5(rand() * time()) . ".$ext";
list($width, $height, $type, $attr) = getimagesize($image['tmp_name']);
// make sure the image width does not exceed the
// maximum allowed width
if (LIMIT_PRODUCT_WIDTH && $width > MAX_PRODUCT_IMAGE_WIDTH) {
$result = createThumbnail($image['tmp_name'], $uploadDir . $imagePath, MAX_PRODUCT_IMAGE_WIDTH);
$imagePath = $result;
} else {
$result = move_uploaded_file($image['tmp_name'], $uploadDir . $imagePath);
}
if ($result) {
// create thumbnail
$thumbnailPath = md5(rand() * time()) . ".$ext";
$result = createThumbnail($uploadDir . $imagePath, $uploadDir . $thumbnailPath, THUMBNAIL_WIDTH);
// create thumbnail failed, delete the image
if (!$result) {
unlink($uploadDir . $imagePath);
$imagePath = $thumbnailPath = '';
} else {
$thumbnailPath = $result;
}
} else {
// the product cannot be upload / resized
$imagePath = $thumbnailPath = '';
}
}
return array('image' => $imagePath, 'thumbnail' => $thumbnailPath);
}
/*
Modify a product
*/
function modifyProduct()
{
$proId = (int)$_GET['productId'];
$catId = $_POST['cboCategory'];
$sku = $_POST['txtSku'];
$name = $_POST['txtName'];
$description = $_POST['mtxDescription'];
$direction = $_POST['txtDirection'];
$benefits = $_POST['txtBenefites'];
$size1 = $_POST['txtSize1'];
$coverage1 = $_POST['txtCoverage1'];
$price1 = str_replace(',', '', (double)$_POST['txtPrice1']);
$qty1 = (int)$_POST['txtQty1'];
$size2 = $_POST['txtSize2'];
$coverage2 = $_POST['txtCoverage2'];
$price2 = str_replace(',', '', (double)$_POST['txtPrice2']);
$qty2 = (int)$_POST['txtQty2'];
$size3 = $_POST['txtSize3'];
$coverage3 = $_POST['txtCoverage3'];
$price3 = str_replace(',', '', (double)$_POST['txtPrice3']);
$qty3 = (int)$_POST['txtQty3'];
if(!empty($_POST['txtSize4'])) {
$size4 = $_POST['txtSize4'];
} else {
$size4 = '';
}
if(!empty($_POST['txtCoverage4'])) {
$coverage4 = $_POST['txtCoverage4'];
} else {
$coverage4 = '';
}
if(!empty($_POST['txtPrice4'])) {
$price4 = str_replace(',', '', (double)$_POST['txtPrice4']);
} else {
$price4 = '';
}
if(!empty($_POST['txtQty4'])) {
$qty4 = (int)$_POST['txtQty4'];
} else {
$qty4 = '';
}
if(!empty($_POST['txtSize5'])) {
$size5 = $_POST['txtSize5'];
} else {
$size5 = '';
}
if(!empty($_POST['txtCoverage5'])) {
$coverage5 = $_POST['txtCoverage5'];
} else {
$coverage5 = '';
}
if(!empty($_POST['txtPrice5'])) {
$price5 = str_replace(',', '', (double)$_POST['txtPrice5']);
} else {
$price5 = '';
}
if(!empty($_POST['txtQty5'])) {
$qty5 = (int)$_POST['txtQty5'];
} else {
$qty5 = '';
}
$shipWithIn = $_POST['txtShipWithin'];
$images = uploadProductImage('fleImage', SRV_ROOT . 'images/product/');
$mainImage = $images['image'];
$thumbnail = $images['thumbnail'];
// if uploading a new image
// remove old image
if ($mainImage != '') {
_deleteImage($proId);
$mainImage = "'$mainImage'";
$thumbnail = "'$thumbnail'";
} else {
// if we're not updating the image
// make sure the old path remain the same
// in the database
$mainImage = 'pd_image';
$thumbnail = 'pd_thumbnail';
}
$sql = "UPDATE tbl_product
SET cat_id = '$catId', pd_sku = '$sku', pd_name = '$name', pd_description = '$description', pd_direction = '$direction', pd_benefits = '$benefits', pd_size1 = '$size1', pd_coverage1 = '$coverage1', pd_price1 = '$price1', pd_qty1 = '$qty1', pd_size2 = '$size2', pd_coverage2 = '$coverage2', pd_price2 = '$price2', pd_qty2 = '$qty2', pd_size3 = '$size3', pd_coverage3 = '$coverage3', pd_price3 = '$price3', pd_qty3 = '$qty3', pd_size4 = '$size4', pd_coverage4 = '$coverage4', pd_price4 = '$price4', pd_qty4 = '$qty4', pd_size5 = '$size5', pd_coverage5 = '$coverage5', pd_price5 = '$price4', pd_qty5 = '$qty5', pd_image = $mainImage, pd_thumbnail = $thumbnail
WHERE pd_id = $proId";
$result = dbQuery($sql);
header('Location: index.php');
}
/*
Remove a product
*/
function deleteProduct()
{
if (isset($_GET['productId']) && (int)$_GET['productId'] > 0) {
$productId = (int)$_GET['productId'];
} else {
header('Location: index.php');
}
// remove any references to this product from
// tbl_order_item and tbl_cart
$sql = "DELETE FROM tbl_order_item
WHERE pd_id = $productId";
dbQuery($sql);
$sql = "DELETE FROM tbl_cart
WHERE pd_id = $productId";
dbQuery($sql);
// get the image name and thumbnail
$sql = "SELECT pd_image, pd_thumbnail
FROM tbl_product
WHERE pd_id = $productId";
$result = dbQuery($sql);
$row = dbFetchAssoc($result);
// remove the product image and thumbnail
if ($row['pd_image']) {
unlink(SRV_ROOT . 'images/product/' . $row['pd_image']);
unlink(SRV_ROOT . 'images/product/' . $row['pd_thumbnail']);
}
// remove the product from database;
$sql = "DELETE FROM tbl_product
WHERE pd_id = $productId";
dbQuery($sql);
header('Location: index.php?catId=' . $_GET['catId']);
}/*
Remove a product image
*/
function deleteImage()
{
if (isset($_GET['productId']) && (int)$_GET['productId'] > 0) {
$productId = (int)$_GET['productId'];
} else {
header('Location: index.php');
}
$deleted = _deleteImage($productId);
// update the image and thumbnail name in the database
$sql = "UPDATE tbl_product
SET pd_image = '', pd_thumbnail = ''
WHERE pd_id = $productId";
dbQuery($sql);
header("Location: index.php?view=modify&productId=$productId");
}
function _deleteImage($productId)
{
// we will return the status
// whether the image deleted successfully
$deleted = false;
$sql = "SELECT pd_image, pd_thumbnail
FROM tbl_product
WHERE pd_id = $productId";
$result = dbQuery($sql) or die('Cannot delete product image. ' . mysql_error());
if (dbNumRows($result)) {
$row = dbFetchAssoc($result);
extract($row);
if ($pd_image && $pd_thumbnail) {
// remove the image file
$deleted = @unlink(SRV_ROOT . "images/product/$pd_image");
$deleted = @unlink(SRV_ROOT . "images/product/$pd_thumbnail");
}
}
return $deleted;
}function addOffert() {
$pdId = (int)$_GET['offId'];
$offerAmount = $_POST['offerAmount'];
$offDescription = $_POST['description'];
$sql = "UPDATE tbl_product SET pd_offer_amount = '$offerAmount', pd_offer_description = '$offDescription' WHERE pd_id = '$pdId'";
$result = dbQuery($sql);
header("Location:index.php");
}
function modifyOffer() {
$pdId = (int)$_GET['offId'];
$offerAmount = $_POST['offerAmount'];
$offDescription = $_POST['description'];
$sql = "UPDATE tbl_product SET pd_offer_amount = '$offerAmount', pd_offer_description = '$offDescription' WHERE pd_id = '$pdId'";
$result = dbQuery($sql);
header("Location:index.php");
}
?>