PageRenderTime 42ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/storecommander/ead6f6fce09/SC/lib/cat/shopshare/cat_shopshare_update_queue.php

https://gitlab.com/ptisky/API_prestashop
PHP | 552 lines | 416 code | 46 blank | 90 comment | 58 complexity | 903db3cb27dbd84dbfeda20c1165eb01 MD5 | raw file
  1. <?php
  2. /**
  3. * Store Commander
  4. *
  5. * @category administration
  6. * @author Store Commander - support@storecommander.com
  7. * @version 2015-09-15
  8. * @uses Prestashop modules
  9. * @since 2009
  10. * @copyright Copyright &copy; 2009-2015, Store Commander
  11. * @license commercial
  12. * All rights reserved! Copying, duplication strictly prohibited
  13. *
  14. * *****************************************
  15. * * STORE COMMANDER *
  16. * * http://www.StoreCommander.com *
  17. * * V 2015-09-15 *
  18. * *****************************************
  19. *
  20. * Compatibility: PS version: 1.1 to 1.6.1
  21. *
  22. **/
  23. @error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING);
  24. @ini_set("display_errors", "ON");
  25. $id_lang = Tools::getValue('id_lang','0');
  26. $action = Tools::getValue('action','');
  27. $return = "ERROR: Try again later";
  28. // FUNCTIONS
  29. $updated_products = array();
  30. function removeInShop($product_id, $shop_id)
  31. {
  32. if(!empty($product_id) && !empty($shop_id))
  33. {
  34. $product = new Product($product_id, false, null, $shop_id);
  35. // product_shop
  36. // delete row pour ce product et ce shop
  37. $sql = "DELETE FROM `"._DB_PREFIX_."product_shop` WHERE `id_product` = '".psql($product_id)."' AND id_shop = '".psql($shop_id)."'";
  38. Db::getInstance()->Execute($sql);
  39. // stock_available
  40. // delete row pour ce product et ce shop
  41. $sql = "DELETE FROM `"._DB_PREFIX_."stock_available` WHERE `id_product` = '".psql($product_id)."' AND id_shop = '".psql($shop_id)."'";
  42. Db::getInstance()->Execute($sql);
  43. // product_lang
  44. // delete row pour ce product et ce shop
  45. $sql = "DELETE FROM `"._DB_PREFIX_."product_lang` WHERE `id_product` = '".psql($product_id)."' AND id_shop = '".psql($shop_id)."'";
  46. Db::getInstance()->Execute($sql);
  47. // product_attribute_shop
  48. // delete row pour ce product et ce shop
  49. $sql = 'SELECT pa.id_product_attribute FROM `'._DB_PREFIX_.'product_attribute` pa WHERE pa.`id_product` = '.(int)$product_id.'';
  50. $attributes = Db::getInstance()->executeS($sql);
  51. foreach($attributes as $attribute)
  52. {
  53. if(!empty($attribute["id_product_attribute"]))
  54. {
  55. $sql = "DELETE FROM `"._DB_PREFIX_."product_attribute_shop` WHERE `id_product_attribute` = '".psql($attribute["id_product_attribute"])."' AND id_shop = '".psql($shop_id)."'";
  56. Db::getInstance()->Execute($sql);
  57. }
  58. }
  59. // image_shop
  60. // delete row pour images de ce product et ce shop
  61. $images = $product->getImages(Configuration::get("PS_LANG_DEFAULT"));
  62. foreach($images as $image)
  63. {
  64. if(!empty($image["id_image"]))
  65. {
  66. $sql = "DELETE FROM `"._DB_PREFIX_."image_shop` WHERE `id_image` = '".psql($image["id_image"])."' AND id_shop = '".psql($shop_id)."'";
  67. Db::getInstance()->Execute($sql);
  68. }
  69. }
  70. // feature_product
  71. // delete row pour ce product et ce feature
  72. $features = $product->getFeatures();
  73. foreach($features as $feature)
  74. {
  75. if(!empty($feature["id_feature"]))
  76. {
  77. $feature = new Feature($feature["id_feature"]);
  78. $associated_shops = $feature->getAssociatedShops();
  79. // si feature_shop existe QUE pour ce shop
  80. if(count($associated_shops)==1 && $associated_shops[0]==$shop_id)
  81. {
  82. $sql = "SELECT id_feature_value FROM `"._DB_PREFIX_."feature_product` WHERE `id_feature` = '".psql($feature->id)."' AND id_product = '".psql($product_id)."'";
  83. $values = Db::getInstance()->ExecuteS($sql);
  84. foreach($values as $value)
  85. {
  86. $feature_value = new FeatureValue($value["id_feature_value"]);
  87. if(!empty($feature_value->id))
  88. $feature_value->delete();
  89. }
  90. $sql = "DELETE FROM `"._DB_PREFIX_."feature_product` WHERE `id_feature` = '".psql($feature->id)."' AND id_product = '".psql($product_id)."'";
  91. Db::getInstance()->Execute($sql);
  92. }
  93. }
  94. }
  95. // supplier_product
  96. // si supplier_shop existe QUE pour ce shop
  97. $sql = "SELECT id_supplier FROM `"._DB_PREFIX_."product_supplier` WHERE id_product = '".psql($product_id)."' GROUP BY id_supplier";
  98. $suppliers = Db::getInstance()->ExecuteS($sql);
  99. foreach($suppliers as $supplier)
  100. {
  101. if(!empty($supplier) && !empty($supplier["id_supplier"]))
  102. {
  103. $supplier = new Supplier($supplier["id_supplier"]);
  104. $associated_shops = $supplier->getAssociatedShops();
  105. // delete row pour ce product et ce suplier
  106. if(count($associated_shops)==1 && $associated_shops[0]==$shop_id)
  107. {
  108. $sql = "DELETE FROM `"._DB_PREFIX_."supplier_product` WHERE `id_supplier` = '".psql($supplier->id)."' AND id_product = '".psql($product_id)."'";
  109. Db::getInstance()->Execute($sql);
  110. }
  111. }
  112. }
  113. // warehouse_product_location
  114. // si warehouse_shop existe QUE pour ce shop
  115. $sql = "SELECT id_warehouse FROM `"._DB_PREFIX_."warehouse_product_location` WHERE id_product = '".psql($product_id)."' GROUP BY id_warehouse";
  116. $warehouses = Db::getInstance()->ExecuteS($sql);
  117. foreach($warehouses as $warehouse)
  118. {
  119. if(!empty($warehouse["id_warehouse"]))
  120. {
  121. $warehouse = new Warehouse($warehouse["id_warehouse"]);
  122. $associated_shops = $warehouse->getAssociatedShops();
  123. // delete row pour ce product et ce warehouse
  124. if(count($associated_shops)==1 && $associated_shops[0]==$shop_id)
  125. {
  126. $sql = "DELETE FROM `"._DB_PREFIX_."warehouse_product_location` WHERE `id_warehouse` = '".psql($warehouse->id)."' AND id_product = '".psql($product_id)."'";
  127. Db::getInstance()->Execute($sql);
  128. }
  129. }
  130. }
  131. }
  132. }
  133. /*function duplicateCombination($id_product, $id_product_attribute, $id_shop_base, $id_shop_new)
  134. {
  135. //$product = new Product($id_product, false, null, $id_shop_base);
  136. $combination = new Combination($id_product_attribute, null, $id_shop_base);
  137. $sql = "INSERT INTO "._DB_PREFIX_."product_attribute_shop (id_product_attribute, id_shop, wholesale_price, price, ecotax, weight, unit_price_impact, default_on, minimal_quantity, available_date)
  138. SELECT '".$id_product_attribute."', '".$id_shop_new."', wholesale_price, price, ecotax, weight, unit_price_impact, default_on, minimal_quantity, available_date FROM "._DB_PREFIX_."product_attribute_shop
  139. WHERE id_product_attribute='".$id_product_attribute."' AND id_shop='".$id_shop_base."'";
  140. Db::getInstance()->Execute($sql);
  141. }*/
  142. // Récupération de toutes les modifications à effectuer
  143. if(!empty($_POST["rows"]) || $action=="insert")
  144. {
  145. if($action!="insert")
  146. {
  147. if(_PS_MAGIC_QUOTES_GPC_)
  148. $_POST["rows"] = stripslashes($_POST["rows"]);
  149. $rows = json_decode($_POST["rows"]);
  150. }
  151. else
  152. {
  153. $rows = array();
  154. $rows[0] = new stdClass();
  155. $rows[0]->name = Tools::getValue('act','');
  156. $rows[0]->action = Tools::getValue('action','');
  157. $rows[0]->row = Tools::getValue('gr_id','');
  158. $rows[0]->callback = Tools::getValue('callback','');
  159. $rows[0]->params = $_POST;
  160. }
  161. if(is_array($rows) && count($rows)>0)
  162. {
  163. $callbacks = '';
  164. // Première boucle pour remplir la table sc_queue_log
  165. // avec toutes ces modifications
  166. $log_ids = array();
  167. $date = date("Y-m-d H:i:s");
  168. foreach($rows as $num => $row)
  169. {
  170. $id = QueueLog::add($row->name, $row->row, $row->action, (!empty($row->params)?$row->params:array()), (!empty($row->callback)?$row->callback:null), $date);
  171. $log_ids[$num] = $id;
  172. }
  173. // Deuxième boucle pour effectuer les
  174. // actions les une après les autres
  175. foreach($rows as $num => $row)
  176. {
  177. if(!empty($log_ids[$num]))
  178. {
  179. $gr_id = intval($row->row);
  180. $action = $row->action;
  181. if(!empty($row->callback))
  182. $callbacks .= $row->callback.";";
  183. if($action!="insert")
  184. {
  185. $_POST=array();
  186. $_POST = (array) json_decode($row->params);
  187. }
  188. if(empty($gr_id))
  189. $gr_id=Tools::getValue('gr_id','0');
  190. if(!empty($action) && $action=="update" && !empty($gr_id))
  191. {
  192. $idlist=$gr_id;
  193. $action_upd=Tools::getValue('action_upd','');
  194. $id_lang=Tools::getValue('id_lang','0');
  195. $id_shop=Tools::getValue('id_shop','0');
  196. $id_actual_shop = SCI::getSelectedShop();
  197. $value=Tools::getValue('value','0');
  198. $auto_share_imgs=intval(Tools::getValue('auto_share_imgs','0'));
  199. if($value=="true")
  200. $value = 1;
  201. else
  202. $value = 0;
  203. $ids = explode(",", $idlist);
  204. if($action_upd!='' && !empty($id_shop) && !empty($idlist)/* && !empty($id_actual_shop)*/)
  205. {
  206. if(!empty($ids) && count($ids)==1)
  207. $updated_products[$ids]=$ids;
  208. elseif(!empty($ids) && count($ids)>1)
  209. $updated_products = array_merge($updated_products,$ids);
  210. switch($action_upd)
  211. {
  212. // Modification de active pour le shop passé en params
  213. // pour un ou plusieurs products passés en params
  214. case 'active':
  215. foreach($ids as $id)
  216. {
  217. $product = new Product($id, false, null, $id_actual_shop);
  218. if(!$product->isAssociatedToShop($id_shop))
  219. {
  220. $product->id_shop_list=array($id_shop);
  221. $product->active = (int)$value;
  222. $product->save();
  223. $sql = 'SELECT pa.id_product_attribute
  224. FROM `'._DB_PREFIX_.'product_attribute` pa
  225. WHERE pa.`id_product` = '.(int)$id.'';
  226. $product_attrs = Db::getInstance()->executeS($sql);
  227. foreach($product_attrs as $product_attr)
  228. {
  229. $combination = new Combination($product_attr["id_product_attribute"], null, $id_actual_shop);
  230. $combination->id_product = $id;
  231. $combination->id_shop_list=array($id_shop);
  232. $combination->minimal_quantity=max(1,(int)$combination->minimal_quantity);
  233. $combination->save();
  234. }
  235. //$images = $product->getImages($id_lang);
  236. if($auto_share_imgs)
  237. {
  238. $sql = 'SELECT pis.id_image
  239. FROM `'._DB_PREFIX_.'image_shop` pis
  240. INNER JOIN `'._DB_PREFIX_.'image` pi ON pi.id_image = pis.id_image
  241. WHERE pi.`id_product` = '.(int)$id.'
  242. AND pis.id_shop = "'.(int)$id_actual_shop.'"
  243. GROUP BY pis.id_image';
  244. $images = Db::getInstance()->executeS($sql);
  245. foreach ($images as $image)
  246. {
  247. /*$image_temp = new Image($image["id_image"]);
  248. $image_temp->id_shop_list=array($id_shop, $id_actual_shop);
  249. $image_temp->save();*/
  250. SCI::duplicateImageToShops($image["id_image"], $id_actual_shop, array($id_shop));
  251. }
  252. }
  253. }
  254. else
  255. {
  256. $sql = "UPDATE `"._DB_PREFIX_."product_shop` SET active='".(int)$value."' WHERE `id_product` = '".psql($id)."' AND id_shop = '".$id_shop."'";
  257. Db::getInstance()->Execute($sql);
  258. }
  259. }
  260. break;
  261. // Modification de present pour le shop passé en params
  262. // pour un ou plusieurs products passés en params
  263. case 'present':
  264. foreach($ids as $id)
  265. {
  266. $product = new Product($id, false, null, $id_actual_shop);
  267. if(!$product->isAssociatedToShop($id_shop) && $value=="1")
  268. {
  269. $product->id_shop_list=array($id_shop);
  270. $product->price=floatval($product->price);
  271. $product->save();
  272. SCI::setQuantity($id, null, StockAvailable::getQuantityAvailableByProduct($id, null, $id_actual_shop), $id_shop);
  273. $sql = 'UPDATE `'._DB_PREFIX_.'product_shop`
  274. SET active = "'._s("CAT_PROD_AUTO_ACTIVATION_MB_SHARE").'"
  275. WHERE `id_product` = '.(int)$id.' AND `id_shop` = '.(int)$id_shop.'';
  276. Db::getInstance()->execute($sql);
  277. $sql = 'SELECT pa.id_product_attribute
  278. FROM `'._DB_PREFIX_.'product_attribute` pa
  279. WHERE pa.`id_product` = '.(int)$id.'';
  280. $product_attrs = Db::getInstance()->executeS($sql);
  281. foreach($product_attrs as $product_attr)
  282. {
  283. $combination = new Combination($product_attr["id_product_attribute"], null, $id_actual_shop);
  284. $combination->id_product = $id;
  285. $combination->id_shop_list=array($id_shop);
  286. $combination->minimal_quantity=max(1,(int)$combination->minimal_quantity);
  287. $combination->save();
  288. }
  289. //$images = $product->getImages($id_lang);
  290. if($auto_share_imgs)
  291. {
  292. $sql = 'SELECT pis.id_image
  293. FROM `'._DB_PREFIX_.'image_shop` pis
  294. INNER JOIN `'._DB_PREFIX_.'image` pi ON pi.id_image = pis.id_image
  295. WHERE pi.`id_product` = '.(int)$id.'
  296. AND pis.id_shop = "'.(int)$id_actual_shop.'"
  297. GROUP BY pis.id_image';
  298. $images = Db::getInstance()->executeS($sql);
  299. foreach ($images as $image)
  300. {
  301. /*$image_temp = new Image($image["id_image"]);
  302. $image_temp->id_shop_list=array($id_shop, $id_actual_shop);
  303. $image_temp->save();*/
  304. SCI::duplicateImageToShops($image["id_image"], $id_actual_shop, array($id_shop));
  305. }
  306. }
  307. }
  308. elseif($product->isAssociatedToShop($id_shop) && empty($value))
  309. {
  310. if($id_shop != $product->id_shop_default)
  311. removeInShop($id, $id_shop);
  312. }
  313. }
  314. break;
  315. // Modification la boutique par défaut
  316. // pour un ou plusieurs products passés en params
  317. case 'default':
  318. foreach($ids as $id)
  319. {
  320. $sql2 ="SELECT id_shop_default
  321. FROM "._DB_PREFIX_."product
  322. WHERE id_product = '".psql($id)."'";
  323. $res2 = Db::getInstance()->getRow($sql2);
  324. if(!empty($res2["id_shop_default"]))
  325. {
  326. $product = new Product($id, false, null, $res2["id_shop_default"]);
  327. if(!$product->isAssociatedToShop($id_shop))
  328. {
  329. $product->id_shop_list=array($id_shop);
  330. $sql = 'SELECT pa.id_product_attribute
  331. FROM `'._DB_PREFIX_.'product_attribute` pa
  332. WHERE pa.`id_product` = '.(int)$id.'';
  333. $product_attrs = Db::getInstance()->executeS($sql);
  334. foreach($product_attrs as $product_attr)
  335. {
  336. $combination = new Combination($product_attr["id_product_attribute"], null, $res2["id_shop_default"]);
  337. $combination->id_product = $id;
  338. $combination->id_shop_list=array($id_shop);
  339. $combination->minimal_quantity=max(1,(int)$combination->minimal_quantity);
  340. $combination->save();
  341. }
  342. //$images = $product->getImages($id_lang);
  343. if($auto_share_imgs)
  344. {
  345. $sql = 'SELECT pis.id_image
  346. FROM `'._DB_PREFIX_.'image_shop` pis
  347. INNER JOIN `'._DB_PREFIX_.'image` pi ON pi.id_image = pis.id_image
  348. WHERE pi.`id_product` = '.(int)$id.'
  349. AND pis.id_shop = "'.(int)$id_actual_shop.'"
  350. GROUP BY pis.id_image';
  351. $images = Db::getInstance()->executeS($sql);
  352. foreach ($images as $image)
  353. {
  354. /*$image_temp = new Image($image["id_image"]);
  355. $image_temp->id_shop_list=array($id_shop, $id_actual_shop);
  356. $image_temp->save();*/
  357. SCI::duplicateImageToShops($image["id_image"], $id_actual_shop, array($id_shop));
  358. }
  359. }
  360. }
  361. $product->id_shop_default=$id_shop;
  362. $product->save();
  363. $sql = 'UPDATE `'._DB_PREFIX_.'product_shop`
  364. SET active = "'._s("CAT_PROD_AUTO_ACTIVATION_MB_SHARE").'"
  365. WHERE `id_product` = '.(int)$id.' AND `id_shop` = '.(int)$id_shop.'';
  366. Db::getInstance()->execute($sql);
  367. $images = array();
  368. $image_temps = $product->getImages($id_lang);
  369. foreach ($image_temps as $image_temp)
  370. {
  371. if(!empty($image_temp["id_image"]))
  372. $images[] = $image_temp["id_image"];
  373. }
  374. if(!empty($images) && count($images)>0)
  375. SCI::addToShops("image", $images,array($id_shop));
  376. }
  377. }
  378. break;
  379. // Modification de present
  380. // pour un ou plusieurs shops passés en params
  381. // pour un ou plusieurs products passés en params
  382. case 'mass_present':
  383. $shops = explode(",", $id_shop);
  384. foreach($shops as $id_shop)
  385. {
  386. foreach($ids as $id)
  387. {
  388. $product = new Product($id, false, null, $id_actual_shop);
  389. if(!$product->isAssociatedToShop($id_shop) && $value=="1")
  390. {
  391. $product->id_shop_list=array($id_shop);
  392. $product->price=floatval($product->price);
  393. $product->save();
  394. SCI::setQuantity($id, null, StockAvailable::getQuantityAvailableByProduct($id, null, $id_actual_shop), $id_shop);
  395. $sql = 'UPDATE `'._DB_PREFIX_.'product_shop`
  396. SET active = "'._s("CAT_PROD_AUTO_ACTIVATION_MB_SHARE").'"
  397. WHERE `id_product` = '.(int)$id.' AND `id_shop` = '.(int)$id_shop.'';
  398. Db::getInstance()->execute($sql);
  399. $sql = 'SELECT pa.id_product_attribute
  400. FROM `'._DB_PREFIX_.'product_attribute` pa
  401. WHERE pa.`id_product` = '.(int)$id.'';
  402. $product_attrs = Db::getInstance()->executeS($sql);
  403. foreach($product_attrs as $product_attr)
  404. {
  405. $combination = new Combination($product_attr["id_product_attribute"], null, $id_actual_shop);
  406. $combination->id_product = $id;
  407. $combination->id_shop_list=array($id_shop);
  408. $combination->minimal_quantity=max(1,(int)$combination->minimal_quantity);
  409. $combination->save();
  410. }
  411. //$images = $product->getImages($id_lang);
  412. if($auto_share_imgs)
  413. {
  414. $sql = 'SELECT pis.id_image
  415. FROM `'._DB_PREFIX_.'image_shop` pis
  416. INNER JOIN `'._DB_PREFIX_.'image` pi ON pi.id_image = pis.id_image
  417. WHERE pi.`id_product` = '.(int)$id.'
  418. AND pis.id_shop = "'.(int)$id_actual_shop.'"
  419. GROUP BY pis.id_image';
  420. $images = Db::getInstance()->executeS($sql);
  421. foreach ($images as $image)
  422. {
  423. /*$image_temp = new Image($image["id_image"]);
  424. $image_temp->id_shop_list=array($id_shop, $id_actual_shop);
  425. $image_temp->save();*/
  426. SCI::duplicateImageToShops($image["id_image"], $id_actual_shop, array($id_shop));
  427. }
  428. }
  429. }
  430. elseif($product->isAssociatedToShop($id_shop) && empty($value))
  431. {
  432. if($id_shop != $product->id_shop_default)
  433. removeInShop($id, $id_shop);
  434. }
  435. }
  436. }
  437. break;
  438. // Modification de active
  439. // pour un ou plusieurs shops passés en params
  440. // pour un ou plusieurs products passés en params
  441. case 'mass_active':
  442. $shops = explode(",", $id_shop);
  443. foreach($shops as $shop)
  444. {
  445. foreach($ids as $id)
  446. {
  447. $product = new Product($id, false, null, $id_actual_shop);
  448. if(!$product->isAssociatedToShop($shop))
  449. {
  450. $product->id_shop_list=array($shop);
  451. $product->active = (int)$value;
  452. $product->save();
  453. $sql = 'SELECT pa.id_product_attribute
  454. FROM `'._DB_PREFIX_.'product_attribute` pa
  455. WHERE pa.`id_product` = '.(int)$id.'';
  456. $product_attrs = Db::getInstance()->executeS($sql);
  457. foreach($product_attrs as $product_attr)
  458. {
  459. $combination = new Combination($product_attr["id_product_attribute"], null, $id_actual_shop);
  460. $combination->id_product = $id;
  461. $combination->id_shop_list=array($shop);
  462. $combination->minimal_quantity=max(1,(int)$combination->minimal_quantity);
  463. $combination->save();
  464. }
  465. //$images = $product->getImages($id_lang);
  466. if($auto_share_imgs)
  467. {
  468. $sql = 'SELECT pis.id_image
  469. FROM `'._DB_PREFIX_.'image_shop` pis
  470. INNER JOIN `'._DB_PREFIX_.'image` pi ON pi.id_image = pis.id_image
  471. WHERE pi.`id_product` = '.(int)$id.'
  472. AND pis.id_shop = "'.(int)$id_actual_shop.'"
  473. GROUP BY pis.id_image';
  474. $images = Db::getInstance()->executeS($sql);
  475. foreach ($images as $image)
  476. {
  477. /*$image_temp = new Image($image["id_image"]);
  478. $image_temp->id_shop_list=array($shop, $id_actual_shop);
  479. $image_temp->save();*/
  480. SCI::duplicateImageToShops($image["id_image"], $id_actual_shop, array($shop));
  481. }
  482. }
  483. }
  484. else
  485. {
  486. $sql = "UPDATE `"._DB_PREFIX_."product_shop` SET active='".(int)$value."' WHERE `id_product` = '".psql($id)."' AND id_shop = '".$shop."'";
  487. Db::getInstance()->Execute($sql);
  488. }
  489. }
  490. }
  491. break;
  492. }
  493. }
  494. }
  495. QueueLog::delete(($log_ids[$num]));
  496. }
  497. }
  498. // PM Cache
  499. if(!empty($updated_products))
  500. ExtensionPMCM::clearFromIdsProduct($updated_products);
  501. // RETURN
  502. $return = json_encode(array("callback"=>$callbacks));
  503. }
  504. }
  505. echo $return;