PageRenderTime 60ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/controllers/admin/AdminShopController.php

https://github.com/netplayer/PrestaShop
PHP | 790 lines | 618 code | 92 blank | 80 comment | 100 complexity | 5564fcb64d76f7f2244de3635bcdcec9 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, LGPL-3.0
  1. <?php
  2. /*
  3. * 2007-2014 PrestaShop
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 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/osl-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-2014 PrestaShop SA
  23. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  24. * International Registered Trademark & Property of PrestaShop SA
  25. */
  26. class AdminShopControllerCore extends AdminController
  27. {
  28. public function __construct()
  29. {
  30. $this->bootstrap = true;
  31. $this->context = Context::getContext();
  32. $this->table = 'shop';
  33. $this->className = 'Shop';
  34. $this->multishop_context = Shop::CONTEXT_ALL;
  35. $this->id_shop_group = Tools::getValue('id_shop_group');
  36. $this->list_skip_actions['delete'] = array((int)Configuration::get('PS_SHOP_DEFAULT'));
  37. $this->fields_list = array(
  38. 'id_shop' => array(
  39. 'title' => $this->l('Shop ID'),
  40. 'align' => 'center',
  41. 'class' => 'fixed-width-xs'
  42. ),
  43. 'name' => array(
  44. 'title' => $this->l('Shop name'),
  45. 'filter_key' => 'a!name',
  46. 'width' => 200,
  47. ),
  48. 'shop_group_name' => array(
  49. 'title' => $this->l('Shop group'),
  50. 'width' => 150,
  51. 'filter_key' => 'gs!name'
  52. ),
  53. 'category_name' => array(
  54. 'title' => $this->l('Root category'),
  55. 'width' => 150,
  56. 'filter_key' => 'cl!name'
  57. ),
  58. 'url' => array(
  59. 'title' => $this->l('Main URL for this shop'),
  60. 'havingFilter' => 'url',
  61. ),
  62. /*'active' => array(
  63. 'title' => $this->l('Enabled'),
  64. 'align' => 'center',
  65. 'active' => 'status',
  66. 'type' => 'bool',
  67. 'orderby' => false,
  68. 'filter_key' => 'active',
  69. 'width' => 50,
  70. )*/
  71. );
  72. parent::__construct();
  73. }
  74. public function viewAccess($disable = false)
  75. {
  76. return Configuration::get('PS_MULTISHOP_FEATURE_ACTIVE');
  77. }
  78. public function initPageHeaderToolbar()
  79. {
  80. parent::initPageHeaderToolbar();
  81. if (!$this->display && $this->id_shop_group)
  82. {
  83. if ($this->id_object)
  84. $this->loadObject();
  85. if (!$this->id_shop_group && $this->object && $this->object->id_shop_group)
  86. $this->id_shop_group = $this->object->id_shop_group;
  87. $this->page_header_toolbar_btn['edit'] = array(
  88. 'desc' => $this->l('Edit this shop group'),
  89. 'href' => $this->context->link->getAdminLink('AdminShopGroup').'&updateshop_group&id_shop_group='
  90. .$this->id_shop_group,
  91. );
  92. $this->page_header_toolbar_btn['new'] = array(
  93. 'desc' => $this->l('Add new shop'),
  94. 'href' => $this->context->link->getAdminLink('AdminShop').'&add'.$this->table.'&id_shop_group='
  95. .$this->id_shop_group,
  96. );
  97. }
  98. }
  99. public function initToolbar()
  100. {
  101. parent::initToolbar();
  102. if ($this->display != 'edit' && $this->display != 'add')
  103. {
  104. if ($this->id_object)
  105. $this->loadObject();
  106. if (!$this->id_shop_group && $this->object && $this->object->id_shop_group)
  107. $this->id_shop_group = $this->object->id_shop_group;
  108. $this->toolbar_btn['new'] = array(
  109. 'desc' => $this->l('Add new shop'),
  110. 'href' => $this->context->link->getAdminLink('AdminShop').'&add'.$this->table.'&id_shop_group='
  111. .$this->id_shop_group,
  112. );
  113. }
  114. }
  115. public function initContent()
  116. {
  117. parent::initContent();
  118. $this->addJqueryPlugin('cooki-plugin');
  119. $data = Shop::getTree();
  120. foreach ($data as $key_group => &$group)
  121. foreach ($group['shops'] as $key_shop => &$shop)
  122. {
  123. $current_shop = new Shop($shop['id_shop']);
  124. $urls = $current_shop->getUrls();
  125. foreach ($urls as $key_url => &$url)
  126. {
  127. $title = $url['domain'].$url['physical_uri'].$url['virtual_uri'];
  128. if (strlen($title) > 23)
  129. $title = substr($title, 0, 23).'...';
  130. $url['name'] = $title;
  131. $shop['urls'][$url['id_shop_url']] = $url;
  132. }
  133. }
  134. $shops_tree = new HelperTreeShops('shops-tree', 'Multistore tree');
  135. $shops_tree->setNodeFolderTemplate('shop_tree_node_folder.tpl')->setNodeItemTemplate('shop_tree_node_item.tpl')
  136. ->setHeaderTemplate('shop_tree_header.tpl')->setActions(array(
  137. new TreeToolbarLink(
  138. 'Collapse All',
  139. '#',
  140. '$(\'#'.$shops_tree->getId().'\').tree(\'collapseAll\'); return false;',
  141. 'icon-collapse-alt'),
  142. new TreeToolbarLink(
  143. 'Expand All',
  144. '#',
  145. '$(\'#'.$shops_tree->getId().'\').tree(\'expandAll\'); return false;',
  146. 'icon-expand-alt')
  147. ))
  148. ->setAttribute('url_shop_group', $this->context->link->getAdminLink('AdminShopGroup'))
  149. ->setAttribute('url_shop', $this->context->link->getAdminLink('AdminShop'))
  150. ->setAttribute('url_shop_url', $this->context->link->getAdminLink('AdminShopUrl'))
  151. ->setData($data);
  152. $shops_tree = $shops_tree->render(null, false, false);
  153. if ($this->display == 'edit')
  154. $this->toolbar_title[] = $this->object->name;
  155. else if (!$this->display && $this->id_shop_group)
  156. {
  157. $group = new ShopGroup($this->id_shop_group);
  158. $this->toolbar_title[] = $group->name;
  159. }
  160. $this->context->smarty->assign(array(
  161. 'toolbar_scroll' => 1,
  162. 'toolbar_btn' => $this->toolbar_btn,
  163. 'title' => $this->toolbar_title,
  164. 'shops_tree' => $shops_tree
  165. ));
  166. }
  167. public function renderList()
  168. {
  169. $this->addRowAction('edit');
  170. $this->addRowAction('delete');
  171. $this->_select = 'gs.name shop_group_name, cl.name category_name, CONCAT(\'http://\', su.domain, su.physical_uri, su.virtual_uri) AS url';
  172. $this->_join = '
  173. LEFT JOIN `'._DB_PREFIX_.'shop_group` gs
  174. ON (a.id_shop_group = gs.id_shop_group)
  175. LEFT JOIN `'._DB_PREFIX_.'category_lang` cl
  176. ON (a.id_category = cl.id_category AND cl.id_lang='.(int)$this->context->language->id.')
  177. LEFT JOIN '._DB_PREFIX_.'shop_url su
  178. ON a.id_shop = su.id_shop AND su.main = 1
  179. ';
  180. $this->_group = 'GROUP BY a.id_shop';
  181. if ($id_shop_group = (int)Tools::getValue('id_shop_group'))
  182. $this->_where = 'AND a.id_shop_group = '.$id_shop_group;
  183. return parent::renderList();
  184. }
  185. public function displayAjaxGetCategoriesFromRootCategory()
  186. {
  187. if (Tools::isSubmit('id_category'))
  188. {
  189. $selected_cat = array((int)Tools::getValue('id_category'));
  190. $children = Category::getChildren((int)Tools::getValue('id_category'), $this->context->language->id);
  191. foreach ($children as $child)
  192. $selected_cat[] = $child['id_category'];
  193. $helper = new HelperTreeCategories('categories-tree', null, (int)Tools::getValue('id_category'), null, false);
  194. $this->content = $helper->setSelectedCategories($selected_cat)->setUseSearch(true)->setUseCheckBox(true)
  195. ->render();
  196. }
  197. parent::displayAjax();
  198. }
  199. public function postProcess()
  200. {
  201. if (Tools::isSubmit('id_category_default'))
  202. $_POST['id_category'] = Tools::getValue('id_category_default');
  203. /*if ((Tools::isSubmit('status') ||
  204. Tools::isSubmit('status'.$this->table) ||
  205. (Tools::isSubmit('submitAdd'.$this->table) && Tools::getValue($this->identifier) && !Tools::getValue('active'))) &&
  206. $this->loadObject() && $this->loadObject()->active)
  207. {
  208. if (Tools::getValue('id_shop') == Configuration::get('PS_SHOP_DEFAULT'))
  209. $this->errors[] = Tools::displayError('You cannot disable the default shop.');
  210. else if (Shop::getTotalShops() == 1)
  211. $this->errors[] = Tools::displayError('You cannot disable the last shop.');
  212. }*/
  213. if (Tools::isSubmit('submitAddshopAndStay') || Tools::isSubmit('submitAddshop'))
  214. {
  215. $shop_group = new ShopGroup((int)Tools::getValue('id_shop_group'));
  216. if ($shop_group->shopNameExists(Tools::getValue('name'), (int)Tools::getValue('id_shop')))
  217. $this->errors[] = Tools::displayError('You cannot have two shops with the same name in the same group.');
  218. }
  219. if (count($this->errors))
  220. return false;
  221. $result = parent::postProcess();
  222. if ($result !== false && (Tools::isSubmit('submitAddshopAndStay') || Tools::isSubmit('submitAddshop')) && (int)$result->id_category != (int)Configuration::get('PS_HOME_CATEGORY', null, null, (int)$result->id))
  223. Configuration::updateValue('PS_HOME_CATEGORY', (int)$result->id_category, false, null, (int)$result->id);
  224. if ($this->redirect_after)
  225. $this->redirect_after .= '&id_shop_group='.$this->id_shop_group;
  226. return $result;
  227. }
  228. public function processDelete()
  229. {
  230. if (!Validate::isLoadedObject($object = $this->loadObject()))
  231. $this->errors[] = Tools::displayError('Unable to load this shop.');
  232. else if (!Shop::hasDependency($object->id))
  233. {
  234. $result = Category::deleteCategoriesFromShop($object->id) && parent::processDelete();
  235. Tools::generateHtaccess();
  236. return $result;
  237. }
  238. else
  239. $this->errors[] = Tools::displayError('You can\'t delete this shop (customer and/or order dependency).');
  240. return false;
  241. }
  242. protected function afterAdd($new_shop)
  243. {
  244. $import_data = Tools::getValue('importData', array());
  245. // The root category should be at least imported
  246. $new_shop->copyShopData((int)Tools::getValue('importFromShop'), $import_data);
  247. // copy default data
  248. if (!Tools::getValue('useImportData') || (is_array($import_data) && !isset($import_data['group'])))
  249. {
  250. $sql = 'INSERT INTO `'._DB_PREFIX_.'group_shop` (`id_shop`, `id_group`)
  251. VALUES
  252. ('.(int)$new_shop->id.', '.(int)Configuration::get('PS_UNIDENTIFIED_GROUP').'),
  253. ('.(int)$new_shop->id.', '.(int)Configuration::get('PS_GUEST_GROUP').'),
  254. ('.(int)$new_shop->id.', '.(int)Configuration::get('PS_CUSTOMER_GROUP').')
  255. ';
  256. Db::getInstance()->execute($sql);
  257. }
  258. return parent::afterAdd($new_shop);
  259. }
  260. protected function afterUpdate($new_shop)
  261. {
  262. $categories = Tools::getValue('categoryBox');
  263. array_unshift($categories, Configuration::get('PS_ROOT_CATEGORY'));
  264. if (!Category::updateFromShop($categories, $new_shop->id))
  265. $this->errors[] = $this->l('You need to select at least the root category.');
  266. if (Tools::getValue('useImportData') && ($import_data = Tools::getValue('importData')) && is_array($import_data))
  267. $new_shop->copyShopData((int)Tools::getValue('importFromShop'), $import_data);
  268. return parent::afterUpdate($new_shop);
  269. }
  270. public function getList($id_lang, $order_by = null, $order_way = null, $start = 0, $limit = null, $id_lang_shop = false)
  271. {
  272. if (Shop::getContext() == Shop::CONTEXT_GROUP)
  273. $this->_where .= ' AND a.id_shop_group = '.(int)Shop::getContextShopGroupID();
  274. parent::getList($id_lang, $order_by, $order_way, $start, $limit, $id_lang_shop);
  275. $shop_delete_list = array();
  276. // don't allow to remove shop which have dependencies (customers / orders / ... )
  277. foreach ($this->_list as &$shop)
  278. {
  279. if (Shop::hasDependency($shop['id_shop']))
  280. $shop_delete_list[] = $shop['id_shop'];
  281. }
  282. $this->context->smarty->assign('shops_having_dependencies', $shop_delete_list);
  283. }
  284. public function renderForm()
  285. {
  286. if (!($obj = $this->loadObject(true)))
  287. return;
  288. $this->fields_form = array(
  289. 'legend' => array(
  290. 'title' => $this->l('Shop'),
  291. 'icon' => 'icon-shopping-cart'
  292. ),
  293. 'input' => array(
  294. array(
  295. 'type' => 'text',
  296. 'label' => $this->l('Shop name'),
  297. 'desc' => array($this->l('This field does not refer to the shop name visible in the Front Office.'),
  298. sprintf($this->l('Follow %sthis link%s to edit the shop name used on the Front Office.'), '<a href="'.$this->context->link->getAdminLink('AdminStores').'#store_fieldset_general">', '</a>')),
  299. 'name' => 'name',
  300. 'required' => true,
  301. )
  302. )
  303. );
  304. $display_group_list = true;
  305. if ($this->display == 'edit')
  306. {
  307. $group = new ShopGroup($obj->id_shop_group);
  308. if ($group->share_customer || $group->share_order || $group->share_stock)
  309. $display_group_list = false;
  310. }
  311. if ($display_group_list)
  312. {
  313. $options = array();
  314. foreach (ShopGroup::getShopGroups() as $group)
  315. {
  316. if ($this->display == 'edit' && ($group->share_customer || $group->share_order || $group->share_stock) && ShopGroup::hasDependency($group->id))
  317. continue;
  318. $options[] = array(
  319. 'id_shop_group' => $group->id,
  320. 'name' => $group->name,
  321. );
  322. }
  323. if ($this->display == 'add')
  324. $group_desc = $this->l('Warning: You won\'t be able to change the group of this shop if this shop belongs to a group with one of these options activated: Share Customers, Share Quantities or Share Orders.');
  325. else
  326. $group_desc = $this->l('You can only move your shop to a shop group with all "share" options disabled -- or to a shop group with no customers/orders.');
  327. $this->fields_form['input'][] = array(
  328. 'type' => 'select',
  329. 'label' => $this->l('Shop group'),
  330. 'desc' => $group_desc,
  331. 'name' => 'id_shop_group',
  332. 'options' => array(
  333. 'query' => $options,
  334. 'id' => 'id_shop_group',
  335. 'name' => 'name',
  336. ),
  337. );
  338. }
  339. else
  340. {
  341. $this->fields_form['input'][] = array(
  342. 'type' => 'hidden',
  343. 'name' => 'id_shop_group',
  344. 'default' => $group->name
  345. );
  346. $this->fields_form['input'][] = array(
  347. 'type' => 'textShopGroup',
  348. 'label' => $this->l('Shop group'),
  349. 'desc' => $this->l('You can\'t edit the shop group because the current shop belongs to a group with the "share" option enabled.'),
  350. 'name' => 'id_shop_group',
  351. 'value' => $group->name
  352. );
  353. }
  354. $categories = Category::getRootCategories($this->context->language->id);
  355. $this->fields_form['input'][] = array(
  356. 'type' => 'select',
  357. 'label' => $this->l('Category root'),
  358. 'desc' => sprintf($this->l('This is the root category of the store that you\'ve created. To define a new root category for your store, %splease click here%s.'), '<a href="'.$this->context->link->getAdminLink('AdminCategories').'&addcategoryroot" target="_blank">', '</a>'),
  359. 'name' => 'id_category',
  360. 'options' => array(
  361. 'query' => $categories,
  362. 'id' => 'id_category',
  363. 'name' => 'name'
  364. )
  365. );
  366. if (Tools::isSubmit('id_shop'))
  367. {
  368. $shop = new Shop((int)Tools::getValue('id_shop'));
  369. $id_root = $shop->id_category;
  370. }
  371. else
  372. $id_root = $categories[0]['id_category'];
  373. $id_shop = (int)Tools::getValue('id_shop');
  374. self::$currentIndex = self::$currentIndex.'&id_shop_group='.(int)(Tools::getValue('id_shop_group') ?
  375. Tools::getValue('id_shop_group') : (isset($obj->id_shop_group) ? $obj->id_shop_group : Shop::getContextShopGroupID()));
  376. $shop = new Shop($id_shop);
  377. $selected_cat = Shop::getCategories($id_shop);
  378. if (empty($selected_cat))
  379. {
  380. // get first category root and preselect all these children
  381. $root_categories = Category::getRootCategories();
  382. $root_category = new Category($root_categories[0]['id_category']);
  383. $children = $root_category->getAllChildren($this->context->language->id);
  384. $selected_cat[] = $root_categories[0]['id_category'];
  385. foreach ($children as $child)
  386. $selected_cat[] = $child->id;
  387. }
  388. if (Shop::getContext() == Shop::CONTEXT_SHOP && Tools::isSubmit('id_shop'))
  389. $root_category = new Category($shop->id_category);
  390. else
  391. $root_category = new Category($id_root);
  392. $this->fields_form['input'][] = array(
  393. 'type' => 'categories',
  394. 'name' => 'categoryBox',
  395. 'label' => $this->l('Associated categories'),
  396. 'tree' => array(
  397. 'id' => 'categories-tree',
  398. 'selected_categories' => $selected_cat,
  399. 'root_category' => $root_category->id,
  400. 'use_search' => true,
  401. 'use_checkbox' => true
  402. ),
  403. 'desc' => $this->l('By selecting associated categories, you are choosing to share the categories between shops. Once associated between shops, any alteration of this category will impact every shop.')
  404. );
  405. /*$this->fields_form['input'][] = array(
  406. 'type' => 'switch',
  407. 'label' => $this->l('Enabled'),
  408. 'name' => 'active',
  409. 'required' => true,
  410. 'is_bool' => true,
  411. 'values' => array(
  412. array(
  413. 'id' => 'active_on',
  414. 'value' => 1
  415. ),
  416. array(
  417. 'id' => 'active_off',
  418. 'value' => 0
  419. )
  420. ),
  421. 'desc' => $this->l('Enable or disable your store?')
  422. );*/
  423. $themes = Theme::getThemes();
  424. if (!isset($obj->id_theme))
  425. foreach ($themes as $theme)
  426. if (isset($theme->id))
  427. {
  428. $id_theme = $theme->id;
  429. break;
  430. }
  431. $this->fields_form['input'][] = array(
  432. 'type' => 'theme',
  433. 'label' => $this->l('Theme'),
  434. 'name' => 'theme',
  435. 'values' => $themes
  436. );
  437. $this->fields_form['submit'] = array(
  438. 'title' => $this->l('Save'),
  439. );
  440. if (Shop::getTotalShops() > 1 && $obj->id)
  441. $disabled = array('active' => false);
  442. else
  443. $disabled = false;
  444. $import_data = array(
  445. 'carrier' => $this->l('Carriers'),
  446. 'cms' => $this->l('CMS pages'),
  447. 'contact' => $this->l('Contact information'),
  448. 'country' => $this->l('Countries'),
  449. 'currency' => $this->l('Currencies'),
  450. 'discount' => $this->l('Discount prices'),
  451. 'employee' => $this->l('Employees'),
  452. 'image' => $this->l('Images'),
  453. 'lang' => $this->l('Languages'),
  454. 'manufacturer' => $this->l('Manufacturers'),
  455. 'module' => $this->l('Modules'),
  456. 'hook_module' => $this->l('Module hooks'),
  457. 'meta_lang' => $this->l('Meta information'),
  458. 'product' => $this->l('Products'),
  459. 'product_attribute' => $this->l('Product combinations'),
  460. 'scene' => $this->l('Scenes'),
  461. 'stock_available' => $this->l('Available quantities for sale'),
  462. 'store' => $this->l('Stores'),
  463. 'warehouse' => $this->l('Warehouses'),
  464. 'webservice_account' => $this->l('Webservice accounts'),
  465. 'attribute_group' => $this->l('Attribute groups'),
  466. 'feature' => $this->l('Features'),
  467. 'group' => $this->l('Customer groups'),
  468. 'tax_rules_group' => $this->l('Tax rules groups'),
  469. 'supplier' => $this->l('Suppliers'),
  470. 'referrer' => $this->l('Referrers/affiliates'),
  471. 'zone' => $this->l('Zones'),
  472. 'cart_rule' => $this->l('Cart rules'),
  473. );
  474. // Hook for duplication of shop data
  475. $modules_list = Hook::getHookModuleExecList('actionShopDataDuplication');
  476. if (is_array($modules_list) && count($modules_list) > 0)
  477. foreach ($modules_list as $m)
  478. $import_data['Module'.ucfirst($m['module'])] = Module::getModuleName($m['module']);
  479. asort($import_data);
  480. if (!$this->object->id)
  481. $this->fields_import_form = array(
  482. 'radio' => array(
  483. 'type' => 'radio',
  484. 'label' => $this->l('Import data'),
  485. 'name' => 'useImportData',
  486. 'value' => 1
  487. ),
  488. 'select' => array(
  489. 'type' => 'select',
  490. 'name' => 'importFromShop',
  491. 'label' => $this->l('Choose the source shop'),
  492. 'options' => array(
  493. 'query' => Shop::getShops(false),
  494. 'name' => 'name'
  495. )
  496. ),
  497. 'allcheckbox' => array(
  498. 'type' => 'checkbox',
  499. 'label' => $this->l('Choose data to import'),
  500. 'values' => $import_data
  501. ),
  502. 'desc' => $this->l('Use this option to associate data (products, modules, etc.) the same way for each selected shop.')
  503. );
  504. $this->fields_value = array(
  505. 'id_shop_group' => (Tools::getValue('id_shop_group') ? Tools::getValue('id_shop_group') :
  506. (isset($obj->id_shop_group)) ? $obj->id_shop_group : Shop::getContextShopGroupID()),
  507. 'id_category' => (Tools::getValue('id_category') ? Tools::getValue('id_category') :
  508. (isset($obj->id_category)) ? $obj->id_category : (int)Configuration::get('PS_HOME_CATEGORY')),
  509. 'id_theme_checked' => (isset($obj->id_theme) ? $obj->id_theme : $id_theme)
  510. );
  511. $ids_category = array();
  512. $shops = Shop::getShops(false);
  513. foreach ($shops as $shop)
  514. $ids_category[$shop['id_shop']] = $shop['id_category'];
  515. $this->tpl_form_vars = array(
  516. 'disabled' => $disabled,
  517. 'checked' => (Tools::getValue('addshop') !== false) ? true : false,
  518. 'defaultShop' => (int)Configuration::get('PS_SHOP_DEFAULT'),
  519. 'ids_category' => $ids_category,
  520. );
  521. if (isset($this->fields_import_form))
  522. $this->tpl_form_vars = array_merge($this->tpl_form_vars, array('form_import' => $this->fields_import_form));
  523. return parent::renderForm();
  524. }
  525. /**
  526. * Object creation
  527. */
  528. public function processAdd()
  529. {
  530. if (!Tools::getValue('categoryBox') || !in_array(Tools::getValue('id_category'), Tools::getValue('categoryBox')))
  531. $this->errors[] = $this->l('You need to select at least the root category.');
  532. if (Tools::isSubmit('id_category_default'))
  533. $_POST['id_category'] = (int)Tools::getValue('id_category_default');
  534. /* Checking fields validity */
  535. $this->validateRules();
  536. if (!count($this->errors))
  537. {
  538. $object = new $this->className();
  539. $this->copyFromPost($object, $this->table);
  540. $this->beforeAdd($object);
  541. if (!$object->add())
  542. {
  543. $this->errors[] = Tools::displayError('An error occurred while creating an object.').
  544. ' <b>'.$this->table.' ('.Db::getInstance()->getMsgError().')</b>';
  545. }
  546. /* voluntary do affectation here */
  547. else if (($_POST[$this->identifier] = $object->id) && $this->postImage($object->id) && !count($this->errors) && $this->_redirect)
  548. {
  549. $parent_id = (int)Tools::getValue('id_parent', 1);
  550. $this->afterAdd($object);
  551. $this->updateAssoShop($object->id);
  552. // Save and stay on same form
  553. if (Tools::isSubmit('submitAdd'.$this->table.'AndStay'))
  554. $this->redirect_after = self::$currentIndex.'&'.$this->identifier.'='.$object->id.'&conf=3&update'.$this->table.'&token='.$this->token;
  555. // Save and back to parent
  556. if (Tools::isSubmit('submitAdd'.$this->table.'AndBackToParent'))
  557. $this->redirect_after = self::$currentIndex.'&'.$this->identifier.'='.$parent_id.'&conf=3&token='.$this->token;
  558. // Default behavior (save and back)
  559. if (empty($this->redirect_after))
  560. $this->redirect_after = self::$currentIndex.($parent_id ? '&'.$this->identifier.'='.$object->id : '').'&conf=3&token='.$this->token;
  561. }
  562. }
  563. $this->errors = array_unique($this->errors);
  564. if (count($this->errors) > 0)
  565. {
  566. $this->display = 'add';
  567. return;
  568. }
  569. $object->associateSuperAdmins();
  570. $categories = Tools::getValue('categoryBox');
  571. array_unshift($categories, Configuration::get('PS_ROOT_CATEGORY'));
  572. Category::updateFromShop($categories, $object->id);
  573. Search::indexation(true);
  574. return $object;
  575. }
  576. public function initCategoriesAssociation($id_root = null)
  577. {
  578. if (is_null($id_root))
  579. $id_root = Configuration::get('PS_ROOT_CATEGORY');
  580. $id_shop = (int)Tools::getValue('id_shop');
  581. $shop = new Shop($id_shop);
  582. $selected_cat = Shop::getCategories($id_shop);
  583. if (empty($selected_cat))
  584. {
  585. // get first category root and preselect all these children
  586. $root_categories = Category::getRootCategories();
  587. $root_category = new Category($root_categories[0]['id_category']);
  588. $children = $root_category->getAllChildren($this->context->language->id);
  589. $selected_cat[] = $root_categories[0]['id_category'];
  590. foreach ($children as $child)
  591. $selected_cat[] = $child->id;
  592. }
  593. if (Shop::getContext() == Shop::CONTEXT_SHOP && Tools::isSubmit('id_shop'))
  594. $root_category = new Category($shop->id_category);
  595. else
  596. $root_category = new Category($id_root);
  597. $root_category = array('id_category' => $root_category->id, 'name' => $root_category->name[$this->context->language->id]);
  598. $helper = new Helper();
  599. return $helper->renderCategoryTree($root_category, $selected_cat, 'categoryBox', false, true);
  600. }
  601. public function ajaxProcessTree()
  602. {
  603. $tree = array();
  604. $sql = 'SELECT g.id_shop_group, g.name as group_name, s.id_shop, s.name as shop_name, u.id_shop_url, u.domain, u.physical_uri, u.virtual_uri
  605. FROM '._DB_PREFIX_.'shop_group g
  606. LEFT JOIN '._DB_PREFIX_.'shop s ON g.id_shop_group = s.id_shop_group
  607. LEFT JOIN '._DB_PREFIX_.'shop_url u ON u.id_shop = s.id_shop
  608. ORDER BY g.name, s.name, u.domain';
  609. $results = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);
  610. foreach ($results as $row)
  611. {
  612. $id_shop_group = $row['id_shop_group'];
  613. $id_shop = $row['id_shop'];
  614. $id_shop_url = $row['id_shop_url'];
  615. // Group list
  616. if (!isset($tree[$id_shop_group]))
  617. $tree[$id_shop_group] = array(
  618. 'data' => array(
  619. 'title' => '<b>'.$this->l('Group').'</b> '.$row['group_name'],
  620. 'icon' => 'themes/'.$this->context->employee->bo_theme.'/img/tree-multishop-groups.png',
  621. 'attr' => array(
  622. 'href' => $this->context->link->getAdminLink('AdminShop').'&id_shop_group='.$id_shop_group,
  623. 'title' => sprintf($this->l('Click here to display the shops in the %s shop group', 'AdminShop', false, false), $row['group_name']),
  624. ),
  625. ),
  626. 'attr' => array(
  627. 'id' => 'tree-group-'.$id_shop_group,
  628. ),
  629. 'children' => array(),
  630. );
  631. // Shop list
  632. if (!$id_shop)
  633. continue;
  634. if (!isset($tree[$id_shop_group]['children'][$id_shop]))
  635. $tree[$id_shop_group]['children'][$id_shop] = array(
  636. 'data' => array(
  637. 'title' => $row['shop_name'],
  638. 'icon' => 'themes/'.$this->context->employee->bo_theme.'/img/tree-multishop-shop.png',
  639. 'attr' => array(
  640. 'href' => $this->context->link->getAdminLink('AdminShopUrl').'&id_shop='.$id_shop,
  641. 'title' => sprintf($this->l('Click here to display the URLs of the %s shop', 'AdminShop', false, false), $row['shop_name']),
  642. )
  643. ),
  644. 'attr' => array(
  645. 'id' => 'tree-shop-'.$id_shop,
  646. ),
  647. 'children' => array(),
  648. );
  649. // Url list
  650. if (!$id_shop_url)
  651. continue;
  652. if (!isset($tree[$id_shop_group]['children'][$id_shop]['children'][$id_shop_url]))
  653. {
  654. $url = $row['domain'].$row['physical_uri'].$row['virtual_uri'];
  655. if (strlen($url) > 23)
  656. $url = substr($url, 0, 23).'...';
  657. $tree[$id_shop_group]['children'][$id_shop]['children'][$id_shop_url] = array(
  658. 'data' => array(
  659. 'title' => $url,
  660. 'icon' => 'themes/'.$this->context->employee->bo_theme.'/img/tree-multishop-url.png',
  661. 'attr' => array(
  662. 'href' => $this->context->link->getAdminLink('AdminShopUrl').'&updateshop_url&id_shop_url='.$id_shop_url,
  663. 'title' => $row['domain'].$row['physical_uri'].$row['virtual_uri'],
  664. )
  665. ),
  666. 'attr' => array(
  667. 'id' => 'tree-url-'.$id_shop_url,
  668. ),
  669. );
  670. }
  671. }
  672. // jstree need to have children as array and not object, so we use sort to get clean keys
  673. // DO NOT REMOVE this code, even if it seems really strange ;)
  674. sort($tree);
  675. foreach ($tree as &$groups)
  676. {
  677. sort($groups['children']);
  678. foreach ($groups['children'] as &$shops)
  679. sort($shops['children']);
  680. }
  681. $tree = array(array(
  682. 'data' => array(
  683. 'title' => '<b>'.$this->l('Shop groups list').'</b>',
  684. 'icon' => 'themes/'.$this->context->employee->bo_theme.'/img/tree-multishop-root.png',
  685. 'attr' => array(
  686. 'href' => $this->context->link->getAdminLink('AdminShopGroup'),
  687. 'title' => $this->l('Click here to display the list of shop groups', 'AdminShop', false, false),
  688. )
  689. ),
  690. 'attr' => array(
  691. 'id' => 'tree-root',
  692. ),
  693. 'state' => 'open',
  694. 'children' => $tree,
  695. ));
  696. die(Tools::jsonEncode($tree));
  697. }
  698. }