Skip to content

Commit

Permalink
Routing: Remove IDs from SEF URLs (#11320)
Browse files Browse the repository at this point in the history
Also fixes various bugs in the new router
  • Loading branch information
Hackwar authored and wilsonge committed Oct 5, 2016
1 parent 1f3db97 commit 9f7bca7
Show file tree
Hide file tree
Showing 15 changed files with 292 additions and 39 deletions.
14 changes: 14 additions & 0 deletions administrator/components/com_contact/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -996,6 +996,20 @@
<option value="1">JGLOBAL_SEF_ADVANCED_MODERN</option>
<option value="0">JGLOBAL_SEF_ADVANCED_LEGACY</option>
</field>

<field
name="sef_ids"
type="radio"
class="btn-group btn-group-yesno"
default="0"
label="JGLOBAL_SEF_NOIDS_LABEL"
description="JGLOBAL_SEF_NOIDS_DESC"
showon="sef_advanced:1"
filter="integer">
<option value="1">JYES</option>
<option value="0">JNO</option>
</field>

</fieldset>

<fieldset
Expand Down
13 changes: 13 additions & 0 deletions administrator/components/com_content/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1000,6 +1000,19 @@
<option value="0">JGLOBAL_SEF_ADVANCED_LEGACY</option>
</field>

<field
name="sef_ids"
type="radio"
class="btn-group btn-group-yesno"
default="0"
label="JGLOBAL_SEF_NOIDS_LABEL"
description="JGLOBAL_SEF_NOIDS_DESC"
showon="sef_advanced:1"
filter="integer">
<option value="1">JYES</option>
<option value="0">JNO</option>
</field>

</fieldset>

<fieldset
Expand Down
13 changes: 13 additions & 0 deletions administrator/components/com_newsfeeds/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,19 @@
<option value="0">JGLOBAL_SEF_ADVANCED_LEGACY</option>
</field>

<field
name="sef_ids"
type="radio"
class="btn-group btn-group-yesno"
default="0"
label="JGLOBAL_SEF_NOIDS_LABEL"
description="JGLOBAL_SEF_NOIDS_DESC"
showon="sef_advanced:1"
filter="integer">
<option value="1">JYES</option>
<option value="0">JNO</option>
</field>

</fieldset>

<fieldset
Expand Down
2 changes: 2 additions & 0 deletions administrator/language/en-GB/en-GB.ini
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,8 @@ JGLOBAL_SEF_ADVANCED_DESC="Modern Routing enables advanced features but may chan
JGLOBAL_SEF_ADVANCED_LABEL="URL Routing"
JGLOBAL_SEF_ADVANCED_LEGACY="Legacy"
JGLOBAL_SEF_ADVANCED_MODERN="Modern"
JGLOBAL_SEF_NOIDS_DESC="Remove the IDs from the URLs of this component."
JGLOBAL_SEF_NOIDS_LABEL="Remove IDs from URLs"
JGLOBAL_SELECT_ALLOW_DENY_GROUP="Change %s permission for %s group."
JGLOBAL_SELECT_AN_OPTION="Select an option"
JGLOBAL_SELECT_NO_RESULTS_MATCH="No results match"
Expand Down
70 changes: 66 additions & 4 deletions components/com_contact/router.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
class ContactRouter extends JComponentRouterView
{
protected $noIDs = false;

/**
* Search Component router constructor
*
Expand All @@ -24,6 +26,8 @@ class ContactRouter extends JComponentRouterView
*/
public function __construct($app = null, $menu = null)
{
$params = JComponentHelper::getParams('com_contact');
$this->noIDs = (bool) $params->get('sef_ids');
$categories = new JComponentRouterViewconfiguration('categories');
$categories->setKey('id');
$this->registerView($categories);
Expand All @@ -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
{
Expand All @@ -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();
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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;
}
}
}
}
Expand Down Expand Up @@ -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;
}
}
Expand Down
70 changes: 66 additions & 4 deletions components/com_content/router.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
class ContentRouter extends JComponentRouterView
{
protected $noIDs = false;

/**
* Content Component router constructor
*
Expand All @@ -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);
Expand All @@ -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
{
Expand All @@ -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();
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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;
}
}
}
}
Expand Down Expand Up @@ -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;
}
}
Expand Down
Loading

0 comments on commit 9f7bca7

Please sign in to comment.