1 <?php
2 /**
3 * @package Joomla.Libraries
4 * @subpackage Language
5 *
6 * @copyright Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
7 * @license GNU General Public License version 2 or later; see LICENSE.txt
8 */
9
10 defined('JPATH_PLATFORM') or die;
11
12 use Joomla\Registry\Registry;
13
14 /**
15 * Utitlity class for associations in multilang
16 *
17 * @since 3.1
18 */
19 class JLanguageAssociations
20 {
21 /**
22 * Get the associations.
23 *
24 * @param string $extension The name of the component.
25 * @param string $tablename The name of the table.
26 * @param string $context The context
27 * @param integer $id The primary key value.
28 * @param string $pk The name of the primary key in the given $table.
29 * @param string $aliasField If the table has an alias field set it here. Null to not use it
30 * @param string $catField If the table has a catid field set it here. Null to not use it
31 *
32 * @return array The associated items
33 *
34 * @since 3.1
35 *
36 * @throws Exception
37 */
38 public static function getAssociations($extension, $tablename, $context, $id, $pk = 'id', $aliasField = 'alias', $catField = 'catid')
39 {
40 // To avoid doing duplicate database queries.
41 static $multilanguageAssociations = array();
42
43 // Multilanguage association array key. If the key is already in the array we don't need to run the query again, just return it.
44 $queryKey = implode('|', func_get_args());
45 if (!isset($multilanguageAssociations[$queryKey]))
46 {
47 $multilanguageAssociations[$queryKey] = array();
48
49 $db = JFactory::getDbo();
50 $categoriesExtraSql = (($tablename === '#__categories') ? ' AND c2.extension = ' . $db->quote($extension) : '');
51 $query = $db->getQuery(true)
52 ->select($db->quoteName('c2.language'))
53 ->from($db->quoteName($tablename, 'c'))
54 ->join('INNER', $db->quoteName('#__associations', 'a') . ' ON a.id = c.' . $db->quoteName($pk) . ' AND a.context=' . $db->quote($context))
55 ->join('INNER', $db->quoteName('#__associations', 'a2') . ' ON a.key = a2.key')
56 ->join('INNER', $db->quoteName($tablename, 'c2') . ' ON a2.id = c2.' . $db->quoteName($pk) . $categoriesExtraSql);
57
58 // Use alias field ?
59 if (!empty($aliasField))
60 {
61 $query->select(
62 $query->concatenate(
63 array(
64 $db->quoteName('c2.' . $pk),
65 $db->quoteName('c2.' . $aliasField),
66 ),
67 ':'
68 ) . ' AS ' . $db->quoteName($pk)
69 );
70 }
71 else
72 {
73 $query->select($db->quoteName('c2.' . $pk));
74 }
75
76 // Use catid field ?
77 if (!empty($catField))
78 {
79 $query->join(
80 'INNER',
81 $db->quoteName('#__categories', 'ca') . ' ON ' . $db->quoteName('c2.' . $catField) . ' = ca.id AND ca.extension = ' . $db->quote($extension)
82 )
83 ->select(
84 $query->concatenate(
85 array('ca.id', 'ca.alias'),
86 ':'
87 ) . ' AS ' . $db->quoteName($catField)
88 );
89 }
90
91 $query->where('c.' . $pk . ' = ' . (int) $id);
92 if ($tablename === '#__categories')
93 {
94 $query->where('c.extension = ' . $db->quote($extension));
95 }
96
97 $db->setQuery($query);
98
99 try
100 {
101 $items = $db->loadObjectList('language');
102 }
103 catch (RuntimeException $e)
104 {
105 throw new Exception($e->getMessage(), 500, $e);
106 }
107
108 if ($items)
109 {
110 foreach ($items as $tag => $item)
111 {
112 // Do not return itself as result
113 if ((int) $item->{$pk} !== $id)
114 {
115 $multilanguageAssociations[$queryKey][$tag] = $item;
116 }
117 }
118 }
119 }
120
121 return $multilanguageAssociations[$queryKey];
122 }
123
124 /**
125 * Method to determine if the language filter Associations parameter is enabled.
126 * This works for both site and administrator.
127 *
128 * @return boolean True if the parameter is implemented; false otherwise.
129 *
130 * @since 3.2
131 */
132 public static function isEnabled()
133 {
134 // Flag to avoid doing multiple database queries.
135 static $tested = false;
136
137 // Status of language filter parameter.
138 static $enabled = false;
139
140 if (JLanguageMultilang::isEnabled())
141 {
142 // If already tested, don't test again.
143 if (!$tested)
144 {
145 $plugin = JPluginHelper::getPlugin('system', 'languagefilter');
146
147 if (!empty($plugin))
148 {
149 $params = new Registry($plugin->params);
150 $enabled = (boolean) $params->get('item_associations', true);
151 }
152
153 $tested = true;
154 }
155 }
156
157 return $enabled;
158 }
159 }
160