-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
association.php
135 lines (115 loc) · 3.6 KB
/
association.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
/**
* @package Joomla.Site
* @subpackage com_content
*
* @copyright Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
defined('_JEXEC') or die;
JLoader::register('ContentHelper', JPATH_ADMINISTRATOR . '/components/com_content/helpers/content.php');
JLoader::register('ContentHelperRoute', JPATH_SITE . '/components/com_content/helpers/route.php');
JLoader::register('CategoryHelperAssociation', JPATH_ADMINISTRATOR . '/components/com_categories/helpers/association.php');
/**
* Content Component Association Helper
*
* @since 3.0
*/
abstract class ContentHelperAssociation extends CategoryHelperAssociation
{
/**
* Method to get the associations for a given item
*
* @param integer $id Id of the item
* @param string $view Name of the view
*
* @return array Array of associations for the item
*
* @since 3.0
*/
public static function getAssociations($id = 0, $view = 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());
if ($view === 'article')
{
if ($id)
{
$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('access IN (' . $groups . ')');
$db->setQuery($query);
$result = (int) $db->loadResult();
if ($result > 0)
{
$return[$tag] = ContentHelperRoute::getArticleRoute($item->id, (int) $item->catid, $item->language);
}
}
}
return $return;
}
}
if ($view === 'category' || $view === 'categories')
{
return self::getCategoryAssociations($id, 'com_content');
}
return array();
}
/**
* Method to display in frontend the associations for a given article
*
* @param integer $id Id of the article
*
* @return array An array containing the association URL and the related language object
*
* @since 3.7.0
*/
public static function displayAssociations($id)
{
$return = array();
if ($associations = self::getAssociations($id, 'article'))
{
$levels = JFactory::getUser()->getAuthorisedViewLevels();
$languages = JLanguageHelper::getLanguages();
foreach ($languages as $language)
{
// Do not display language when no association
if (empty($associations[$language->lang_code]))
{
continue;
}
// Do not display language without frontend UI
if (!array_key_exists($language->lang_code, JLanguageHelper::getInstalledLanguages(0)))
{
continue;
}
// Do not display language without specific home menu
if (!array_key_exists($language->lang_code, JLanguageMultilang::getSiteHomePages()))
{
continue;
}
// Do not display language without authorized access level
if (isset($language->access) && $language->access && !in_array($language->access, $levels))
{
continue;
}
$return[$language->lang_code] = array('item' => $associations[$language->lang_code], 'language' => $language);
}
}
return $return;
}
}