PageRenderTime 55ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/controllers/admin/AdminShopController.php

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