PageRenderTime 88ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/shopimporter/shopimporter.php

http://marocmall.googlecode.com/
PHP | 1266 lines | 1174 code | 54 blank | 38 comment | 150 complexity | 5b6f45f4a21155b3a9a6f7af6f0704b3 MD5 | raw file
Possible License(s): LGPL-2.1

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. class shopimporter extends ImportModule
  3. {
  4. public $supportedImports = array();
  5. public function __construct()
  6. {
  7. global $cookie;
  8. $this->name = 'shopimporter';
  9. $this->tab = 'migration_tools';
  10. $this->version = '1.0';
  11. $this->author = 'PrestaShop';
  12. $this->need_instance = 0;
  13. parent::__construct ();
  14. $this->displayName = $this->l('Shop Importer');
  15. $this->description = $this->l('This module allows you to import your shop from another system into Prestashop.');
  16. $this->supportedImports = array(
  17. 'language' => array('methodName' => 'getLangagues',
  18. 'name' => $this->l('Language'),
  19. 'className' => 'Language',
  20. 'label' => $this->l('Import Languages'),
  21. 'table' => 'lang',
  22. 'identifier' => 'id_lang',
  23. 'alterTable' => array('id_lang' => 'int(10)'),
  24. 'info' => $this->l('New languages will automatically add translations.'),
  25. 'defaultId' => 'PS_LANG_DEFAULT'
  26. ),
  27. 'currency' => array('methodName' => 'getCurrencies',
  28. 'name' => $this->l('Currency'),
  29. 'className' => 'Currency',
  30. 'label' => $this->l('Import Currencies'),
  31. 'table' => 'currency',
  32. 'identifier' => 'id_currency',
  33. 'alterTable' => array('id_currency' => 'int(10)'),
  34. 'delete' => true,
  35. 'defaultId' => 'PS_CURRENCY_DEFAULT'
  36. ),
  37. /*
  38. 'tax' => array('methodName' => 'getTaxes',
  39. 'name' => $this->l('Taxes'),
  40. 'className' => 'Tax',
  41. 'label' => $this->l('Import Taxes'),
  42. 'table' => 'tax',
  43. 'identifier' => 'id_tax',
  44. 'alterTable' => array('id_tax' => 'int(10)'),
  45. 'delete' => true
  46. ),
  47. 'tax_rule' => array('methodName' => 'getTaxes rules',
  48. 'name' => $this->l('Taxes rules'),
  49. 'className' => 'TaxRule',
  50. 'label' => $this->l('Import Taxes Rules'),
  51. 'table' => 'tax_rules_group',
  52. 'identifier' => 'id_tax',
  53. 'alterTable' => array('id_tax_rules_group' => 'int(10)'),
  54. 'delete' => true
  55. ),
  56. */
  57. 'zone' => array('methodName' => 'getZones',
  58. 'name' => $this->l('Zone'),
  59. 'className' => 'Zone',
  60. 'label' => $this->l('Import Zones'),
  61. 'table' => 'zone',
  62. 'identifier' => 'id_zone',
  63. 'alterTable' => array('id_zone' => 'int(10)'),
  64. 'delete' => true
  65. ),
  66. 'country' => array('methodName' => 'getCountries',
  67. 'name' => $this->l('Country'),
  68. 'className' => 'Country',
  69. 'label' => $this->l('Import Countries'),
  70. 'table' => 'country',
  71. 'identifier' => 'id_country',
  72. 'foreign_key' => array('id_zone', 'id_currency'),
  73. 'alterTable' => array('id_country' => 'int(10)'),
  74. 'delete' => true,
  75. 'defaultId' => 'PS_COUNTRY_DEFAULT'
  76. ),
  77. 'state' => array('methodName' => 'getStates',
  78. 'name' => $this->l('State'),
  79. 'className' => 'State',
  80. 'label' => $this->l('Import States'),
  81. 'table' => 'state',
  82. 'identifier' => 'id_state',
  83. 'foreign_key' => array('id_zone', 'id_country'),
  84. 'alterTable' => array('id_state' => 'int(10)'),
  85. 'delete' => true
  86. ),
  87. 'group' => array('methodName' => 'getGroups',
  88. 'name' => $this->l('Group'),
  89. 'className' => 'Group',
  90. 'label' => $this->l('Import Groups'),
  91. 'table' => 'group',
  92. 'identifier' => 'id_group',
  93. 'alterTable' => array('id_group' => 'int(10)'),
  94. 'delete' => true,
  95. ),
  96. 'customer' => array('methodName' => 'getCustomers',
  97. 'name' => $this->l('Customer'),
  98. 'className' => 'Customer',
  99. 'label' => $this->l('Import Customers'),
  100. 'table' => 'customer',
  101. 'identifier' => 'id_customer',
  102. 'foreign_key' => array('id_group'),
  103. 'alterTable' => array('id_customer' => 'int(10)', 'passwd' => 'varchar(100)'),
  104. 'delete' => true,
  105. 'association' => array(
  106. array(
  107. 'table' => 'customer_group',
  108. 'fields' => array('id_customer', 'id_group'),
  109. 'matchTable' => array('customer', 'group'),
  110. )
  111. )
  112. ),
  113. 'address' => array('methodName' => 'getAddresses',
  114. 'name' => $this->l('Address'),
  115. 'className' => 'Address',
  116. 'label' => $this->l('Import Addresses'),
  117. 'table' => 'address',
  118. 'identifier' => 'id_address',
  119. 'foreign_key' => array('id_country', 'id_state', 'id_customer'),
  120. 'alterTable' => array('id_address' => 'int(10)'),
  121. 'delete' => true
  122. ),
  123. 'manufacturer' => array('methodName' => 'getManufacturers',
  124. 'name' => $this->l('Manufacturer'),
  125. 'className' => 'Manufacturer',
  126. 'label' => $this->l('Import Manufacturers'),
  127. 'table' => 'manufacturer',
  128. 'identifier' => 'id_manufacturer',
  129. 'delete' => true,
  130. 'alterTable' => array('id_manufacturer' => 'int(10)'),
  131. 'hasImage' => true
  132. ),
  133. 'supplier' => array('methodName' => 'getSuppliers',
  134. 'name' => $this->l('Supplier'),
  135. 'className' => 'Supplier',
  136. 'label' => $this->l('Import Suppliers'),
  137. 'table' => 'supplier',
  138. 'identifier' => 'id_supplier',
  139. 'delete' => true,
  140. 'alterTable' => array('id_supplier' => 'int(10)'),
  141. 'hasImage' => true
  142. ),
  143. 'category' => array('methodName' => 'getCategories',
  144. 'name' => $this->l('Category'),
  145. 'className' => 'Category',
  146. 'label' => $this->l('Import Categories'),
  147. 'table' => 'category',
  148. 'identifier' => 'id_category',
  149. 'alterTable' => array('id_category' => 'int(10)'),
  150. 'delete' => true,
  151. 'hasImage' => true,
  152. ),
  153. 'attributegroup' => array('methodName' => 'getAttributesGroups',
  154. 'name' => $this->l('AttributeGroup'),
  155. 'className' => 'AttributeGroup',
  156. 'label' => $this->l('Import Attributes Groups'),
  157. 'table' => 'attribute_group',
  158. 'identifier' => 'id_attribute_group',
  159. 'alterTable' => array('id_attribute_group' => 'int(10)'),
  160. 'delete' => true
  161. ),
  162. 'attribute' => array('methodName' => 'getAttributes',
  163. 'name' => $this->l('Attribute'),
  164. 'className' => 'Attribute',
  165. 'label' => $this->l('Import Attributes'),
  166. 'table' => 'attribute',
  167. 'identifier' => 'id_attribute',
  168. 'alterTable' => array('id_attribute' => 'int(10)'),
  169. 'foreign_key' => array('id_attribute_group'),
  170. 'delete' => true
  171. ),
  172. 'product' => array('methodName' => 'getProducts',
  173. 'name' => $this->l('Product'),
  174. 'className' => 'Product',
  175. 'label' => $this->l('Import Products'),
  176. 'table' => 'product',
  177. 'identifier' => 'id_product',
  178. 'alterTable' => array('id_product' => 'int(10)'),
  179. 'foreign_key' => array('id_category'),
  180. 'delete' => true,
  181. 'association' => array(
  182. array(
  183. 'table' => 'category_product',
  184. 'fields' => array('id_category', 'id_product'),
  185. 'matchTable' => array('category', 'product')
  186. )
  187. ),
  188. 'hasImage' => true
  189. ),
  190. 'combination' => array('methodName' => 'getProductsCombination',
  191. 'name' => $this->l('Combination'),
  192. 'className' => 'Combination',
  193. 'label' => $this->l('Import Products Combinations'),
  194. 'table' => 'product_attribute',
  195. 'identifier' => 'id_product_attribute',
  196. 'alterTable' => array('id_product_attribute' => 'int(10)', 'id_product' => 'int(10)'),
  197. 'foreign_key' => array('id_product'),
  198. 'delete' => false,
  199. 'association' => array(
  200. array(
  201. 'table' => 'product_attribute_combination',
  202. 'fields' => array('id_attribute', 'id_product_attribute'),
  203. 'matchTable' => array('attribute', 'product_attribute')
  204. )
  205. )
  206. ),
  207. 'orderstate' => array('methodName' => 'getOrdersStates',
  208. 'name' => $this->l('Orders States'),
  209. 'className' => 'OrderState',
  210. 'label' => $this->l('Import Orders States'),
  211. 'table' => 'order_state',
  212. 'identifier' => 'id_order_state',
  213. 'alterTable' => array('id_order_state' => 'int(10)')
  214. ),
  215. 'cart' => array('methodName' => 'getOrders',
  216. 'name' => $this->l('Order'),
  217. 'className' => 'Cart',
  218. 'label' => $this->l('Import Orders'),
  219. 'table' => 'cart',
  220. 'identifier' => 'id_cart',
  221. 'foreign_key' => array('id_address_delivery', 'id_address_invoice', 'id_customer'),
  222. 'alterTable' => array('id_cart' => 'int(10)')
  223. ),
  224. 'orderhistory' => array('methodName' => 'getOrdersHistory',
  225. 'name' => $this->l('Order history'),
  226. 'className' => 'Order history',
  227. 'label' => $this->l('Import Order History'),
  228. 'table' => 'order_history',
  229. 'identifier' => 'id_order_history',
  230. 'foreign_key' => array('id_order', 'id_order_state'),
  231. 'alterTable' => array('id_order_history' => 'int(10)'),
  232. 'hidden' => true
  233. ),
  234. 'order' => array('methodName' => 'getOrders',
  235. 'name' => $this->l('Order'),
  236. 'className' => 'Order',
  237. 'label' => $this->l('Import Order History'),
  238. 'table' => 'orders',
  239. 'identifier' => 'id_order',
  240. 'alterTable' => array('id_order' => 'int(10)'),
  241. 'hidden' => true
  242. )
  243. );
  244. }
  245. public function install()
  246. {
  247. return parent::install();
  248. }
  249. public function uninstall()
  250. {
  251. return parent::uninstall();
  252. }
  253. public function getContent()
  254. {
  255. global $cookie;
  256. $exportModules = parent::getImportModulesOnDisk();
  257. //get installed module only
  258. foreach($exportModules as $key => $module)
  259. if ($module->name == $this->name OR !(bool)$module->id)
  260. unset($exportModules[$key]);
  261. $html = '<script type="text/javascript">var globalAjaxShopImporterToken = "'.sha1(_COOKIE_KEY_.'ajaxShopImporter').'";</script>
  262. <script type="text/javascript" src="../modules/shopimporter/shopimporter.js"></script>
  263. <script src="'._PS_JS_DIR_.'jquery/jquery.scrollTo-1.4.2-min.js"></script>
  264. <script type="text/javascript">
  265. var conf = new Array(); ';
  266. $i = 0;
  267. foreach($this->supportedImports as $import)
  268. {
  269. if(!array_key_exists('hidden', $import))
  270. $html .= 'conf['.$i.'] = new Array(\''.addslashes($import['methodName']).'\', \''.addslashes($import['label']).'\', \''.addslashes($import['className']).'\', \''.addslashes($import['name']).'\');';
  271. $i++;
  272. }
  273. $html .= ' var notExist = "'.$this->l('is not available in this module').'";
  274. var databaseOk = "'.$this->l('Connection to the database OK').'";
  275. var showErrors = "'.$this->l('Show errors').'";
  276. var testImport = "'.$this->l('Test import process').'";
  277. var runImport = "'.$this->l('Run Import').'";
  278. var importHasErrors = "'.$this->l('Errors occurred during import. For more details click on "Show errors".').'"
  279. var importFinish = "'.$this->l('Import is complete.').'"
  280. var truncateTable = "'.$this->l('Remove data').'"
  281. var oneThing = "'.$this->l('Please choose one thing to import').'"
  282. </script>
  283. <style>
  284. .margin-form{padding: 0px 0px 1em 120px;width:300px;}
  285. label{width: 170px;}
  286. .import{background-color: #CCCCCC;border: 1px solid gray;margin: 0px 0px 10px;padding: 10px 15px;line-height: 20px;}
  287. </style>
  288. <fieldset><legend><img src="'.$this->_path.'logo.gif" alt="" />'.$this->l('Import from another system').'</legend>
  289. <div class="warn" ><img src="../img/admin/warn2.png">
  290. '.$this->l('Before starting the import please backup your database. ').'
  291. <a href="index.php?tab=AdminBackup&token='.Tools::getAdminToken('AdminBackup'.intval(Tab::getIdFromClassName('AdminBackup')).intval($cookie->id_employee)).'"">'.$this->l(' Click here to backup').'</a>
  292. </div>
  293. <br>
  294. <div style="float:right;width:450px" id="steps"></div>';
  295. if (sizeof($exportModules))
  296. {
  297. $html .= '
  298. <label>'.$this->l('Choose your import').' : </label>
  299. <div class="margin-form">
  300. <select name="import_module_name" id="import_module_name">
  301. <option value="0">---</option>';
  302. foreach($exportModules as $key => $module)
  303. (($module->name != $this->name AND $module->id) ? $html .= '<option value="'.$module->name.'">'.$module->displayName.'</option>' : '' );
  304. $html .= '</select><input type="submit" class="button" id="choose_module_name" value="'.$this->l('Choose').'">
  305. </div>';
  306. }
  307. else
  308. $html .= '<div class="warn" ><img src="../img/admin/warn2.png">'.$this->l('No import module installed').'</div>';
  309. $html .= '
  310. <div id="db_config" style="display:none;width:420px;padding-right:20px">
  311. <div id="db_input">
  312. <label>'.$this->l('Server').' : </label>
  313. <div class="margin-form">
  314. <input type="text" name="server" id="server" value="">
  315. </div>
  316. <label>'.$this->l('User').' : </label>
  317. <div class="margin-form">
  318. <input type="text" name="user" id="user" value="">
  319. </div>
  320. <label>'.$this->l('Password').' : </label>
  321. <div class="margin-form">
  322. <input type="password" name="password" id="password" value="">
  323. </div>
  324. <label>'.$this->l('Database').' : </label>
  325. <div class="margin-form" style="">
  326. <input type="text" name="database" id="database" value="">
  327. </div>
  328. <label>'.$this->l('Database prefix').' : </label>
  329. <div class="margin-form" style="">
  330. <input type="text" name="prefix" id="prefix" value="">
  331. </div>
  332. </div>
  333. <div class="margin-form">
  334. <input type="submit" name="displayOptions" id="displayOptions" class="button" value="'.$this->l('Next Step').'">
  335. </div>
  336. <hr>
  337. <div style="display:none" id="importOptions">
  338. <h2>'.$this->l('Import Options').'</h2>
  339. <div id="importOptionsYesNo">';
  340. foreach($this->supportedImports as $key => $import)
  341. {
  342. if(!array_key_exists('hidden', $import))
  343. $html .= '<label>'.$import['name'].' : </label>
  344. <div class="margin-form">
  345. <label class="t" for="'.$import['identifier'].'_on'.'"><img src="../img/admin/enabled.gif" alt="Yes" title="Yes"></label>
  346. <input type="radio" id="'.$import['identifier'].'_on'.'" name="'.$import['methodName'].'" class="'.$key.'" value="1" checked="checked">
  347. <label class="t" for="'.$import['identifier'].'_off'.'"><img src="../img/admin/disabled.gif" alt="No" title="No" style="margin-left: 10px;"></label>
  348. <input type="radio" id="'.$import['identifier'].'_off'.'" name="'.$import['methodName'].'" class="'.$key.'" value="0">
  349. '.(array_key_exists('delete', $import) ? '
  350. <label class="t"><img src="../img/admin/delete.gif" alt="Delete" title="Delete"></label>
  351. <input type="checkbox" class="truncateTable" id="'.$key.'" name="delete_'.$import['className'].'">' : '' ).
  352. (array_key_exists('hasImage', $import) ? '
  353. <label class="t"><img src="../img/admin/picture.gif" alt="Images" title="Images"></label>
  354. <input type="checkbox" class="importImages" id="'.$key.'" name="images_'.$import['className'].'">' : '' ).
  355. (array_key_exists('info', $import) ? '<p>'.$import['info'].'</p>' : '').'
  356. </div>';
  357. }
  358. $html .= '</div><hr>
  359. <h2>'.$this->l('Advanced Options').'</h2>
  360. <div class="warn" id="warnSkip" style="display:none"><img src="../img/admin/warn2.png">
  361. '.$this->l('This mode is dangerous').'
  362. </div>
  363. <label>'.$this->l('Import for every').' : </label>
  364. <div class="margin-form">
  365. <select name="nbr_import" id="nbr_import">
  366. <option value="10">10</option>
  367. <option value="50">50</option>
  368. <option value="100" selected="selected">100</option>
  369. <option value="200">200</option>
  370. </select>
  371. <p>'.$this->l('Select the number of occurrences for each query to import').'</p>
  372. </div>
  373. <label>'.$this->l('If errors occur').' : </label>
  374. <div class="margin-form">
  375. <label class="t"><img src="'.$this->_path.'img/stop.png"></label>
  376. <input type="radio" name="hasErrors" id="hasErrors" value="0" checked="checked">
  377. <label class="t">'.$this->l('Stop').'</label>
  378. <label class="t"><img src="'.$this->_path.'img/skip.png" style="margin-left: 10px;"></label>
  379. <input type="radio" name="hasErrors" id="hasErrors" value="1">
  380. <label class="t">'.$this->l('Skip').'</label>
  381. <label class="t"><img src="'.$this->_path.'img/force.gif" style="margin-left: 10px;"></label>
  382. <input type="radio" name="hasErrors" id="hasErrors" value="2">
  383. <label class="t">'.$this->l('Force').'</label>
  384. <p>'.$this->l('Stop: if there are errors with the data, import will not run.').'</p>
  385. <p>'.$this->l('Skip: if there are errors with the data, import will skip incorrect data.').'</p>
  386. <p>'.$this->l('Force: if there are errors with the data, import will replace incorrect data by generic data.').'</p>
  387. </div>
  388. <hr>
  389. <div style="display:none" id="specificOptions">
  390. <h2>'.$this->l('Specific Options').'</h2>
  391. <div style="display:none" class="error" id="specificOptionsErrors"></div>
  392. <div id="specificOptionsContent">
  393. </div>
  394. </div>
  395. <hr>
  396. <div class="margin-form">
  397. <input type="submit" class="button" name="checkAndSaveConfig" id="checkAndSaveConfig" value="'.$this->l('Next Step').'">
  398. </div>
  399. </div>
  400. </div>
  401. </fieldset>';
  402. return $html;
  403. }
  404. public function generiqueImport($className, $fields, $save = false)
  405. {
  406. $return = '';
  407. $json = array();
  408. $errors = array();
  409. $json['hasError'] = false;
  410. $json['datas'] = array_values($fields);
  411. $languages = array();
  412. $defaultLanguage = '';
  413. $table = $this->supportedImports[strtolower($className)]['table'];
  414. $object = new $className();
  415. $rules = call_user_func(array($className, 'getValidationRules'), $className);
  416. if ((sizeof($rules['requiredLang']) OR sizeof($rules['sizeLang']) OR sizeof($rules['validateLang']) OR Tools::isSubmit('syncLang') OR Tools::isSubmit('syncCurrency')))
  417. {
  418. $moduleName = Tools::getValue('moduleName');
  419. if (file_exists('../../modules/'.$moduleName.'/'.$moduleName.'.php'))
  420. {
  421. require_once('../../modules/'.$moduleName.'/'.$moduleName.'.php');
  422. $importModule = new $moduleName();
  423. $importModule->server = Tools::getValue('server');
  424. $importModule->user = Tools::getValue('user');
  425. $importModule->passwd = Tools::getValue('password');
  426. $importModule->database = Tools::getValue('database');
  427. $importModule->prefix = Tools::getValue('prefix');
  428. $defaultLanguage = new Language((int)Configuration::get('PS_LANG_DEFAULT'));
  429. $languages = $importModule->getLangagues(0);
  430. if (Tools::isSubmit('syncLang'))
  431. {
  432. $defaultIdLand = $importModule->getDefaultIdLang();
  433. $defaultLanguageImport = new Language(Language::getIdByIso($languages[$defaultIdLand]['iso_code']));
  434. if ($defaultLanguage->iso_code != $defaultLanguageImport->iso_code)
  435. $errors[] = $this->l('Default language doesn\'t match : ').'<br>'.Configuration::get('PS_SHOP_NAME').' : '.$defaultLanguage->name.' â&#x2030; 
  436. '.$importModule->displayName.' : '.$defaultLanguageImport->name.'<br>'.$this->l('Please change default language in your configuration');
  437. }
  438. if (Tools::isSubmit('syncCurrency'))
  439. {
  440. $defaultIdCurrency = $importModule->getDefaultIdCurrency();
  441. $currencies = $importModule->getCurrencies(0);
  442. if(!empty($currencies[$defaultIdCurrency]['iso_code']))
  443. $defaultCurrencyImport = new Currency((int)Currency::getIdByIsoCode($currencies[$defaultIdCurrency]['iso_code']));
  444. else
  445. $defaultCurrencyImport = new Currency((int)Currency::getIdByIsoCodeNum($currencies[$defaultIdCurrency]['iso_code_num']));
  446. $defaultCurrency = new Currency((int)Configuration::get('PS_CURRENCY_DEFAULT'));
  447. if ($defaultCurrency->iso_code != $defaultCurrencyImport->iso_code)
  448. $errors[] = $this->l('Default currency doesn\'t match : ').'<br>'.Configuration::get('PS_SHOP_NAME').' : '.$defaultCurrency->name.' â&#x2030;  '.$importModule->displayName.' : '.$defaultCurrencyImport->name.'<br>'.$this->l('Please change default currency in your configuration');
  449. }
  450. if (!empty($errors))
  451. die('{"hasError" : true, "error" : '.Tools::jsonEncode($errors).'}');
  452. }
  453. else
  454. die('{"hasError" : true, "error" : ["FATAL ERROR"], "datas" : []}');
  455. }
  456. foreach($fields as $key => $field)
  457. {
  458. $id = $this->supportedImports[strtolower($className)]['identifier'];
  459. //remove wrong fields (ex : id_toto in Customer)
  460. foreach($field as $name => $value)
  461. if (!array_key_exists($name, get_object_vars($object)) AND ($name != $id) AND ($name != 'association') AND ($name != 'images') AND (strtolower($className) != 'cart'))
  462. unset($field[$name]);
  463. $return = $this->validateRules($rules, $field, $className, $languages, $defaultLanguage);
  464. $fields[$key] = $field;
  465. if (!empty($return))
  466. {
  467. //skip mode
  468. if (Tools::getValue('hasErrors') == 1)
  469. unset($fields[$key]);
  470. $errors[] = $return;
  471. array_unshift($errors[sizeof($errors)-1], $field[$id]);
  472. }
  473. }
  474. if (sizeof($errors) > 0)
  475. {
  476. $json['hasError'] = true;
  477. $json['error'] = $errors;
  478. }
  479. if ($save OR Tools::isSubmit('syncLang'))
  480. {
  481. //add language if not exist in prestashop
  482. if ($className == 'Language')
  483. {
  484. if (Tools::isSubmit('syncLang'))
  485. $add = true;
  486. else
  487. $add = false;
  488. $errors = $this->checkAndAddLang($fields, $add);
  489. }
  490. elseif ($className == 'Cart')
  491. {
  492. $this->saveOrders($fields);
  493. }
  494. else
  495. {
  496. $return = $this->saveObject($className, $fields);
  497. $this->cleanPositions($table);
  498. //insert association
  499. if (array_key_exists('association', $this->supportedImports[strtolower($className)]))
  500. $this->insertAssociation(strtolower($className), $fields);
  501. if (!empty($return))
  502. {
  503. $json['hasError'] = true;
  504. $json['error'] = $return;
  505. }
  506. }
  507. if ($className == 'Category' AND (sizeof($fields) != (int)Tools::getValue('nbr_import')))
  508. $this->updateCat();
  509. }
  510. if (sizeof($errors) > 0 AND is_array($errors))
  511. {
  512. $json['hasError'] = true;
  513. $json['error'] = $errors;
  514. }
  515. die(Tools::jsonEncode($json));
  516. }
  517. private function saveObject($className, $items)
  518. {
  519. $return = array();
  520. $table = $this->supportedImports[strtolower($className)]['table'];
  521. //creating temporary fields for identifiers matching and password
  522. if (array_key_exists('alterTable', $this->supportedImports[strtolower($className)]))
  523. $this->alterTable(strtolower($className));
  524. foreach($items as $item)
  525. {
  526. $object = new $className;
  527. $id = $item[$this->supportedImports[strtolower($className)]['identifier']];
  528. if (array_key_exists('foreign_key', $this->supportedImports[strtolower($className)]))
  529. $this->replaceForeignKey($item, $table);
  530. $matchIdLang = $this->getMatchIdLang(1);
  531. foreach($item as $key => $val)
  532. {
  533. if ($key == 'passwd')
  534. {
  535. $password = $val;
  536. $val = substr($val,0,29);
  537. }
  538. if (is_array($val) AND $key != 'images')
  539. {
  540. foreach($matchIdLang as $k => $v)
  541. if ($k != $v)
  542. {
  543. $item[$key][$v] = $val[$k];
  544. unset($item[$key][$k]);
  545. }
  546. $object->$key = $item[$key];
  547. }
  548. else
  549. $object->$key = $val;
  550. }
  551. if (!$object->save(false, false))
  552. $return[] = array($item[$this->supportedImports[strtolower($className)]['identifier']], $this->l('An error occurred when adding the object'));
  553. else
  554. {
  555. $this->saveMatchId(strtolower($className), (int)$object->id, (int)$id);
  556. if ($className == 'Customer')
  557. Db::getInstance()->Execute('UPDATE '._DB_PREFIX_.'customer SET passwd_'.Tools::getValue('moduleName').' = \''.$password.'\' WHERE id_customer = '.(int)$object->id);
  558. if (array_key_exists('hasImage', $this->supportedImports[strtolower($className)]) AND Tools::isSubmit('images_'.$className))
  559. $this->copyImg($item, $className);
  560. }
  561. }
  562. return $return;
  563. }
  564. private function saveOrders($items)
  565. {
  566. $this->saveObject('cart', $items);
  567. //import cart product
  568. $foreignKey = $this->getForeignKey('Cart', array('id_cart', 'id_product', 'id_product_attribute', 'id_customer','id_address_invoice', 'id_address_delivery'));
  569. foreach($items as &$item)
  570. {
  571. foreach($item['cart_products'] as $k => $cart_products)
  572. {
  573. foreach($cart_products as $key => $val)
  574. if (array_key_exists($key, $foreignKey))
  575. if (array_key_exists($val, $foreignKey[$key]))
  576. $item['cart_products'][$k][$key] = $foreignKey[$key][$val];
  577. else
  578. $item['cart_products'][$k][$key] = 0;
  579. Db::getInstance()->autoExecute(_DB_PREFIX_.'cart_product', $item['cart_products'][$k],'INSERT');
  580. }
  581. foreach($item['order_products'] as $k => $order_products)
  582. {
  583. foreach($order_products as $key => $val)
  584. if (array_key_exists($key, $foreignKey))
  585. if (array_key_exists($val, $foreignKey[$key]))
  586. $item['order_products'][$k][$key] = $foreignKey[$key][$val];
  587. else
  588. $item['order_products'][$k][$key] = 0;
  589. }
  590. }
  591. //cart to order
  592. $this->cartToOrder($items, $foreignKey);
  593. $foreignKey = array_merge($this->getForeignKey('Cart', array('id_order', 'id_order_state')), $foreignKey);
  594. foreach($items as &$item)
  595. {
  596. //insert product in order detail
  597. foreach($item['order_products'] as $k => $order_products)
  598. {
  599. foreach($order_products as $key => $val)
  600. if (array_key_exists($key, $foreignKey))
  601. if (array_key_exists($val, $foreignKey[$key]))
  602. $item['order_products'][$k][$key] = $foreignKey[$key][$val];
  603. else
  604. $item['order_products'][$k][$key] = 0;
  605. Db::getInstance()->autoExecute(_DB_PREFIX_.'order_detail', $item['order_products'][$k],'INSERT');
  606. }
  607. //save order history
  608. foreach($item['order_history'] as $k => $order_history)
  609. {
  610. foreach($order_history as $key => $val)
  611. if (array_key_exists($key, $foreignKey))
  612. if (array_key_exists($val, $foreignKey[$key]))
  613. $item['order_history'][$k][$key] = $foreignKey[$key][$val];
  614. else
  615. $item['order_history'][$k][$key] = 0;
  616. Db::getInstance()->autoExecute(_DB_PREFIX_.'order_history', $item['order_history'][$k],'INSERT');
  617. }
  618. }
  619. }
  620. private function insertAssociation($table, $items)
  621. {
  622. foreach($this->supportedImports[$table]['association'] AS $association)
  623. {
  624. $associatFields = '';
  625. $associatFieldsName = implode('`, `', $association['fields']);
  626. $tableAssociation = $association['table'];
  627. $matchTable = $association['matchTable'];
  628. if (!empty($items))
  629. {
  630. $match = array();
  631. foreach($matchTable as $mTable)
  632. {
  633. $tmp = $this->getForeignKey($mTable, array('id_'.$mTable));
  634. if (array_key_exists('id_'.$mTable, $tmp))
  635. $match['id_'.$mTable] = $tmp['id_'.$mTable];
  636. else
  637. $match['id_'.$mTable] = $this->getDefaultId($table);
  638. }
  639. foreach($items AS $item)
  640. foreach($item AS $key => $val)
  641. if ($key == 'association' AND !empty($key))
  642. foreach($val[$tableAssociation] AS $k => $v)
  643. {
  644. $associatFields .= ' ('.((array_key_exists($k, $match[$association['fields'][0]])) ? (int)$match[$association['fields'][0]][$k] : '1').', ';
  645. $associatFields .= (int)$match[$association['fields'][1]][$v].'), ';
  646. }
  647. if ($associatFields != '')
  648. Db::getInstance()->Execute('INSERT INTO `'._DB_PREFIX_.pSQL($tableAssociation).'` (`'.$associatFieldsName.'`) VALUES '.rtrim($associatFields, ', '));
  649. }
  650. }
  651. }
  652. private function saveMatchId($className, $psId, $matchId)
  653. {
  654. $table = $this->supportedImports[$className]['table'];
  655. $moduleName = Tools::getValue('moduleName');
  656. $identifier = $this->supportedImports[$className]['identifier'];
  657. Db::getInstance()->Execute('UPDATE '._DB_PREFIX_.pSQL($table).' SET `'.pSQL($identifier).'_'.pSQL($moduleName).'` = '.(int)$matchId.' WHERE `'.pSQL($identifier).'` = '.(int)$psId);
  658. }
  659. private function getMatchId($className)
  660. {
  661. $table = $this->supportedImports[$className]['table'];
  662. $moduleName = Tools::getValue('moduleName');
  663. $identifier = $this->supportedImports[$className]['identifier'];
  664. $returns = Db::getInstance()->ExecuteS('SELECT `'.pSQL($identifier).'_'.pSQL($moduleName).'`, `'.pSQL($identifier).'` FROM '._DB_PREFIX_.pSQL($table).' WHERE `'.pSQL($identifier).'_'.pSQL($moduleName).'` != 0 ');
  665. $match = array();
  666. foreach($returns as $return)
  667. $match[$return[$identifier.'_'.$moduleName]] = $return[$identifier];
  668. return $match;
  669. }
  670. private function getDefaultId($table)
  671. {
  672. $defaultId = 1;
  673. if (array_key_exists('defaultId', $this->supportedImports[strtolower($table)]))
  674. $defaultId = Configuration::get($this->supportedImports[strtolower($table)]['defaultId']);
  675. return $defaultId;
  676. }
  677. private function copyImg($item, $className)
  678. {
  679. require_once('../../images.inc.php');
  680. $identifier = $this->supportedImports[strtolower($className)]['identifier'];
  681. $matchId = $this->getMatchId(strtolower($className));
  682. $matchIdLang = $this->getMatchIdLang();
  683. switch($className)
  684. {
  685. default:
  686. case 'Product':
  687. $path = _PS_PROD_IMG_DIR_;
  688. $type = 'products';
  689. break;
  690. case 'Category':
  691. $path = _PS_CAT_IMG_DIR_;
  692. $type = 'categories';
  693. break;
  694. case 'Manufacturer':
  695. $path = _PS_MANU_IMG_DIR_;
  696. $type = 'manufacturers';
  697. break;
  698. case 'Supplier':
  699. $path = _PS_SUPP_IMG_DIR_;
  700. $type = 'suppliers';
  701. break;
  702. }
  703. $cover = 1;
  704. if (array_key_exists($item[$identifier], $matchId))
  705. foreach($item['images'] as $key => $image)
  706. {
  707. $tmpfile = tempnam(_PS_TMP_IMG_DIR_, 'import');
  708. if (@copy($image, $tmpfile))
  709. {
  710. $imagesTypes = ImageType::getImagesTypes($type);
  711. imageResize($tmpfile, $path.(int)$matchId[$item[$identifier]].'.jpg');
  712. if ($className == 'Product')
  713. {
  714. $image = new Image();
  715. $image->id_product = (int)($matchId[$item[$identifier]]);
  716. $image->cover = $cover;
  717. $image->position = Image::getHighestPosition((int)$matchId[$item[$identifier]]) + 1;
  718. $legend = array();
  719. foreach($item['name'] as $key => $val)
  720. if (array_key_exists($key, $matchIdLang))
  721. $legend[$matchIdLang[$key]] = Tools::link_rewrite($val);
  722. else
  723. $legend[Configuration::get('PS_LANG_DEFAULT')] = Tools::link_rewrite($val);
  724. $image->legend = $legend;
  725. $image->add();
  726. imageResize($tmpfile, $path.(int)$matchId[$item[$identifier]].'-'.(int)$image->id.'.jpg');
  727. foreach ($imagesTypes AS $k => $imageType)
  728. imageResize($tmpfile, $path.(int)$matchId[$item[$identifier]].'-'.(int)$image->id.'-'.stripslashes($imageType['name']).'.jpg', $imageType['width'], $imageType['height']);
  729. }
  730. else
  731. foreach ($imagesTypes AS $k => $imageType)
  732. imageResize($tmpfile, $path.(int)$matchId[$item[$identifier]].'-'.stripslashes($imageType['name']).'.jpg', $imageType['width'], $imageType['height']);
  733. }
  734. else
  735. @unlink($tmpfile);
  736. @unlink($tmpfile);
  737. $cover = 0;
  738. }
  739. }
  740. private function replaceForeignKey(&$item, $table)
  741. {
  742. if ($table == 'product_attribute')
  743. $table2 = 'combination';
  744. else
  745. $table2 = $table;
  746. $foreingKey = $this->supportedImports[$table2]['foreign_key'];
  747. $foreingKeyValue = $this->getForeignKey($table, $foreingKey);
  748. foreach($foreingKey as $key)
  749. {
  750. if ($table == 'product' AND $key == 'id_category')
  751. $key2 = 'id_category_default';
  752. else if ($table == 'customer' AND $key == 'id_group')
  753. $key2 = 'id_default_group';
  754. else
  755. $key2 = $key;
  756. if (array_key_exists($key, $foreingKeyValue) && $item[$key2] != 0)
  757. $item[$key2] = (array_key_exists($item[$key2], $foreingKeyValue[$key]) ? $item[$key2] = $foreingKeyValue[$key][$item[$key2]] : $item[$key2] = 0);
  758. else
  759. $item[$key2] = $this->getDefaultId($table);
  760. }
  761. }
  762. private function alterTable($className)
  763. {
  764. $query ='';
  765. $queryTmp = '';
  766. $from = $this->supportedImports[$className]['table'];
  767. $result = array();
  768. $result = Db::getInstance()->getRow('SELECT * FROM `'._DB_PREFIX_.pSQL($from).'`');
  769. if (!$result)
  770. $result = array();
  771. foreach ($this->supportedImports[$className]['alterTable'] AS $name => $type)
  772. {
  773. $moduleName = Tools::getValue('moduleName');
  774. if (!array_key_exists($name.'_'.$moduleName, $result))
  775. $queryTmp .= ' ADD `'.$name.'_'.$moduleName.'` '.$type.' NOT NULL,';
  776. }
  777. if (!empty($queryTmp))
  778. {
  779. $query = 'ALTER TABLE `'._DB_PREFIX_.pSQL($from).'` ';
  780. $query .= rtrim($queryTmp, ',');
  781. Db::getInstance()->Execute($query);
  782. }
  783. }
  784. private function updateCat()
  785. {
  786. $moduleName = Tools::getValue('moduleName');
  787. Db::getInstance()->Execute('UPDATE
  788. '._DB_PREFIX_.'category c
  789. INNER JOIN
  790. '._DB_PREFIX_.'category c2
  791. ON
  792. c.id_parent = c2.id_category_'.pSQL($moduleName).'
  793. SET
  794. c.id_parent = c2.id_category
  795. WHERE c.id_category_'.pSQL($moduleName).' != 0');
  796. $category = new Category();
  797. $cats = $category->getSimpleCategories((int)Configuration::get('PS_LANG_DEFAULT'));
  798. foreach($cats as $cat)
  799. {
  800. $cat = new Category((int)$cat['id_category']);
  801. $cat->level_depth = $cat->calcLevelDepth();
  802. $cat->update();
  803. }
  804. }
  805. private function getForeignKey($className, $foreign_key = null)
  806. {
  807. $moduleName = Tools::getValue('moduleName');
  808. if (is_null($foreign_key))
  809. $foreign_key = $this->supportedImports[$className]['foreign_key'];
  810. $match = array();
  811. foreach($foreign_key AS $key)
  812. {
  813. if (in_array($key , array('id_address_invoice', 'id_address_delivery')))
  814. $key2 = 'id_address';
  815. elseif (in_array($key , array('id_category_default')))
  816. $key2 = 'id_category';
  817. else
  818. $key2 = $key;
  819. foreach($this->supportedImports AS $table => $conf)
  820. if ($conf['identifier'] == $key2)
  821. $from = $this->supportedImports[$table]['table'];
  822. $return = Db::getInstance()->ExecuteS('SELECT `'.pSQL($key2).'_'.pSQL($moduleName).'`, `'.pSQL($key2).'` FROM `'._DB_PREFIX_.pSQL($from).'` WHERE `'.pSQL($key2).'_'.pSQL($moduleName).'` != 0');
  823. if (!empty($return))
  824. foreach($return AS $name => $val)
  825. $match[$key][$val[$key2.'_'.$moduleName]] = $val[$key2];
  826. }
  827. return $match;
  828. }
  829. private function getForeignKeyLang($table)
  830. {
  831. $id = $this->supportedImports[$table]['identifier'];
  832. $moduleName = Tools::getValue('moduleName');
  833. $return = Db::getInstance()->ExecuteS('SELECT `'.pSQL($id).'_'.pSQL($moduleName).'`, `'.pSQL($id).'` FROM `'._DB_PREFIX_.pSQL($table).'` WHERE `'.pSQL($id).'_'.pSQL($moduleName).'` != 0');
  834. $match = array();
  835. foreach($return AS $name => $val)
  836. $match[$val[$id.'_'.$moduleName]] = $val[$id];
  837. return $match;
  838. }
  839. private function getMatchIdLang($order = 1)
  840. {
  841. $moduleName = Tools::getValue('moduleName');
  842. $return = Db::getInstance()->ExecuteS('SELECT `id_lang`, `id_lang_'.pSQL($moduleName).'` FROM `'._DB_PREFIX_.'lang'.'` WHERE `id_lang_'.pSQL($moduleName).'` != 0');
  843. $match = array();
  844. foreach($return AS $name => $val)
  845. if ((bool)$order)
  846. $match[$val['id_lang_'.$moduleName]] = $val['id_lang'];
  847. else
  848. $match[$val['id_lang']] = $val['id_lang_'.$moduleName];
  849. return $match;
  850. }
  851. private function validateRules($rules, &$fields, $className, $languages, $defaultLanguage)
  852. {
  853. $returnErrors = array();
  854. $hasErrors = Tools::getValue('hasErrors');
  855. /* Checking for required fields */
  856. foreach ($rules['required'] AS $field)
  857. if (($value = $fields[$field]) == false AND (string)$value != '0')
  858. if ($hasErrors == 2)
  859. {
  860. if (array_key_exists($field, $rules['size']))
  861. $size = $rules['size'][$field];
  862. else
  863. $size = 1;
  864. $fields[$field] = $this->generateData($size, $rules['validate'][$field]);
  865. }
  866. else
  867. $returnErrors[] = $this->l('the field').' <b>'.call_user_func(array($className, 'displayFieldName'), $field, $className).'</b> '.$this->l('is required');
  868. /* Checking for maximum fields sizes */
  869. foreach ($rules['size'] AS $field => $maxLength)
  870. if (array_key_exists($field, $fields) AND $field != 'passwd')
  871. if ($fields[$field] !== false AND Tools::strlen($fields[$field]) > $maxLength)
  872. if ($hasErrors == 2)
  873. $fields[$field] = substr($fields[$field], 0, $maxLength);
  874. else
  875. $returnErrors[] = $this->l('the field').' <b>'.call_user_func(array($className, 'displayFieldName'), $field, $className).'</b>'.$this->l('is too long').' ('.$maxLength.' '.$this->l('chars max').')';
  876. /* Checking for fields validity */
  877. foreach ($rules['validate'] AS $field => $function)
  878. if (array_key_exists($field, $fields))
  879. if (($value = $fields[$field]) !== false AND ($field != 'passwd'))
  880. if (!Validate::$function($value))
  881. if ($hasErrors == 2)
  882. {
  883. if (array_key_exists($field, $rules['size']))
  884. $size = $rules['size'][$field];
  885. else
  886. $size = 1;
  887. $fields[$field] = $this->generateData($size, $rules['validate'][$field]);
  888. }
  889. else
  890. $returnErrors[] = $this->l('the field').' <b>'.call_user_func(array($className, 'displayFieldName'), $field, $className).'</b> '.$this->l('is invalid');
  891. if ((sizeof($rules['requiredLang']) OR sizeof($rules['sizeLang']) OR sizeof($rules['validateLang'])))
  892. {
  893. $matchIdLang = $this->getMatchIdLang(0);
  894. /* Checking for multilingual required fields */
  895. foreach ($rules['requiredLang'] AS $fieldLang)
  896. {
  897. if (($empty = $fields[$fieldLang][$matchIdLang[$defaultLanguage->id]]) === false OR empty($empty))
  898. if ($hasErrors == 2)
  899. {
  900. if (array_key_exists($fieldLang, $rules['sizeLang']))
  901. $size = $rules['sizeLang'][$fieldLang];
  902. else
  903. $size = 1;
  904. $fields[$fieldLang][$matchIdLang[$defaultLanguage->id]] = $this->generateData($size, $rules['validateLang'][$fieldLang]);
  905. }
  906. else
  907. $returnErrors[] = $this->l('the field').' <b>'.call_user_func(array($className, 'displayFieldName'), $fieldLang, $className).'</b> '.$this->l('is required at least in').' '.$defaultLanguage->name;
  908. }
  909. /* Checking for maximum multilingual fields size */
  910. foreach ($rules['sizeLang'] AS $fieldLang => $maxLength)
  911. foreach ($languages AS $language)
  912. if (isset($fields[$fieldLang][$language['id_lang']]) && $fields[$fieldLang] !== false AND Tools::strlen($fields[$fieldLang][$language['id_lang']]) > $maxLength)
  913. if ($hasErrors == 2)
  914. $fields[$fieldLang] = substr($fields[$fieldLang], 0, $maxLength);
  915. else
  916. $returnErrors[] = $this->l('the field').' <b>'.call_user_func(array($className, 'displayFieldName'), $fieldLang, $className).' ('.$language['name'].')</b> '.$this->l('is too long').' ('.$maxLength.' '.$this->l('chars max').')';
  917. /* Checking for multilingual fields validity */
  918. foreach ($rules['validateLang'] AS $fieldLang => $function)
  919. foreach ($languages AS $language)
  920. {
  921. if (array_key_exists($fieldLang, $fields) AND ($value = $fields[$fieldLang][$language['id_lang']]) !== false AND !empty($value))
  922. {
  923. if (!Validate::$function($value))
  924. if ($hasErrors == 2)
  925. {
  926. if (array_key_exists($fieldLang, $rules['sizeLang']))
  927. $size = $rules['sizeLang'][$fieldLang];
  928. else
  929. $size = 1;
  930. $fields[$fieldLang][$language['id_lang']] = $this->generateData($size, $rules['validateLang'][$fieldLang]);
  931. }
  932. else
  933. $returnErrors[] = $this->l('the field').' <b>'.call_user_func(array($className, 'displayFieldName'), $fieldLang, $className).' ('.$language['name'].')</b> '.$this->l('is invalid');
  934. }
  935. }
  936. }
  937. return $returnErrors;
  938. }
  939. public function checkAndAddLang ($languages, $add = true)
  940. {
  941. $errors = '';
  942. $moduleName = Tools::getValue('moduleName');
  943. $this->alterTable('language');
  944. foreach($languages as $language)
  945. {
  946. $iso = $language['iso_code'];
  947. if (!Language::isInstalled($iso))
  948. {
  949. if ($add)
  950. {
  951. if (@fsockopen('www.prestashop.com', 80))
  952. {
  953. if ($lang_pack = Tools::jsonDecode(Tools::file_get_contents('http://www.prestashop.com/download/lang_packs/get_language_pack.php?version='._PS_VERSION_.'&iso_lang='.$iso)))
  954. {
  955. if ($content = Tools::file_get_contents('http://www.prestashop.com/download/lang_packs/gzip/'.$lang_pack->version.'/'.$iso.'.gzip'))
  956. {
  957. $file = _PS_TRANSLATIONS_DIR_.$iso.'.gzip';
  958. if (file_put_contents($file, $content))
  959. {
  960. require_once('../../tools/tar/Archive_Tar.php');
  961. $gz = new Archive_Tar($file, true);
  962. if ($gz->extract(_PS_TRANSLATIONS_DIR_.'../', false))
  963. {
  964. if (!Language::checkAndAddLanguage($iso))
  965. $errors[] = Tools::displayError('Archive cannot be extracted.');
  966. else
  967. {
  968. $newId = Language::getIdByIso($iso);
  969. Db::getInstance()->Execute('UPDATE `'._DB_PREFIX_.'lang`
  970. SET `id_lang_'.pSQL($moduleName).'` = '.(int)$language['id_lang'].'
  971. WHERE `id_lang` = '.(int)$newId);
  972. }
  973. }
  974. $errors[] = Tools::displayError('Archive cannot be extracted.');
  975. }
  976. else
  977. $errors[] = Tools::displayError('Server does not have permissions for writing.');
  978. }
  979. else
  980. $errors[] = Tools::displayError('Language not found');
  981. }
  982. else
  983. $errors[] = Tools::displayError('archive cannot be downloaded from prestashop.com.');
  984. }
  985. else
  986. $errors[] = Tools::displayError('archive cannot be downloaded from prestashop.com.');
  987. }
  988. }
  989. else
  990. {
  991. $newId = Language::getIdByIso($iso);
  992. Db::getInstance()->Execute('UPDATE `'._DB_PREFIX_.'lang`
  993. SET `id_lang_'.pSQL($moduleName).'` = '.(int)$language['id_lang'].'
  994. WHERE `id_lang` = '.(int)$newId);
  995. }
  996. }
  997. }
  998. public function truncateTable($table)
  999. {
  1000. switch ($table)
  1001. {
  1002. case 'customer' :
  1003. Db::getInstance()->Execute('TRUNCATE TABLE `'._DB_PREFIX_.'customer');
  1004. Db::getInstance()->Execute('TRUNCATE TABLE `'._DB_PREFIX_.'customer_group');
  1005. break;
  1006. case 'address' :
  1007. Db::getInstance()->Execute('TRUNCATE TABLE `'._DB_PREFIX_.'address');
  1008. break;
  1009. case 'category' :
  1010. Db::getInstance()->Execute('DELETE FROM `'._DB_PREFIX_.'category` WHERE id_category != 1');
  1011. Db::getInstance()->Execute('DELETE FROM `'._DB_PREFIX_.'category_lang` WHERE id_category != 1');
  1012. Db::getInstance()->Execute('ALTER TABLE `'._DB_PREFIX_.'category` AUTO_INCREMENT = 2 ');
  1013. foreach (scandir(_PS_CAT_IMG_DIR_) AS $d)
  1014. if (preg_match('/^[0-9]+(\-(.*))?\.jpg$/', $d))
  1015. unlink(_PS_CAT_IMG_DIR_.$d);
  1016. Image::clearTmpDir();
  1017. break;
  1018. case 'product' :
  1019. Db::getInstance()->Execute('TRUNCATE TABLE `'._DB_PREFIX_.'product');
  1020. Db::getInstance()->Execute('TRUNCATE TABLE `'._DB_PREFIX_.'feature_product');
  1021. Db::getInstance()->Execute('TRUNCATE TABLE `'._DB_PREFIX_.'product_lang');
  1022. Db::getInstance()->Execute('TRUNCATE TABLE `'._DB_PREFIX_.'category_product');
  1023. Db::getInstance()->Execute('TRUNCATE TABLE `'._DB_PREFIX_.'product_tag');
  1024. Db::getInstance()->Execute('TRUNCATE TABLE `'._DB_PREFIX_.'image');
  1025. Db::getInstance()->Execute('TRUNCATE TABLE `'._DB_PREFIX_.'image_lang');
  1026. Db::getInstance()->Execute('TRUNCATE TABLE `'._DB_PREFIX_.'product_attribute');
  1027. Db::getInstance()->Execute('TRUNCATE TABLE `'._DB_PREFIX_.'product_attribute_combination');
  1028. Db::getInstance()->Execute('TRUNCATE TABLE `'._DB_PREFIX_.'specific_price');
  1029. Db::getInstance()->Execute('TRUNCATE TABLE `'._DB_PREFIX_.'specific_price_priority');
  1030. Image::deleteAllImages(_PS_PROD_IMG_DIR_);
  1031. Image::clearTmpDir();
  1032. break;
  1033. case 'Manufacturers' :
  1034. Db::getInstance()->Execute('TRUNCATE TABLE `'._DB_PREFIX_.'manufacturer');
  1035. Db::getInstance()->Execute('TRUNCATE TABLE `'._DB_PREFIX_.'manufacturer_lang');
  1036. foreach (scandir(_PS_MANU_IMG_DIR_) AS $d)
  1037. if (preg_match('/^[0-9]+(\-(.*))?\.jpg$/', $d))
  1038. unlink(_PS_MANU_IMG_DIR_.$d);
  1039. Image::clearTmpDir();
  1040. break;
  1041. case 'Suppliers' :
  1042. Db::getInstance()->Execute('TRUNCATE TABLE `'._DB_PREFIX_.'supplier');
  1043. Db::getInstance()->Execute('TRUNCATE TABLE `'._DB_PREFIX_.'supplier_lang');
  1044. foreach (scandir(_PS_SUPP_IMG_DIR_) AS $d)
  1045. if (preg_match('/^[0-9]+(\-(.*))?\.jpg$/', $d))
  1046. unlink(_PS_SUPP_IMG_DIR_.$d);
  1047. Image::clearTmpDir();
  1048. break;
  1049. case 'attribute' :
  1050. Db::getInstance()->Execute('TRUNCATE TABLE `'._DB_PREFIX_.'attribute');
  1051. Db::getInstance()->Execute('TRUNCATE TABLE `'._DB_PREFIX_.'attribute_lang');
  1052. break;
  1053. case 'attributegroup' :
  1054. Db::getInstance()->Execute('TRUNCATE TABLE `'._DB_PREFIX_.'attribute_group');
  1055. Db::getInstance()->Execute('TRUNCATE TABLE `'._DB_PREFIX_.'attribute_group_lang');
  1056. break;
  1057. case 'currency' :
  1058. case 'customer' :
  1059. case 'zone' :
  1060. case 'state' :
  1061. case 'group' :
  1062. Db::getInstance()->Execute('TRUNCATE TABLE `'._DB_PREFIX_.pSQL($table));
  1063. break;
  1064. }
  1065. return true;
  1066. }
  1067. public function cleanPositions($table)
  1068. {
  1069. if($table == 'category')
  1070. {
  1071. //clean category position
  1072. $cat = Category::getCategories(1, false, false);
  1073. foreach($cat AS $i => $categ)
  1074. Category::cleanPositions((int)($categ['id_category']));
  1075. }
  1076. if($table == 'product')
  1077. {
  1078. //clean products position
  1079. $cat = Category::getCategories(1, false, false);
  1080. foreach($cat AS $i => $categ)
  1081. Product::cleanPositions((int)($categ['id_category']));
  1082. }
  1083. }
  1084. public function getDefaultIdLang()
  1085. {
  1086. return;
  1087. }
  1088. private function generateData($size = 1, $type)
  1089. {
  1090. $type = str_replace('is', '', $type);
  1091. $dom = array('com', 'net', 'org', 'biz', 'info');
  1092. $alphaNum = '0123456789abcdefghijklmnopqrstuvwxyz';
  1093. $alpha = 'abcdefghijklmnopqrstuvwxyz';
  1094. $num = '0123456789';
  1095. $return = '';
  1096. switch($type)
  1097. {
  1098. case 'CityName':
  1099. case 'Name':
  1100. case 'GenericName':
  1101. case 'CatalogName':
  1102. case 'Address':
  1103. case 'LinkRewrite':
  1104. case 'String':
  1105. $a = mt_rand($size/2, $size);
  1106. for ($i = 1; $i <= $a; $i++)
  1107. $return .= substr($alpha, mt_rand(0, strlen($alpha)), 1);
  1108. break;
  1109. case 'LanguageIsoCode':
  1110. for ($i = 1; $i <= 2; $i++)
  1111. $return .= substr($alpha, mt_rand(0, strlen($alpha)), 1);
  1112. break;
  1113. case 'LanguageCode':
  1114. for ($i = 1; $i <= 2; $i++)
  1115. $return .= substr($alphaNum, mt_rand(0, strlen($alphaNum)), 1);
  1116. $return .= '-';
  1117. for ($i = 1; $i <= 2; $i++)
  1118. $return .= substr($alphaNum, mt_rand(0, strlen($alphaNum)), 1);
  1119. break;
  1120. case 'Bool':
  1121. $return .= mt_rand(0,1);
  1122. break;
  1123. case 'Int':
  1124. case 'UnsignedId':
  1125. case 'NumericIsoCode':
  1126. case 'PhoneNumber':
  1127. case 'PostCode':
  1128. $a = mt_rand($size/2, $size);
  1129. for ($i = 1; $i <= $a; $i++)
  1130. $return .= substr($num, mt_rand(0, strlen($num)), 1);
  1131. break;
  1132. case 'Price':
  1133. case 'Float':
  1134. $a = mt_rand(4, 10);
  1135. $b = mt_rand(4, 10);
  1136. for ($i = 1; $i <= $a; $i++)
  1137. $return .= substr($alphaNum, mt_rand(0, strlen($alphaNum)), 1);
  1138. $return .= '-';
  1139. for ($i = 1; $i <= $b; $i++)
  1140. $return .= substr($alphaNum, mt_rand(0, strlen($alphaNum)), 1);
  1141. $return .= '.'.$dom[mt_rand(0, (sizeof($dom)-1))];
  1142. break;
  1143. case 'ZipCodeFormat';
  1144. $str = 'NLC -';
  1145. $a = mt_rand($size/2, $size);
  1146. for ($i = 1; $i <= $a; $i++)
  1147. $return .= substr($str, mt_rand(0, strlen($str)), 1);
  1148. break;
  1149. case 'StateIsoCode';
  1150. break;
  1151. case 'Email':
  1152. $a = mt_rand(4, 10);
  1153. $b = mt_rand(4, 10);
  1154. for ($i = 1; $i <= $a; $i++)
  1155. $return .= substr($alphaNum, mt_rand(0, strlen($alphaNum)), 1);
  1156. $return .= '@';
  1157. for ($i = 1; $i <= $b; $i++)
  1158. $return .= substr($alphaNum, mt_rand(0, strlen($alphaNum)), 1);
  1159. $ret

Large files files are truncated, but you can click here to view the full file