/src/components/com_contact/router.php

https://bitbucket.org/ke2083/transfans.co.uk-website · PHP · 251 lines · 133 code · 28 blank · 90 comment · 13 complexity · 5e0add591a56090bd22d4ff523020ec1 MD5 · raw file

  1. <?php
  2. /**
  3. * @package Joomla.Site
  4. * @subpackage com_contact
  5. *
  6. * @copyright Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE.txt
  8. */
  9. defined('_JEXEC') or die;
  10. /**
  11. * Routing class from com_contact
  12. *
  13. * @since 3.3
  14. */
  15. class ContactRouter extends JComponentRouterView
  16. {
  17. protected $noIDs = false;
  18. /**
  19. * Search Component router constructor
  20. *
  21. * @param JApplicationCms $app The application object
  22. * @param JMenu $menu The menu object to work with
  23. */
  24. public function __construct($app = null, $menu = null)
  25. {
  26. $params = JComponentHelper::getParams('com_contact');
  27. $this->noIDs = (bool) $params->get('sef_ids');
  28. $categories = new JComponentRouterViewconfiguration('categories');
  29. $categories->setKey('id');
  30. $this->registerView($categories);
  31. $category = new JComponentRouterViewconfiguration('category');
  32. $category->setKey('id')->setParent($categories, 'catid')->setNestable();
  33. $this->registerView($category);
  34. $contact = new JComponentRouterViewconfiguration('contact');
  35. $contact->setKey('id')->setParent($category, 'catid');
  36. $this->registerView($contact);
  37. $this->registerView(new JComponentRouterViewconfiguration('featured'));
  38. parent::__construct($app, $menu);
  39. $this->attachRule(new JComponentRouterRulesMenu($this));
  40. if ($params->get('sef_advanced', 0))
  41. {
  42. $this->attachRule(new JComponentRouterRulesStandard($this));
  43. $this->attachRule(new JComponentRouterRulesNomenu($this));
  44. }
  45. else
  46. {
  47. JLoader::register('ContactRouterRulesLegacy', __DIR__ . '/helpers/legacyrouter.php');
  48. $this->attachRule(new ContactRouterRulesLegacy($this));
  49. }
  50. }
  51. /**
  52. * Method to get the segment(s) for a category
  53. *
  54. * @param string $id ID of the category to retrieve the segments for
  55. * @param array $query The request that is built right now
  56. *
  57. * @return array|string The segments of this item
  58. */
  59. public function getCategorySegment($id, $query)
  60. {
  61. $category = JCategories::getInstance($this->getName())->get($id);
  62. if ($category)
  63. {
  64. $path = array_reverse($category->getPath(), true);
  65. $path[0] = '1:root';
  66. if ($this->noIDs)
  67. {
  68. foreach ($path as &$segment)
  69. {
  70. list($id, $segment) = explode(':', $segment, 2);
  71. }
  72. }
  73. return $path;
  74. }
  75. return array();
  76. }
  77. /**
  78. * Method to get the segment(s) for a category
  79. *
  80. * @param string $id ID of the category to retrieve the segments for
  81. * @param array $query The request that is built right now
  82. *
  83. * @return array|string The segments of this item
  84. */
  85. public function getCategoriesSegment($id, $query)
  86. {
  87. return $this->getCategorySegment($id, $query);
  88. }
  89. /**
  90. * Method to get the segment(s) for a contact
  91. *
  92. * @param string $id ID of the contact to retrieve the segments for
  93. * @param array $query The request that is built right now
  94. *
  95. * @return array|string The segments of this item
  96. */
  97. public function getContactSegment($id, $query)
  98. {
  99. if (!strpos($id, ':'))
  100. {
  101. $db = JFactory::getDbo();
  102. $dbquery = $db->getQuery(true);
  103. $dbquery->select($dbquery->qn('alias'))
  104. ->from($dbquery->qn('#__contact_details'))
  105. ->where('id = ' . $dbquery->q((int) $id));
  106. $db->setQuery($dbquery);
  107. $id .= ':' . $db->loadResult();
  108. }
  109. if ($this->noIDs)
  110. {
  111. list($void, $segment) = explode(':', $id, 2);
  112. return array($void => $segment);
  113. }
  114. return array((int) $id => $id);
  115. }
  116. /**
  117. * Method to get the id for a category
  118. *
  119. * @param string $segment Segment to retrieve the ID for
  120. * @param array $query The request that is parsed right now
  121. *
  122. * @return mixed The id of this item or false
  123. */
  124. public function getCategoryId($segment, $query)
  125. {
  126. if (isset($query['id']))
  127. {
  128. $category = JCategories::getInstance($this->getName(), array('access' => false))->get($query['id']);
  129. if ($category)
  130. {
  131. foreach ($category->getChildren() as $child)
  132. {
  133. if ($this->noIDs)
  134. {
  135. if ($child->alias == $segment)
  136. {
  137. return $child->id;
  138. }
  139. }
  140. else
  141. {
  142. if ($child->id == (int) $segment)
  143. {
  144. return $child->id;
  145. }
  146. }
  147. }
  148. }
  149. }
  150. return false;
  151. }
  152. /**
  153. * Method to get the segment(s) for a category
  154. *
  155. * @param string $segment Segment to retrieve the ID for
  156. * @param array $query The request that is parsed right now
  157. *
  158. * @return mixed The id of this item or false
  159. */
  160. public function getCategoriesId($segment, $query)
  161. {
  162. return $this->getCategoryId($segment, $query);
  163. }
  164. /**
  165. * Method to get the segment(s) for a contact
  166. *
  167. * @param string $segment Segment of the contact to retrieve the ID for
  168. * @param array $query The request that is parsed right now
  169. *
  170. * @return mixed The id of this item or false
  171. */
  172. public function getContactId($segment, $query)
  173. {
  174. if ($this->noIDs)
  175. {
  176. $db = JFactory::getDbo();
  177. $dbquery = $db->getQuery(true);
  178. $dbquery->select($dbquery->qn('id'))
  179. ->from($dbquery->qn('#__contact_details'))
  180. ->where('alias = ' . $dbquery->q($segment))
  181. ->where('catid = ' . $dbquery->q($query['id']));
  182. $db->setQuery($dbquery);
  183. return (int) $db->loadResult();
  184. }
  185. return (int) $segment;
  186. }
  187. }
  188. /**
  189. * Contact router functions
  190. *
  191. * These functions are proxys for the new router interface
  192. * for old SEF extensions.
  193. *
  194. * @param array &$query An array of URL arguments
  195. *
  196. * @return array The URL arguments to use to assemble the subsequent URL.
  197. *
  198. * @deprecated 4.0 Use Class based routers instead
  199. */
  200. function ContactBuildRoute(&$query)
  201. {
  202. $app = JFactory::getApplication();
  203. $router = new ContactRouter($app, $app->getMenu());
  204. return $router->build($query);
  205. }
  206. /**
  207. * Contact router functions
  208. *
  209. * These functions are proxys for the new router interface
  210. * for old SEF extensions.
  211. *
  212. * @param array $segments The segments of the URL to parse.
  213. *
  214. * @return array The URL attributes to be used by the application.
  215. *
  216. * @deprecated 4.0 Use Class based routers instead
  217. */
  218. function ContactParseRoute($segments)
  219. {
  220. $app = JFactory::getApplication();
  221. $router = new ContactRouter($app, $app->getMenu());
  222. return $router->parse($segments);
  223. }