PageRenderTime 72ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/importerosc/importerosc.php

https://bitbucket.org/enurkov/prestashop
PHP | 631 lines | 540 code | 64 blank | 27 comment | 34 complexity | 15d009dbc7b139da644362e1f1bfae56 MD5 | raw file
  1. <?php
  2. /*
  3. * 2007-2012 PrestaShop
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Academic Free License (AFL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/afl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@prestashop.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
  18. * versions in the future. If you wish to customize PrestaShop for your
  19. * needs please refer to http://www.prestashop.com for more information.
  20. *
  21. * @author PrestaShop SA <contact@prestashop.com>
  22. * @copyright 2007-2012 PrestaShop SA
  23. * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
  24. * International Registered Trademark & Property of PrestaShop SA
  25. */
  26. if (!defined('_PS_VERSION_'))
  27. exit;
  28. class importerosc extends ImportModule
  29. {
  30. public function __construct()
  31. {
  32. $this->name = 'importerosc';
  33. $this->tab = 'migration_tools';
  34. $this->version = '1.0';
  35. $this->author = 'PrestaShop';
  36. $this->theImporter = 1;
  37. $this->need_instance = 1;
  38. parent::__construct ();
  39. $this->displayName = $this->l('Importer osCommerce');
  40. $this->description = $this->l('This module allows you to import from osCommerce to Prestashop.');
  41. }
  42. public function install()
  43. {
  44. if (!parent::install() || !$this->registerHook('beforeAuthentication'))
  45. return false;
  46. return true;
  47. }
  48. public function uninstall()
  49. {
  50. if (!parent::uninstall())
  51. return false;
  52. return true;
  53. }
  54. public function displaySpecificOptions()
  55. {
  56. $html = '';
  57. if (!$this->checkCategoriesLevel())
  58. $html .= $this->displayCategoriesLevelConf();
  59. $langagues = $this->executeS('SELECT * FROM `'.bqSQL($this->prefix).'languages`');
  60. $curencies = $this->executeS('SELECT * FROM `'.bqSQL($this->prefix).'currencies`');
  61. $html .= '<label style=\'width:220px\'>'.$this->l('Default osCommerce language : ').'</label>
  62. <div class="margin-form">
  63. <select name=\'defaultOscLang\'><option value=\'0\'>------</option>';
  64. foreach($langagues AS $lang)
  65. $html .= '<option value="'.$lang['languages_id'].'">'.$lang['name'].'</option>';
  66. $html .= '</select></div>
  67. <label style=\'width:220px\'>'.$this->l('Default osCommerce currency : ').'</label>
  68. <div class=\'margin-form\'>
  69. <select name=\'defaultOscCurrency\'><option value=\'0\'>------</option>';
  70. foreach($curencies AS $curency)
  71. $html .= '<option value=\''.$curency['currencies_id'].'\'>'.$curency['title'].'</option>';
  72. $html .= '</select></div>';
  73. $html .= '<label style=\'width:220px\'>'.$this->l('Shop url : ').'</label>
  74. <div class=\'margin-form\'>
  75. http://<input type="text" name="shop_url">/
  76. <p>'.$this->l('Specify the root URL of your site oscommerce').'</p>
  77. </div>';
  78. return $html;
  79. }
  80. public function validateSpecificOptions()
  81. {
  82. $errors = array();
  83. if (Tools::getValue('defaultOscLang') == 0)
  84. $errors[] = $this->l('Please select a default language');
  85. if (Tools::getValue('defaultOscCurrency') == 0)
  86. $errors[] = $this->l('Please select a default currency');
  87. if (Tools::getValue('shop_url') == '')
  88. $errors[] = $this->l('Please set your shop url');
  89. if (!sizeof($errors))
  90. die('{"hasError" : false, "error" : []}');
  91. else
  92. die('{"hasError" : true, "error" : '.Tools::jsonEncode($errors).'}');
  93. }
  94. public function getDefaultIdLang ()
  95. {
  96. return Tools::getValue('defaultOscLang');
  97. }
  98. public function getDefaultIdCurrency ()
  99. {
  100. return Tools::getValue('defaultOscCurrency');
  101. }
  102. public function getLangagues($limit = 0, $nrb_import = 100)
  103. {
  104. $identifier = 'id_lang';
  105. $langagues = $this->executeS('
  106. SELECT languages_id as id_lang, name as name, code as iso_code, 1 as active, (\'m/j/Y\') as date_format_lite, (\'m/j/Y H:i:s\') as date_format_full
  107. FROM `'.bqSQL($this->prefix).'languages`
  108. LIMIT '.(int)$limit.' , '.(int)$nrb_import);
  109. return $this->autoFormat($langagues, $identifier);
  110. }
  111. public function getCurrencies($limit = 0, $nrb_import = 100)
  112. {
  113. $identifier = 'id_currency';
  114. $currencies = $this->executeS('
  115. SELECT currencies_id as id_currency, title as name, code as iso_code, 0 as format, 999 as iso_code_num, 1 as decimals,
  116. CONCAT(`symbol_left`, `symbol_right`) as sign, value as conversion_rate
  117. FROM `'.bqSQL($this->prefix).'currencies` LIMIT '.(int)($limit).' , '.(int)$nrb_import
  118. );
  119. return $this->autoFormat($currencies, $identifier);
  120. }
  121. public function getZones($limit = 0, $nrb_import = 100)
  122. {
  123. $identifier = 'id_zone';
  124. $zones = $this->executeS('SELECT geo_zone_id as id_zone, geo_zone_name as name, 1 as active FROM `'.bqSQL($this->prefix).'geo_zones` LIMIT '.(int)($limit).' , '.(int)$nrb_import);
  125. return $this->autoFormat($zones, $identifier);
  126. }
  127. public function getCountries($limit = 0, $nrb_import = 100)
  128. {
  129. $multiLangFields = array('name');
  130. $keyLanguage = 'id_lang';
  131. $identifier = 'id_country';
  132. $defaultIdLang = $this->getDefaultIdLang();
  133. $countries = $this->executeS('
  134. SELECT countries_id as id_country, countries_name as name, countries_iso_code_2 as iso_code, '.(int)$defaultIdLang.' as id_lang,
  135. 1 as id_zone, 0 as id_currency, 1 as contains_states, 1 as need_identification_number, 1 as active, 1 as display_tax_label
  136. FROM `'.bqSQL($this->prefix).'countries` as c LIMIT '.(int)($limit).' , '.(int)$nrb_import);
  137. return $this->autoFormat($countries, $identifier, $keyLanguage, $multiLangFields);
  138. }
  139. public function getStates($limit = 0, $nrb_import = 100)
  140. {
  141. $identifier = 'id_state';
  142. $states = array(
  143. 0 => array(
  144. 'id_state' => 0,
  145. 'id_country' => 0,
  146. 'id_zone' => 0,
  147. 'iso_code' => 999,
  148. 'name' => 'osc',
  149. 'active' => 0
  150. )
  151. );
  152. return $this->autoFormat($states, $identifier);
  153. }
  154. public function getGroups()
  155. {
  156. $idLang = $this->getDefaultIdLang();
  157. return array( 1 => array(
  158. 'id_group' => 1,
  159. 'price_display_method' => 0,
  160. 'name' => array($idLang => $this->l('Default osCommerce Group'))
  161. )
  162. );
  163. }
  164. public function getCustomers($limit = 0, $nrb_import = 100)
  165. {
  166. $genderMatch = array('m' => 1,'f' => 2);
  167. $identifier = 'id_customer';
  168. $customers = $this->executeS('
  169. SELECT c.`customers_id` as id_customer, 1 as id_default_group, c.`customers_gender` as id_gender, c.`customers_firstname` as firstname,
  170. IFNULL( STRCMP(c.`customers_newsletter`, \'\') , 0 ) as newsletter, c.`customers_lastname` as lastname,
  171. DATE(c.`customers_dob`) as birthday, c.`customers_email_address` as email, c.`customers_password` as passwd, 1 as active,
  172. ci.`customers_info_date_account_created` as date_add
  173. FROM `'.bqSQL($this->prefix).'customers` c
  174. LEFT JOIN `'.bqSQL($this->prefix).'customers_info` ci ON (c.`customers_id` = ci.`customers_info_id`)
  175. LIMIT '.(int)($limit).' , '.(int)$nrb_import
  176. );
  177. foreach($customers AS &$customer)
  178. if (isset($customer['id_gender']) && array_key_exists($customer['id_gender'], $genderMatch))
  179. $customer['id_gender'] = $genderMatch[$customer['id_gender']];
  180. else
  181. $customer['id_gender'] = 0;
  182. return $this->autoFormat($customers, $identifier);
  183. }
  184. public function getAddresses($limit = 0, $nrb_import = 100)
  185. {
  186. $identifier = 'id_address';
  187. $addresses = $this->executeS('
  188. SELECT address_book_id as id_address, customers_id as id_customer, CONCAT(customers_id, \'_address\') as alias, entry_company as company, entry_firstname as firstname,
  189. entry_lastname as lastname, entry_street_address as address1, entry_postcode as postcode, entry_city as city, entry_country_id as id_country, 0 as id_state
  190. FROM `'.bqSQL($this->prefix).'address_book` LIMIT '.(int)($limit).' , '.(int)$nrb_import);
  191. return $this->autoFormat($addresses, $identifier);
  192. }
  193. public function getCategories($limit = 0, $nrb_import = 100)
  194. {
  195. $multiLangFields = array('name', 'link_rewrite');
  196. $keyLanguage = 'id_lang';
  197. $identifier = 'id_category';
  198. $categories = $this->executeS('
  199. SELECT c.categories_id as id_category, c.parent_id as id_parent, level as level_depth, cd.language_id as id_lang, cd.categories_name as name , 1 as active, categories_image as images
  200. FROM `'.bqSQL($this->prefix).'categories` c
  201. LEFT JOIN `'.bqSQL($this->prefix).'categories_description` cd ON (c.categories_id = cd.categories_id)
  202. WHERE cd.categories_name IS NOT NULL AND cd.language_id IS NOT NULL
  203. ORDER BY c.level ASC , c.`categories_id`
  204. LIMIT '.(int)($limit).' , '.(int)$nrb_import);
  205. foreach($categories as& $cat)
  206. {
  207. $cat['link_rewrite'] = Tools::link_rewrite($cat['name']);
  208. $cat['images'] = array(Tools::getProtocol().Tools::getValue('shop_url').'/images/'.$cat['images']);
  209. }
  210. return $this->autoFormat($categories, $identifier, $keyLanguage, $multiLangFields);
  211. }
  212. public function getAttributesGroups($limit = 0, $nrb_import = 100)
  213. {
  214. $multiLangFields = array('name', 'public_name');
  215. $keyLanguage = 'id_lang';
  216. $identifier = 'id_attribute_group';
  217. $countries = $this->executeS('
  218. SELECT products_options_id as id_attribute_group, products_options_name as name , products_options_name as public_name, language_id as id_lang, 0 as is_color_group
  219. FROM `'.bqSQL($this->prefix).'products_options`
  220. LIMIT '.(int)($limit).' , '.(int)$nrb_import);
  221. return $this->autoFormat($countries, $identifier, $keyLanguage, $multiLangFields);
  222. }
  223. public function getAttributes($limit = 0, $nrb_import = 100)
  224. {
  225. $multiLangFields = array('name');
  226. $keyLanguage = 'id_lang';
  227. $identifier = 'id_attribute';
  228. $countries = $this->executeS('
  229. SELECT p.`products_options_values_id` as id_attribute, p.`products_options_values_name` as name, p.`language_id` as id_lang , po.`products_options_id` as id_attribute_group
  230. FROM `'.bqSQL($this->prefix).'products_options_values` p
  231. LEFT JOIN `'.bqSQL($this->prefix).'products_options_values_to_products_options` po ON (po.products_options_values_id = p.products_options_values_id)
  232. ORDER BY p.`products_options_values_id`
  233. LIMIT '.(int)($limit).' , '.(int)$nrb_import);
  234. return $this->autoFormat($countries, $identifier, $keyLanguage, $multiLangFields);
  235. }
  236. public function getProducts($limit = 0, $nrb_import = 100)
  237. {
  238. $multiLangFields = array('name', 'link_rewrite', 'description');
  239. $keyLanguage = 'id_lang';
  240. $identifier = 'id_product';
  241. $products = $this->executeS('
  242. SELECT p.`products_id` as id_product, p.`products_quantity` as quantity, p.`products_model` as reference, p.`products_price` as price, p.`products_weight` as weight,
  243. IFNULL(STRCMP(p.`products_status`, \'\') , 0 ) as active, p.`manufacturers_id` as id_manufacturer, pd.language_id as id_lang, pd.products_name as name,
  244. pd.products_description as description, CONCAT(\''.pSQL(Tools::getProtocol()).pSQL(Tools::getValue('shop_url')).'\/images/\',p.`products_image`) as images,
  245. (SELECT ptc.categories_id FROM `'.bqSQL($this->prefix).'products_to_categories` ptc WHERE ptc.`products_id` = p.`products_id` LIMIT 1) as id_category_default,
  246. p.`products_date_added` as date_add
  247. FROM `'.bqSQL($this->prefix).'products` p
  248. LEFT JOIN `'.bqSQL($this->prefix).'products_description` pd ON (p.products_id = pd.products_id)
  249. WHERE pd.products_name IS NOT NULL AND pd.language_id IS NOT NULL
  250. ORDER BY p.`products_id`
  251. LIMIT '.(int)($limit).' , '.(int)$nrb_import);
  252. $this->execute('CREATE TABLE IF NOT EXISTS`products_images` (
  253. `id` int(11) NOT NULL AUTO_INCREMENT,
  254. `products_id` int(11) NOT NULL,
  255. `image` varchar(64) DEFAULT NULL,
  256. `htmlcontent` text,
  257. `sort_order` int(11) NOT NULL,
  258. PRIMARY KEY (`id`),
  259. KEY `products_images_prodid` (`products_id`)
  260. )');
  261. foreach($products as& $product)
  262. {
  263. $result = $this->executeS('SELECT `image` FROM `'.bqSQL($this->prefix).'products_images` WHERE products_id = '.(int)$product['id_product']);
  264. $images = array();
  265. foreach($result as $res)
  266. $images[] = Tools::getProtocol().Tools::getValue('shop_url').'/images/'.$res['image'];
  267. $product['images'] = array_merge(array($product['images']), $images);
  268. $product['link_rewrite'] = Tools::link_rewrite($product['name']);
  269. $result = $this->executeS('SELECT `categories_id` FROM `'.bqSQL($this->prefix).'products_to_categories` WHERE products_id = '.(int)$product['id_product']);
  270. $category_product = array('category_product' => array($product['id_category_default'] => $product['id_product']));
  271. foreach($result as $res)
  272. $category_product['category_product'][$res['categories_id']] = $product['id_product'];
  273. $product['association'] = $category_product;
  274. }
  275. return $this->autoFormat($products, $identifier, $keyLanguage, $multiLangFields);
  276. }
  277. public function getProductsCombination($limit = 0, $nrb_import = 100)
  278. {
  279. $identifier = 'id_product_attribute';
  280. $combinations = $this->executeS('
  281. SELECT products_attributes_id as id_product_attribute, products_id as id_product, options_values_price as price, options_values_id
  282. FROM `'.bqSQL($this->prefix).'products_attributes` LIMIT '.(int)($limit).' , '.(int)$nrb_import);
  283. foreach($combinations as& $combination)
  284. {
  285. $combination['association'] = array('product_attribute_combination' => array($combination['options_values_id'] => $combination['id_product_attribute']));
  286. unset($combination['options_values_id']);
  287. }
  288. return $this->autoFormat($combinations, $identifier);
  289. }
  290. public function getManufacturers($limit = 0, $nrb_import = 100)
  291. {
  292. $identifier = 'id_manufacturer';
  293. $manufacturers = $this->executeS('
  294. SELECT manufacturers_id as id_manufacturer, manufacturers_name as name, 1 as active, manufacturers_image as images
  295. FROM `'.bqSQL($this->prefix).'manufacturers` LIMIT '.(int)($limit).' , '.(int)$nrb_import);
  296. foreach($manufacturers as& $manufacturer)
  297. $manufacturer['images'] = array(Tools::getProtocol().Tools::getValue('shop_url').'/images/'.$manufacturer['images']);
  298. return $this->autoFormat($manufacturers, $identifier);
  299. }
  300. public function getOrdersStates($limit = 0, $nrb_import = 100)
  301. {
  302. $multiLangFields = array('name');
  303. $keyLanguage = 'id_lang';
  304. $identifier = 'id_order_state';
  305. $ordersStates = $this->executeS('
  306. SELECT `orders_status_id` as id_order_state, `language_id` as id_lang, `orders_status_name` as name , 1 as hidden
  307. FROM `'.bqSQL($this->prefix).'orders_status`
  308. LIMIT '.(int)($limit).' , '.(int)$nrb_import);//IF(`public_flag` = 0, 1, 0) as hidden
  309. return $this->autoFormat($ordersStates, $identifier, $keyLanguage, $multiLangFields);
  310. }
  311. public function getOrders($limit = 0, $nrb_import = 100)
  312. {
  313. $orders = array();
  314. $addresses = $this->executeS('SELECT customers_id as id_customer, address_book_id as id_address FROM `'.bqSQL($this->prefix).'address_book` GROUP BY customers_id');
  315. $matchAddresses = array();
  316. foreach($addresses as $address)
  317. $matchAddresses[$address['id_customer']] = $address['id_address'];
  318. $psCarrierDefault = (int)Configuration::get('PS_CARRIER_DEFAULT');
  319. $psCurrency = Currency::getCurrencies();
  320. foreach($psCurrency as $key => $currency)
  321. {
  322. $psCurrency[$currency['iso_code']] = $currency['id_currency'];
  323. unset($psCurrency[$key]);
  324. }
  325. $orders = $this->executeS('
  326. SELECT orders_id as id_cart, '.$psCarrierDefault.' as id_carrier, 1 as id_lang, currency as id_currency, customers_id as id_customer, payment_method as payment, 1 as valid,
  327. date_purchased as date_add, last_modified as date_upd
  328. FROM `'.bqSQL($this->prefix).'orders` LIMIT '.(int)($limit).' , '.(int)$nrb_import);
  329. foreach($orders as $key => $order)
  330. {
  331. $orders[$key]['id_currency'] = (array_key_exists($order['id_currency'], $psCurrency) ? $psCurrency[$order['id_currency']] : 0);
  332. $orders[$key]['id_address_delivery'] = (array_key_exists($order['id_customer'], $matchAddresses) ? $matchAddresses[$order['id_customer']] : 0);
  333. $orders[$key]['id_address_invoice'] = (array_key_exists($order['id_customer'], $matchAddresses) ? $matchAddresses[$order['id_customer']] : 0);
  334. $orders[$key]['total_paid'] = $this->getValue('SELECT value FROM `'.bqSQL($this->prefix).'orders_total` WHERE `orders_id` = '.(int)$order['id_cart'].' AND class=\'ot_total\'');
  335. $orders[$key]['total_paid_real'] = $this->getValue('SELECT value FROM `'.bqSQL($this->prefix).'orders_total` WHERE `orders_id` = '.(int)$order['id_cart'].' AND class=\'ot_total\'');
  336. $orders[$key]['total_products'] = $this->getValue('SELECT value FROM `'.bqSQL($this->prefix).'orders_total` WHERE `orders_id` = '.(int)$order['id_cart'].' AND class=\'ot_shipping\'');
  337. $tax = $this->getValue('SELECT value FROM `'.bqSQL($this->prefix).'orders_total` WHERE `orders_id` = '.(int)$order['id_cart'].' AND class=\'ot_tax\'');
  338. $orders[$key]['total_products_wt'] = $this->getValue('SELECT value FROM `'.bqSQL($this->prefix).'orders_total` WHERE `orders_id` = '.(int)$order['id_cart'].' AND class=\'ot_total\'') - $tax;
  339. $orders[$key]['total_shipping'] = $this->getValue('SELECT value FROM `'.bqSQL($this->prefix).'orders_total` WHERE `orders_id` = '.(int)$order['id_cart'].' AND class=\'ot_shipping\'');
  340. $orders[$key]['total_discounts'] = 0;
  341. $orders[$key]['total_wrapping'] = 0;
  342. $orders[$key]['cart_products'] = $this->executeS('
  343. SELECT `orders_id` as id_cart, `products_id` as id_product, 0 as id_product_attribute, `products_quantity` as quantity
  344. FROM `'.bqSQL($this->prefix).'orders_products` WHERE `orders_id` = '.(int)$order['id_cart']);
  345. $orders[$key]['order_products'] = $this->executeS('
  346. SELECT `orders_id` as id_order, `products_id` as product_id, 0 as product_attribute_id, `products_name` as product_name, `products_quantity` as product_quantity,
  347. `final_price` as product_price, 0 as product_weight
  348. FROM `'.bqSQL($this->prefix).'orders_products` WHERE `orders_id` = '.(int)$order['id_cart']);
  349. $orders[$key]['order_history'] = $this->executeS('
  350. SELECT `orders_status_history_id` as id_order_history, 0 as id_employee, `orders_id` as id_order, `orders_status_id` as id_order_state, `date_added` as date_add
  351. FROM `'.bqSQL($this->prefix).'orders_status_history` WHERE `orders_id` = '.(int)$order['id_cart']);
  352. }
  353. return $orders;
  354. }
  355. private function autoFormat($items, $identifier, $keyLanguage = NULL, $multiLangFields = array())
  356. {
  357. $array = array();
  358. foreach ($items AS $item)
  359. if (sizeof($multiLangFields) && is_array($multiLangFields) && isset($array[$item[$identifier]][$multiLangFields[0]]))
  360. foreach ($multiLangFields AS $key)
  361. $array[$item[$identifier]][$key][$item[$keyLanguage]] = $item[$key];
  362. else
  363. foreach ($item AS $key => $value)
  364. if (sizeof($multiLangFields) AND in_array($key, $multiLangFields))
  365. $array[$item[$identifier]][$key] = array($item[$keyLanguage] => $value);
  366. elseif (sizeof($multiLangFields) AND $key == $keyLanguage)
  367. continue;
  368. else
  369. $array[$item[$identifier]][$key] = $value;
  370. return $array;
  371. }
  372. public function hookbeforeAuthentication($params)
  373. {
  374. $passwd = trim(Tools::getValue('passwd'));
  375. $email = trim(Tools::getValue('email'));
  376. $result = Db::getInstance()->GetRow('
  377. SELECT *
  378. FROM `'._DB_PREFIX_ .'customer`
  379. WHERE `active` = 1 AND `email` = \''.pSQL($email).'\'');
  380. if ($result && !empty($result['passwd_'.$this->name]))
  381. {
  382. if($this->checkPwd($passwd, $result['passwd_'.pSQL($this->name)]))
  383. {
  384. $ps_passwd = md5(pSQL(_COOKIE_KEY_.$passwd));
  385. Db::getInstance()->execute('
  386. UPDATE `'._DB_PREFIX_.'customer`
  387. SET `passwd` = \''.pSQL($ps_passwd).'\', `passwd_'.bqSQL($this->name).'` = \'\'
  388. WHERE `'._DB_PREFIX_.'customer`.`id_customer` ='.(int)$result['id_customer'].' LIMIT 1');
  389. }
  390. }
  391. }
  392. private function checkPwd($passwd, $encrypt_pwd)
  393. {
  394. //checks the type of encryption password
  395. if (preg_match('/^[A-Z0-9]{32}\:[A-Z0-9]{2}$/i', $encrypt_pwd) === 1)
  396. {
  397. //salt
  398. $stack = explode(':', $encrypt_pwd);
  399. if (sizeof($stack) != 2)
  400. return false;
  401. if (md5($stack[1] . $passwd) == $stack[0])
  402. return true;
  403. else
  404. return false;
  405. }
  406. else
  407. {
  408. //phpass
  409. if (file_exists(dirname(__FILE__).'/passwordhash.php'))
  410. {
  411. include(dirname(__FILE__).'/passwordhash.php');
  412. $hasher = new PasswordHash(10, true);
  413. return $hasher->CheckPassword($passwd, $encrypt_pwd);
  414. }
  415. else
  416. return false;
  417. }
  418. }
  419. public function displayConfigConnector()
  420. {
  421. $content = '<script>var type_connector = "db";</script>
  422. <label>'.$this->l('Server').' : </label>
  423. <div class="margin-form">
  424. <input type="text" name="server" id="server" value="">
  425. <p>'.$this->l('(eg : mysql.mydomain.com)').'</p>
  426. </div>
  427. <label>'.$this->l('User').' : </label>
  428. <div class="margin-form">
  429. <input type="text" name="user" id="user" value="">
  430. </div>
  431. <label>'.$this->l('Password').' : </label>
  432. <div class="margin-form">
  433. <input type="password" name="password" id="password" value="">
  434. <p>'.$this->l('(password can be empty)').'</p>
  435. </div>
  436. <label>'.$this->l('Database').' : </label>
  437. <div class="margin-form" style="">
  438. <input type="text" name="database" id="database" value="">
  439. </div>
  440. <label>'.$this->l('Database prefix').' : </label>
  441. <div class="margin-form" style="">
  442. <input type="text" name="prefix" id="prefix" value="">
  443. <p>'.$this->l('(The prefix is optional. If all of your database tables start with "pref_", your prefix is "pref_")').'</p>
  444. </div>';
  445. return $content;
  446. }
  447. public function checkCategoriesLevel()
  448. {
  449. $columns = $this->executeS('SHOW COLUMNS FROM `'.bqSQL($this->prefix).'categories` ');
  450. foreach($columns as $field)
  451. if ($field['Field'] == 'level')
  452. return true;
  453. return false;
  454. }
  455. public function displayCategoriesLevelConf()
  456. {
  457. $html = '<div class="warn" id="warn_category_level" style="width:450px;display:none">
  458. <img src="../img/admin/warn2.png">
  459. '.$this->l('You are trying to import categories and we\'ve detected, that your oscommerce database don\'t have the field "level" in the table categorie. You must have this field to continue the import of categories.');
  460. $html .= '<button class="button" onclick="addAndCalculateLevel();" style="padding:10px;font-size:13px;text:align:center">'.$this->l('Click to add and calculate the filed "level" .').'</button> <span id="loading" style="display:none"><img src="../img/loader.gif"></span></div>
  461. <div class="conf" id="conf_category_level" style="width:450px;display:none"><img src="../img/admin/ok2.png">'.$this->l('Level field\'s has been created and calculated, You can continue').'</div>';
  462. $html .= '
  463. <script>
  464. $(document).ready(function (){
  465. function checkCategorySelected()
  466. {
  467. if ($(\'#id_category_on:radio\').attr(\'checked\'))
  468. {
  469. $(\'#warn_category_level\').show();
  470. $(\'#checkAndSaveConfig\').attr(\'disabled\', \'disabled\');
  471. $(\'#checkAndSaveConfig\').hide();
  472. }
  473. else
  474. {
  475. $(\'#warn_category_level\').hide();
  476. $(\'#checkAndSaveConfig\').removeAttr(\'disabled\');
  477. $(\'#checkAndSaveConfig\').show();
  478. }
  479. }
  480. checkCategorySelected();
  481. $(\'input[name="getCategories"]\').change( function () {
  482. checkCategorySelected();
  483. });
  484. });
  485. function addAndCalculateLevel()
  486. {
  487. if (confirm(\''.$this->l('It is highly recommended to backup your database before proceeding. Did you make a backup?').'\'))
  488. {
  489. $(\'#loading\').show();
  490. $.ajax({
  491. type: "GET",
  492. url: "../modules/importerosc/ajax.php",
  493. async: false,
  494. cache: false,
  495. dataType : "json",
  496. data: "ajax=true&token='.sha1(_COOKIE_KEY_.'importosc').'&server="+$(\'#server\').val()+"&user="+$(\'#user\').val()+"&password="+$(\'#password\').val()+"&database="+$(\'#database\').val()+"&prefix="+$(\'#prefix\').val() ,
  497. success: function (jsonData)
  498. {
  499. if (jsonData.hasError)
  500. alert(jsonData.error);
  501. else
  502. {
  503. $(\'#warn_category_level\').remove();
  504. $(\'#checkAndSaveConfig\').removeAttr(\'disabled\');
  505. $(\'#checkAndSaveConfig\').show();
  506. $(\'#conf_category_level\').show();
  507. }
  508. },
  509. error: function (XMLHttpRequest, textStatus, errorThrown)
  510. {
  511. }
  512. });
  513. }
  514. return false;
  515. }
  516. </script>';
  517. return $html;
  518. }
  519. public function createLevelAndCalculate()
  520. {
  521. if ($this->checkCategoriesLevel())
  522. die('{"hasError" : false}');
  523. if ($this->createLevel())
  524. $this->calculateLevel();
  525. else
  526. die('{"hasError" : true, "error" : "'.$this->l('Can not ALTER TABLE').'"}');
  527. }
  528. public function createLevel()
  529. {
  530. return $this->execute('ALTER TABLE `'.bqSQL($this->prefix).'categories` ADD `level` INT NOT NULL');
  531. }
  532. public function calculateLevel()
  533. {
  534. $this->updateLevel($this->getSubCat(0), 1);
  535. die('{"hasError" : false}');
  536. }
  537. public function updateLevel($ids_cat, $level = 1)
  538. {
  539. $this->execute('
  540. UPDATE `'.bqSQL($this->prefix).'categories`
  541. SET level = '.(int)$level.'
  542. WHERE categories_id IN ('.implode(',', $ids_cat).')');
  543. foreach($ids_cat as $id)
  544. if ($sub_cat = $this->getSubCat($id))
  545. $this->updateLevel($sub_cat, $level + 1);
  546. }
  547. public function getSubCat($id_parent)
  548. {
  549. $result = $this->executeS('SELECT `categories_id` FROM `'.bqSQL($this->prefix).'categories` WHERE `parent_id`='.(int)$id_parent);
  550. if (!is_array($result) OR empty($result))
  551. return false;
  552. return $this->formatCategoriesIds($result);
  553. }
  554. public function formatCategoriesIds($result)
  555. {
  556. $return = array();
  557. foreach($result as $key => $val)
  558. $return[] = $val['categories_id'];
  559. return $return;
  560. }
  561. }
  562. ?>