PageRenderTime 44ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/prestashop/_/controllers/admin/AdminShopGroupController.php

https://gitlab.com/A.Julien/sendstockbymail-module-prestashop
PHP | 371 lines | 296 code | 35 blank | 40 comment | 31 complexity | 5b3d21061b31c499416693fbba772203 MD5 | raw file
  1. <?php
  2. /*
  3. * 2007-2016 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-2016 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 ShopGroup $object
  28. */
  29. class AdminShopGroupControllerCore extends AdminController
  30. {
  31. public function __construct()
  32. {
  33. $this->bootstrap = true;
  34. $this->table = 'shop_group';
  35. $this->className = 'ShopGroup';
  36. $this->lang = false;
  37. $this->multishop_context = Shop::CONTEXT_ALL;
  38. $this->addRowAction('edit');
  39. $this->addRowAction('delete');
  40. $this->context = Context::getContext();
  41. if (!Tools::getValue('realedit')) {
  42. $this->deleted = false;
  43. }
  44. $this->show_toolbar = false;
  45. $this->fields_list = array(
  46. 'id_shop_group' => array(
  47. 'title' => $this->l('ID'),
  48. 'align' => 'center',
  49. 'class' => 'fixed-width-xs',
  50. ),
  51. 'name' => array(
  52. 'title' => $this->l('Shop group'),
  53. 'width' => 'auto',
  54. 'filter_key' => 'a!name',
  55. ),
  56. /*'active' => array(
  57. 'title' => $this->l('Enabled'),
  58. 'align' => 'center',
  59. 'active' => 'status',
  60. 'type' => 'bool',
  61. 'orderby' => false,
  62. 'filter_key' => 'active',
  63. 'width' => 50,
  64. ),*/
  65. );
  66. $this->fields_options = array(
  67. 'general' => array(
  68. 'title' => $this->l('Multistore options'),
  69. 'fields' => array(
  70. 'PS_SHOP_DEFAULT' => array(
  71. 'title' => $this->l('Default shop'),
  72. 'cast' => 'intval',
  73. 'type' => 'select',
  74. 'identifier' => 'id_shop',
  75. 'list' => Shop::getShops(),
  76. 'visibility' => Shop::CONTEXT_ALL
  77. )
  78. ),
  79. 'submit' => array('title' => $this->l('Save'))
  80. )
  81. );
  82. parent::__construct();
  83. }
  84. public function viewAccess($disable = false)
  85. {
  86. return Configuration::get('PS_MULTISHOP_FEATURE_ACTIVE');
  87. }
  88. public function initContent()
  89. {
  90. parent::initContent();
  91. $this->addJqueryPlugin('cooki-plugin');
  92. $data = Shop::getTree();
  93. foreach ($data as $key_group => &$group) {
  94. foreach ($group['shops'] as $key_shop => &$shop) {
  95. $current_shop = new Shop($shop['id_shop']);
  96. $urls = $current_shop->getUrls();
  97. foreach ($urls as $key_url => &$url) {
  98. $title = $url['domain'].$url['physical_uri'].$url['virtual_uri'];
  99. if (strlen($title) > 23) {
  100. $title = substr($title, 0, 23).'...';
  101. }
  102. $url['name'] = $title;
  103. $shop['urls'][$url['id_shop_url']] = $url;
  104. }
  105. }
  106. }
  107. $shops_tree = new HelperTreeShops('shops-tree', $this->l('Multistore tree'));
  108. $shops_tree->setNodeFolderTemplate('shop_tree_node_folder.tpl')->setNodeItemTemplate('shop_tree_node_item.tpl')
  109. ->setHeaderTemplate('shop_tree_header.tpl')->setActions(array(
  110. new TreeToolbarLink(
  111. 'Collapse All',
  112. '#',
  113. '$(\'#'.$shops_tree->getId().'\').tree(\'collapseAll\'); return false;',
  114. 'icon-collapse-alt'),
  115. new TreeToolbarLink(
  116. 'Expand All',
  117. '#',
  118. '$(\'#'.$shops_tree->getId().'\').tree(\'expandAll\'); return false;',
  119. 'icon-expand-alt')
  120. ))
  121. ->setAttribute('url_shop_group', $this->context->link->getAdminLink('AdminShopGroup'))
  122. ->setAttribute('url_shop', $this->context->link->getAdminLink('AdminShop'))
  123. ->setAttribute('url_shop_url', $this->context->link->getAdminLink('AdminShopUrl'))
  124. ->setData($data);
  125. $shops_tree = $shops_tree->render(null, false, false);
  126. if ($this->display == 'edit') {
  127. $this->toolbar_title[] = $this->object->name;
  128. }
  129. $this->context->smarty->assign(array(
  130. 'toolbar_scroll' => 1,
  131. 'toolbar_btn' => $this->toolbar_btn,
  132. 'title' => $this->toolbar_title,
  133. 'shops_tree' => $shops_tree
  134. ));
  135. }
  136. public function initPageHeaderToolbar()
  137. {
  138. parent::initPageHeaderToolbar();
  139. if ($this->display != 'add' && $this->display != 'edit') {
  140. $this->page_header_toolbar_btn['new'] = array(
  141. 'desc' => $this->l('Add a new shop group'),
  142. 'href' => self::$currentIndex.'&add'.$this->table.'&token='.$this->token
  143. );
  144. $this->page_header_toolbar_btn['new_2'] = array(
  145. 'desc' => $this->l('Add a new shop'),
  146. 'href' => $this->context->link->getAdminLink('AdminShop').'&addshop',
  147. 'imgclass' => 'new_2',
  148. 'icon' => 'process-icon-new'
  149. );
  150. }
  151. }
  152. public function initToolbar()
  153. {
  154. parent::initToolbar();
  155. if ($this->display != 'add' && $this->display != 'edit') {
  156. $this->toolbar_btn['new'] = array(
  157. 'desc' => $this->l('Add a new shop group'),
  158. 'href' => self::$currentIndex.'&add'.$this->table.'&token='.$this->token,
  159. );
  160. }
  161. }
  162. public function renderForm()
  163. {
  164. $this->fields_form = array(
  165. 'legend' => array(
  166. 'title' => $this->l('Shop group'),
  167. 'icon' => 'icon-shopping-cart'
  168. ),
  169. 'description' => $this->l('Warning: Enabling the "share customers" and "share orders" options is not recommended. Once activated and orders are created, you will not be able to disable these options. If you need these options, we recommend using several categories rather than several shops.'),
  170. 'input' => array(
  171. array(
  172. 'type' => 'text',
  173. 'label' => $this->l('Shop group name'),
  174. 'name' => 'name',
  175. 'required' => true
  176. ),
  177. array(
  178. 'type' => 'switch',
  179. 'label' => $this->l('Share customers'),
  180. 'name' => 'share_customer',
  181. 'required' => true,
  182. 'class' => 't',
  183. 'is_bool' => true,
  184. 'disabled' => ($this->id_object && $this->display == 'edit' && ShopGroup::hasDependency($this->id_object, 'customer')) ? true : false,
  185. 'values' => array(
  186. array(
  187. 'id' => 'share_customer_on',
  188. 'value' => 1
  189. ),
  190. array(
  191. 'id' => 'share_customer_off',
  192. 'value' => 0
  193. )
  194. ),
  195. 'desc' => $this->l('Once this option is enabled, the shops in this group will share customers. If a customer registers in any one of these shops, the account will automatically be available in the others shops of this group.').'<br/>'.$this->l('Warning: you will not be able to disable this option once you have registered customers.'),
  196. ),
  197. array(
  198. 'type' => 'switch',
  199. 'label' => $this->l('Share available quantities to sell'),
  200. 'name' => 'share_stock',
  201. 'required' => true,
  202. 'class' => 't',
  203. 'is_bool' => true,
  204. 'values' => array(
  205. array(
  206. 'id' => 'share_stock_on',
  207. 'value' => 1
  208. ),
  209. array(
  210. 'id' => 'share_stock_off',
  211. 'value' => 0
  212. )
  213. ),
  214. 'desc' => $this->l('Share available quantities between shops of this group. When changing this option, all available products quantities will be reset to 0.'),
  215. ),
  216. array(
  217. 'type' => 'switch',
  218. 'label' => $this->l('Share orders'),
  219. 'name' => 'share_order',
  220. 'required' => true,
  221. 'class' => 't',
  222. 'is_bool' => true,
  223. 'disabled' => ($this->id_object && $this->display == 'edit' && ShopGroup::hasDependency($this->id_object, 'order')) ? true : false,
  224. 'values' => array(
  225. array(
  226. 'id' => 'share_order_on',
  227. 'value' => 1
  228. ),
  229. array(
  230. 'id' => 'share_order_off',
  231. 'value' => 0
  232. )
  233. ),
  234. 'desc' => $this->l('Once this option is enabled (which is only possible if customers and available quantities are shared among shops), the customer\'s cart will be shared by all shops in this group. This way, any purchase started in one shop will be able to be completed in another shop from the same group.').'<br/>'.$this->l('Warning: You will not be able to disable this option once you\'ve started to accept orders.')
  235. ),
  236. array(
  237. 'type' => 'switch',
  238. 'label' => $this->l('Status'),
  239. 'name' => 'active',
  240. 'required' => true,
  241. 'class' => 't',
  242. 'is_bool' => true,
  243. 'values' => array(
  244. array(
  245. 'id' => 'active_on',
  246. 'value' => 1
  247. ),
  248. array(
  249. 'id' => 'active_off',
  250. 'value' => 0
  251. )
  252. ),
  253. 'desc' => $this->l('Enable or disable this shop group?')
  254. )
  255. ),
  256. 'submit' => array(
  257. 'title' => $this->l('Save'),
  258. )
  259. );
  260. if (!($obj = $this->loadObject(true))) {
  261. return;
  262. }
  263. if (Shop::getTotalShops() > 1 && $obj->id) {
  264. $disabled = array(
  265. 'share_customer' => true,
  266. 'share_stock' => true,
  267. 'share_order' => true,
  268. 'active' => false
  269. );
  270. } else {
  271. $disabled = false;
  272. }
  273. $default_shop = new Shop(Configuration::get('PS_SHOP_DEFAULT'));
  274. $this->tpl_form_vars = array(
  275. 'disabled' => $disabled,
  276. 'checked' => (Tools::getValue('addshop_group') !== false) ? true : false,
  277. 'defaultGroup' => $default_shop->id_shop_group,
  278. );
  279. $this->fields_value = array(
  280. 'active' => true
  281. );
  282. return parent::renderForm();
  283. }
  284. public function getList($id_lang, $order_by = null, $order_way = null, $start = 0, $limit = null, $id_lang_shop = false)
  285. {
  286. parent::getList($id_lang, $order_by, $order_way, $start, $limit, $id_lang_shop);
  287. $shop_group_delete_list = array();
  288. // test store authorized to remove
  289. foreach ($this->_list as $shop_group) {
  290. $shops = Shop::getShops(true, $shop_group['id_shop_group']);
  291. if (!empty($shops)) {
  292. $shop_group_delete_list[] = $shop_group['id_shop_group'];
  293. }
  294. }
  295. $this->addRowActionSkipList('delete', $shop_group_delete_list);
  296. }
  297. public function postProcess()
  298. {
  299. if (Tools::isSubmit('delete'.$this->table) || Tools::isSubmit('status') || Tools::isSubmit('status'.$this->table)) {
  300. /** @var ShopGroup $object */
  301. $object = $this->loadObject();
  302. if (ShopGroup::getTotalShopGroup() == 1) {
  303. $this->errors[] = Tools::displayError('You cannot delete or disable the last shop group.');
  304. } elseif ($object->haveShops()) {
  305. $this->errors[] = Tools::displayError('You cannot delete or disable a shop group in use.');
  306. }
  307. if (count($this->errors)) {
  308. return false;
  309. }
  310. }
  311. return parent::postProcess();
  312. }
  313. protected function afterAdd($new_shop_group)
  314. {
  315. //Reset available quantitites
  316. StockAvailable::resetProductFromStockAvailableByShopGroup($new_shop_group);
  317. }
  318. protected function afterUpdate($new_shop_group)
  319. {
  320. //Reset available quantitites
  321. StockAvailable::resetProductFromStockAvailableByShopGroup($new_shop_group);
  322. }
  323. public function renderOptions()
  324. {
  325. if ($this->fields_options && is_array($this->fields_options)) {
  326. $this->display = 'options';
  327. $this->show_toolbar = false;
  328. $helper = new HelperOptions($this);
  329. $this->setHelperDisplay($helper);
  330. $helper->id = $this->id;
  331. $helper->tpl_vars = $this->tpl_option_vars;
  332. $options = $helper->generateOptions($this->fields_options);
  333. return $options;
  334. }
  335. }
  336. }