languages.php
5.91 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<?php
/**
* @package Joomla.Administrator
* @subpackage com_installer
* @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;
jimport('joomla.updater.update');
use Joomla\String\StringHelper;
/**
* Languages Installer Model
*
* @since 2.5.7
*/
class InstallerModelLanguages extends JModelList
{
/**
* Language count
*
* @var integer
* @since 3.7.0
*/
private $languageCount;
/**
* Constructor override, defines a whitelist of column filters.
*
* @param array $config An optional associative array of configuration settings.
*
* @since 2.5.7
*/
public function __construct($config = array())
{
if (empty($config['filter_fields']))
{
$config['filter_fields'] = array(
'name',
'element',
);
}
parent::__construct($config);
}
/**
* Get the Update Site
*
* @since 3.7.0
*
* @return string The URL of the Accredited Languagepack Updatesite XML
*/
private function getUpdateSite()
{
$db = $this->getDbo();
$query = $db->getQuery(true)
->select($db->qn('us.location'))
->from($db->qn('#__extensions', 'e'))
->where($db->qn('e.type') . ' = ' . $db->q('package'))
->where($db->qn('e.element') . ' = ' . $db->q('pkg_en-GB'))
->where($db->qn('e.client_id') . ' = 0')
->join('LEFT', $db->qn('#__update_sites_extensions', 'use') . ' ON ' . $db->qn('use.extension_id') . ' = ' . $db->qn('e.extension_id'))
->join('LEFT', $db->qn('#__update_sites', 'us') . ' ON ' . $db->qn('us.update_site_id') . ' = ' . $db->qn('use.update_site_id'));
return $db->setQuery($query)->loadResult();
}
/**
* Method to get an array of data items.
*
* @return mixed An array of data items on success, false on failure.
*
* @since 3.7.0
*/
public function getItems()
{
// Get a storage key.
$store = $this->getStoreId();
// Try to load the data from internal storage.
if (isset($this->cache[$store]))
{
return $this->cache[$store];
}
try
{
// Load the list items and add the items to the internal cache.
$this->cache[$store] = $this->getLanguages();
}
catch (RuntimeException $e)
{
$this->setError($e->getMessage());
return false;
}
return $this->cache[$store];
}
/**
* Gets an array of objects from the updatesite.
*
* @return object[] An array of results.
*
* @since 3.0
* @throws RuntimeException
*/
protected function getLanguages()
{
$updateSite = $this->getUpdateSite();
// Check whether the updateserver is found
if (empty($updateSite))
{
JFactory::getApplication()->enqueueMessage(JText::_('COM_INSTALLER_MSG_WARNING_NO_LANGUAGES_UPDATESERVER'), 'warning');
return;
}
$http = new JHttp;
try
{
$response = $http->get($updateSite);
}
catch (RuntimeException $e)
{
$response = null;
}
if ($response === null || $response->code !== 200)
{
JFactory::getApplication()->enqueueMessage(JText::sprintf('COM_INSTALLER_MSG_ERROR_CANT_CONNECT_TO_UPDATESERVER', $updateSite), 'error');
return;
}
$updateSiteXML = simplexml_load_string($response->body);
$languages = array();
$search = strtolower($this->getState('filter.search'));
foreach ($updateSiteXML->extension as $extension)
{
$language = new stdClass;
foreach ($extension->attributes() as $key => $value)
{
$language->$key = (string) $value;
}
if ($search)
{
if (strpos(strtolower($language->name), $search) === false
&& strpos(strtolower($language->element), $search) === false)
{
continue;
}
}
$languages[$language->name] = $language;
}
// Workaround for php 5.3
$that = $this;
// Sort the array by value of subarray
usort(
$languages,
function($a, $b) use ($that)
{
$ordering = $that->getState('list.ordering');
if (strtolower($that->getState('list.direction')) === 'asc')
{
return StringHelper::strcmp($a->$ordering, $b->$ordering);
}
else
{
return StringHelper::strcmp($b->$ordering, $a->$ordering);
}
}
);
// Count the non-paginated list
$this->languageCount = count($languages);
$limit = ($this->getState('list.limit') > 0) ? $this->getState('list.limit') : $this->languageCount;
return array_slice($languages, $this->getStart(), $limit);
}
/**
* Returns a record count for the updatesite.
*
* @param JDatabaseQuery|string $query The query.
*
* @return integer Number of rows for query.
*
* @since 3.7.0
*/
protected function _getListCount($query)
{
return $this->languageCount;
}
/**
* Method to get a store id based on model configuration state.
*
* @param string $id A prefix for the store id.
*
* @return string A store id.
*
* @since 2.5.7
*/
protected function getStoreId($id = '')
{
// Compile the store id.
$id .= ':' . $this->getState('filter.search');
return parent::getStoreId($id);
}
/**
* Method to auto-populate the model state.
*
* Note. Calling getState in this method will result in recursion.
*
* @param string $ordering list order
* @param string $direction direction in the list
*
* @return void
*
* @since 2.5.7
*/
protected function populateState($ordering = 'name', $direction = 'asc')
{
$this->setState('filter.search', $this->getUserStateFromRequest($this->context . '.filter.search', 'filter_search', '', 'string'));
$this->setState('extension_message', JFactory::getApplication()->getUserState('com_installer.extension_message'));
parent::populateState($ordering, $direction);
}
/**
* Method to compare two languages in order to sort them.
*
* @param object $lang1 The first language.
* @param object $lang2 The second language.
*
* @return integer
*
* @since 3.7.0
*/
protected function compareLanguages($lang1, $lang2)
{
return strcmp($lang1->name, $lang2->name);
}
}