bebeboutik/modules/braderie/ajax.php
2016-03-15 16:07:49 +01:00

174 lines
3.9 KiB
PHP

<?php
require_once(dirname(__FILE__).'../../../config/config.inc.php');
require_once(dirname(__FILE__).'../../../init.php');
header('Content-Type: application/json');
switch (Tools::getValue('action')) {
case 'getCategories':
die(json_encode(getCategories()));
break;
case 'getProductId':
die(json_encode(getProductId()));
break;
case 'addToSellout':
die(json_encode(addToSellout()));
break;
default:
http_response_code(418);
die('I\'m a teapot');
break;
}
function getCategories()
{
global $cookie;
$id_sale = (int)Tools::getValue('sale', false);
if (!$id_sale) {
http_response_code(500);
return Tools::displayError('Catégorie invalide');
}
$db = Db::getInstance();
$sql = 'SELECT * FROM `ps_privatesale_category` pc LEFT JOIN `ps_category_lang` cl ON pc.`id_category` = cl.`id_category` WHERE pc.`id_sale` = '.$id_sale.' AND cl.`id_lang` = '.$cookie->id_lang;
return $db->ExecuteS($sql);
}
function getProductId()
{
global $cookie;
$ean = (float)Tools::getValue('ean', false);
if (!$ean) {
http_response_code(500);
return Tools::displayError('Code EAN invalide');
}
if (!is_float($ean)) {
http_response_code(500);
return Tools::displayError('Code EAN invalide');
}
$db = Db::getInstance();
$sql = 'SELECT `ps_product_lang`.`id_product`, `ps_product_lang`.`name` FROM `ps_product` LEFT JOIN `ps_product_lang` ON `ps_product`.id_product = `ps_product_lang`.id_product WHERE `ean13` = '.$ean.' AND `id_lang` = '.$cookie->id_lang.' ORDER BY `date_add` DESC LIMIT 1';
$result = $db->ExecuteS($sql);
if (count($result) > 0) {
return $result[0];
} else {
http_response_code(500);
return Tools::displayError('Aucun produit trouvé');
}
}
function addToSellout()
{
$category = (int)Tools::getValue('category', false);
if (!$category || !is_int($category)) {
http_response_code(500);
return Toold::displayError('La catégorie n\'est pas valide');
}
$product_id = (int)Tools::getValue('product', false);
if (!$product_id || !is_int($product_id)) {
http_response_code(500);
return Tools::displayError('Le produit n\'est pas valide');
}
$storage = Tools::getValue('storage');
if (empty($storage)) {
http_response_code(500);
return Tools::displayError('L\'emplacement n\'est pas valide');
}
// duplication produit
$db = Db::getInstance();
$sql = 'SELECT * FROM `ps_product` WHERE `ps_product`.`id_product` = '.$product_id.' ORDER BY `date_add` DESC LIMIT 1';
$product = $db->ExecuteS($sql)[0];
unset($product['id_product']);
$product['reference'] = $storage.'_'.$product['reference'];
$r = $db->autoExecute('ps_product', pSQLArray($product), INSERT);
if (!$r) {
http_response_code(500);
return Tools::displayError('Une erreur s\'est produite');
}
$last_product_id = $db->Insert_ID();
// duplication categorie
$r = $db->autoExecute('ps_category_product', [
'id_category' => pSQL($category),
'id_product' => pSQL($last_product_id),
'position' => 0
], INSERT);
if (!$r) {
http_response_code(500);
return Tools::displayError('Une erreur s\'est produite');
}
// duplication lang
$sql = 'SELECT * FROM `ps_product_lang` WHERE `ps_product_lang`.`id_product` = '.$product_id;
$products_lang = $db->ExecuteS($sql);
foreach ($products_lang as $key => $p) {
$p['id_product'] = $last_product_id;
$r = $db->autoExecute('ps_product_lang', pSQLArray($p), INSERT);
if (!$r) {
http_response_code(500);
return Tools::displayError('Une erreur s\'est produite');
}
}
return Tools::displayError('Le produit à été mis dans la braderie');
}
function pSQLArray($data)
{
foreach ($data as $key => $value) {
if (is_array($value)) {
$this->pSQLArray($value);
} else {
$data[$key] = pSQL($value);
}
}
return $data;
}