PageRenderTime 53ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/controllers/admin/AdminTabsController.php

https://gitlab.com/staging06/myproject
PHP | 377 lines | 302 code | 31 blank | 44 comment | 42 complexity | 6d874af932c4fe98d1985a9aa13d65ff 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 Tab $object
  28. */
  29. class AdminTabsControllerCore extends AdminController
  30. {
  31. protected $position_identifier = 'id_tab';
  32. public function __construct()
  33. {
  34. $this->bootstrap = true;
  35. $this->context = Context::getContext();
  36. $this->multishop_context = Shop::CONTEXT_ALL;
  37. $this->table = 'tab';
  38. $this->list_id = 'tab';
  39. $this->className = 'Tab';
  40. $this->lang = true;
  41. $this->fieldImageSettings = array(
  42. 'name' => 'icon',
  43. 'dir' => 't'
  44. );
  45. $this->imageType = 'gif';
  46. $this->bulk_actions = array(
  47. 'delete' => array(
  48. 'text' => $this->l('Delete selected'),
  49. 'confirm' => $this->l('Delete selected items?'),
  50. 'icon' => 'icon-trash'
  51. )
  52. );
  53. $this->fields_list = array(
  54. 'id_tab' => array(
  55. 'title' => $this->l('ID'),
  56. 'align' => 'center',
  57. 'class' => 'fixed-width-xs'
  58. ),
  59. 'name' => array(
  60. 'title' => $this->l('Name')
  61. ),
  62. 'class_name' => array(
  63. 'title' => $this->l('Class')
  64. ),
  65. 'module' => array(
  66. 'title' => $this->l('Module')
  67. ),
  68. 'active' => array(
  69. 'title' => $this->l('Enabled'),
  70. 'align' => 'center',
  71. 'active' => 'status',
  72. 'type' => 'bool',
  73. 'orderby' => false
  74. ),
  75. 'position' => array(
  76. 'title' => $this->l('Position'),
  77. 'filter_key' => 'a!position',
  78. 'position' => 'position',
  79. 'align' => 'center',
  80. 'class' => 'fixed-width-md'
  81. )
  82. );
  83. parent::__construct();
  84. }
  85. public function initPageHeaderToolbar()
  86. {
  87. $this->page_header_toolbar_title = $this->l('Menus');
  88. if ($this->display == 'details') {
  89. $this->page_header_toolbar_btn['back_to_list'] = array(
  90. 'href' => Context::getContext()->link->getAdminLink('AdminTabs'),
  91. 'desc' => $this->l('Back to list', null, null, false),
  92. 'icon' => 'process-icon-back'
  93. );
  94. } elseif (empty($this->display)) {
  95. $this->page_header_toolbar_btn['new_menu'] = array(
  96. 'href' => self::$currentIndex.'&addtab&token='.$this->token,
  97. 'desc' => $this->l('Add new menu', null, null, false),
  98. 'icon' => 'process-icon-new'
  99. );
  100. }
  101. parent::initPageHeaderToolbar();
  102. }
  103. /**
  104. * AdminController::renderForm() override
  105. * @see AdminController::renderForm()
  106. */
  107. public function renderForm()
  108. {
  109. $tabs = Tab::getTabs($this->context->language->id, 0);
  110. // If editing, we clean itself
  111. if (Tools::isSubmit('id_tab')) {
  112. foreach ($tabs as $key => $tab) {
  113. if ($tab['id_tab'] == Tools::getValue('id_tab')) {
  114. unset($tabs[$key]);
  115. }
  116. }
  117. }
  118. // added category "Home" in var $tabs
  119. $tab_zero = array(
  120. 'id_tab' => 0,
  121. 'name' => $this->l('Home')
  122. );
  123. array_unshift($tabs, $tab_zero);
  124. $this->fields_form = array(
  125. 'legend' => array(
  126. 'title' => $this->l('Menus'),
  127. 'icon' => 'icon-list-ul'
  128. ),
  129. 'input' => array(
  130. array(
  131. 'type' => 'hidden',
  132. 'name' => 'position',
  133. 'required' => false
  134. ),
  135. array(
  136. 'type' => 'text',
  137. 'label' => $this->l('Name'),
  138. 'name' => 'name',
  139. 'lang' => true,
  140. 'required' => true,
  141. 'hint' => $this->l('Invalid characters:').' &lt;&gt;;=#{}'
  142. ),
  143. array(
  144. 'type' => 'text',
  145. 'label' => $this->l('Class'),
  146. 'name' => 'class_name',
  147. 'required' => true
  148. ),
  149. array(
  150. 'type' => 'text',
  151. 'label' => $this->l('Module'),
  152. 'name' => 'module'
  153. ),
  154. array(
  155. 'type' => 'switch',
  156. 'label' => $this->l('Status'),
  157. 'name' => 'active',
  158. 'required' => false,
  159. 'is_bool' => true,
  160. 'values' => array(
  161. array(
  162. 'id' => 'active_on',
  163. 'value' => 1,
  164. 'label' => $this->l('Enabled')
  165. ),
  166. array(
  167. 'id' => 'active_off',
  168. 'value' => 0,
  169. 'label' => $this->l('Disabled')
  170. )
  171. ),
  172. 'hint' => $this->l('Show or hide menu.')
  173. ),
  174. ),
  175. 'submit' => array(
  176. 'title' => $this->l('Save'),
  177. )
  178. );
  179. $display_parent = true;
  180. if (Validate::isLoadedObject($this->object) && !class_exists($this->object->class_name.'Controller')) {
  181. $display_parent = false;
  182. }
  183. if ($display_parent) {
  184. $this->fields_form['input'][] = array(
  185. 'type' => 'select',
  186. 'label' => $this->l('Parent'),
  187. 'name' => 'id_parent',
  188. 'options' => array(
  189. 'query' => $tabs,
  190. 'id' => 'id_tab',
  191. 'name' => 'name'
  192. )
  193. );
  194. }
  195. return parent::renderForm();
  196. }
  197. /**
  198. * AdminController::renderList() override
  199. * @see AdminController::renderList()
  200. */
  201. public function renderList()
  202. {
  203. $this->addRowAction('edit');
  204. $this->addRowAction('details');
  205. $this->addRowAction('delete');
  206. $this->_where = 'AND a.`id_parent` = 0';
  207. $this->_orderBy = 'position';
  208. return parent::renderList();
  209. }
  210. public function initProcess()
  211. {
  212. if (Tools::getIsset('details'.$this->table)) {
  213. $this->list_id = 'details';
  214. if (isset($_POST['submitReset'.$this->list_id])) {
  215. $this->processResetFilters();
  216. }
  217. } else {
  218. $this->list_id = 'tab';
  219. }
  220. return parent::initProcess();
  221. }
  222. public function renderDetails()
  223. {
  224. if (($id = Tools::getValue('id_tab'))) {
  225. $this->lang = false;
  226. $this->list_id = 'details';
  227. $this->addRowAction('edit');
  228. $this->addRowAction('delete');
  229. $this->toolbar_btn = array();
  230. /** @var Tab $tab */
  231. $tab = $this->loadObject($id);
  232. $this->toolbar_title = $tab->name[$this->context->employee->id_lang];
  233. $this->_select = 'b.*';
  234. $this->_join = 'LEFT JOIN `'._DB_PREFIX_.'tab_lang` b ON (b.`id_tab` = a.`id_tab` AND b.`id_lang` = '.$this->context->language->id.')';
  235. $this->_where = 'AND a.`id_parent` = '.(int)$id;
  236. $this->_orderBy = 'position';
  237. $this->_use_found_rows = false;
  238. self::$currentIndex = self::$currentIndex.'&details'.$this->table;
  239. $this->processFilter();
  240. return parent::renderList();
  241. }
  242. }
  243. public function postProcess()
  244. {
  245. /* PrestaShop demo mode */
  246. if (_PS_MODE_DEMO_) {
  247. $this->errors[] = Tools::displayError('This functionality has been disabled.');
  248. return;
  249. }
  250. /* PrestaShop demo mode*/
  251. if (($id_tab = (int)Tools::getValue('id_tab')) && ($direction = Tools::getValue('move')) && Validate::isLoadedObject($tab = new Tab($id_tab))) {
  252. if ($tab->move($direction)) {
  253. Tools::redirectAdmin(self::$currentIndex.'&token='.$this->token);
  254. }
  255. } elseif (Tools::getValue('position') && !Tools::isSubmit('submitAdd'.$this->table)) {
  256. if ($this->tabAccess['edit'] !== '1') {
  257. $this->errors[] = Tools::displayError('You do not have permission to edit this.');
  258. } elseif (!Validate::isLoadedObject($object = new Tab((int)Tools::getValue($this->identifier)))) {
  259. $this->errors[] = Tools::displayError('An error occurred while updating the status for an object.').
  260. ' <b>'.$this->table.'</b> '.Tools::displayError('(cannot load object)');
  261. }
  262. if (!$object->updatePosition((int)Tools::getValue('way'), (int)Tools::getValue('position'))) {
  263. $this->errors[] = Tools::displayError('Failed to update the position.');
  264. } else {
  265. Tools::redirectAdmin(self::$currentIndex.'&conf=5&token='.Tools::getAdminTokenLite('AdminTabs'));
  266. }
  267. } elseif (Tools::isSubmit('submitAdd'.$this->table) && Tools::getValue('id_tab') === Tools::getValue('id_parent')) {
  268. $this->errors[] = Tools::displayError('You can\'t put this menu inside itself. ');
  269. } elseif (Tools::isSubmit('submitAdd'.$this->table) && $id_parent = (int)Tools::getValue('id_parent')) {
  270. $this->redirect_after = self::$currentIndex.'&id_'.$this->table.'='.$id_parent.'&details'.$this->table.'&conf=4&token='.$this->token;
  271. } elseif (isset($_GET['details'.$this->table]) && is_array($this->bulk_actions)) {
  272. $submit_bulk_actions = array_merge(array(
  273. 'enableSelection' => array(
  274. 'text' => $this->l('Enable selection'),
  275. 'icon' => 'icon-power-off text-success'
  276. ),
  277. 'disableSelection' => array(
  278. 'text' => $this->l('Disable selection'),
  279. 'icon' => 'icon-power-off text-danger'
  280. )
  281. ), $this->bulk_actions);
  282. foreach ($submit_bulk_actions as $bulk_action => $params) {
  283. if (Tools::isSubmit('submitBulk'.$bulk_action.$this->table) || Tools::isSubmit('submitBulk'.$bulk_action)) {
  284. if ($this->tabAccess['edit'] === '1') {
  285. $this->action = 'bulk'.$bulk_action;
  286. $this->boxes = Tools::getValue($this->list_id.'Box');
  287. } else {
  288. $this->errors[] = Tools::displayError('You do not have permission to edit this.');
  289. }
  290. break;
  291. } elseif (Tools::isSubmit('submitBulk')) {
  292. if ($this->tabAccess['edit'] === '1') {
  293. $this->action = 'bulk'.Tools::getValue('select_submitBulk');
  294. $this->boxes = Tools::getValue($this->list_id.'Box');
  295. } else {
  296. $this->errors[] = Tools::displayError('You do not have permission to edit this.');
  297. }
  298. break;
  299. }
  300. }
  301. } else {
  302. // Temporary add the position depend of the selection of the parent category
  303. if (!Tools::isSubmit('id_tab')) { // @todo Review
  304. $_POST['position'] = Tab::getNbTabs(Tools::getValue('id_parent'));
  305. }
  306. }
  307. if (!count($this->errors)) {
  308. parent::postProcess();
  309. }
  310. }
  311. protected function afterImageUpload()
  312. {
  313. /** @var Tab $obj */
  314. if (!($obj = $this->loadObject(true))) {
  315. return;
  316. }
  317. @rename(_PS_IMG_DIR_.'t/'.$obj->id.'.gif', _PS_IMG_DIR_.'t/'.$obj->class_name.'.gif');
  318. }
  319. public function ajaxProcessUpdatePositions()
  320. {
  321. $way = (int)(Tools::getValue('way'));
  322. $id_tab = (int)(Tools::getValue('id'));
  323. $positions = Tools::getValue('tab');
  324. // when changing positions in a tab sub-list, the first array value is empty and needs to be removed
  325. if (!$positions[0]) {
  326. unset($positions[0]);
  327. // reset indexation from 0
  328. $positions = array_merge($positions);
  329. }
  330. foreach ($positions as $position => $value) {
  331. $pos = explode('_', $value);
  332. if (isset($pos[2]) && (int)$pos[2] === $id_tab) {
  333. if ($tab = new Tab((int)$pos[2])) {
  334. if (isset($position) && $tab->updatePosition($way, $position)) {
  335. echo 'ok position '.(int)$position.' for tab '.(int)$pos[1].'\r\n';
  336. } else {
  337. echo '{"hasError" : true, "errors" : "Can not update tab '.(int)$id_tab.' to position '.(int)$position.' "}';
  338. }
  339. } else {
  340. echo '{"hasError" : true, "errors" : "This tab ('.(int)$id_tab.') can t be loaded"}';
  341. }
  342. break;
  343. }
  344. }
  345. }
  346. }