Skip to content

Commit

Permalink
Optimization and fix of multilingual associations and add layouts to …
Browse files Browse the repository at this point in the history
…com_content links (#20229)

* Revert #19681

* Revert #19683

* Remove addition query and check after #19314

* Add layout to com_content links

* Add layout to com_content article associations

* Add layout to category associations

* add advanced where clause param

* add advanced where clause for com_content article associations

* drone code formatting fix

* drone code formatting fix

* drone code formatting fix

* Line exceeds 150 characters

* PHPCS rules

* Remove parenthesis

* Change queryKey

* Fix typo

* Improve description
  • Loading branch information
Septdir authored and Michael Babker committed May 5, 2018
1 parent 29a2bd3 commit 0042b1a
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ abstract class CategoryHelperAssociation
*
* @param integer $id Id of the item
* @param string $extension Name of the component
* @param string $layout Category layout
*
* @return array Array of associations for the component categories
*
* @since 3.0
*/
public static function getCategoryAssociations($id = 0, $extension = 'com_content')
public static function getCategoryAssociations($id = 0, $extension = 'com_content', $layout = null)
{
$return = array();

Expand All @@ -46,11 +47,13 @@ public static function getCategoryAssociations($id = 0, $extension = 'com_conten
{
if (class_exists($helperClassname) && is_callable(array($helperClassname, 'getCategoryRoute')))
{
$return[$tag] = $helperClassname::getCategoryRoute($item, $tag);
$return[$tag] = $helperClassname::getCategoryRoute($item, $tag, $layout);
}
else
{
$return[$tag] = 'index.php?option=' . $extension . '&view=category&id=' . $item;
$viewLayout = $layout ? '&layout=' . $layout : '';

$return[$tag] = 'index.php?option=' . $extension . '&view=category&id=' . $item . $viewLayout;
}
}
}
Expand Down
102 changes: 48 additions & 54 deletions components/com_content/helpers/association.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,81 +20,75 @@
*/
abstract class ContentHelperAssociation extends CategoryHelperAssociation
{
/**
* Cached array of the content item id.
*
* @var array
* @since __DEPLOY_VERSION__
*/
protected static $filters = array();

/**
* Method to get the associations for a given item
*
* @param integer $id Id of the item
* @param string $view Name of the view
* @param integer $id Id of the item
* @param string $view Name of the view
* @param string $layout View layout
*
* @return array Array of associations for the item
*
* @since 3.0
*/
public static function getAssociations($id = 0, $view = null)
public static function getAssociations($id = 0, $view = null, $layout = null)
{
$jinput = JFactory::getApplication()->input;
$view = $view === null ? $jinput->get('view') : $view;
$id = empty($id) ? $jinput->getInt('id') : $id;
$user = JFactory::getUser();
$groups = implode(',', $user->getAuthorisedViewLevels());
$jinput = JFactory::getApplication()->input;
$view = $view === null ? $jinput->get('view') : $view;
$component = $jinput->getCmd('option');
$id = empty($id) ? $jinput->getInt('id') : $id;

if ($layout === null && $jinput->get('view') == $view && $component == 'com_content')
{
$layout = $jinput->get('layout', '', 'string');
}

if ($view === 'article')
{
if ($id)
{
if (!isset(static::$filters[$id]))
$user = JFactory::getUser();
$groups = implode(',', $user->getAuthorisedViewLevels());
$db = JFactory::getDbo();
$advClause = array();

// Filter by user groups
$advClause[] = 'c2.access IN (' . $groups . ')';

// Filter by current language
$advClause[] = 'c2.language != ' . $db->quote(JFactory::getLanguage()->getTag());

if (!$user->authorise('core.edit.state', 'com_content') && !$user->authorise('core.edit', 'com_content'))
{
// Filter by start and end dates.
$nullDate = $db->quote($db->getNullDate());
$date = JFactory::getDate();

$nowDate = $db->quote($date->toSql());

$advClause[] = '(c2.publish_up = ' . $nullDate . ' OR c2.publish_up <= ' . $nowDate . ')';
$advClause[] = '(c2.publish_down = ' . $nullDate . ' OR c2.publish_down >= ' . $nowDate . ')';

// Filter by published
$advClause[] = 'c2.state = 1';
}

$associations = JLanguageAssociations::getAssociations('com_content', '#__content', 'com_content.item', $id, 'id', 'alias', 'catid', $advClause);

$return = array();

foreach ($associations as $tag => $item)
{
$associations = JLanguageAssociations::getAssociations('com_content', '#__content', 'com_content.item', $id);

$return = array();

foreach ($associations as $tag => $item)
{
if ($item->language != JFactory::getLanguage()->getTag())
{
$arrId = explode(':', $item->id);
$assocId = $arrId[0];

$db = JFactory::getDbo();
$query = $db->getQuery(true)
->select($db->qn('state'))
->from($db->qn('#__content'))
->where($db->qn('id') . ' = ' . (int) $assocId)
->where($db->qn('access') . ' IN (' . $groups . ')');
$db->setQuery($query);

$result = (int) $db->loadResult();

if ($result > 0)
{
$return[$tag] = ContentHelperRoute::getArticleRoute($item->id, (int) $item->catid, $item->language);
}
}

static::$filters[$id] = $return;
}

if (count($associations) === 0)
{
static::$filters[$id] = array();
}
$return[$tag] = ContentHelperRoute::getArticleRoute($item->id, (int) $item->catid, $item->language, $layout);
}

return static::$filters[$id];
return $return;
}
}

if ($view === 'category' || $view === 'categories')
{
return self::getCategoryAssociations($id, 'com_content');
return self::getCategoryAssociations($id, 'com_content', $layout);
}

return array();
Expand All @@ -105,7 +99,7 @@ public static function getAssociations($id = 0, $view = null)
*
* @param integer $id Id of the article
*
* @return array An array containing the association URL and the related language object
* @return array An array containing the association URL and the related language object
*
* @since 3.7.0
*/
Expand Down
34 changes: 18 additions & 16 deletions components/com_content/helpers/route.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ abstract class ContentHelperRoute
* @param integer $id The route of the content item.
* @param integer $catid The category ID.
* @param integer $language The language code.
* @param string $layout The layout value.
*
* @return string The article route.
*
* @since 1.5
*/
public static function getArticleRoute($id, $catid = 0, $language = 0)
public static function getArticleRoute($id, $catid = 0, $language = 0, $layout = null)
{
// Create the link
$link = 'index.php?option=com_content&view=article&id=' . $id;
Expand All @@ -42,6 +43,11 @@ public static function getArticleRoute($id, $catid = 0, $language = 0)
$link .= '&lang=' . $language;
}

if ($layout)
{
$link .= '&layout=' . $layout;
}

return $link;
}

Expand All @@ -50,12 +56,13 @@ public static function getArticleRoute($id, $catid = 0, $language = 0)
*
* @param integer $catid The category ID.
* @param integer $language The language code.
* @param string $layout The layout value.
*
* @return string The article route.
*
* @since 1.5
*/
public static function getCategoryRoute($catid, $language = 0)
public static function getCategoryRoute($catid, $language = 0, $layout = null)
{
if ($catid instanceof JCategoryNode)
{
Expand All @@ -68,24 +75,19 @@ public static function getCategoryRoute($catid, $language = 0)

if ($id < 1)
{
$link = '';
return '';
}
else
{
$link = 'index.php?option=com_content&view=category&id=' . $id;

if ($language && $language !== '*' && JLanguageMultilang::isEnabled())
{
$link .= '&lang=' . $language;
}
$link = 'index.php?option=com_content&view=category&id=' . $id;

$jinput = JFactory::getApplication()->input;
$layout = $jinput->get('layout');
if ($language && $language !== '*' && JLanguageMultilang::isEnabled())
{
$link .= '&lang=' . $language;
}

if ($layout !== '')
{
$link .= '&layout=' . $layout;
}
if ($layout)
{
$link .= '&layout=' . $layout;
}

return $link;
Expand Down
17 changes: 14 additions & 3 deletions libraries/src/Language/Associations.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,22 @@ class Associations
* @param string $pk The name of the primary key in the given $table.
* @param string $aliasField If the table has an alias field set it here. Null to not use it
* @param string $catField If the table has a catid field set it here. Null to not use it
* @param array $advClause Additional advanced 'where' clause; use c as parent column key, c2 as associations column key
*
* @return array The associated items
* @return array The associated items
*
* @since 3.1
*
* @throws \Exception
*/
public static function getAssociations($extension, $tablename, $context, $id, $pk = 'id', $aliasField = 'alias', $catField = 'catid')
public static function getAssociations($extension, $tablename, $context, $id, $pk = 'id', $aliasField = 'alias', $catField = 'catid',
$advClause = array())
{
// To avoid doing duplicate database queries.
static $multilanguageAssociations = array();

// Multilanguage association array key. If the key is already in the array we don't need to run the query again, just return it.
$queryKey = implode('|', func_get_args());
$queryKey = md5(serialize(array_merge(array($extension, $tablename, $context, $id), $advClause)));

if (!isset($multilanguageAssociations[$queryKey]))
{
Expand Down Expand Up @@ -97,6 +99,15 @@ public static function getAssociations($extension, $tablename, $context, $id, $p
$query->where('c.extension = ' . $db->quote($extension));
}

// Advanced where clause
if (!empty($advClause))
{
foreach ($advClause as $clause)
{
$query->where($clause);
}
}

$db->setQuery($query);

try
Expand Down

0 comments on commit 0042b1a

Please sign in to comment.