2016-03-14 18:06:51 +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' )) {
case 'getCategories' :
die ( json_encode ( getCategories ()));
break ;
2016-03-15 15:05:40 +01:00
case 'getProductId' :
die ( json_encode ( getProductId ()));
break ;
2016-03-15 16:07:49 +01:00
case 'addToSellout' :
die ( json_encode ( addToSellout ()));
break ;
2016-03-14 18:06:51 +01:00
default :
http_response_code ( 418 );
die ( 'I\'m a teapot' );
break ;
}
function getCategories ()
{
global $cookie ;
2016-03-15 15:05:40 +01:00
$id_sale = ( int ) Tools :: getValue ( 'sale' , false );
2016-03-14 18:06:51 +01:00
if ( ! $id_sale ) {
http_response_code ( 500 );
2016-03-15 16:07:49 +01:00
return Tools :: displayError ( 'Catégorie invalide' );
2016-03-14 18:06:51 +01:00
}
$db = Db :: getInstance ();
2016-03-16 12:14:19 +01:00
$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 ;
2016-03-14 18:06:51 +01:00
return $db -> ExecuteS ( $sql );
2016-03-15 15:05:40 +01:00
}
function getProductId ()
{
global $cookie ;
$ean = ( float ) Tools :: getValue ( 'ean' , false );
if ( ! $ean ) {
http_response_code ( 500 );
2016-03-15 16:07:49 +01:00
return Tools :: displayError ( 'Code EAN invalide' );
2016-03-15 15:05:40 +01:00
}
if ( ! is_float ( $ean )) {
http_response_code ( 500 );
2016-03-15 16:07:49 +01:00
return Tools :: displayError ( 'Code EAN invalide' );
2016-03-15 15:05:40 +01:00
}
$db = Db :: getInstance ();
2016-03-16 12:14:19 +01:00
$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' ;
2016-03-15 15:05:40 +01:00
$result = $db -> ExecuteS ( $sql );
2016-03-21 10:07:35 +01:00
if ( count ( $result ) == 0 ) {
$sql = 'SELECT * FROM `' . _DB_PREFIX_ . 'product_attribute` WHERE `ean13` = ' . pSQL ( $ean ) . ' ORDER BY `id_product_attribute` DESC LIMIT 1' ;
$result = $db -> ExecuteS ( $sql );
$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' ];
}
2016-03-15 15:05:40 +01:00
if ( count ( $result ) > 0 ) {
return $result [ 0 ];
} else {
http_response_code ( 500 );
return Tools :: displayError ( 'Aucun produit trouvé' );
}
2016-03-15 16:07:49 +01:00
}
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' );
}
2016-03-21 10:07:35 +01:00
$attribute = ( int ) Tools :: getValue ( 'attribute' , false );
2016-03-16 18:05:41 +01:00
$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' );
}
2016-03-15 16:07:49 +01:00
$storage = Tools :: getValue ( 'storage' );
if ( empty ( $storage )) {
http_response_code ( 500 );
return Tools :: displayError ( 'L\'emplacement n\'est pas valide' );
}
$db = Db :: getInstance ();
2016-03-16 12:14:19 +01:00
// vérifie si le produit n'es pas déjçà dans la vente
$sql = 'SELECT * FROM `' . _DB_PREFIX_ . 'category_product` WHERE `id_product` = ' . pSQL ( $product_id ) . ' AND `id_category` = ' . pSQL ( $category ) . ' LIMIT 1' ;
//var_dump($sql);
$product = $db -> ExecuteS ( $sql );
2016-03-16 12:57:28 +01:00
if ( ! empty ( $product )) {
2016-03-16 12:14:19 +01:00
http_response_code ( 500 );
return Tools :: displayError ( 'Le produit existe déjà dans la vente' );
}
// duplication produit
2016-03-16 12:58:12 +01:00
$sql = 'SELECT * FROM `' . _DB_PREFIX_ . 'product` WHERE `' . _DB_PREFIX_ . 'product`.`id_product` = ' . pSQL ( $product_id ) . ' ORDER BY `date_add` DESC LIMIT 1' ;
2016-03-15 16:07:49 +01:00
$product = $db -> ExecuteS ( $sql )[ 0 ];
2016-03-21 10:07:35 +01:00
if ( $attribute != 0 ) {
$sql = 'SELECT * FROM `' . _DB_PREFIX_ . 'product_attribute` WHERE `' . _DB_PREFIX_ . 'product`.`id_product` = ' . pSQL ( $product_id ) . ' ORDER BY `date_add` DESC LIMIT 1' ;
$product = $db -> ExecuteS ( $sql )[ 0 ];
}
2016-03-15 16:07:49 +01:00
unset ( $product [ 'id_product' ]);
2016-03-16 18:05:41 +01:00
$product [ 'quantity' ] = $quantity ;
$product [ 'active' ] = 1 ;
2016-03-17 11:57:24 +01:00
$product [ 'id_category_default' ] = $category ;
2016-03-15 16:07:49 +01:00
$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
2016-03-16 12:14:19 +01:00
$r = $db -> autoExecute ( _DB_PREFIX_ . 'category_product' , [
2016-03-15 16:07:49 +01:00
'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
2016-03-16 12:14:19 +01:00
$sql = 'SELECT * FROM `' . _DB_PREFIX_ . 'product_lang` WHERE `' . _DB_PREFIX_ . 'product_lang`.`id_product` = ' . $product_id ;
2016-03-15 16:07:49 +01:00
$products_lang = $db -> ExecuteS ( $sql );
foreach ( $products_lang as $key => $p ) {
$p [ 'id_product' ] = $last_product_id ;
2016-03-16 12:14:19 +01:00
$r = $db -> autoExecute ( _DB_PREFIX_ . 'product_lang' , pSQLArray ( $p ), INSERT );
2016-03-15 16:07:49 +01:00
if ( ! $r ) {
http_response_code ( 500 );
return Tools :: displayError ( 'Une erreur s\'est produite' );
}
}
2016-03-17 11:57:24 +01:00
// duplication images
$last_product_image_id = 0 ;
$sql = 'SELECT * FROM `' . _DB_PREFIX_ . 'image_lang` im LEFT JOIN `' . _DB_PREFIX_ . 'image` i ON i.id_image = im.id_image WHERE `' . _DB_PREFIX_ . 'image_lang`.`id_product` = ' . $product_id ;
$images = $db -> ExecuteS ( $sql );
foreach ( $images as $key => $image ) {
unset ( $image [ 'id_image' ]);
$r = $db -> autoExecute ( _DB_PREFIX_ . 'image_lang' , pSQLArray ([
'id_lang' => $image [ 'id_lang' ],
'legent' => $image [ 'legent' ],
]), INSERT );
if ( ! $r ) {
http_response_code ( 500 );
return Tools :: displayError ( 'Une erreur s\'est produite' );
}
$last_product_image_id = $db -> Insert_ID ();
$r = $db -> autoExecute ( _DB_PREFIX_ . 'image' , pSQLArray ([
'id_image' => $last_product_image_id ,
'id_product' => $last_product_id ,
'position' => $image [ 'position' ],
'cover' => $image [ 'cover' ]
]), INSERT );
if ( ! $r ) {
http_response_code ( 500 );
return Tools :: displayError ( 'Une erreur s\'est produite' );
}
}
2016-03-21 10:07:35 +01:00
if ( $attribute == 0 ) {
Product :: duplicateAttributes ( $product_id , $last_product_id );
} else {
Product :: duplicateOneAttribute ( $product_id , $last_product_id , $attribute );
}
2016-03-17 11:57:24 +01:00
2016-03-21 10:07:35 +01:00
recurse_copy ( _PS_IMG_DIR_ . $product_id , _PS_IMG_DIR_ . $last_product_id , $attribute );
2016-03-15 16:07:49 +01:00
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 ;
2016-03-16 12:57:28 +01:00
}
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 );
}
}
}
}
}