<?php
/**
* Product: Katyshop
* @version 0.3.2.1
* @author Catalin Hulea - hide@address.com
* @copyright Copyright (C) 2007 Catalin Hulea
* @license GNU General Public License version 3
* You can find a copy of GNU GPL v3 at this path: /docs/LICENSE
* @link https://sourceforge.net/projects/katyshop
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
require_once(dirname(dirname(dirname(__FILE__))) . "/init.php");
require_once(WEB_DIR . "/includes/req_admin.php");
$db = Application::getDb();
if(@$_GET["action"] == "save")
{
$p = new Product();
$p->copyFromArray($_POST);
if(Tools::validateFormattedNumber(@$_POST["price"], 2, ",", "."))
$p->price = readPrice(@$_POST["price"]);
else
$p->price = 0;
if($p->validate())
{
if($p->id > 0)
{
$old = $db->tbProduct->getRecordById($p->id);
$p->picture = $old->picture;
}
$db->tbProduct->save($p);
Application::addMessage("Produsul a fost salvat");
$up = new UploadFile("picture", WEB_DIR . "/img/products/large", DATA_DIR . "/temp");
if(!$up->isEmpty())
{
$up->setTmpPrefix("tmpprod" . $p->id);
$up->setPrefix("prod" . $p->id);
$up->validateInput();
$up->validateImage();
$up->upload(false);
$up->resize_limitwh(1024, 768);
if($up->commit($p->picture))
{
if(!empty($p->picture))
{
if(is_file(WEB_DIR . "/img/products/medium/" . $p->picture))
@unlink(WEB_DIR . "/img/products/medium/" . $p->picture);
if(is_file(WEB_DIR . "/img/products/small/" . $p->picture))
@unlink(WEB_DIR . "/img/products/small/" . $p->picture);
}
$p->picture = $up->newFilename;
$db->tbProduct->save($p);
// make medium image:
$im = new RESIZEIMAGE();
$im->setImage(WEB_DIR . "/img/products/large/" . $p->picture);
$im->resize_limitwh(150, 150, WEB_DIR . "/img/products/medium/" . $p->picture);
// make small image:
$im = new RESIZEIMAGE();
$im->setImage(WEB_DIR . "/img/products/large/" . $p->picture);
$im->resize_limitwh(75, 75, WEB_DIR . "/img/products/small/" . $p->picture);
Application::addMessage("Imaginea a fost schimbata");
}
else
{
Application::appendErrors($up->errors);
}
}
if(Application::hasErrors())
{
SessionHandler::set("editProduct", $p);
Tools::redirect("../product.php?action=edit&id_category=" . intval($p->id_category));
}
else
{
Tools::redirect("../product.php?id_product={$p->id}");
}
}
else
{
SessionHandler::set("editProduct", $p);
Tools::redirect("../product.php?action=edit&id_category=" . intval($p->id_category));
}
}
elseif (@$_GET["action"] == "delete")
{
$p = $db->tbProduct->getRecordById(@$_POST["id_product"]);
if($p->id == 0)
{
Application::addError("Nu a putut fi gasit produsul care trebuie sters");
Tools::redirect("../category.php");
}
else
{
$db->tbProduct->deleteObj($p);
Application::addMessage("Produsul a fost sters");
Tools::redirect("../category.php?id_category={$p->id_category}#products");
}
}
elseif (@$_GET["action"] == "change_parent")
{
$id_product = intval(@$_POST["id_product"]);
$id_destination = intval(@$_POST["id_destination"]);
$p = $db->tbProduct->getRecordById($id_product);
if($p->id == 0)
{
Application::addError("Nu a putut fi gasit produsul care trebuie mutat");
}
else
{
$p->id_category = $id_category;
$p->pos = $db->tbProduct->getMaxPos($id_destination) + 1;
if($p->validate())
{
$db->tbProduct->save($p);
Application::addMessage("Produsul a fost mutat");
}
}
if(Application::hasErrors())
Tools::redirect("../product.php?action=change_parent&id_product=$id_product&id_destination=$id_destination");
else
Tools::redirect("../category.php?id_category=$id_destination#products");
}
elseif (@$_GET["action"] == "change_position")
{
$p = $db->tbProduct->getRecordById(@$_POST["id_product"]);
if($p->id == 0)
{
Application::addError("Nu a putut fi gasit produsul pentru a-i schimba pozitia");
}
else
{
if(@$_POST["direction"] == "up")
$p->pos--;
elseif (@$_POST["direction"] == "down")
$p->pos++;
else
Application::addError("Trebuie sa specificati cum trebuie schimbata pozitia");
$maxPos = $db->tbProduct->getMaxPos($p->id_category);
if($p->pos < 1)
$p->pos = 1;
elseif ($p->pos > $maxPos)
$p->pos = $maxPos;
if($p->validate())
{
$db->tbProduct->save($p);
Application::addMessage("Pozitia produsului a fost actualizata");
}
}
Tools::redirect("../category.php?id_category=" . intval($p->id_category) . "#products");
}
elseif (@$_GET["action"] == "change_active_state")
{
$p = $db->tbProduct->getRecordById(@$_POST["id_product"]);
if($p->id == 0)
{
Application::addError("Nu am putut gasi produsul care trebuie activat / dezactivat");
}
else
{
$p->active = (@$_POST["active"] == "1") ? 1 : 0;
if($p->validate())
{
$db->tbProduct->save($p);
if($p->active == 1)
Application::addMessage("Produsul a fost activat");
else
Application::addMessage("Produsul a fost dezactivat");
}
}
Tools::redirect("../category.php?id_category=" . intval($p->id_category) . "#products");
}
?>