PageRenderTime 64ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/controllers/admin/AdminShopUrlController.php

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