/modules/updateproducts/send.php

https://gitlab.com/ptisky/API_prestashop · PHP · 276 lines · 248 code · 28 blank · 0 comment · 49 complexity · 017ee37e2257c97acf18b471f5e3ba20 MD5 · raw file

  1. <?php
  2. include_once(dirname(__FILE__).'/../../config/config.inc.php');
  3. include_once(dirname(__FILE__).'/../../init.php');
  4. if(empty($_SERVER['HTTP_X_REQUESTED_WITH']) || Tools::strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest'){
  5. header('HTTP/1.0 403 Forbidden');
  6. echo 'You are forbidden!'; die;
  7. }
  8. $json = array();
  9. try {
  10. ini_set("max_execution_time","0");
  11. ini_set('memory_limit', '1024M');
  12. if( Tools::getValue('removeSettingUpdate') !== false){
  13. $id = Tools::getValue('id');
  14. Configuration::deleteByName('GOMAKOIL_FIELDS_CHECKED_UPDATE_'.$id);
  15. Configuration::deleteByName('GOMAKOIL_LANG_CHECKED_UPDATE_'.$id);
  16. Configuration::deleteByName('GOMAKOIL_TYPE_FILE_UPDATE_'.$id);
  17. Configuration::deleteByName('GOMAKOIL_NAME_SETTING_UPDATE_'.$id);
  18. $settings = array();
  19. $settings = Tools::unserialize(Configuration::get('GOMAKOIL_ALL_UPDATE_SETTINGS', '' ,Tools::getValue('shopGroupId'), Tools::getValue('id_shop')));
  20. if(in_array($id, $settings)){
  21. $key = array_search($id, $settings);
  22. unset ($settings[$key]);
  23. $settings =serialize($settings);
  24. Configuration::updateValue('GOMAKOIL_ALL_UPDATE_SETTINGS', $settings, false, Tools::getValue('shopGroupId'), Tools::getValue('id_shop'));
  25. }
  26. $json['success'] = true;
  27. }
  28. if( Tools::getValue('removeSetting') !== false){
  29. $id = Tools::getValue('id');
  30. Configuration::deleteByName('GOMAKOIL_PRODUCTS_CHECKED_'.$id);
  31. Configuration::deleteByName('GOMAKOIL_MANUFACTURERS_CHECKED_'.$id);
  32. Configuration::deleteByName('GOMAKOIL_SUPPLIERS_CHECKED_'.$id);
  33. Configuration::deleteByName('GOMAKOIL_CATEGORIES_CHECKED_'.$id);
  34. Configuration::deleteByName('GOMAKOIL_FIELDS_CHECKED_'.$id);
  35. Configuration::deleteByName('GOMAKOIL_LANG_CHECKED_'.$id);
  36. Configuration::deleteByName('GOMAKOIL_TYPE_FILE_'.$id);
  37. Configuration::deleteByName('GOMAKOIL_NAME_SETTING_'.$id);
  38. $settings = array();
  39. $settings = Tools::unserialize(Configuration::get('GOMAKOIL_ALL_SETTINGS', '' ,Tools::getValue('shopGroupId'), Tools::getValue('id_shop')));
  40. if(in_array($id, $settings)){
  41. $key = array_search($id, $settings);
  42. unset ($settings[$key]);
  43. $settings =serialize($settings);
  44. Configuration::updateValue('GOMAKOIL_ALL_SETTINGS', $settings, false, Tools::getValue('shopGroupId'), Tools::getValue('id_shop'));
  45. }
  46. $json['success'] = true;
  47. }
  48. if( Tools::getValue('saveSettingsUpdate') !== false) {
  49. if( !Tools::getValue('save_setting_update') || Tools::getValue('save_setting_update') == ' ' ){
  50. throw new Exception(Module::getInstanceByName('updateproducts')->l('Please enter name setting!'));
  51. }
  52. $name_config = 'GOMAKOIL_NAME_SETTING_UPDATE_'.Tools::getValue('last_id_update');
  53. $config = Configuration::get($name_config, '' ,Tools::getValue('shopGroupId'), Tools::getValue('id_shop'));
  54. if($config && $config !== Tools::getValue('save_setting_update')){
  55. $all_setting = Tools::unserialize( Configuration::get('GOMAKOIL_ALL_UPDATE_SETTINGS','',Tools::getValue('shopGroupId'), Tools::getValue('id_shop')));
  56. if($all_setting){
  57. $all_setting = max($all_setting);
  58. $id = $all_setting + 1;
  59. }
  60. }
  61. else{
  62. $id = Tools::getValue('last_id_update');
  63. }
  64. Configuration::updateValue('GOMAKOIL_NAME_SETTING_UPDATE_'.$id, trim(Tools::getValue('save_setting_update')), false, Tools::getValue('shopGroupId'), Tools::getValue('id_shop'));
  65. $name_config = 'GOMAKOIL_FIELDS_CHECKED_UPDATE_'.$id;
  66. Configuration::updateValue($name_config, serialize(Tools::getValue('field_update')), false, Tools::getValue('shopGroupId'), Tools::getValue('id_shop'));
  67. $name_config = 'GOMAKOIL_LANG_CHECKED_UPDATE_'.$id;
  68. Configuration::updateValue($name_config, Tools::getValue('id_lang_update'), false, Tools::getValue('shopGroupId'), Tools::getValue('id_shop'));
  69. $name_config = 'GOMAKOIL_TYPE_FILE_UPDATE_'.$id;
  70. Configuration::updateValue($name_config, Tools::getValue('format_file_update'), false, Tools::getValue('shopGroupId'), Tools::getValue('id_shop'));
  71. $settings = array();
  72. $settings = Tools::unserialize(Configuration::get('GOMAKOIL_ALL_UPDATE_SETTINGS', '' ,Tools::getValue('shopGroupId'), Tools::getValue('id_shop')));
  73. if($settings){
  74. if(!in_array($id, $settings)){
  75. $settings[] = $id;
  76. $settings =serialize($settings);
  77. Configuration::updateValue('GOMAKOIL_ALL_UPDATE_SETTINGS', $settings, false, Tools::getValue('shopGroupId'), Tools::getValue('id_shop'));
  78. }
  79. }
  80. else{
  81. $settings[] = $id;
  82. $settings =serialize($settings);
  83. Configuration::updateValue('GOMAKOIL_ALL_UPDATE_SETTINGS', $settings, false, Tools::getValue('shopGroupId'), Tools::getValue('id_shop'));
  84. }
  85. $json['id'] = $id;
  86. }
  87. if( Tools::getValue('saveSettings') !== false){
  88. if( !Tools::getValue('save_setting') ){
  89. throw new Exception(Module::getInstanceByName('updateproducts')->l('Please enter name setting!'));
  90. }
  91. $name_config = 'GOMAKOIL_NAME_SETTING_'.Tools::getValue('last_id');
  92. $config = Configuration::get($name_config, '' ,Tools::getValue('shopGroupId'), Tools::getValue('id_shop'));
  93. if($config && $config !== Tools::getValue('save_setting')){
  94. $all_setting = Tools::unserialize( Configuration::get('GOMAKOIL_ALL_SETTINGS','',Tools::getValue('shopGroupId'), Tools::getValue('id_shop')));
  95. if($all_setting){
  96. $all_setting = max($all_setting);
  97. $id = $all_setting + 1;
  98. }
  99. }
  100. else{
  101. $id = Tools::getValue('last_id');
  102. }
  103. Configuration::updateValue('GOMAKOIL_NAME_SETTING_'.$id, Tools::getValue('save_setting'), false, Tools::getValue('shopGroupId'), Tools::getValue('id_shop'));
  104. $config = Configuration::get('GOMAKOIL_PRODUCTS_CHECKED', '' ,Tools::getValue('shopGroupId'), Tools::getValue('id_shop'));
  105. $name_config = 'GOMAKOIL_PRODUCTS_CHECKED_'.$id;
  106. Configuration::updateValue($name_config, $config, false, Tools::getValue('shopGroupId'), Tools::getValue('id_shop'));
  107. $config = Configuration::get('GOMAKOIL_MANUFACTURERS_CHECKED', '' ,Tools::getValue('shopGroupId'), Tools::getValue('id_shop'));
  108. $name_config = 'GOMAKOIL_MANUFACTURERS_CHECKED_'.$id;
  109. Configuration::updateValue($name_config, $config, false, Tools::getValue('shopGroupId'), Tools::getValue('id_shop'));
  110. $config = Configuration::get('GOMAKOIL_SUPPLIERS_CHECKED', '' ,Tools::getValue('shopGroupId'), Tools::getValue('id_shop'));
  111. $name_config = 'GOMAKOIL_SUPPLIERS_CHECKED_'.$id;
  112. Configuration::updateValue($name_config, $config, false, Tools::getValue('shopGroupId'), Tools::getValue('id_shop'));
  113. $name_config = 'GOMAKOIL_CATEGORIES_CHECKED_'.$id;
  114. Configuration::updateValue($name_config, serialize(Tools::getValue('categories')), false, Tools::getValue('shopGroupId'), Tools::getValue('id_shop'));
  115. $name_config = 'GOMAKOIL_FIELDS_CHECKED_'.$id;
  116. Configuration::updateValue($name_config, serialize(Tools::getValue('field')), false, Tools::getValue('shopGroupId'), Tools::getValue('id_shop'));
  117. $name_config = 'GOMAKOIL_LANG_CHECKED_'.$id;
  118. Configuration::updateValue($name_config, Tools::getValue('id_lang'), false, Tools::getValue('shopGroupId'), Tools::getValue('id_shop'));
  119. $name_config = 'GOMAKOIL_TYPE_FILE_'.$id;
  120. Configuration::updateValue($name_config, Tools::getValue('format_file'), false, Tools::getValue('shopGroupId'), Tools::getValue('id_shop'));
  121. $settings = array();
  122. $settings = Tools::unserialize(Configuration::get('GOMAKOIL_ALL_SETTINGS', '' ,Tools::getValue('shopGroupId'), Tools::getValue('id_shop')));
  123. if($settings){
  124. if(!in_array($id, $settings)){
  125. $settings[] = $id;
  126. $settings =serialize($settings);
  127. Configuration::updateValue('GOMAKOIL_ALL_SETTINGS', $settings, false, Tools::getValue('shopGroupId'), Tools::getValue('id_shop'));
  128. }
  129. }
  130. else{
  131. $settings[] = $id;
  132. $settings =serialize($settings);
  133. Configuration::updateValue('GOMAKOIL_ALL_SETTINGS', $settings, false, Tools::getValue('shopGroupId'), Tools::getValue('id_shop'));
  134. }
  135. $json['id'] = $id;
  136. }
  137. if( Tools::getValue('add_product') !== false){
  138. $name_config = 'GOMAKOIL_PRODUCTS_CHECKED';
  139. $config = Tools::unserialize(Configuration::get($name_config, '' ,Tools::getValue('shopGroupId'), Tools::getValue('id_shop')));
  140. if( !$config ){
  141. $config = array();
  142. }
  143. if (!in_array( Tools::getValue('id_product'), $config)){
  144. array_push($config, Tools::getValue('id_product'));
  145. }
  146. else{
  147. $key = array_search(Tools::getValue('id_product'), $config);
  148. if ($key !== false)
  149. {
  150. unset ($config[$key]);
  151. }
  152. }
  153. $products =serialize($config);
  154. Configuration::updateValue($name_config, $products, false, Tools::getValue('shopGroupId'), Tools::getValue('id_shop'));
  155. }
  156. if( Tools::getValue('add_manufacturer') !== false){
  157. $name_config = 'GOMAKOIL_MANUFACTURERS_CHECKED';
  158. $config = Tools::unserialize(Configuration::get($name_config, '' ,Tools::getValue('shopGroupId'), Tools::getValue('id_shop')));
  159. if( !$config ){
  160. $config = array();
  161. }
  162. if (!in_array( Tools::getValue('id_manufacturer'), $config)){
  163. array_push($config, Tools::getValue('id_manufacturer'));
  164. }
  165. else{
  166. $key = array_search(Tools::getValue('id_manufacturer'), $config);
  167. if ($key !== false)
  168. {
  169. unset($config[$key]);
  170. }
  171. }
  172. $config = serialize($config);
  173. Configuration::updateValue($name_config, $config, false, Tools::getValue('shopGroupId'), Tools::getValue('id_shop'));
  174. }
  175. if( Tools::getValue('add_supplier') !== false){
  176. $name_config = 'GOMAKOIL_SUPPLIERS_CHECKED';
  177. $config = Tools::unserialize(Configuration::get($name_config, '' ,Tools::getValue('shopGroupId'), Tools::getValue('id_shop')));
  178. if( !$config ){
  179. $config = array();
  180. }
  181. if (!in_array( Tools::getValue('id_supplier'), $config)){
  182. array_push($config, Tools::getValue('id_supplier'));
  183. }
  184. else{
  185. $key = array_search(Tools::getValue('id_supplier'), $config);
  186. if ($key !== false)
  187. {
  188. unset($config[$key]);
  189. }
  190. }
  191. $config = serialize($config);
  192. Configuration::updateValue($name_config, $config, false, Tools::getValue('shopGroupId'), Tools::getValue('id_shop'));
  193. }
  194. if( Tools::getValue('search_product') !== false){
  195. $json['products'] = Module::getInstanceByName('updateproducts')->searchProducts(Tools::getValue('search_product'), Tools::getValue('id_shop'), Tools::getValue('id_lang'),Tools::getValue('shopGroupId'));
  196. }
  197. if( Tools::getValue('search_manufacturer') !== false){
  198. $json['manufacturers'] = Module::getInstanceByName('updateproducts')->searchManufacturers(Tools::getValue('search_manufacturer'),Tools::getValue('id_shop'), Tools::getValue('shopGroupId'));
  199. }
  200. if( Tools::getValue('search_supplier') !== false){
  201. $json['suppliers'] = Module::getInstanceByName('updateproducts')->searchSuppliers(Tools::getValue('search_supplier'),Tools::getValue('id_shop'), Tools::getValue('shopGroupId'));
  202. }
  203. if( Tools::getValue('show_checked_products') !== false){
  204. $json['products'] = Module::getInstanceByName('updateproducts')->showCheckedProducts(Tools::getValue('id_shop'), Tools::getValue('id_lang'), Tools::getValue('shopGroupId'));
  205. }
  206. if( Tools::getValue('show_checked_manufacturers') !== false){
  207. $json['manufacturers'] = Module::getInstanceByName('updateproducts')->showCheckedManufacturers(Tools::getValue('id_shop'), Tools::getValue('shopGroupId'));
  208. }
  209. if( Tools::getValue('show_checked_suppliers') !== false){
  210. $json['suppliers'] = Module::getInstanceByName('updateproducts')->showCheckedSuppliers(Tools::getValue('id_shop'), Tools::getValue('shopGroupId'));
  211. }
  212. if( Tools::getValue('show_all_products') !== false){
  213. $json['products'] = Module::getInstanceByName('updateproducts')->showAllProducts(Tools::getValue('id_shop'), Tools::getValue('id_lang'), Tools::getValue('shopGroupId'));
  214. }
  215. if( Tools::getValue('show_all_manufacturers') !== false){
  216. $json['manufacturers'] = Module::getInstanceByName('updateproducts')->showAllManufacturers(Tools::getValue('id_shop'), Tools::getValue('shopGroupId'));
  217. }
  218. if( Tools::getValue('show_all_suppliers') !== false){
  219. $json['suppliers'] = Module::getInstanceByName('updateproducts')->showAllSuppliers(Tools::getValue('id_shop'), Tools::getValue('shopGroupId'));
  220. }
  221. if( Tools::getValue('export') !== false){
  222. Configuration::updateValue('GOMAKOIL_CATEGORIES_CHECKED', '', false, Tools::getValue('shopGroupId'), Tools::getValue('id_shop'));
  223. if( Tools::getValue('categories') ){
  224. Configuration::updateValue('GOMAKOIL_CATEGORIES_CHECKED', serialize(Tools::getValue('categories')), false, Tools::getValue('shopGroupId'), Tools::getValue('id_shop'));
  225. }
  226. if( Tools::getValue('field') ){
  227. Configuration::updateValue('GOMAKOIL_FIELDS_CHECKED', serialize(Tools::getValue('field')), false, Tools::getValue('shopGroupId'), Tools::getValue('id_shop'));
  228. }
  229. include_once('export.php');
  230. $export = new exportProducts( Tools::getValue('id_shop'), Tools::getValue('id_lang'), Tools::getValue('format_file'), Tools::getValue('shopGroupId') );
  231. $fileName = $export->export();
  232. $json['file'] = $fileName;
  233. }
  234. if( Tools::getValue('update') !== false){
  235. if( !Tools::getValue('field_update') ){
  236. throw new Exception(Module::getInstanceByName('updateproducts')->l('Please select fields for update!'));
  237. }
  238. include_once('update.php');
  239. $export = new updateProductCatalog( Tools::getValue('id_shop'), Tools::getValue('id_lang'), Tools::getValue('format_file'), Tools::getValue('field_update') );
  240. $res = $export->update();
  241. $json['success'] = $res;
  242. }
  243. echo Tools::jsonEncode($json);
  244. }
  245. catch( Exception $e ){
  246. $json['error'] = $e->getMessage();
  247. echo Tools::jsonEncode($json);
  248. }