-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optimization and fix of multilingual associations and add layouts to com_content links #20229
Changes from 13 commits
0d9b502
4e3ae20
01e6786
35f8ac0
9183852
1686ab3
d99dca5
79ed099
aa7c21e
0aac63a
85af069
cb41a47
a3406a2
425345e
bb21207
f4ca53e
9a40a4c
2b14e47
f5d85ff
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need imho for double parenthesis line 61 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @infograf768 At work it does not affect. Perhaps double parentheses were made for better code performance ? Remove double parenthesis in line 61 or leave? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
{ | ||
// 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(); | ||
|
@@ -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 | ||
*/ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 Array with advanced where clause use c as parent column key, c2 as associations column key | ||
* | ||
* @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 = implode('|', array($extension, $tablename, $context, $id)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mbabker I thought about that. But that key can turn out too long Current key
Key with
Key with all arguments
Choose, in this issue I do not have a principled position The key with all the parameters is the most accurate if we talk about caching, but I'm not sure that such precision is needed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Personally I'm not concerned with key length, I'm more concerned with ensuring accuracy in the code. I do think having the Make this the key: $queryKey = md5(serialize(array_merge(array($extension, $tablename, $context, $id), $advClause))); The key length is now consistent and has enough entropy that if you run this method with two different There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for the code. Great idea. Done |
||
|
||
if (!isset($multilanguageAssociations[$queryKey])) | ||
{ | ||
|
@@ -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 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove parenthesis
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done