2016-03-22 17:34:39 +01:00
< ? php
require_once ( dirname ( __FILE__ ) . '../../../config/config.inc.php' );
require_once ( dirname ( __FILE__ ) . '../../../init.php' );
header ( 'Content-Type: application/json' );
switch ( Tools :: getValue ( 'action' )) {
2016-03-23 11:49:34 +01:00
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 ;
2016-03-22 17:34:39 +01:00
}
function getCategories ()
{
2016-03-23 11:49:34 +01:00
global $cookie ;
$id_sale = ( int ) Tools :: getValue ( 'sale' , false );
2016-03-22 17:34:39 +01:00
2016-03-23 11:49:34 +01:00
if ( ! $id_sale ) {
http_response_code ( 500 );
return Tools :: displayError ( 'Catégorie invalide' );
}
2016-03-22 17:34:39 +01:00
2016-03-31 11:51:37 +02:00
//$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
);
2016-03-22 17:34:39 +01:00
}
function getProductId ()
{
2016-03-23 11:49:34 +01:00
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' );
}
2016-03-31 12:47:51 +02:00
//$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.'
2016-04-11 12:13:41 +02:00
ORDER BY `date_add` ASC
2016-03-31 12:47:51 +02:00
' );
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' ;
2016-03-23 11:49:34 +01:00
$tmp = $db -> ExecuteS ( $sql )[ 0 ];
$result [ 0 ][ 'name' ] = $tmp [ 'name' ];
2016-03-31 12:47:51 +02:00
$result [ 0 ][ 'reference' ] = ( $result [ 0 ][ 'reference' ]) ? $result [ 0 ][ 'reference' ] : $tmp [ 'reference' ]; */
$result = Db :: getInstance () -> getRow ( '
2016-04-11 12:13:41 +02:00
SELECT pa .* , pl . `name` , p . `reference` as product_reference , al . `name` as attribute_name
2016-03-31 12:47:51 +02:00
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` )
2016-04-11 12:13:41 +02:00
LEFT JOIN `'._DB_PREFIX_.'product_attribute_combination` pac ON ( pac . `id_product_attribute` = pa . `id_product_attribute` )
LEFT JOIN `'._DB_PREFIX_.'attribute_lang` al ON ( al . `id_attribute` = pac . `id_attribute` )
2016-03-31 12:47:51 +02:00
WHERE pa . `ean13` = '.pSQL($ean).'
AND pl . `id_lang` = '.(int)$cookie->id_lang.'
ORDER BY pa . `id_product_attribute`
2016-04-11 12:13:41 +02:00
ASC '
2016-03-31 12:47:51 +02:00
);
$result [ 'reference' ] = ( ! empty ( $result [ 'reference' ])) ? $result [ 'reference' ] : $result [ 'product_reference' ];
2016-03-23 11:49:34 +01:00
}
2016-03-31 12:47:51 +02:00
if ( ! empty ( $result )) {
return $result ;
2016-03-23 11:49:34 +01:00
} else {
http_response_code ( 500 );
return Tools :: displayError ( 'Aucun produit trouvé' );
}
2016-03-22 17:34:39 +01:00
}
function addToSellout ()
{
2016-04-01 11:51:58 +02:00
$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' );
}
2016-03-23 11:49:34 +01:00
$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 ;
2016-03-31 11:51:37 +02:00
$product -> reference = $storage . '-' . $product -> reference ;
2016-04-04 13:34:26 +02:00
$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' ]);
}
2016-03-23 11:49:34 +01:00
$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 );
2016-04-04 14:18:12 +02:00
$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 );
2016-03-23 11:49:34 +01:00
$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 ) ."'
) " );
2016-04-01 11:51:58 +02:00
$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 ) ."'
2016-04-04 14:18:12 +02:00
) " );*/
2016-04-01 11:51:58 +02:00
2016-03-23 11:49:34 +01:00
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' );
2016-03-22 17:34:39 +01:00
}
function pSQLArray ( $data )
{
2016-03-23 11:49:34 +01:00
foreach ( $data as $key => $value ) {
if ( is_array ( $value )) {
$this -> pSQLArray ( $value );
} else {
$data [ $key ] = pSQL ( $value );
}
}
return $data ;
2016-03-22 17:34:39 +01:00
}
function recurse_copy ( $src , $dst ) {
2016-03-23 11:49:34 +01:00
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 );
}
}
}
}
}