Advertisement
Guest User

Untitled

a guest
Apr 15th, 2016
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.27 KB | None | 0 0
  1. <?php
  2. /*
  3. * 2013 Ha!*!*y
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Academic Free License (AFL 3.0)
  8. * It is available through the world-wide-web at this URL:
  9. * http://opensource.org/licenses/afl-3.0.php
  10. *
  11. * DISCLAIMER
  12. * This code is provided as is without any warranty.
  13. * No promise of being safe or secure
  14. *
  15. * @author Ha!*!*y <ha99ys@gmail.com>
  16. * @copyright 2012-2013 Ha!*!*y
  17. * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
  18. * @code sorce: http://prestashop.com
  19. */
  20. class Link extends LinkCore
  21. {
  22. /**
  23. * Create a link to a category
  24. *
  25. * @param mixed $category Category object (can be an ID category, but deprecated)
  26. * @param string $alias
  27. * @param int $id_lang
  28. * @param string $selected_filters Url parameter to autocheck filters of the module blocklayered
  29. * @return string
  30. */
  31. /*
  32. * module: cleanurls
  33. * date: 2015-11-12 00:28:13
  34. * version: 0.42
  35. */
  36. public function getCategoryLink($category, $alias = null, $id_lang = null, $selected_filters = null, $id_shop = null)
  37. {
  38. if (!$id_lang)
  39. $id_lang = Context::getContext()->language->id;
  40.  
  41. if ($id_shop === null)
  42. $shop = Context::getContext()->shop;
  43. else
  44. $shop = new Shop($id_shop);
  45. $url = 'http://'.$shop->domain.$shop->getBaseURI().$this->getLangLink($id_lang, null, $id_shop);
  46. if (!is_object($category))
  47. $category = new Category($category, $id_lang);
  48. $params = array();
  49. $params['id'] = $category->id;
  50. $params['rewrite'] = (!$alias) ? $category->link_rewrite : $alias;
  51. $params['meta_keywords'] = Tools::str2url($category->meta_keywords);
  52. $params['meta_title'] = Tools::str2url($category->meta_title);
  53. $selected_filters = is_null($selected_filters) ? '' : $selected_filters;
  54. if (empty($selected_filters))
  55. $rule = 'category_rule';
  56. else
  57. {
  58. $rule = 'layered_rule';
  59. $params['selected_filters'] = $selected_filters;
  60. }
  61. $cats = array();
  62. $subCategories = $this->_getParentsCategories($category->id);
  63. $subCategories = is_array($subCategories) === TRUE ? array_reverse($subCategories) : $subCategories;
  64. $skip_list = Link::$category_disable_rewrite;
  65. $skip_list[] = $category->id;
  66. foreach ($subCategories as $cat)
  67. {
  68. if (!in_array($cat['id_category'], $skip_list))//remove root and home category from the URL
  69. $cats[] = $cat['link_rewrite'];
  70. }
  71. $params['categories'] = implode('/', $cats);
  72.  
  73. return $url.Dispatcher::getInstance()->createUrl($rule, $id_lang, $params, $this->allow);
  74. }
  75. /**
  76. * Get Each parent category of this category until the root category
  77. *
  78. * @param integer $id_lang Language ID
  79. * @return array Corresponding categories
  80. */
  81. /*
  82. * module: cleanurls
  83. * date: 2015-11-12 00:28:13
  84. * version: 0.42
  85. */
  86. public function _getParentsCategories($id_current = NULL)
  87. {
  88. $context = Context::getContext()->cloneContext();
  89. $context->shop = clone($context->shop);
  90. $id_lang = $context->language->id;
  91. $categories = null;
  92. if (count(Category::getCategoriesWithoutParent()) > 1 && Configuration::get('PS_MULTISHOP_FEATURE_ACTIVE') && count(Shop::getShops(true, null, true)) != 1)
  93. $context->shop->id_category = Category::getTopCategory()->id;
  94. elseif (!$context->shop->id)
  95. $context->shop = new Shop(Configuration::get('PS_SHOP_DEFAULT'));
  96. $id_shop = $context->shop->id;
  97. while (true)
  98. {
  99. $sql = '
  100. SELECT c.*, cl.*
  101. FROM `'._DB_PREFIX_.'category` c
  102. LEFT JOIN `'._DB_PREFIX_.'category_lang` cl
  103. ON (c.`id_category` = cl.`id_category`
  104. AND `id_lang` = '.(int)$id_lang.Shop::addSqlRestrictionOnLang('cl').')';
  105. if (Shop::isFeatureActive() && Shop::getContext() == Shop::CONTEXT_SHOP)
  106. $sql .= '
  107. LEFT JOIN `'._DB_PREFIX_.'category_shop` cs
  108. ON (c.`id_category` = cs.`id_category` AND cs.`id_shop` = '.(int)$id_shop.')';
  109. $sql .= '
  110. WHERE c.`id_category` = '.(int)$id_current;
  111. if (Shop::isFeatureActive() && Shop::getContext() == Shop::CONTEXT_SHOP)
  112. $sql .= '
  113. AND cs.`id_shop` = '.(int)$context->shop->id;
  114. $root_category = Category::getRootCategory();
  115. if (Shop::isFeatureActive() && Shop::getContext() == Shop::CONTEXT_SHOP &&
  116. (!Tools::isSubmit('id_category') ||
  117. (int)Tools::getValue('id_category') == (int)$root_category->id ||
  118. (int)$root_category->id == (int)$context->shop->id_category))
  119. $sql .= '
  120. AND c.`id_parent` != 0';
  121. $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);
  122. if (isset($result[0]))
  123. $categories[] = $result[0];
  124. else if (!$categories)
  125. $categories = array();
  126. if (!$result || ($result[0]['id_category'] == $context->shop->id_category))
  127. return $categories;
  128. $id_current = $result[0]['id_parent'];
  129. }
  130. }
  131. /**
  132. * Get pagination link
  133. *
  134. * @param string $type Controller name
  135. * @param int $id_object
  136. * @param boolean $nb Show nb element per page attribute
  137. * @param boolean $sort Show sort attribute
  138. * @param boolean $pagination Show page number attribute
  139. * @param boolean $array If false return an url, if true return an array
  140. */
  141. /*
  142. * module: cleanurls
  143. * date: 2015-11-12 00:28:13
  144. * version: 0.42
  145. */
  146. public function getPaginationLink($type, $id_object, $nb = false, $sort = false, $pagination = false, $array = false)
  147. {
  148. if (!$type && !$id_object)
  149. {
  150. $method_name = 'get'.Dispatcher::getInstance()->getController().'Link';
  151. if (method_exists($this, $method_name) && isset($_GET['id_'.Dispatcher::getInstance()->getController()]))
  152. {
  153. $type = Dispatcher::getInstance()->getController();
  154. $id_object = $_GET['id_'.$type];
  155. }
  156. }
  157. if ($type && $id_object)
  158. $url = $this->{'get'.$type.'Link'}($id_object, null);
  159. else
  160. {
  161. if (isset(Context::getContext()->controller->php_self))
  162. $name = Context::getContext()->controller->php_self;
  163. else
  164. $name = Dispatcher::getInstance()->getController();
  165. $url = $this->getPageLink($name);
  166. }
  167. $vars = array();
  168. $vars_nb = array('n', 'search_query');
  169. $vars_sort = array('orderby', 'orderway');
  170. $vars_pagination = array('p');
  171. foreach ($_GET as $k => $value)
  172. {
  173. if ($k != 'id_'.$type && $k != $type.'_rewrite' && $k != 'controller')
  174. {
  175. if (Configuration::get('PS_REWRITING_SETTINGS') && ($k == 'isolang' || $k == 'id_lang'))
  176. continue;
  177. $if_nb = (!$nb || ($nb && !in_array($k, $vars_nb)));
  178. $if_sort = (!$sort || ($sort && !in_array($k, $vars_sort)));
  179. $if_pagination = (!$pagination || ($pagination && !in_array($k, $vars_pagination)));
  180. if ($if_nb && $if_sort && $if_pagination)
  181. {
  182. if (!is_array($value))
  183. $vars[urlencode($k)] = $value;
  184. else
  185. {
  186. foreach (explode('&', http_build_query(array($k => $value), '', '&')) as $key => $val)
  187. {
  188. $data = explode('=', $val);
  189. $vars[urldecode($data[0])] = $data[1];
  190. }
  191. }
  192. }
  193. }
  194. }
  195. if (!$array)
  196. if (count($vars))
  197. return $url.(($this->allow == 1 || $url == $this->url) ? '?' : '&').http_build_query($vars, '', '&');
  198. else
  199. return $url;
  200.  
  201. $vars['requestUrl'] = $url;
  202. if ($type && $id_object)
  203. $vars['id_'.$type] = (is_object($id_object) ? (int)$id_object->id : (int)$id_object);
  204.  
  205. if (!$this->allow == 1)
  206. $vars['controller'] = Dispatcher::getInstance()->getController();
  207. return $vars;
  208. }
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement