1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package Com_Localise |
4
|
|
|
* @subpackage model |
5
|
|
|
* |
6
|
|
|
* @copyright Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved. |
7
|
|
|
* @license GNU General Public License version 2 or later; see LICENSE.txt |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
defined('_JEXEC') or die; |
11
|
|
|
|
12
|
|
|
jimport('joomla.filesystem.folder'); |
13
|
|
|
jimport('joomla.filesystem.file'); |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Packages Model class for the Localise component |
17
|
|
|
* |
18
|
|
|
* @package Extensions.Components |
19
|
|
|
* @subpackage Localise |
20
|
|
|
* |
21
|
|
|
* @since 1.0 |
22
|
|
|
*/ |
23
|
|
|
class LocaliseModelPackages extends JModelList |
24
|
|
|
{ |
25
|
|
|
protected $context = 'com_localise.packages'; |
26
|
|
|
|
27
|
|
|
protected $items; |
28
|
|
|
|
29
|
|
|
protected $packages; |
30
|
|
|
|
31
|
|
|
protected $filter_fields = array('title', 'language', 'version', 'core'); |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Method to auto-populate the model state. |
35
|
|
|
* |
36
|
|
|
* Note. Calling getState in this method will result in recursion. |
37
|
|
|
* |
38
|
|
|
* @param string $ordering An optional ordering field. |
39
|
|
|
* @param string $direction An optional direction (asc|desc). |
40
|
|
|
* |
41
|
|
|
* @return void |
42
|
|
|
* |
43
|
|
|
* @since 1.6 |
44
|
|
|
*/ |
45
|
|
|
protected function populateState($ordering = null, $direction = null) |
46
|
|
|
{ |
47
|
|
|
$app = JFactory::getApplication(); |
48
|
|
|
$data = $app->input->get('filters', array(), 'array'); |
49
|
|
|
|
50
|
|
|
if (empty($data)) |
51
|
|
|
{ |
52
|
|
|
$data = array(); |
53
|
|
|
$data['search'] = $app->getUserState('com_localise.packages.search'); |
54
|
|
|
} |
55
|
|
|
else |
56
|
|
|
{ |
57
|
|
|
$app->setUserState('com_localise.packages.search', $data['search']); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$this->setState('filter.search', isset($data['search']['expr']) ? $data['search']['expr'] : ''); |
61
|
|
|
|
62
|
|
|
$this->setState('filter.title', isset($data['select']['title']) ? $data['select']['title'] : ''); |
63
|
|
|
|
64
|
|
|
$this->setState('filter.language', isset($data['select']['language']) ? $data['select']['language'] : ''); |
65
|
|
|
|
66
|
|
|
$this->setState('filter.version', isset($data['select']['version']) ? $data['select']['version'] : ''); |
67
|
|
|
|
68
|
|
|
$this->setState('filter.core', isset($data['select']['core']) ? $data['select']['core'] : ''); |
69
|
|
|
|
70
|
|
|
$params = JComponentHelper::getParams('com_localise'); |
71
|
|
|
$this->setState('params', $params); |
72
|
|
|
|
73
|
|
|
parent::populateState('title', 'asc'); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Get packages |
78
|
|
|
* |
79
|
|
|
* @return array |
80
|
|
|
*/ |
81
|
|
|
private function _getPackages() |
82
|
|
|
{ |
83
|
|
|
if (!isset($this->packages)) |
84
|
|
|
{ |
85
|
|
|
$search = $this->getState('filter.search'); |
86
|
|
|
$this->packages = array(); |
87
|
|
|
$paths = array ( |
88
|
|
|
JPATH_COMPONENT_ADMINISTRATOR . '/packages', |
89
|
|
|
JPATH_SITE . '/media/com_localise/packages', |
90
|
|
|
); |
91
|
|
|
|
92
|
|
|
foreach ($paths as $path) |
93
|
|
|
{ |
94
|
|
|
if (JFolder::exists($path)) |
95
|
|
|
{ |
96
|
|
|
$files = JFolder::files($path, '\.xml$'); |
97
|
|
|
|
98
|
|
|
foreach ($files as $file) |
99
|
|
|
{ |
100
|
|
|
$id = LocaliseHelper::getFileId("$path/$file"); |
101
|
|
|
$context = LocaliseHelper::isCorePackage("$path/$file") ? |
102
|
|
|
'package' : 'packagefile'; |
103
|
|
|
$model = JModelLegacy::getInstance($context, 'LocaliseModel', array('ignore_request' => true)); |
104
|
|
|
$model->setState("$context.id", $id); |
105
|
|
|
$package = $model->getItem(); |
106
|
|
|
|
107
|
|
|
if (empty($search) || preg_match("/$search/i", $package->title)) |
108
|
|
|
{ |
109
|
|
|
$this->packages[] = $package; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$ordering = $this->getState('list.ordering') ? $this->getState('list.ordering') : 'title'; |
116
|
|
|
JArrayHelper::sortObjects($this->packages, $ordering, $this->getState('list.direction') == 'desc' ? -1 : 1); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $this->packages; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Get Items |
124
|
|
|
* |
125
|
|
|
* @return array|mixed |
126
|
|
|
*/ |
127
|
|
View Code Duplication |
public function getItems() |
|
|
|
|
128
|
|
|
{ |
129
|
|
|
if (empty($this->items)) |
130
|
|
|
{ |
131
|
|
|
$packages = $this->_getPackages(); |
132
|
|
|
$count = count($packages); |
133
|
|
|
$start = $this->getState('list.start'); |
134
|
|
|
$limit = $this->getState('list.limit'); |
135
|
|
|
|
136
|
|
|
if ($start > $count) |
137
|
|
|
{ |
138
|
|
|
$start = 0; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
if ($limit == 0) |
142
|
|
|
{ |
143
|
|
|
$start = 0; |
144
|
|
|
$limit = null; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
$this->items = array_slice($packages, $start, $limit); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return $this->items; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Get Total |
155
|
|
|
* |
156
|
|
|
* @return int |
157
|
|
|
*/ |
158
|
|
|
public function getTotal() |
159
|
|
|
{ |
160
|
|
|
return count($this->_getPackages()); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Method to get the row form. |
165
|
|
|
* |
166
|
|
|
* @return mixed JForm object on success, false on failure. |
167
|
|
|
*/ |
168
|
|
View Code Duplication |
public function getForm() |
|
|
|
|
169
|
|
|
{ |
170
|
|
|
// Initialise variables. |
171
|
|
|
$app = JFactory::getApplication(); |
172
|
|
|
|
173
|
|
|
// Get the form. |
174
|
|
|
jimport('joomla.form.form'); |
175
|
|
|
JForm::addFormPath(JPATH_COMPONENT . '/models/forms'); |
176
|
|
|
JForm::addFieldPath(JPATH_COMPONENT . '/models/fields'); |
177
|
|
|
|
178
|
|
|
$form = JForm::getInstance('com_localise.packages', 'packages', array('control' => 'filters', 'event' => 'onPrepareForm')); |
179
|
|
|
|
180
|
|
|
// Check for an error. |
181
|
|
|
if (JError::isError($form)) |
182
|
|
|
{ |
183
|
|
|
$this->setError($form->getMessage()); |
184
|
|
|
|
185
|
|
|
return false; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
// Check the session for previously entered form data. |
189
|
|
|
$data = $app->getUserState('com_localise.select', array()); |
190
|
|
|
|
191
|
|
|
// Bind the form data if present. |
192
|
|
|
if (!empty($data)) |
193
|
|
|
{ |
194
|
|
|
$form->bind(array('select' => $data)); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
// Check the session for previously entered form data. |
198
|
|
|
$data = $app->getUserState('com_localise.packages.search', array()); |
199
|
|
|
|
200
|
|
|
// Bind the form data if present. |
201
|
|
|
if (!empty($data)) |
202
|
|
|
{ |
203
|
|
|
$form->bind(array('search' => $data)); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
return $form; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Remove packages |
211
|
|
|
* |
212
|
|
|
* @param array $selected array of selected packages |
213
|
|
|
* |
214
|
|
|
* @return boolean true for success, false for failure |
215
|
|
|
*/ |
216
|
|
|
public function delete($selected) |
217
|
|
|
{ |
218
|
|
|
// Sanitize the array. |
219
|
|
|
$selected = (array) $selected; |
220
|
|
|
|
221
|
|
|
// Get a row instance. |
222
|
|
|
$table = JTable::getInstance('Localise', 'LocaliseTable'); |
223
|
|
|
|
224
|
|
|
foreach ($selected as $packageId) |
225
|
|
|
{ |
226
|
|
|
$path = LocaliseHelper::getFilePath($packageId); |
227
|
|
|
$package = JFile::stripExt(basename($path)); |
228
|
|
|
|
229
|
|
|
if (!JFile::delete($path)) |
230
|
|
|
{ |
231
|
|
|
$this->setError(JText::sprintf('COM_LOCALISE_ERROR_PACKAGES_REMOVE', $package)); |
232
|
|
|
|
233
|
|
|
return false; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
if (!$table->delete((int) $packageId)) |
237
|
|
|
{ |
238
|
|
|
$this->setError($table->getError()); |
239
|
|
|
|
240
|
|
|
return false; |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
return true; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Export packages |
249
|
|
|
* |
250
|
|
|
* @param array $selected array of selected packages |
251
|
|
|
* |
252
|
|
|
* @return boolean success or failure |
253
|
|
|
*/ |
254
|
|
|
public function export($selected) |
255
|
|
|
{ |
256
|
|
|
foreach ($selected as $packageId) |
257
|
|
|
{ |
258
|
|
|
$path = LocaliseHelper::getFilePath($packageId); |
259
|
|
|
$package = JFile::stripExt(basename($path)); |
260
|
|
|
|
261
|
|
|
if (JFile::exists($path)) |
262
|
|
|
{ |
263
|
|
|
ob_clean(); |
264
|
|
|
$pack = file_get_contents($path); |
265
|
|
|
header("Expires: 0"); |
266
|
|
|
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); |
267
|
|
|
header('Content-Type: application/xml'); |
268
|
|
|
header('Content-Disposition: attachment; filename="' . $package . '.xml"'); |
269
|
|
|
header('Content-Length: ' . strlen($pack)); |
270
|
|
|
header("Cache-Control: maxage=1"); |
271
|
|
|
header("Pragma: public"); |
272
|
|
|
header("Content-Transfer-Encoding: binary"); |
273
|
|
|
echo $pack; |
274
|
|
|
exit; |
275
|
|
|
} |
276
|
|
|
else |
277
|
|
|
{ |
278
|
|
|
$this->setError(JText::sprintf('COM_LOCALISE_ERROR_PACKAGES_EXPORT', $package)); |
279
|
|
|
} |
280
|
|
|
} |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* Clone packages |
285
|
|
|
* |
286
|
|
|
* @param array $selected array of selected packages |
287
|
|
|
* |
288
|
|
|
* @return boolean success or failure |
289
|
|
|
*/ |
290
|
|
|
public function duplicate($selected) |
291
|
|
|
{ |
292
|
|
|
foreach ($selected as $packageId) |
293
|
|
|
{ |
294
|
|
|
$path = LocaliseHelper::getFilePath($packageId); |
295
|
|
|
$package = JFile::stripExt(basename($path)); |
296
|
|
|
|
297
|
|
|
if (JFile::exists($path)) |
298
|
|
|
{ |
299
|
|
|
$pack = file_get_contents($path); |
300
|
|
|
$newpackage = $package . '_' . JFactory::getDate()->format("Y-m-d-H-i-s"); |
301
|
|
|
$newpath = JPATH_COMPONENT_ADMINISTRATOR . "/packages/$newpackage.xml"; |
302
|
|
|
|
303
|
|
|
JFile::write($newpath, $pack); |
304
|
|
|
} |
305
|
|
|
else |
306
|
|
|
{ |
307
|
|
|
$this->setError(JText::sprintf('COM_LOCALISE_ERROR_PACKAGES_READ', $package)); |
308
|
|
|
|
309
|
|
|
return false; |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
if (!JFile::exists($newpath)) |
313
|
|
|
{ |
314
|
|
|
$this->setError(JText::sprintf('COM_LOCALISE_ERROR_PACKAGES_CLONE', $package)); |
315
|
|
|
|
316
|
|
|
return false; |
317
|
|
|
} |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
return true; |
321
|
|
|
} |
322
|
|
|
} |
323
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.