/modules/storecommander/ead6f6fce09/SC/lib/cat/carrier/cat_carrier_update_queue.php

https://gitlab.com/ptisky/API_prestashop · PHP · 270 lines · 226 code · 16 blank · 28 comment · 25 complexity · 84e431ce8aebb4471d7cb1972ed2d82c 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. // Récupération de toutes les modifications à effectuer
  30. if(!empty($_POST["rows"]) || $action=="insert")
  31. {
  32. if($action!="insert")
  33. {
  34. if(_PS_MAGIC_QUOTES_GPC_)
  35. $_POST["rows"] = stripslashes($_POST["rows"]);
  36. $rows = json_decode($_POST["rows"]);
  37. }
  38. else
  39. {
  40. $rows = array();
  41. $rows[0] = new stdClass();
  42. $rows[0]->name = Tools::getValue('act','');
  43. $rows[0]->action = Tools::getValue('action','');
  44. $rows[0]->row = Tools::getValue('gr_id','');
  45. $rows[0]->callback = Tools::getValue('callback','');
  46. $rows[0]->params = $_POST;
  47. }
  48. if(is_array($rows) && count($rows)>0)
  49. {
  50. $callbacks = '';
  51. // Première boucle pour remplir la table sc_queue_log
  52. // avec toutes ces modifications
  53. $log_ids = array();
  54. $date = date("Y-m-d H:i:s");
  55. foreach($rows as $num => $row)
  56. {
  57. $id = QueueLog::add($row->name, $row->row, $row->action, (!empty($row->params)?$row->params:array()), (!empty($row->callback)?$row->callback:null), $date);
  58. $log_ids[$num] = $id;
  59. }
  60. // Deuxième boucle pour effectuer les
  61. // actions les une après les autres
  62. foreach($rows as $num => $row)
  63. {
  64. if(!empty($log_ids[$num]))
  65. {
  66. $gr_id = intval($row->row);
  67. $action = $row->action;
  68. if(!empty($row->callback))
  69. $callbacks .= $row->callback.";";
  70. if($action!="insert")
  71. {
  72. $_POST=array();
  73. $_POST = (array) json_decode($row->params);
  74. }
  75. if(!empty($action) && $action=="update")
  76. {
  77. $idlist=Tools::getValue('idlist','');
  78. $sub_action=Tools::getValue('sub_action','');
  79. $value=(Tools::getValue('value'));
  80. if($value=="true")
  81. $value = 1;
  82. else
  83. $value = 0;
  84. if(!empty($idlist))
  85. {
  86. $products=(explode(',',$idlist));
  87. if($sub_action=="present")
  88. {
  89. $id_carrier = (int)$gr_id;
  90. if(!empty($id_carrier))
  91. {
  92. if($value == 1)
  93. {
  94. if(SCMS)
  95. {
  96. $shops = SCI::getSelectedShopActionList();
  97. foreach($shops as $shop_id)
  98. {
  99. foreach($products as $id_product)
  100. {
  101. $sql = 'SELECT *
  102. FROM `'._DB_PREFIX_.'product_carrier`
  103. WHERE `id_carrier_reference` = "'.(int)$id_carrier.'"
  104. AND `id_product` = '.(int)$id_product.'
  105. AND id_shop = "'.(int)$shop_id.'"';
  106. $tmp_used = Db::getInstance()->executeS($sql);
  107. if(empty($tmp_used))
  108. {
  109. $sql = 'INSERT INTO `'._DB_PREFIX_.'product_carrier` (id_product, id_carrier_reference, id_shop)
  110. VALUES ("'.(int)$id_product.'","'.(int)$id_carrier.'","'.(int)$shop_id.'")';
  111. Db::getInstance()->execute($sql);
  112. }
  113. }
  114. }
  115. }
  116. else
  117. {
  118. foreach($products as $id_product)
  119. {
  120. $sql = 'SELECT *
  121. FROM `'._DB_PREFIX_.'product_carrier`
  122. WHERE `id_carrier_reference` = "'.(int)$id_carrier.'"
  123. AND `id_product` = '.(int)$id_product.'';
  124. $tmp_used = Db::getInstance()->executeS($sql);
  125. if(empty($tmp_used))
  126. {
  127. $sql = 'INSERT INTO `'._DB_PREFIX_.'product_carrier` (id_product, id_carrier_reference, id_shop)
  128. VALUES ("'.(int)$id_product.'","'.(int)$id_carrier.'","1")';
  129. Db::getInstance()->execute($sql);
  130. }
  131. }
  132. }
  133. }
  134. else
  135. {
  136. if(SCMS)
  137. {
  138. $shops = SCI::getSelectedShopActionList();
  139. foreach($shops as $shop_id)
  140. {
  141. foreach($products as $id_product)
  142. {
  143. $sql = 'DELETE FROM `'._DB_PREFIX_.'product_carrier`
  144. WHERE `id_carrier_reference` = "'.(int)$id_carrier.'"
  145. AND `id_product` = '.(int)$id_product.'
  146. AND id_shop = "'.(int)$shop_id.'"';
  147. Db::getInstance()->execute($sql);
  148. }
  149. }
  150. }
  151. else
  152. {
  153. foreach($products as $id_product)
  154. {
  155. $sql = 'DELETE FROM `'._DB_PREFIX_.'product_carrier`
  156. WHERE `id_carrier_reference` = "'.(int)$id_carrier.'"
  157. AND `id_product` = '.(int)$id_product.'';
  158. Db::getInstance()->execute($sql);
  159. }
  160. }
  161. }
  162. }
  163. }
  164. elseif($sub_action=="mass_add")
  165. {
  166. $carriers=Tools::getValue('carriers','');
  167. $carriers=(explode(',',$carriers));
  168. $shops = SCI::getSelectedShopActionList();
  169. foreach($carriers as $id_carrier)
  170. {
  171. if(SCMS)
  172. {
  173. foreach($shops as $shop_id)
  174. {
  175. foreach($products as $id_product)
  176. {
  177. $sql = 'SELECT *
  178. FROM `'._DB_PREFIX_.'product_carrier`
  179. WHERE `id_carrier_reference` = "'.(int)$id_carrier.'"
  180. AND `id_product` = '.(int)$id_product.'
  181. AND id_shop = "'.(int)$shop_id.'"';
  182. $tmp_used = Db::getInstance()->executeS($sql);
  183. if(empty($tmp_used))
  184. {
  185. $sql = 'INSERT INTO `'._DB_PREFIX_.'product_carrier` (id_product, id_carrier_reference, id_shop)
  186. VALUES ("'.(int)$id_product.'","'.(int)$id_carrier.'","'.(int)$shop_id.'")';
  187. Db::getInstance()->execute($sql);
  188. }
  189. }
  190. }
  191. }
  192. else
  193. {
  194. foreach($products as $id_product)
  195. {
  196. $sql = 'SELECT *
  197. FROM `'._DB_PREFIX_.'product_carrier`
  198. WHERE `id_carrier_reference` = "'.(int)$id_carrier.'"
  199. AND `id_product` = '.(int)$id_product.'';
  200. $tmp_used = Db::getInstance()->executeS($sql);
  201. if(empty($tmp_used))
  202. {
  203. $sql = 'INSERT INTO `'._DB_PREFIX_.'product_carrier` (id_product, id_carrier_reference, id_shop)
  204. VALUES ("'.(int)$id_product.'","'.(int)$id_carrier.'","1")';
  205. Db::getInstance()->execute($sql);
  206. }
  207. }
  208. }
  209. }
  210. }
  211. elseif($sub_action=="mass_delete")
  212. {
  213. $carriers=Tools::getValue('carriers','');
  214. $carriers=(explode(',',$carriers));
  215. $shops = SCI::getSelectedShopActionList();
  216. foreach($carriers as $id_carrier)
  217. {
  218. if(SCMS)
  219. {
  220. $shops = SCI::getSelectedShopActionList();
  221. foreach($shops as $shop_id)
  222. {
  223. foreach($products as $id_product)
  224. {
  225. $sql = 'DELETE FROM `'._DB_PREFIX_.'product_carrier`
  226. WHERE `id_carrier_reference` = "'.(int)$id_carrier.'"
  227. AND `id_product` = '.(int)$id_product.'
  228. AND id_shop = "'.(int)$shop_id.'"';
  229. Db::getInstance()->execute($sql);
  230. }
  231. }
  232. }
  233. else
  234. {
  235. foreach($products as $id_product)
  236. {
  237. $sql = 'DELETE FROM `'._DB_PREFIX_.'product_carrier`
  238. WHERE `id_carrier_reference` = "'.(int)$id_carrier.'"
  239. AND `id_product` = '.(int)$id_product.'';
  240. Db::getInstance()->execute($sql);
  241. }
  242. }
  243. }
  244. }
  245. }
  246. }
  247. QueueLog::delete(($log_ids[$num]));
  248. }
  249. }
  250. // RETURN
  251. $return = json_encode(array("callback"=>$callbacks));
  252. }
  253. }
  254. echo $return;