helper.php
3.97 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
/**
* @package Joomla.Site
* @subpackage mod_languages
*
* @copyright Copyright (C) 2005 - 2019 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('MenusHelper', JPATH_ADMINISTRATOR . '/components/com_menus/helpers/menus.php');
/**
* Helper for mod_languages
*
* @since 1.6
*/
abstract class ModLanguagesHelper
{
/**
* Gets a list of available languages
*
* @param \Joomla\Registry\Registry &$params module params
*
* @return array
*/
public static function getList(&$params)
{
$user = JFactory::getUser();
$lang = JFactory::getLanguage();
$languages = JLanguageHelper::getLanguages();
$app = JFactory::getApplication();
$menu = $app->getMenu();
$active = $menu->getActive();
// Get menu home items
$homes = array();
$homes['*'] = $menu->getDefault('*');
foreach ($languages as $item)
{
$default = $menu->getDefault($item->lang_code);
if ($default && $default->language === $item->lang_code)
{
$homes[$item->lang_code] = $default;
}
}
// Load associations
$assoc = JLanguageAssociations::isEnabled();
if ($assoc)
{
if ($active)
{
$associations = MenusHelper::getAssociations($active->id);
}
// Load component associations
$option = $app->input->get('option');
$class = ucfirst(str_replace('com_', '', $option)) . 'HelperAssociation';
\JLoader::register($class, JPATH_SITE . '/components/' . $option . '/helpers/association.php');
if (class_exists($class) && is_callable(array($class, 'getAssociations')))
{
$cassociations = call_user_func(array($class, 'getAssociations'));
}
}
$levels = $user->getAuthorisedViewLevels();
$sitelangs = JLanguageHelper::getInstalledLanguages(0);
$multilang = JLanguageMultilang::isEnabled();
// Filter allowed languages
foreach ($languages as $i => &$language)
{
// Do not display language without frontend UI
if (!array_key_exists($language->lang_code, $sitelangs))
{
unset($languages[$i]);
}
// Do not display language without specific home menu
elseif (!isset($homes[$language->lang_code]))
{
unset($languages[$i]);
}
// Do not display language without authorized access level
elseif (isset($language->access) && $language->access && !in_array($language->access, $levels))
{
unset($languages[$i]);
}
else
{
$language->active = ($language->lang_code === $lang->getTag());
// Fetch language rtl
// If loaded language get from current JLanguage metadata
if ($language->active)
{
$language->rtl = $lang->isRtl();
}
// If not loaded language fetch metadata directly for performance
else
{
$languageMetadata = JLanguageHelper::getMetadata($language->lang_code);
$language->rtl = $languageMetadata['rtl'];
}
if ($multilang)
{
if (isset($cassociations[$language->lang_code]))
{
$language->link = JRoute::_($cassociations[$language->lang_code] . '&lang=' . $language->sef);
}
elseif (isset($associations[$language->lang_code]) && $menu->getItem($associations[$language->lang_code]))
{
$itemid = $associations[$language->lang_code];
$language->link = JRoute::_('index.php?lang=' . $language->sef . '&Itemid=' . $itemid);
}
elseif ($active && $active->language == '*')
{
$language->link = JRoute::_('index.php?lang=' . $language->sef . '&Itemid=' . $active->id);
}
else
{
if ($language->active)
{
$language->link = JUri::getInstance()->toString(array('path', 'query'));
}
else
{
$itemid = isset($homes[$language->lang_code]) ? $homes[$language->lang_code]->id : $homes['*']->id;
$language->link = JRoute::_('index.php?lang=' . $language->sef . '&Itemid=' . $itemid);
}
}
}
else
{
$language->link = JRoute::_('&Itemid=' . $homes['*']->id);
}
}
}
return $languages;
}
}