From 49313506b0d91c8fc7baa9c660481cef64202a6c Mon Sep 17 00:00:00 2001 From: Hannes Papenberg Date: Sun, 1 May 2016 19:35:17 +0200 Subject: [PATCH] Removing IDs from SEF URLs --- .../components/com_contact/config.xml | 11 +++ .../components/com_content/config.xml | 12 ++++ .../components/com_newsfeeds/config.xml | 11 +++ administrator/language/en-GB/en-GB.ini | 2 + components/com_contact/router.php | 70 +++++++++++++++++-- components/com_content/router.php | 70 +++++++++++++++++-- components/com_newsfeeds/router.php | 70 +++++++++++++++++-- components/com_users/router.php | 1 + libraries/cms/component/router/rules/menu.php | 2 +- .../cms/component/router/rules/standard.php | 6 +- libraries/legacy/categories/categories.php | 2 +- 11 files changed, 240 insertions(+), 17 deletions(-) diff --git a/administrator/components/com_contact/config.xml b/administrator/components/com_contact/config.xml index b8938dbadccdd..56c1e15c42683 100644 --- a/administrator/components/com_contact/config.xml +++ b/administrator/components/com_contact/config.xml @@ -827,6 +827,17 @@ + + + +
JNO + + + + +
JNO + + + +
noIDs = (bool) $params->get('sef_ids'); $categories = new JComponentRouterViewconfiguration('categories'); $categories->setKey('id'); $this->registerView($categories); @@ -44,6 +48,7 @@ public function __construct($app = null, $menu = null) if ($params->get('sef_advanced', 0)) { $this->attachRule(new JComponentRouterRulesStandard($this)); + $this->attachRule(new JComponentRouterRulesNomenu($this)); } else { @@ -66,7 +71,20 @@ public function getCategorySegment($id, $query) if ($category) { - return array_reverse($category->getPath()); + if ($this->noIDs) + { + $path = array_reverse($category->getPath(), true); + foreach ($path as &$segment) + { + list($id, $segment) = explode(':', $segment, 2); + } + + return $path; + } + else + { + return array_reverse($category->getPath(), true); + } } return array(); @@ -95,7 +113,28 @@ public function getCategoriesSegment($id, $query) */ public function getContactSegment($id, $query) { - return array($id); + if ($this->noIDs) + { + if (strpos($id, ':')) + { + list($void, $segment) = explode(':', $id, 2); + + return array($void => $segment); + } + else + { + $db = JFactory::getDbo(); + $dbquery = $db->getQuery(true); + $dbquery->select($dbquery->qn('alias')) + ->from($dbquery->qn('#__contact_details')) + ->where('id = ' . $dbquery->q((int) $id)); + $db->setQuery($dbquery); + + return array($id => $id . ':' . $db->loadResult()); + } + } + + return array((int) $id => $id); } /** @@ -114,9 +153,19 @@ public function getCategoryId($segment, $query) foreach ($category->getChildren() as $child) { - if ($child->id == (int) $segment) + if ($this->noIDs) { - return $child->id; + if ($child->alias == $segment) + { + return $child->id; + } + } + else + { + if ($child->id == (int) $segment) + { + return $child->id; + } } } } @@ -147,6 +196,19 @@ public function getCategoriesId($segment, $query) */ public function getContactId($segment, $query) { + if ($this->noIDs) + { + $db = JFactory::getDbo(); + $dbquery = $db->getQuery(true); + $dbquery->select($dbquery->qn('id')) + ->from($dbquery->qn('#__contact_details')) + ->where('alias = ' . $dbquery->q($segment)) + ->where('catid = ' . $dbquery->q($query['id'])); + $db->setQuery($dbquery); + + return (int) $db->loadResult(); + } + return (int) $segment; } } diff --git a/components/com_content/router.php b/components/com_content/router.php index c25a607d6c707..351b14cc3668f 100644 --- a/components/com_content/router.php +++ b/components/com_content/router.php @@ -16,6 +16,8 @@ */ class ContentRouter extends JComponentRouterView { + protected $noIDs = false; + /** * Content Component router constructor * @@ -24,6 +26,8 @@ class ContentRouter extends JComponentRouterView */ public function __construct($app = null, $menu = null) { + $params = JComponentHelper::getParams('com_content'); + $this->noIDs = (bool) $params->get('sef_ids'); $categories = new JComponentRouterViewconfiguration('categories'); $categories->setKey('id'); $this->registerView($categories); @@ -46,6 +50,7 @@ public function __construct($app = null, $menu = null) if ($params->get('sef_advanced', 0)) { $this->attachRule(new JComponentRouterRulesStandard($this)); + $this->attachRule(new JComponentRouterRulesNomenu($this)); } else { @@ -68,7 +73,20 @@ public function getCategorySegment($id, $query) if ($category) { - return array_reverse($category->getPath()); + if ($this->noIDs) + { + $path = array_reverse($category->getPath(), true); + foreach ($path as &$segment) + { + list($id, $segment) = explode(':', $segment, 2); + } + + return $path; + } + else + { + return array_reverse($category->getPath(), true); + } } return array(); @@ -97,7 +115,28 @@ public function getCategoriesSegment($id, $query) */ public function getArticleSegment($id, $query) { - return array($id); + if ($this->noIDs) + { + if (strpos($id, ':')) + { + list($void, $segment) = explode(':', $id, 2); + + return array($void => $segment); + } + else + { + $db = JFactory::getDbo(); + $dbquery = $db->getQuery(true); + $dbquery->select($dbquery->qn('alias')) + ->from($dbquery->qn('#__content')) + ->where('id = ' . $dbquery->q($id)); + $db->setQuery($dbquery); + + return array($id => $id . ':' . $db->loadResult()); + } + } + + return array((int) $id => $id); } /** @@ -116,9 +155,19 @@ public function getCategoryId($segment, $query) foreach ($category->getChildren() as $child) { - if ($child->id == (int) $segment) + if ($this->noIDs) { - return $child->id; + if ($child->alias == $segment) + { + return $child->id; + } + } + else + { + if ($child->id == (int) $segment) + { + return $child->id; + } } } } @@ -149,6 +198,19 @@ public function getCategoriesId($segment, $query) */ public function getArticleId($segment, $query) { + if ($this->noIDs) + { + $db = JFactory::getDbo(); + $dbquery = $db->getQuery(true); + $dbquery->select($dbquery->qn('id')) + ->from($dbquery->qn('#__content')) + ->where('alias = ' . $dbquery->q($segment)) + ->where('catid = ' . $dbquery->q($query['id'])); + $db->setQuery($dbquery); + + return (int) $db->loadResult(); + } + return (int) $segment; } } diff --git a/components/com_newsfeeds/router.php b/components/com_newsfeeds/router.php index 5435e097474e7..81b1d0ed60f50 100644 --- a/components/com_newsfeeds/router.php +++ b/components/com_newsfeeds/router.php @@ -16,6 +16,8 @@ */ class NewsfeedsRouter extends JComponentRouterView { + protected $noIDs = false; + /** * Newsfeeds Component router constructor * @@ -24,6 +26,8 @@ class NewsfeedsRouter extends JComponentRouterView */ public function __construct($app = null, $menu = null) { + $params = JComponentHelper::getParams('com_newsfeeds'); + $this->noIDs = (bool) $params->get('sef_ids'); $categories = new JComponentRouterViewconfiguration('categories'); $categories->setKey('id'); $this->registerView($categories); @@ -43,6 +47,7 @@ public function __construct($app = null, $menu = null) if ($params->get('sef_advanced', 0)) { $this->attachRule(new JComponentRouterRulesStandard($this)); + $this->attachRule(new JComponentRouterRulesNomenu($this)); } else { @@ -64,7 +69,20 @@ public function getCategorySegment($id, $query) $category = JCategories::getInstance($this->getName())->get($id); if ($category) { - return array_reverse($category->getPath()); + if ($this->noIDs) + { + $path = array_reverse($category->getPath(), true); + foreach ($path as &$segment) + { + list($id, $segment) = explode(':', $segment, 2); + } + + return $path; + } + else + { + return array_reverse($category->getPath(), true); + } } return array(); @@ -93,7 +111,28 @@ public function getCategoriesSegment($id, $query) */ public function getNewsfeedSegment($id, $query) { - return array($id); + if ($this->noIDs) + { + if (strpos($id, ':')) + { + list($void, $segment) = explode(':', $id, 2); + + return array($void => $segment); + } + else + { + $db = JFactory::getDbo(); + $dbquery = $db->getQuery(true); + $dbquery->select($dbquery->qn('alias')) + ->from($dbquery->qn('#__newsfeeds')) + ->where('id = ' . $dbquery->q((int) $id)); + $db->setQuery($dbquery); + + return array($id => $id . ':' . $db->loadResult()); + } + } + + return array((int) $id => $id); } /** @@ -112,9 +151,19 @@ public function getCategoryId($segment, $query) foreach ($category->getChildren() as $child) { - if ($child->id == (int) $segment) + if ($this->noIDs) { - return $child->id; + if ($child->alias == $segment) + { + return $child->id; + } + } + else + { + if ($child->id == (int) $segment) + { + return $child->id; + } } } } @@ -145,6 +194,19 @@ public function getCategoriesId($segment, $query) */ public function getNewsfeedId($segment, $query) { + if ($this->noIDs) + { + $db = JFactory::getDbo(); + $dbquery = $db->getQuery(true); + $dbquery->select($dbquery->qn('id')) + ->from($dbquery->qn('#__newsfeeds')) + ->where('alias = ' . $dbquery->q($segment)) + ->where('catid = ' . $dbquery->q($query['id'])); + $db->setQuery($dbquery); + + return (int) $db->loadResult(); + } + return (int) $segment; } } diff --git a/components/com_users/router.php b/components/com_users/router.php index 73fee2ac22bf3..61411937327d1 100644 --- a/components/com_users/router.php +++ b/components/com_users/router.php @@ -41,6 +41,7 @@ public function __construct($app = null, $menu = null) if ($params->get('sef_advanced', 0)) { $this->attachRule(new JComponentRouterRulesStandard($this)); + $this->attachRule(new JComponentRouterRulesNomenu($this)); } else { diff --git a/libraries/cms/component/router/rules/menu.php b/libraries/cms/component/router/rules/menu.php index da5304f31d29a..1f7af8bec9bc2 100644 --- a/libraries/cms/component/router/rules/menu.php +++ b/libraries/cms/component/router/rules/menu.php @@ -91,7 +91,7 @@ public function preprocess(&$query) $query['Itemid'] = $this->lookup[$language][$view . $layout]; return; } - foreach ($ids as $id) + foreach ($ids as $id => $segment) { if (isset($this->lookup[$language][$view . $layout][(int) $id])) { diff --git a/libraries/cms/component/router/rules/standard.php b/libraries/cms/component/router/rules/standard.php index 8562eaaa50f5d..fce7db66f4446 100644 --- a/libraries/cms/component/router/rules/standard.php +++ b/libraries/cms/component/router/rules/standard.php @@ -231,11 +231,11 @@ public function build(&$query, &$segments) $ids = array_shift($path); if ($views[$view]->nestable) { - foreach (array_reverse($ids) as $id) + foreach (array_reverse($ids) as $id => $segment) { if ($found2) { - $segments[] = str_replace(':', '-', $id); + $segments[] = str_replace(':', '-', $segment); } else { @@ -254,7 +254,7 @@ public function build(&$query, &$segments) } else { - $segments[] = str_replace(':', '-', $ids[0]); + $segments[] = str_replace(':', '-', array_shift($ids)); } } } diff --git a/libraries/legacy/categories/categories.php b/libraries/legacy/categories/categories.php index a7f392ddbba83..eef53d070dadb 100644 --- a/libraries/legacy/categories/categories.php +++ b/libraries/legacy/categories/categories.php @@ -732,7 +732,7 @@ public function setParent($parent) $this->_path = $parent->getPath(); } - $this->_path[] = $this->id . ':' . $this->alias; + $this->_path[$this->id] = $this->id . ':' . $this->alias; } if (count($parent->_children) > 1)