2016-04-04 14:18:12 +02:00

229 lines
8.5 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 `'._DB_PREFIX_.'privatesale_category` pc LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON pc.`id_category` = cl.`id_category` WHERE pc.`id_sale` = '.$id_sale.' AND cl.`id_lang` = '.(int)$cookie->id_lang;
return Db::getInstance()->ExecuteS('
SELECT *
FROM `'._DB_PREFIX_.'category` c
LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON c.`id_category` = cl.`id_category`
WHERE c.`id_parent` = '.$id_sale.'
AND cl.`id_lang` = '.(int)$cookie->id_lang
);
}
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 `'._DB_PREFIX_.'product_lang`.`id_product`, `'._DB_PREFIX_.'product_lang`.`name` FROM `'._DB_PREFIX_.'product` LEFT JOIN `'._DB_PREFIX_.'product_lang` ON `'._DB_PREFIX_.'product`.id_product = `'._DB_PREFIX_.'product_lang`.id_product WHERE `ean13` = '.$ean.' AND `id_lang` = '.(int)$cookie->id_lang.' ORDER BY `date_add` DESC LIMIT 1';
$result = Db::getInstance()->getRow('
SELECT p.`id_product`, p.`reference`, pl.`name`
FROM `'._DB_PREFIX_.'product` p
LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (pl.`id_product` = p.`id_product`)
WHERE p.`ean13` = '.pSQL($ean).'
AND pl.`id_lang` = '.(int)$cookie->id_lang.'
ORDER BY `date_add` DESC
');
if (empty($result)) {
//$sql = 'SELECT * FROM `'._DB_PREFIX_.'product_attribute` WHERE `ean13` = '.pSQL($ean).' ORDER BY `id_product_attribute` DESC LIMIT 1';
/*$sql = 'SELECT * FROM `'._DB_PREFIX_.'product_lang` WHERE `id_product` = '.(int)($result[0]['id_product']).' LIMIT 1';
$tmp = $db->ExecuteS($sql)[0];
$result[0]['name'] = $tmp['name'];
$result[0]['reference'] = ($result[0]['reference']) ? $result[0]['reference'] : $tmp['reference'];*/
$result = Db::getInstance()->getRow('
SELECT pa.*, pl.`name`, p.`reference` as product_reference
FROM `'._DB_PREFIX_.'product_attribute` pa
LEFT JOIN `'._DB_PREFIX_.'product` p ON (p.`id_product` = pa.`id_product`)
LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (pl.`id_product` = p.`id_product`)
WHERE pa.`ean13` = '.pSQL($ean).'
AND pl.`id_lang` = '.(int)$cookie->id_lang.'
ORDER BY pa.`id_product_attribute`
DESC'
);
$result['reference'] = (!empty($result['reference'])) ? $result['reference'] : $result['product_reference'];
}
if (!empty($result)) {
return $result;
} else {
http_response_code(500);
return Tools::displayError('Aucun produit trouvé');
}
}
function addToSellout()
{
$parent_category = (int)Tools::getValue('parent_category', false);
if (!$parent_category || !is_int($parent_category)) {
http_response_code(500);
return Toold::displayError('La catégorie principale n\'est pas valide');
}
$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');
}
$quantity = (int)Tools::getValue('quantity', false);
if (!$quantity || !is_int($quantity)) {
http_response_code(500);
return Tools::displayError('La quantitée n\'est pas valide');
}
$storage = Tools::getValue('storage');
if (empty($storage)) {
http_response_code(500);
return Tools::displayError('L\'emplacement n\'est pas valide');
}
$id_attribute = (int)Tools::getValue('attribute', false);
// duplication produit classique
$product = new Product($product_id);
$id_product_old = $product->id;
unset($product->id);
unset($product->id_product);
$product->active = 1;
$product->quantity = $quantity;
$product->reference = $storage.'-'.$product->reference;
$languages = Language::getLanguages(false);
foreach ($languages as $key => $language) {
$category_name = Db::getInstance()->getRow('
SELECT cl.`name`
FROM `'._DB_PREFIX_.'category_lang` cl
LEFT JOIN `'._DB_PREFIX_.'privatesale` ps ON (ps.`id_category` = cl.`id_category`)
LEFT JOIN `'._DB_PREFIX_.'product_ps_cache` pps ON (pps.`id_sale` = ps.`id_sale`)
WHERE pps.`id_product` = '.(int)$id_product_old.'
AND cl.`id_lang` = '.(int)$language['id_lang']
);
$product->name[(int)$language['id_lang']] = $product->name[$language['id_lang']].' - '.strtoupper($category_name['name']);
}
$product->id_category_default = (int)$category;
if ($product->add()) {
if (!$id_attribute) {
$combinationImages = Product::duplicateAttributes($id_product_old, $product->id, $quantity);
} else {
// duplication spécific attribute
$combinationImages = Product::duplicateOneAttribute($id_product_old, $product->id, (int) $id_attribute, $quantity);
}
Product::duplicateSpecificPrices($id_product_old, $product->id);
Product::duplicateFeatures($id_product_old, $product->id);
Product::duplicateAccessories($id_product_old, $product->id);
GroupReduction::duplicateReduction($id_product_old, $product->id);
$product->UpdateCategories(array((int)$category,(int)$parent_category));
/*$max_position = Db::getInstance()->getValue("SELECT MAX(cp.`position`) AS max FROM `"._DB_PREFIX_."category_product` cp WHERE cp.`id_category`=" . (int)$category);
$add_category = Db::getInstance()->Execute("INSERT INTO `"._DB_PREFIX_."category_product` (`id_product`, `id_category`, `position`)
VALUES (
'". $product->id ."',
'". (int)$category ."',
'". (int)($max_position + 1 ) ."'
)");
$max_position_in_parent = Db::getInstance()->getValue("SELECT MAX(cp.`position`) AS max FROM `"._DB_PREFIX_."category_product` cp WHERE cp.`id_category`=" . (int)$parent_category);
$add_parent_category = Db::getInstance()->Execute("INSERT INTO `"._DB_PREFIX_."category_product` (`id_product`, `id_category`, `position`)
VALUES (
'". $product->id ."',
'". (int)$parent_category ."',
'". (int)($max_position_in_parent + 1 ) ."'
)");*/
if ($product->hasAttributes())
Product::updateDefaultAttribute($product->id);
Image::duplicateProductImages($id_product_old, $product->id, $combinationImages);
}
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;
}
function recurse_copy($src, $dst) {
if (is_dir($src) && is_dir($dst)) {
$dir = opendir($src);
mkdir($dst);
while(false !== ( $file = readdir($dir)) ) {
if (( $file != '.' ) && ( $file != '..' )) {
if ( is_dir($src . '/' . $file) ) {
recurse_copy($src . '/' . $file, $dst . '/' . $file);
}
else {
copy($src . '/' . $file, $dst . '/' . $file);
}
}
}
}
}