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

/controllers/admin/AdminQuickAccessesController.php

https://gitlab.com/mtellezgalindo/PrestaShop
PHP | 237 lines | 194 code | 18 blank | 25 comment | 19 complexity | dfc4383ec10a8187e69e4c1a209ec0f3 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, 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 AdminQuickAccessesControllerCore extends AdminController
  27. {
  28. public function __construct()
  29. {
  30. $this->bootstrap = true;
  31. $this->table = 'quick_access';
  32. $this->className = 'QuickAccess';
  33. $this->lang = true;
  34. $this->addRowAction('edit');
  35. $this->addRowAction('delete');
  36. $this->context = Context::getContext();
  37. if (!Tools::getValue('realedit'))
  38. $this->deleted = false;
  39. $this->bulk_actions = array(
  40. 'delete' => array(
  41. 'text' => $this->l('Delete selected'),
  42. 'confirm' => $this->l('Delete selected items?'),
  43. 'icon' => 'icon-trash'
  44. )
  45. );
  46. $this->fields_list = array(
  47. 'id_quick_access' => array(
  48. 'title' => $this->l('ID'),
  49. 'align' => 'center',
  50. 'class' => 'fixed-width-xs'
  51. ),
  52. 'name' => array(
  53. 'title' => $this->l('Name')
  54. ),
  55. 'link' => array(
  56. 'title' => $this->l('Link')
  57. ),
  58. 'new_window' => array(
  59. 'title' => $this->l('New window'),
  60. 'align' => 'center',
  61. 'type' => 'bool',
  62. 'active' => 'new_window',
  63. 'class' => 'fixed-width-sm'
  64. )
  65. );
  66. $this->fields_form = array(
  67. 'legend' => array(
  68. 'title' => $this->l('Quick Access menu'),
  69. 'icon' => 'icon-align-justify'
  70. ),
  71. 'input' => array(
  72. array(
  73. 'type' => 'text',
  74. 'label' => $this->l('Name'),
  75. 'name' => 'name',
  76. 'lang' => true,
  77. 'maxlength' => 32,
  78. 'required' => true,
  79. 'hint' => $this->l('Forbidden characters:').' &lt;&gt;;=#{}'
  80. ),
  81. array(
  82. 'type' => 'text',
  83. 'label' => $this->l('URL'),
  84. 'name' => 'link',
  85. 'maxlength' => 128,
  86. 'required' => true,
  87. 'hint' => $this->l('If it\'s a URL that comes from your Back Office, you MUST remove the security token.')
  88. ),
  89. array(
  90. 'type' => 'switch',
  91. 'label' => $this->l('Open in new window'),
  92. 'name' => 'new_window',
  93. 'required' => false,
  94. 'values' => array(
  95. array(
  96. 'id' => 'new_window_on',
  97. 'value' => 1,
  98. 'label' => '<img src="../img/admin/enabled.gif" alt="'.$this->l('Enabled').'" title="'.$this->l('Enabled').'" />'
  99. ),
  100. array(
  101. 'id' => 'new_window_off',
  102. 'value' => 0,
  103. 'label' => '<img src="../img/admin/disabled.gif" alt="'.$this->l('Disabled').'" title="'.$this->l('Disabled').'" />'
  104. )
  105. )
  106. )
  107. ),
  108. 'submit' => array(
  109. 'title' => $this->l('Save'),
  110. )
  111. );
  112. parent::__construct();
  113. }
  114. public function initPageHeaderToolbar()
  115. {
  116. if (empty($this->display))
  117. $this->page_header_toolbar_btn['new_quick_access'] = array(
  118. 'href' => self::$currentIndex.'&addquick_access&token='.$this->token,
  119. 'desc' => $this->l('Add new quick access', null, null, false),
  120. 'icon' => 'process-icon-new'
  121. );
  122. parent::initPageHeaderToolbar();
  123. }
  124. public function initProcess()
  125. {
  126. if ((isset($_GET['new_window'.$this->table]) || isset($_GET['new_window'])) && Tools::getValue($this->identifier))
  127. {
  128. if ($this->tabAccess['edit'] === '1')
  129. $this->action = 'newWindow';
  130. else
  131. $this->errors[] = Tools::displayError('You do not have permission to edit this.');
  132. }
  133. parent::initProcess();
  134. }
  135. public function getQuickAccessesList()
  136. {
  137. $links = QuickAccess::getQuickAccesses($this->context->language->id);
  138. return Tools::jsonEncode(array_map(array($this, 'getLinkToken'), $links));
  139. }
  140. public function getLinkToken($item){
  141. $url = parse_url($item['link']);
  142. parse_str($url['query'], $query);
  143. $controller = $query['controller'];
  144. $item['token'] = Tools::getAdminTokenLite($controller);
  145. return $item;
  146. }
  147. public function addQuickLink()
  148. {
  149. if (!isset($this->className) || empty($this->className))
  150. return false;
  151. $this->validateRules();
  152. if (count($this->errors) <= 0)
  153. {
  154. $this->object = new $this->className();
  155. $this->copyFromPost($this->object, $this->table);
  156. $exists = Db::getInstance()->getValue('SELECT id_quick_access FROM '._DB_PREFIX_.'quick_access WHERE link = "'.pSQL($this->object->link).'"');
  157. if ($exists)
  158. return true;
  159. $this->beforeAdd($this->object);
  160. if (method_exists($this->object, 'add') && !$this->object->add())
  161. {
  162. $this->errors[] = Tools::displayError('An error occurred while creating an object.').
  163. ' <b>'.$this->table.' ('.Db::getInstance()->getMsgError().')</b>';
  164. }
  165. /* voluntary do affectation here */
  166. elseif (($_POST[$this->identifier] = $this->object->id) && $this->postImage($this->object->id) && !count($this->errors) && $this->_redirect)
  167. {
  168. PrestaShopLogger::addLog(sprintf($this->l('%s addition', 'AdminTab', false, false), $this->className), 1, null, $this->className, (int)$this->object->id, true, (int)$this->context->employee->id);
  169. $this->afterAdd($this->object);
  170. }
  171. }
  172. $this->errors = array_unique($this->errors);
  173. if (!empty($this->errors))
  174. {
  175. d($this->errors);
  176. return false;
  177. }
  178. return $this->getQuickAccessesList();
  179. }
  180. public function processDelete()
  181. {
  182. parent::processDelete();
  183. return $this->getQuickAccessesList();
  184. }
  185. public function ajaxProcessGetUrl()
  186. {
  187. if (Tools::strtolower(Tools::getValue('method')) === 'add')
  188. {
  189. $params['new_window'] = 0;
  190. $params['name_'.(int)Configuration::get('PS_LANG_DEFAULT')] = Tools::getValue('name');
  191. $params['link'] = 'index.php?'.Tools::getValue('url');
  192. $params['submitAddquick_access'] = 1;
  193. unset($_POST['name']);
  194. $_POST = array_merge($_POST, $params);
  195. die($this->addQuickLink());
  196. }
  197. elseif (Tools::strtolower(Tools::getValue('method')) === 'remove')
  198. {
  199. $params['deletequick_access'] = 1;
  200. $_POST = array_merge($_POST, $params);
  201. die($this->processDelete());
  202. }
  203. }
  204. public function processNewWindow()
  205. {
  206. if (Validate::isLoadedObject($object = $this->loadObject()))
  207. {
  208. if ($object->toggleNewWindow())
  209. $this->redirect_after = self::$currentIndex.'&conf=5&token='.$this->token;
  210. else
  211. $this->errors[] = Tools::displayError('An error occurred while updating new window property.');
  212. }
  213. else
  214. $this->errors[] = Tools::displayError('An error occurred while updating the new window property for this object.').
  215. ' <b>'.$this->table.'</b> '.
  216. Tools::displayError('(cannot load object)');
  217. return $object;
  218. }
  219. }