PageRenderTime 45ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/Link.php

https://github.com/netplayer/PrestaShop
PHP | 643 lines | 392 code | 96 blank | 155 comment | 131 complexity | 1fe2ed352e19d2d7616f19e4d994d1d4 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 LinkCore
  27. {
  28. /** @var boolean Rewriting activation */
  29. protected $allow;
  30. protected $url;
  31. public static $cache = array('page' => array());
  32. public $protocol_link;
  33. public $protocol_content;
  34. protected $ssl_enable;
  35. protected static $category_disable_rewrite = null;
  36. /**
  37. * Constructor (initialization only)
  38. */
  39. public function __construct($protocol_link = null, $protocol_content = null)
  40. {
  41. $this->allow = (int)Configuration::get('PS_REWRITING_SETTINGS');
  42. $this->url = $_SERVER['SCRIPT_NAME'];
  43. $this->protocol_link = $protocol_link;
  44. $this->protocol_content = $protocol_content;
  45. if (!defined('_PS_BASE_URL_'))
  46. define('_PS_BASE_URL_', Tools::getShopDomain(true));
  47. if (!defined('_PS_BASE_URL_SSL_'))
  48. define('_PS_BASE_URL_SSL_', Tools::getShopDomainSsl(true));
  49. if (Link::$category_disable_rewrite === null)
  50. Link::$category_disable_rewrite = array(Configuration::get('PS_HOME_CATEGORY'), Configuration::get('PS_ROOT_CATEGORY'));
  51. $this->ssl_enable = Configuration::get('PS_SSL_ENABLED');
  52. }
  53. /**
  54. * Create a link to delete a product
  55. *
  56. * @param mixed $product ID of the product OR a Product object
  57. * @param int $id_picture ID of the picture to delete
  58. * @return string
  59. */
  60. public function getProductDeletePictureLink($product, $id_picture)
  61. {
  62. $url = $this->getProductLink($product);
  63. return $url.((strpos($url, '?')) ? '&' : '?').'&deletePicture='.$id_picture;
  64. }
  65. /**
  66. * Create a link to a product
  67. *
  68. * @param mixed $product Product object (can be an ID product, but deprecated)
  69. * @param string $alias
  70. * @param string $category
  71. * @param string $ean13
  72. * @param int $id_lang
  73. * @param int $id_shop (since 1.5.0) ID shop need to be used when we generate a product link for a product in a cart
  74. * @param int $ipa ID product attribute
  75. * @return string
  76. */
  77. public function getProductLink($product, $alias = null, $category = null, $ean13 = null, $id_lang = null, $id_shop = null, $ipa = 0, $force_routes = false)
  78. {
  79. $dispatcher = Dispatcher::getInstance();
  80. if (!$id_lang)
  81. $id_lang = Context::getContext()->language->id;
  82. $url = $this->getBaseLink($id_shop).$this->getLangLink($id_lang, null, $id_shop);
  83. if (!is_object($product))
  84. {
  85. if (is_array($product) && isset($product['id_product']))
  86. $product = new Product($product['id_product'], false, $id_lang, $id_shop);
  87. elseif ((int)$product)
  88. $product = new Product((int)$product, false, $id_lang, $id_shop);
  89. else
  90. throw new PrestaShopException('Invalid product vars');
  91. }
  92. // Set available keywords
  93. $params = array();
  94. $params['id'] = $product->id;
  95. $params['rewrite'] = (!$alias) ? $product->getFieldByLang('link_rewrite') : $alias;
  96. $params['ean13'] = (!$ean13) ? $product->ean13 : $ean13;
  97. $params['meta_keywords'] = Tools::str2url($product->getFieldByLang('meta_keywords'));
  98. $params['meta_title'] = Tools::str2url($product->getFieldByLang('meta_title'));
  99. if ($dispatcher->hasKeyword('product_rule', $id_lang, 'manufacturer', $id_shop))
  100. $params['manufacturer'] = Tools::str2url($product->isFullyLoaded ? $product->manufacturer_name : Manufacturer::getNameById($product->id_manufacturer));
  101. if ($dispatcher->hasKeyword('product_rule', $id_lang, 'supplier', $id_shop))
  102. $params['supplier'] = Tools::str2url($product->isFullyLoaded ? $product->supplier_name : Supplier::getNameById($product->id_supplier));
  103. if ($dispatcher->hasKeyword('product_rule', $id_lang, 'price', $id_shop))
  104. $params['price'] = $product->isFullyLoaded ? $product->price : Product::getPriceStatic($product->id, false, null, 6, null, false, true, 1, false, null, null, null, $product->specificPrice);
  105. if ($dispatcher->hasKeyword('product_rule', $id_lang, 'tags', $id_shop))
  106. $params['tags'] = Tools::str2url($product->getTags($id_lang));
  107. if ($dispatcher->hasKeyword('product_rule', $id_lang, 'category', $id_shop))
  108. $params['category'] = (!is_null($product->category) && !empty($product->category)) ? Tools::str2url($product->category) : Tools::str2url($category);
  109. if ($dispatcher->hasKeyword('product_rule', $id_lang, 'reference', $id_shop))
  110. $params['reference'] = Tools::str2url($product->reference);
  111. if ($dispatcher->hasKeyword('product_rule', $id_lang, 'categories', $id_shop))
  112. {
  113. $params['category'] = (!$category) ? $product->category : $category;
  114. $cats = array();
  115. foreach ($product->getParentCategories() as $cat)
  116. if (!in_array($cat['id_category'], Link::$category_disable_rewrite))//remove root and home category from the URL
  117. $cats[] = $cat['link_rewrite'];
  118. $params['categories'] = implode('/', $cats);
  119. }
  120. $anchor = $ipa ? $product->getAnchor($ipa) : '';
  121. return $url.$dispatcher->createUrl('product_rule', $id_lang, $params, $force_routes, $anchor, $id_shop);
  122. }
  123. /**
  124. * Create a link to a category
  125. *
  126. * @param mixed $category Category object (can be an ID category, but deprecated)
  127. * @param string $alias
  128. * @param int $id_lang
  129. * @param string $selected_filters Url parameter to autocheck filters of the module blocklayered
  130. * @return string
  131. */
  132. public function getCategoryLink($category, $alias = null, $id_lang = null, $selected_filters = null, $id_shop = null)
  133. {
  134. if (!$id_lang)
  135. $id_lang = Context::getContext()->language->id;
  136. $url = $this->getBaseLink($id_shop).$this->getLangLink($id_lang, null, $id_shop);
  137. if (!is_object($category))
  138. $category = new Category($category, $id_lang);
  139. // Set available keywords
  140. $params = array();
  141. $params['id'] = $category->id;
  142. $params['rewrite'] = (!$alias) ? $category->link_rewrite : $alias;
  143. $params['meta_keywords'] = Tools::str2url($category->getFieldByLang('meta_keywords'));
  144. $params['meta_title'] = Tools::str2url($category->getFieldByLang('meta_title'));
  145. // Selected filters is used by the module blocklayered
  146. $selected_filters = is_null($selected_filters) ? '' : $selected_filters;
  147. if (empty($selected_filters))
  148. $rule = 'category_rule';
  149. else
  150. {
  151. $rule = 'layered_rule';
  152. $params['selected_filters'] = $selected_filters;
  153. }
  154. return $url.Dispatcher::getInstance()->createUrl($rule, $id_lang, $params, $this->allow, '', $id_shop);
  155. }
  156. /**
  157. * Create a link to a CMS category
  158. *
  159. * @param mixed $category CMSCategory object (can be an ID category, but deprecated)
  160. * @param string $alias
  161. * @param int $id_lang
  162. * @return string
  163. */
  164. public function getCMSCategoryLink($cms_category, $alias = null, $id_lang = null, $id_shop = null)
  165. {
  166. if (!$id_lang)
  167. $id_lang = Context::getContext()->language->id;
  168. $url = $this->getBaseLink($id_shop).$this->getLangLink($id_lang, null, $id_shop);
  169. $dispatcher = Dispatcher::getInstance();
  170. if (!is_object($cms_category))
  171. {
  172. if ($alias !== null && !$dispatcher->hasKeyword('cms_category_rule', $id_lang, 'meta_keywords', $id_shop) && !$dispatcher->hasKeyword('cms_category_rule', $id_lang, 'meta_title', $id_shop))
  173. return $url.$dispatcher->createUrl('cms_category_rule', $id_lang, array('id' => (int)$cms_category, 'rewrite' => (string)$alias), $this->allow, '', $id_shop);
  174. $cms_category = new CMSCategory($cms_category, $id_lang);
  175. }
  176. // Set available keywords
  177. $params = array();
  178. $params['id'] = $cms_category->id;
  179. $params['rewrite'] = (!$alias) ? $cms_category->link_rewrite : $alias;
  180. $params['meta_keywords'] = Tools::str2url($cms_category->meta_keywords);
  181. $params['meta_title'] = Tools::str2url($cms_category->meta_title);
  182. return $url.$dispatcher->createUrl('cms_category_rule', $id_lang, $params, $this->allow, '', $id_shop);
  183. }
  184. /**
  185. * Create a link to a CMS page
  186. *
  187. * @param mixed $cms CMS object (can be an ID CMS, but deprecated)
  188. * @param string $alias
  189. * @param bool $ssl
  190. * @param int $id_lang
  191. * @return string
  192. */
  193. public function getCMSLink($cms, $alias = null, $ssl = null, $id_lang = null, $id_shop = null)
  194. {
  195. if (!$id_lang)
  196. $id_lang = Context::getContext()->language->id;
  197. $url = $this->getBaseLink($id_shop, $ssl).$this->getLangLink($id_lang, null, $id_shop);
  198. $dispatcher = Dispatcher::getInstance();
  199. if (!is_object($cms))
  200. {
  201. if ($alias !== null && !$dispatcher->hasKeyword('cms_rule', $id_lang, 'meta_keywords', $id_shop) && !$dispatcher->hasKeyword('cms_rule', $id_lang, 'meta_title', $id_shop))
  202. return $url.$dispatcher->createUrl('cms_rule', $id_lang, array('id' => (int)$cms, 'rewrite' => (string)$alias), $this->allow, '', $id_shop);
  203. $cms = new CMS($cms, $id_lang);
  204. }
  205. // Set available keywords
  206. $params = array();
  207. $params['id'] = $cms->id;
  208. $params['rewrite'] = (!$alias) ? (is_array($cms->link_rewrite) ? $cms->link_rewrite[(int)$id_lang] : $cms->link_rewrite) : $alias;
  209. $params['meta_keywords'] = '';
  210. if (isset($cms->meta_keywords) && !empty($cms->meta_keywords))
  211. $params['meta_keywords'] = is_array($cms->meta_keywords) ? Tools::str2url($cms->meta_keywords[(int)$id_lang]) : Tools::str2url($cms->meta_keywords);
  212. $params['meta_title'] = '';
  213. if (isset($cms->meta_title) && !empty($cms->meta_title))
  214. $params['meta_title'] = is_array($cms->meta_title) ? Tools::str2url($cms->meta_title[(int)$id_lang]) : Tools::str2url($cms->meta_title);
  215. return $url.$dispatcher->createUrl('cms_rule', $id_lang, $params, $this->allow, '', $id_shop);
  216. }
  217. /**
  218. * Create a link to a supplier
  219. *
  220. * @param mixed $supplier Supplier object (can be an ID supplier, but deprecated)
  221. * @param string $alias
  222. * @param int $id_lang
  223. * @return string
  224. */
  225. public function getSupplierLink($supplier, $alias = null, $id_lang = null, $id_shop = null)
  226. {
  227. if (!$id_lang)
  228. $id_lang = Context::getContext()->language->id;
  229. $url = $this->getBaseLink($id_shop).$this->getLangLink($id_lang, null, $id_shop);
  230. $dispatcher = Dispatcher::getInstance();
  231. if (!is_object($supplier))
  232. {
  233. if ($alias !== null && !$dispatcher->hasKeyword('supplier_rule', $id_lang, 'meta_keywords', $id_shop) && !$dispatcher->hasKeyword('supplier_rule', $id_lang, 'meta_title', $id_shop))
  234. return $url.$dispatcher->createUrl('supplier_rule', $id_lang, array('id' => (int)$supplier, 'rewrite' => (string)$alias), $this->allow, '', $id_shop);
  235. $supplier = new Supplier($supplier, $id_lang);
  236. }
  237. // Set available keywords
  238. $params = array();
  239. $params['id'] = $supplier->id;
  240. $params['rewrite'] = (!$alias) ? $supplier->link_rewrite : $alias;
  241. $params['meta_keywords'] = Tools::str2url($supplier->meta_keywords);
  242. $params['meta_title'] = Tools::str2url($supplier->meta_title);
  243. return $url.$dispatcher->createUrl('supplier_rule', $id_lang, $params, $this->allow, '', $id_shop);
  244. }
  245. /**
  246. * Create a link to a manufacturer
  247. *
  248. * @param mixed $manufacturer Manufacturer object (can be an ID supplier, but deprecated)
  249. * @param string $alias
  250. * @param int $id_lang
  251. * @return string
  252. */
  253. public function getManufacturerLink($manufacturer, $alias = null, $id_lang = null, $id_shop = null)
  254. {
  255. if (!$id_lang)
  256. $id_lang = Context::getContext()->language->id;
  257. $url = $this->getBaseLink($id_shop).$this->getLangLink($id_lang, null, $id_shop);
  258. $dispatcher = Dispatcher::getInstance();
  259. if (!is_object($manufacturer))
  260. {
  261. if ($alias !== null && !$dispatcher->hasKeyword('manufacturer_rule', $id_lang, 'meta_keywords', $id_shop) && !$dispatcher->hasKeyword('manufacturer_rule', $id_lang, 'meta_title', $id_shop))
  262. return $url.$dispatcher->createUrl('manufacturer_rule', $id_lang, array('id' => (int)$manufacturer, 'rewrite' => (string)$alias), $this->allow, '', $id_shop);
  263. $manufacturer = new Manufacturer($manufacturer, $id_lang);
  264. }
  265. // Set available keywords
  266. $params = array();
  267. $params['id'] = $manufacturer->id;
  268. $params['rewrite'] = (!$alias) ? $manufacturer->link_rewrite : $alias;
  269. $params['meta_keywords'] = Tools::str2url($manufacturer->meta_keywords);
  270. $params['meta_title'] = Tools::str2url($manufacturer->meta_title);
  271. return $url.$dispatcher->createUrl('manufacturer_rule', $id_lang, $params, $this->allow, '', $id_shop);
  272. }
  273. /**
  274. * Create a link to a module
  275. *
  276. * @since 1.5.0
  277. * @param string $module Module name
  278. * @param string $process Action name
  279. * @param int $id_lang
  280. * @return string
  281. */
  282. public function getModuleLink($module, $controller = 'default', array $params = array(), $ssl = null, $id_lang = null, $id_shop = null)
  283. {
  284. if (!$id_lang)
  285. $id_lang = Context::getContext()->language->id;
  286. $url = $this->getBaseLink($id_shop, $ssl).$this->getLangLink($id_lang, null, $id_shop);
  287. // Set available keywords
  288. $params['module'] = $module;
  289. $params['controller'] = $controller ? $controller : 'default';
  290. // If the module has its own route ... just use it !
  291. if (Dispatcher::getInstance()->hasRoute('module-'.$module.'-'.$controller, $id_lang, $id_shop))
  292. return $this->getPageLink('module-'.$module.'-'.$controller, $ssl, $id_lang, $params);
  293. else
  294. return $url.Dispatcher::getInstance()->createUrl('module', $id_lang, $params, $this->allow, '', $id_shop);
  295. }
  296. /**
  297. * Use controller name to create a link
  298. *
  299. * @param string $controller
  300. * @param boolean $with_token include or not the token in the url
  301. * @return string url
  302. */
  303. public function getAdminLink($controller, $with_token = true)
  304. {
  305. $id_lang = Context::getContext()->language->id;
  306. $params = $with_token ? array('token' => Tools::getAdminTokenLite($controller)) : array();
  307. return Dispatcher::getInstance()->createUrl($controller, $id_lang, $params, false);
  308. }
  309. /**
  310. * Returns a link to a product image for display
  311. * Note: the new image filesystem stores product images in subdirectories of img/p/
  312. *
  313. * @param string $name rewrite link of the image
  314. * @param string $ids id part of the image filename - can be "id_product-id_image" (legacy support, recommended) or "id_image" (new)
  315. * @param string $type
  316. */
  317. public function getImageLink($name, $ids, $type = null)
  318. {
  319. $not_default = false;
  320. // legacy mode or default image
  321. $theme = ((Shop::isFeatureActive() && file_exists(_PS_PROD_IMG_DIR_.$ids.($type ? '-'.$type : '').'-'.(int)Context::getContext()->shop->id_theme.'.jpg')) ? '-'.Context::getContext()->shop->id_theme : '');
  322. if ((Configuration::get('PS_LEGACY_IMAGES')
  323. && (file_exists(_PS_PROD_IMG_DIR_.$ids.($type ? '-'.$type : '').$theme.'.jpg')))
  324. || ($not_default = strpos($ids, 'default') !== false))
  325. {
  326. if ($this->allow == 1 && !$not_default)
  327. $uri_path = __PS_BASE_URI__.$ids.($type ? '-'.$type : '').$theme.'/'.$name.'.jpg';
  328. else
  329. $uri_path = _THEME_PROD_DIR_.$ids.($type ? '-'.$type : '').$theme.'.jpg';
  330. }
  331. else
  332. {
  333. // if ids if of the form id_product-id_image, we want to extract the id_image part
  334. $split_ids = explode('-', $ids);
  335. $id_image = (isset($split_ids[1]) ? $split_ids[1] : $split_ids[0]);
  336. $theme = ((Shop::isFeatureActive() && file_exists(_PS_PROD_IMG_DIR_.Image::getImgFolderStatic($id_image).$id_image.($type ? '-'.$type : '').'-'.(int)Context::getContext()->shop->id_theme.'.jpg')) ? '-'.Context::getContext()->shop->id_theme : '');
  337. if ($this->allow == 1)
  338. $uri_path = __PS_BASE_URI__.$id_image.($type ? '-'.$type : '').$theme.'/'.$name.'.jpg';
  339. else
  340. $uri_path = _THEME_PROD_DIR_.Image::getImgFolderStatic($id_image).$id_image.($type ? '-'.$type : '').$theme.'.jpg';
  341. }
  342. return $this->protocol_content.Tools::getMediaServer($uri_path).$uri_path;
  343. }
  344. public function getMediaLink($filepath)
  345. {
  346. return $this->protocol_content.Tools::getMediaServer($filepath).$filepath;
  347. }
  348. /**
  349. * Create a simple link
  350. *
  351. * @param string $controller
  352. * @param bool $ssl
  353. * @param int $id_lang
  354. * @param string|array $request
  355. * @param bool $request_url_encode Use URL encode
  356. *
  357. * @return string Page link
  358. */
  359. public function getPageLink($controller, $ssl = null, $id_lang = null, $request = null, $request_url_encode = false, $id_shop = null)
  360. {
  361. //If $controller contains '&' char, it means that $controller contains request data and must be parsed first
  362. $p = strpos($controller, '&');
  363. if ($p !== false) {
  364. $request = substr($controller, $p + 1);
  365. $request_url_encode = false;
  366. $controller = substr($controller, 0, $p);
  367. }
  368. $controller = Tools::strReplaceFirst('.php', '', $controller);
  369. if (!$id_lang)
  370. $id_lang = (int)Context::getContext()->language->id;
  371. //need to be unset because getModuleLink need those params when rewrite is enable
  372. if (is_array($request))
  373. {
  374. if (isset($request['module']))
  375. unset($request['module']);
  376. if (isset($request['controller']))
  377. unset($request['controller']);
  378. }
  379. else
  380. {
  381. // @FIXME html_entity_decode has been added due to '&amp;' => '%3B' ...
  382. $request = html_entity_decode($request);
  383. if ($request_url_encode)
  384. $request = urlencode($request);
  385. parse_str($request, $request);
  386. }
  387. $uri_path = Dispatcher::getInstance()->createUrl($controller, $id_lang, $request, false, '', $id_shop);
  388. return $this->getBaseLink($id_shop, $ssl).$this->getLangLink($id_lang, null, $id_shop).ltrim($uri_path, '/');
  389. }
  390. public function getCatImageLink($name, $id_category, $type = null)
  391. {
  392. if($this->allow == 1 && $type)
  393. $uri_path = __PS_BASE_URI__.'c/'.$id_category.'-'.$type.'/'.$name.'.jpg';
  394. else
  395. $uri_path = _THEME_CAT_DIR_.$id_category.($type ? '-'.$type : '').'.jpg';
  396. return $this->protocol_content.Tools::getMediaServer($uri_path).$uri_path;
  397. }
  398. /**
  399. * Create link after language change, for the change language block
  400. *
  401. * @param integer $id_lang Language ID
  402. * @return string link
  403. */
  404. public function getLanguageLink($id_lang, Context $context = null)
  405. {
  406. if (!$context)
  407. $context = Context::getContext();
  408. $params = $_GET;
  409. unset($params['isolang'], $params['controller']);
  410. if (!$this->allow)
  411. $params['id_lang'] = $id_lang;
  412. else
  413. unset($params['id_lang']);
  414. $controller = Dispatcher::getInstance()->getController();
  415. if (!empty(Context::getContext()->controller->php_self))
  416. $controller = Context::getContext()->controller->php_self;
  417. if ($controller == 'product' && isset($params['id_product']))
  418. return $this->getProductLink((int)$params['id_product'], null, null, null, (int)$id_lang);
  419. elseif ($controller == 'category' && isset($params['id_category']))
  420. return $this->getCategoryLink((int)$params['id_category'], null, (int)$id_lang);
  421. elseif ($controller == 'supplier' && isset($params['id_supplier']))
  422. return $this->getSupplierLink((int)$params['id_supplier'], null, (int)$id_lang);
  423. elseif ($controller == 'manufacturer' && isset($params['id_manufacturer']))
  424. return $this->getManufacturerLink((int)$params['id_manufacturer'], null, (int)$id_lang);
  425. elseif ($controller == 'cms' && isset($params['id_cms']))
  426. return $this->getCMSLink((int)$params['id_cms'], null, false, (int)$id_lang);
  427. elseif ($controller == 'cms' && isset($params['id_cms_category']))
  428. return $this->getCMSCategoryLink((int)$params['id_cms_category'], null, (int)$id_lang);
  429. elseif (isset($params['fc']) && $params['fc'] == 'module')
  430. {
  431. $module = Validate::isModuleName(Tools::getValue('module')) ? Tools::getValue('module') : '';
  432. if (!empty($module))
  433. {
  434. unset($params['fc'], $params['module']);
  435. return $this->getModuleLink($module, $controller, $params, null, (int)$id_lang);
  436. }
  437. }
  438. return $this->getPageLink($controller, null, $id_lang, $params);
  439. }
  440. public function goPage($url, $p)
  441. {
  442. $url = rtrim(str_replace('?&', '?', $url), '?');
  443. return $url.($p == 1 ? '' : (!strstr($url, '?') ? '?' : '&').'p='.(int)$p);
  444. }
  445. /**
  446. * Get pagination link
  447. *
  448. * @param string $type Controller name
  449. * @param int $id_object
  450. * @param boolean $nb Show nb element per page attribute
  451. * @param boolean $sort Show sort attribute
  452. * @param boolean $pagination Show page number attribute
  453. * @param boolean $array If false return an url, if true return an array
  454. */
  455. public function getPaginationLink($type, $id_object, $nb = false, $sort = false, $pagination = false, $array = false)
  456. {
  457. // If no parameter $type, try to get it by using the controller name
  458. if (!$type && !$id_object)
  459. {
  460. $method_name = 'get'.Dispatcher::getInstance()->getController().'Link';
  461. if (method_exists($this, $method_name) && isset($_GET['id_'.Dispatcher::getInstance()->getController()]))
  462. {
  463. $type = Dispatcher::getInstance()->getController();
  464. $id_object = $_GET['id_'.$type];
  465. }
  466. }
  467. if ($type && $id_object)
  468. $url = $this->{'get'.$type.'Link'}($id_object, null);
  469. else
  470. {
  471. if (isset(Context::getContext()->controller->php_self))
  472. $name = Context::getContext()->controller->php_self;
  473. else
  474. $name = Dispatcher::getInstance()->getController();
  475. $url = $this->getPageLink($name);
  476. }
  477. $vars = array();
  478. $vars_nb = array('n', 'search_query');
  479. $vars_sort = array('orderby', 'orderway');
  480. $vars_pagination = array('p');
  481. foreach ($_GET as $k => $value)
  482. {
  483. if ($k != 'id_'.$type && $k != 'controller')
  484. {
  485. if (Configuration::get('PS_REWRITING_SETTINGS') && ($k == 'isolang' || $k == 'id_lang'))
  486. continue;
  487. $if_nb = (!$nb || ($nb && !in_array($k, $vars_nb)));
  488. $if_sort = (!$sort || ($sort && !in_array($k, $vars_sort)));
  489. $if_pagination = (!$pagination || ($pagination && !in_array($k, $vars_pagination)));
  490. if ($if_nb && $if_sort && $if_pagination)
  491. {
  492. if (!is_array($value))
  493. $vars[urlencode($k)] = $value;
  494. else
  495. {
  496. foreach (explode('&', http_build_query(array($k => $value), '', '&')) as $key => $val)
  497. {
  498. $data = explode('=', $val);
  499. $vars[urldecode($data[0])] = $data[1];
  500. }
  501. }
  502. }
  503. }
  504. }
  505. if (!$array)
  506. if (count($vars))
  507. return $url.(($this->allow == 1 || $url == $this->url) ? '?' : '&').http_build_query($vars, '', '&');
  508. else
  509. return $url;
  510. $vars['requestUrl'] = $url;
  511. if ($type && $id_object)
  512. $vars['id_'.$type] = (is_object($id_object) ? (int)$id_object->id : (int)$id_object);
  513. if (!$this->allow == 1)
  514. $vars['controller'] = Dispatcher::getInstance()->getController();
  515. return $vars;
  516. }
  517. public function addSortDetails($url, $orderby, $orderway)
  518. {
  519. return $url.(!strstr($url, '?') ? '?' : '&').'orderby='.urlencode($orderby).'&orderway='.urlencode($orderway);
  520. }
  521. protected function getLangLink($id_lang = null, Context $context = null, $id_shop = null)
  522. {
  523. if (!$context)
  524. $context = Context::getContext();
  525. if ((!$this->allow && in_array($id_shop, array($context->shop->id, null))) || !Language::isMultiLanguageActivated($id_shop) || !(int)Configuration::get('PS_REWRITING_SETTINGS', null, null, $id_shop))
  526. return '';
  527. if (!$id_lang)
  528. $id_lang = $context->language->id;
  529. return Language::getIsoById($id_lang).'/';
  530. }
  531. protected function getBaseLink($id_shop = null, $ssl = null)
  532. {
  533. static $force_ssl = null;
  534. if ($ssl === null)
  535. {
  536. if ($force_ssl === null)
  537. $force_ssl = (Configuration::get('PS_SSL_ENABLED') && Configuration::get('PS_SSL_ENABLED_EVERYWHERE'));
  538. $ssl = $force_ssl;
  539. }
  540. if (Configuration::get('PS_MULTISHOP_FEATURE_ACTIVE') && $id_shop !== null)
  541. $shop = new Shop($id_shop);
  542. else
  543. $shop = Context::getContext()->shop;
  544. $base = (($ssl && $this->ssl_enable) ? 'https://'.$shop->domain_ssl : 'http://'.$shop->domain);
  545. return $base.$shop->getBaseURI();
  546. }
  547. }