PageRenderTime 44ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/controllers/admin/AdminShopUrlController.php

https://gitlab.com/staging06/myproject
PHP | 564 lines | 453 code | 68 blank | 43 comment | 74 complexity | 321199d264e9e39c1bca5f48b7e720d3 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 ShopUrl $object
  28. */
  29. class AdminShopUrlControllerCore extends AdminController
  30. {
  31. public function __construct()
  32. {
  33. $this->bootstrap = true;
  34. $this->table = 'shop_url';
  35. $this->className = 'ShopUrl';
  36. $this->lang = false;
  37. $this->requiredDatabase = true;
  38. $this->multishop_context = Shop::CONTEXT_ALL;
  39. $this->bulk_actions = array();
  40. /* if $_GET['id_shop'] is transmitted, virtual url can be loaded in config.php, so we wether transmit shop_id in herfs */
  41. if ($this->id_shop = (int)Tools::getValue('shop_id')) {
  42. $_GET['id_shop'] = $this->id_shop;
  43. } else {
  44. $this->id_shop = (int)Tools::getValue('id_shop');
  45. }
  46. $this->context = Context::getContext();
  47. if (!Tools::getValue('realedit')) {
  48. $this->deleted = false;
  49. }
  50. $this->fields_list = array(
  51. 'id_shop_url' => array(
  52. 'title' => $this->l('Shop URL ID'),
  53. 'align' => 'center',
  54. 'class' => 'fixed-width-xs'
  55. ),
  56. 'shop_name' => array(
  57. 'title' => $this->l('Shop name'),
  58. 'filter_key' => 's!name'
  59. ),
  60. 'url' => array(
  61. 'title' => $this->l('URL'),
  62. 'filter_key' => 'url',
  63. 'havingFilter' => true
  64. ),
  65. 'main' => array(
  66. 'title' => $this->l('Is it the main URL?'),
  67. 'align' => 'center',
  68. 'activeVisu' => 'main',
  69. 'active' => 'main',
  70. 'type' => 'bool',
  71. 'orderby' => false,
  72. 'filter_key' => 'main',
  73. 'class' => 'fixed-width-md'
  74. ),
  75. 'active' => array(
  76. 'title' => $this->l('Enabled'),
  77. 'align' => 'center',
  78. 'active' => 'status',
  79. 'type' => 'bool',
  80. 'orderby' => false,
  81. 'filter_key' => 'active',
  82. 'class' => 'fixed-width-md'
  83. ),
  84. );
  85. parent::__construct();
  86. }
  87. public function viewAccess($disable = false)
  88. {
  89. return Configuration::get('PS_MULTISHOP_FEATURE_ACTIVE');
  90. }
  91. public function renderList()
  92. {
  93. $this->addRowActionSkipList('delete', array(1));
  94. $this->addRowAction('edit');
  95. $this->addRowAction('delete');
  96. $this->_select = 's.name AS shop_name, CONCAT(\'http://\', a.domain, a.physical_uri, a.virtual_uri) AS url';
  97. $this->_join = 'LEFT JOIN `'._DB_PREFIX_.'shop` s ON (s.id_shop = a.id_shop)';
  98. if ($id_shop = (int)Tools::getValue('id_shop')) {
  99. $this->_where = 'AND a.id_shop = '.$id_shop;
  100. }
  101. $this->_use_found_rows = false;
  102. return parent::renderList();
  103. }
  104. public function renderForm()
  105. {
  106. $update_htaccess = Tools::modRewriteActive() && ((file_exists('.htaccess') && is_writable('.htaccess')) || is_writable(dirname('.htaccess')));
  107. $this->multiple_fieldsets = true;
  108. if (!$update_htaccess) {
  109. $desc_virtual_uri = array(
  110. '<span class="warning_mod_rewrite">'.$this->l('If you want to add a virtual URL, you need to activate URL rewriting on your web server and enable Friendly URL option.').'</span>'
  111. );
  112. } else {
  113. $desc_virtual_uri = array(
  114. $this->l('You can use this option if you want to create a store with a URL that doesn\'t exist on your server (e.g. if you want your store to be available with the URL www.example.com/my-store/shoes/, you have to set shoes/ in this field, assuming that my-store/ is your Physical URL).'),
  115. '<strong>'.$this->l('URL rewriting must be activated on your server to use this feature.').'</strong>'
  116. );
  117. }
  118. $this->fields_form = array(
  119. array(
  120. 'form' => array(
  121. 'legend' => array(
  122. 'title' => $this->l('URL options'),
  123. 'icon' => 'icon-cogs'
  124. ),
  125. 'input' => array(
  126. array(
  127. 'type' => 'select',
  128. 'label' => $this->l('Shop'),
  129. 'name' => 'id_shop',
  130. 'onchange' => 'checkMainUrlInfo(this.value);',
  131. 'options' => array(
  132. 'optiongroup' => array(
  133. 'query' => Shop::getTree(),
  134. 'label' => 'name'
  135. ),
  136. 'options' => array(
  137. 'query' => 'shops',
  138. 'id' => 'id_shop',
  139. 'name' => 'name'
  140. )
  141. )
  142. ),
  143. array(
  144. 'type' => 'switch',
  145. 'label' => $this->l('Is it the main URL for this shop?'),
  146. 'name' => 'main',
  147. 'is_bool' => true,
  148. 'class' => 't',
  149. 'values' => array(
  150. array(
  151. 'id' => 'main_on',
  152. 'value' => 1
  153. ),
  154. array(
  155. 'id' => 'main_off',
  156. 'value' => 0
  157. )
  158. ),
  159. 'desc' => array(
  160. $this->l('If you set this URL as the Main URL for the selected shop, all URLs set to this shop will be redirected to this URL (you can only have one Main URL per shop).'),
  161. array(
  162. 'text' => $this->l('Since the selected shop has no main URL, you have to set this URL as the Main URL.'),
  163. 'id' => 'mainUrlInfo'
  164. ),
  165. array(
  166. 'text' => $this->l('The selected shop already has a Main URL. Therefore, if you set this one as the Main URL, the older Main URL will be set as a regular URL.'),
  167. 'id' => 'mainUrlInfoExplain'
  168. )
  169. )
  170. ),
  171. array(
  172. 'type' => 'switch',
  173. 'label' => $this->l('Enabled'),
  174. 'name' => 'active',
  175. 'required' => false,
  176. 'is_bool' => true,
  177. 'class' => 't',
  178. 'values' => array(
  179. array(
  180. 'id' => 'active_on',
  181. 'value' => 1
  182. ),
  183. array(
  184. 'id' => 'active_off',
  185. 'value' => 0
  186. )
  187. )
  188. )
  189. ),
  190. 'submit' => array(
  191. 'title' => $this->l('Save'),
  192. ),
  193. ),
  194. ),
  195. array(
  196. 'form' => array(
  197. 'legend' => array(
  198. 'title' => $this->l('Shop URL'),
  199. 'icon' => 'icon-shopping-cart'
  200. ),
  201. 'input' => array(
  202. array(
  203. 'type' => 'text',
  204. 'label' => $this->l('Domain'),
  205. 'name' => 'domain',
  206. 'size' => 50,
  207. ),
  208. array(
  209. 'type' => 'text',
  210. 'label' => $this->l('SSL Domain'),
  211. 'name' => 'domain_ssl',
  212. 'size' => 50,
  213. ),
  214. ),
  215. 'submit' => array(
  216. 'title' => $this->l('Save'),
  217. ),
  218. ),
  219. ),
  220. );
  221. if (!defined('_PS_HOST_MODE_')) {
  222. $this->fields_form[1]['form']['input'] = array_merge($this->fields_form[1]['form']['input'],
  223. array(
  224. array(
  225. 'type' => 'text',
  226. 'label' => $this->l('Physical URL'),
  227. 'name' => 'physical_uri',
  228. 'desc' => $this->l('This is the physical folder for your store on the web server. Leave this field empty if your store is installed on the root path. For instance, if your store is available at www.example.com/my-store/, you must input my-store/ in this field.'),
  229. 'size' => 50,
  230. )
  231. )
  232. );
  233. }
  234. $this->fields_form[1]['form']['input'] = array_merge($this->fields_form[1]['form']['input'],
  235. array(
  236. array(
  237. 'type' => 'text',
  238. 'label' => $this->l('Virtual URL'),
  239. 'name' => 'virtual_uri',
  240. 'desc' => $desc_virtual_uri,
  241. 'size' => 50,
  242. 'hint' => (!$update_htaccess) ? $this->l('Warning: URL rewriting (e.g. mod_rewrite for Apache) seems to be disabled. If your Virtual URL doesn\'t work, please check with your hosting provider on how to activate URL rewriting.') : null,
  243. ),
  244. array(
  245. 'type' => 'text',
  246. 'label' => $this->l('Final URL'),
  247. 'name' => 'final_url',
  248. 'size' => 76,
  249. 'readonly' => true
  250. ),
  251. )
  252. );
  253. if (!($obj = $this->loadObject(true))) {
  254. return;
  255. }
  256. self::$currentIndex = self::$currentIndex.($obj->id ? '&shop_id='.(int)$obj->id_shop : '');
  257. $current_shop = Shop::initialize();
  258. $list_shop_with_url = array();
  259. foreach (Shop::getShops(false, null, true) as $id) {
  260. $list_shop_with_url[$id] = (bool)count(ShopUrl::getShopUrls($id));
  261. }
  262. $this->tpl_form_vars = array(
  263. 'js_shop_url' => Tools::jsonEncode($list_shop_with_url)
  264. );
  265. $this->fields_value = array(
  266. 'domain' => trim(Validate::isLoadedObject($obj) ? $this->getFieldValue($obj, 'domain') : $current_shop->domain),
  267. 'domain_ssl' => trim(Validate::isLoadedObject($obj) ? $this->getFieldValue($obj, 'domain_ssl') : $current_shop->domain_ssl),
  268. 'physical_uri' => trim(Validate::isLoadedObject($obj) ? $this->getFieldValue($obj, 'physical_uri') : $current_shop->physical_uri),
  269. 'active' => trim(Validate::isLoadedObject($obj) ? $this->getFieldValue($obj, 'active') : true)
  270. );
  271. return parent::renderForm();
  272. }
  273. public function initPageHeaderToolbar()
  274. {
  275. parent::initPageHeaderToolbar();
  276. if ($this->display != 'add' && $this->display != 'edit') {
  277. if ($this->id_object) {
  278. $this->loadObject();
  279. }
  280. if (!$this->id_shop && $this->object && $this->object->id_shop) {
  281. $this->id_shop = $this->object->id_shop;
  282. }
  283. $this->page_header_toolbar_btn['edit'] = array(
  284. 'desc' => $this->l('Edit this shop'),
  285. 'href' => $this->context->link->getAdminLink('AdminShop').'&updateshop&shop_id='.(int)$this->id_shop,
  286. );
  287. $this->page_header_toolbar_btn['new'] = array(
  288. 'desc' => $this->l('Add a new URL'),
  289. 'href' => $this->context->link->getAdminLink('AdminShopUrl').'&add'.$this->table.'&shop_id='.(int)$this->id_shop,
  290. );
  291. }
  292. }
  293. public function initToolbar()
  294. {
  295. parent::initToolbar();
  296. if ($this->display != 'add' && $this->display != 'edit') {
  297. if ($this->id_object) {
  298. $this->loadObject();
  299. }
  300. if (!$this->id_shop && $this->object && $this->object->id_shop) {
  301. $this->id_shop = $this->object->id_shop;
  302. }
  303. $this->toolbar_btn['new'] = array(
  304. 'desc' => $this->l('Add a new URL'),
  305. 'href' => $this->context->link->getAdminLink('AdminShopUrl').'&add'.$this->table.'&shop_id='.(int)$this->id_shop,
  306. );
  307. }
  308. }
  309. public function initContent()
  310. {
  311. parent::initContent();
  312. $this->addJqueryPlugin('cooki-plugin');
  313. $data = Shop::getTree();
  314. foreach ($data as $key_group => &$group) {
  315. foreach ($group['shops'] as $key_shop => &$shop) {
  316. $current_shop = new Shop($shop['id_shop']);
  317. $urls = $current_shop->getUrls();
  318. foreach ($urls as $key_url => &$url) {
  319. $title = $url['domain'].$url['physical_uri'].$url['virtual_uri'];
  320. if (strlen($title) > 23) {
  321. $title = substr($title, 0, 23).'...';
  322. }
  323. $url['name'] = $title;
  324. $shop['urls'][$url['id_shop_url']] = $url;
  325. }
  326. }
  327. }
  328. $shops_tree = new HelperTreeShops('shops-tree', $this->l('Multistore tree'));
  329. $shops_tree->setNodeFolderTemplate('shop_tree_node_folder.tpl')->setNodeItemTemplate('shop_tree_node_item.tpl')
  330. ->setHeaderTemplate('shop_tree_header.tpl')->setActions(array(
  331. new TreeToolbarLink(
  332. 'Collapse All',
  333. '#',
  334. '$(\'#'.$shops_tree->getId().'\').tree(\'collapseAll\'); return false;',
  335. 'icon-collapse-alt'),
  336. new TreeToolbarLink(
  337. 'Expand All',
  338. '#',
  339. '$(\'#'.$shops_tree->getId().'\').tree(\'expandAll\'); return false;',
  340. 'icon-expand-alt')
  341. ))
  342. ->setAttribute('url_shop_group', $this->context->link->getAdminLink('AdminShopGroup'))
  343. ->setAttribute('url_shop', $this->context->link->getAdminLink('AdminShop'))
  344. ->setAttribute('url_shop_url', $this->context->link->getAdminLink('AdminShopUrl'))
  345. ->setData($data);
  346. $shops_tree = $shops_tree->render(null, false, false);
  347. if (!$this->display && $this->id_shop) {
  348. $shop = new Shop($this->id_shop);
  349. $this->toolbar_title[] = $shop->name;
  350. }
  351. $this->context->smarty->assign(array(
  352. 'toolbar_scroll' => 1,
  353. 'toolbar_btn' => $this->toolbar_btn,
  354. 'title' => $this->toolbar_title,
  355. 'shops_tree' => $shops_tree
  356. ));
  357. }
  358. public function postProcess()
  359. {
  360. $token = Tools::getValue('token') ? Tools::getValue('token') : $this->token;
  361. $result = true;
  362. if ((Tools::isSubmit('status'.$this->table) || Tools::isSubmit('status')) && Tools::getValue($this->identifier)) {
  363. if ($this->tabAccess['edit'] === '1') {
  364. if (Validate::isLoadedObject($object = $this->loadObject())) {
  365. /** @var ShopUrl $object */
  366. if ($object->main) {
  367. $this->errors[] = Tools::displayError('You cannot disable the Main URL.');
  368. } elseif ($object->toggleStatus()) {
  369. Tools::redirectAdmin(self::$currentIndex.'&conf=5&token='.$token);
  370. } else {
  371. $this->errors[] = Tools::displayError('An error occurred while updating the status.');
  372. }
  373. } else {
  374. $this->errors[] = Tools::displayError('An error occurred while updating the status for an object.').' <b>'.$this->table.'</b> '.Tools::displayError('(cannot load object)');
  375. }
  376. } else {
  377. $this->errors[] = Tools::displayError('You do not have permission to edit this.');
  378. }
  379. } elseif (Tools::isSubmit('main'.$this->table) && Tools::getValue($this->identifier)) {
  380. if ($this->tabAccess['edit'] === '1') {
  381. if (Validate::isLoadedObject($object = $this->loadObject())) {
  382. /** @var ShopUrl $object */
  383. if (!$object->main) {
  384. $result = $object->setMain();
  385. Tools::redirectAdmin(self::$currentIndex.'&conf=4&token='.$token);
  386. } else {
  387. $this->errors[] = Tools::displayError('You cannot change a main URL to a non-main URL. You have to set another URL as your Main URL for the selected shop.');
  388. }
  389. } else {
  390. $this->errors[] = Tools::displayError('An error occurred while updating the status for an object.').' <b>'.$this->table.'</b> '.Tools::displayError('(cannot load object)');
  391. }
  392. } else {
  393. $this->errors[] = Tools::displayError('You do not have permission to edit this.');
  394. }
  395. } else {
  396. $result = parent::postProcess();
  397. }
  398. if ($this->redirect_after) {
  399. $this->redirect_after .= '&shop_id='.(int)$this->id_shop;
  400. }
  401. return $result;
  402. }
  403. public function processSave()
  404. {
  405. /** @var ShopUrl $object */
  406. $object = $this->loadObject(true);
  407. if ($object->canAddThisUrl(Tools::getValue('domain'), Tools::getValue('domain_ssl'), Tools::getValue('physical_uri'), Tools::getValue('virtual_uri'))) {
  408. $this->errors[] = Tools::displayError('A shop URL that uses this domain already exists.');
  409. }
  410. $unallowed = str_replace('/', '', Tools::getValue('virtual_uri'));
  411. if ($unallowed == 'c' || $unallowed == 'img' || is_numeric($unallowed)) {
  412. $this->errors[] = sprintf(Tools::displayError('A shop virtual URL can not be "%s"'), $unallowed);
  413. }
  414. $return = parent::processSave();
  415. if (!$this->errors) {
  416. Tools::generateHtaccess();
  417. Tools::clearSmartyCache();
  418. Media::clearCache();
  419. }
  420. return $return;
  421. }
  422. public function processAdd()
  423. {
  424. /** @var ShopUrl $object */
  425. $object = $this->loadObject(true);
  426. if ($object->canAddThisUrl(Tools::getValue('domain'), Tools::getValue('domain_ssl'), Tools::getValue('physical_uri'), Tools::getValue('virtual_uri'))) {
  427. $this->errors[] = Tools::displayError('A shop URL that uses this domain already exists.');
  428. }
  429. if (Tools::getValue('main') && !Tools::getValue('active')) {
  430. $this->errors[] = Tools::displayError('You cannot disable the Main URL.');
  431. }
  432. return parent::processAdd();
  433. }
  434. public function processUpdate()
  435. {
  436. $this->redirect_shop_url = false;
  437. $current_url = parse_url($_SERVER['REQUEST_URI']);
  438. if (trim(dirname(dirname($current_url['path'])), '/') == trim($this->object->getBaseURI(), '/')) {
  439. $this->redirect_shop_url = true;
  440. }
  441. /** @var ShopUrl $object */
  442. $object = $this->loadObject(true);
  443. if ($object->main && !Tools::getValue('main')) {
  444. $this->errors[] = Tools::displayError('You cannot change a main URL to a non-main URL. You have to set another URL as your Main URL for the selected shop.');
  445. }
  446. if (($object->main || Tools::getValue('main')) && !Tools::getValue('active')) {
  447. $this->errors[] = Tools::displayError('You cannot disable the Main URL.');
  448. }
  449. return parent::processUpdate();
  450. }
  451. /**
  452. * @param ShopUrl $object
  453. * @return void
  454. */
  455. protected function afterUpdate($object)
  456. {
  457. if ($object->id && Tools::getValue('main')) {
  458. $object->setMain();
  459. }
  460. if ($this->redirect_shop_url) {
  461. $this->redirect_after = $object->getBaseURI().basename(_PS_ADMIN_DIR_).'/'.$this->context->link->getAdminLink('AdminShopUrl');
  462. }
  463. }
  464. /**
  465. * @param string $token
  466. * @param int $id
  467. * @param string $name
  468. * @return mixed
  469. */
  470. public function displayDeleteLink($token = null, $id, $name = null)
  471. {
  472. $tpl = $this->createTemplate('helpers/list/list_action_delete.tpl');
  473. if (!array_key_exists('Delete', self::$cache_lang)) {
  474. self::$cache_lang['Delete'] = $this->l('Delete', 'Helper');
  475. }
  476. if (!array_key_exists('DeleteItem', self::$cache_lang)) {
  477. self::$cache_lang['DeleteItem'] = $this->l('Delete selected item?', 'Helper');
  478. }
  479. if (!array_key_exists('Name', self::$cache_lang)) {
  480. self::$cache_lang['Name'] = $this->l('Name:', 'Helper');
  481. }
  482. if (!is_null($name)) {
  483. $name = '\n\n'.self::$cache_lang['Name'].' '.$name;
  484. }
  485. $data = array(
  486. $this->identifier => $id,
  487. 'href' => self::$currentIndex.'&'.$this->identifier.'='.$id.'&delete'.$this->table.'&shop_id='.(int)$this->id_shop.'&token='.($token != null ? $token : $this->token),
  488. 'action' => self::$cache_lang['Delete'],
  489. );
  490. if ($this->specificConfirmDelete !== false) {
  491. $data['confirm'] = !is_null($this->specificConfirmDelete) ? '\r'.$this->specificConfirmDelete : self::$cache_lang['DeleteItem'].$name;
  492. }
  493. $tpl->assign(array_merge($this->tpl_delete_link_vars, $data));
  494. return $tpl->fetch();
  495. }
  496. }