These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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 | |||
14 | /** |
||
15 | * Languages Model class for the Localise component |
||
16 | * |
||
17 | * @since 1.0 |
||
18 | */ |
||
19 | class LocaliseModelLanguages extends JModelList |
||
20 | { |
||
21 | protected $filter_fields = array('tag', 'client', 'name'); |
||
22 | |||
23 | protected $context = 'com_localise.languages'; |
||
24 | |||
25 | protected $items; |
||
26 | |||
27 | protected $languages; |
||
28 | |||
29 | /** |
||
30 | * Method to auto-populate the model state. |
||
31 | * |
||
32 | * Note. Calling getState in this method will result in recursion. |
||
33 | * |
||
34 | * @param string $ordering An optional ordering field. |
||
35 | * @param string $direction An optional direction (asc|desc). |
||
36 | * |
||
37 | * @return void |
||
38 | * |
||
39 | * @since 1.6 |
||
40 | */ |
||
41 | protected function populateState($ordering = null, $direction = null) |
||
42 | { |
||
43 | $app = JFactory::getApplication(); |
||
44 | $data = $app->input->get('filters', array(), 'array'); |
||
45 | |||
46 | View Code Duplication | if (empty($data)) |
|
0 ignored issues
–
show
|
|||
47 | { |
||
48 | $data = array(); |
||
49 | $data['select'] = $app->getUserState('com_localise.select'); |
||
50 | $data['search'] = $app->getUserState('com_localise.languages.search'); |
||
51 | } |
||
52 | else |
||
53 | { |
||
54 | $app->setUserState('com_localise.select', $data['select']); |
||
55 | $app->setUserState('com_localise.languages.search', $data['search']); |
||
56 | } |
||
57 | |||
58 | $this->setState('filter.search', isset($data['search']['expr']) ? $data['search']['expr'] : ''); |
||
59 | |||
60 | $this->setState('filter.client', isset($data['select']['client']) ? $data['select']['client'] : ''); |
||
61 | |||
62 | $this->setState('filter.tag', isset($data['select']['tag']) ? $data['select']['tag'] : ''); |
||
63 | |||
64 | $this->setState('filter.name', isset($data['select']['name']) ? $data['select']['name'] : ''); |
||
65 | |||
66 | // Load the parameters. |
||
67 | $params = JComponentHelper::getParams('com_localise'); |
||
68 | $this->setState('params', $params); |
||
69 | |||
70 | // Call auto-populate parent method |
||
71 | parent::populateState('tag', 'asc'); |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * Method to get the row form. |
||
76 | * |
||
77 | * @return mixed JForm object on success, false on failure. |
||
78 | */ |
||
79 | View Code Duplication | public function getForm() |
|
80 | { |
||
81 | // Initialise variables. |
||
82 | $app = JFactory::getApplication(); |
||
83 | |||
84 | // Get the form. |
||
85 | jimport('joomla.form.form'); |
||
86 | JForm::addFormPath(JPATH_COMPONENT . '/models/forms'); |
||
87 | JForm::addFieldPath(JPATH_COMPONENT . '/models/fields'); |
||
88 | $form = JForm::getInstance('com_localise.languages', 'languages', array('control' => 'filters','event' => 'onPrepareForm')); |
||
89 | |||
90 | // Check for an error. |
||
91 | if (JError::isError($form)) |
||
92 | { |
||
93 | $this->setError($form->getMessage()); |
||
94 | |||
95 | return false; |
||
96 | } |
||
97 | |||
98 | // Check the session for previously entered form data. |
||
99 | $data = $app->getUserState('com_localise.select', array()); |
||
100 | |||
101 | // Bind the form data if present. |
||
102 | if (!empty($data)) |
||
103 | { |
||
104 | $form->bind(array('select' => $data)); |
||
105 | } |
||
106 | |||
107 | // Check the session for previously entered form data. |
||
108 | $data = $app->getUserState('com_localise.languages.search', array()); |
||
109 | |||
110 | // Bind the form data if present. |
||
111 | if (!empty($data)) |
||
112 | { |
||
113 | $form->bind(array('search' => $data)); |
||
114 | } |
||
115 | |||
116 | return $form; |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * Get the items (according the filters and the pagination) |
||
121 | * |
||
122 | * @return array array of object items |
||
123 | */ |
||
124 | View Code Duplication | public function getItems() |
|
125 | { |
||
126 | if (!isset($this->items)) |
||
127 | { |
||
128 | $languages = $this->getLanguages(); |
||
129 | $count = count($languages); |
||
130 | $start = $this->getState('list.start'); |
||
131 | $limit = $this->getState('list.limit'); |
||
132 | |||
133 | if ($start > $count) |
||
134 | { |
||
135 | $start = 0; |
||
136 | } |
||
137 | |||
138 | if ($limit == 0) |
||
139 | { |
||
140 | $start = 0; |
||
141 | $limit = null; |
||
142 | } |
||
143 | |||
144 | $this->items = array_slice($languages, $start, $limit); |
||
145 | } |
||
146 | |||
147 | return $this->items; |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * Get total number of languages (according to filters) |
||
152 | * |
||
153 | * @return int number of languages |
||
154 | */ |
||
155 | public function getTotal() |
||
156 | { |
||
157 | return count($this->getLanguages()); |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * Get all languages (according to filters) |
||
162 | * |
||
163 | * @return array array of object items |
||
164 | */ |
||
165 | protected function getLanguages() |
||
166 | { |
||
167 | if (!isset($this->languages)) |
||
168 | { |
||
169 | $this->languages = array(); |
||
170 | $client = $this->getState('filter.client'); |
||
171 | $tag = $this->getState('filter.tag'); |
||
172 | $search = $this->getState('filter.search'); |
||
173 | |||
174 | if (empty($client)) |
||
175 | { |
||
176 | $clients = array('site', 'administrator'); |
||
177 | |||
178 | if (LocaliseHelper::hasInstallation()) |
||
179 | { |
||
180 | $clients[] = 'installation'; |
||
181 | } |
||
182 | } |
||
183 | else |
||
184 | { |
||
185 | $clients = array($client); |
||
186 | } |
||
187 | |||
188 | foreach ($clients as $client) |
||
189 | { |
||
190 | if (empty($tag)) |
||
191 | { |
||
192 | $folders = JFolder::folders( |
||
193 | constant('LOCALISEPATH_' . strtoupper($client)) . '/language', |
||
194 | '.', |
||
195 | false, |
||
196 | false, |
||
197 | array('.svn', 'CVS','.DS_Store','__MACOSX','pdf_fonts','overrides') |
||
198 | ); |
||
199 | } |
||
200 | else |
||
201 | { |
||
202 | $folders = JFolder::folders( |
||
203 | constant('LOCALISEPATH_' . strtoupper($client)) . '/language', |
||
204 | '^' . $tag . '$', |
||
205 | false, |
||
206 | false, |
||
207 | array('.svn','CVS','.DS_Store','__MACOSX','pdf_fonts','overrides') |
||
208 | ); |
||
209 | } |
||
210 | |||
211 | foreach ($folders as $folder) |
||
212 | { |
||
213 | // Move to first |
||
214 | $id = LocaliseHelper::getFileId(constant('LOCALISEPATH_' . strtoupper($client)) . "/language/$folder/$folder.xml"); |
||
215 | |||
216 | // If it was not found a file. |
||
217 | if ($id < 1) |
||
218 | { |
||
219 | continue; |
||
220 | } |
||
221 | |||
222 | $model = JModelLegacy::getInstance('Language', 'LocaliseModel', array('ignore_request' => true)); |
||
223 | $model->setState('language.tag', $folder); |
||
224 | $model->setState('language.client', $client); |
||
225 | $model->setState('language.id', $id); |
||
226 | |||
227 | $language = $model->getItem(); |
||
228 | |||
229 | if (empty($search) || preg_match("/$search/i", $language->name)) |
||
230 | { |
||
231 | $this->languages[] = $language; |
||
232 | } |
||
233 | } |
||
234 | } |
||
235 | |||
236 | $ordering = $this->getState('list.ordering') |
||
237 | ? $this->getState('list.ordering') |
||
238 | : 'name'; |
||
239 | JArrayHelper::sortObjects( |
||
240 | $this->languages, |
||
241 | $ordering, $this->getState('list.direction') == 'desc' ? -1 : 1 |
||
242 | ); |
||
243 | } |
||
244 | |||
245 | return $this->languages; |
||
246 | } |
||
247 | |||
248 | /** |
||
249 | * Cleans out _localise table. |
||
250 | * |
||
251 | * @return bool True on success |
||
252 | * |
||
253 | * @throws Exception |
||
254 | * @since 1.0 |
||
255 | */ |
||
256 | public function purge() |
||
257 | { |
||
258 | // Get the localise data |
||
259 | $query = $this->_db->getQuery(true); |
||
260 | $query->select("l.id"); |
||
261 | $query->from("#__localise AS l"); |
||
262 | $query->join('LEFT', '#__assets AS ast ON ast.id = l.asset_id'); |
||
263 | $query->order('ast.rgt DESC'); |
||
264 | $this->_db->setQuery($query); |
||
265 | |||
266 | try |
||
267 | { |
||
268 | $data = $this->_db->loadObjectList(); |
||
269 | } |
||
270 | catch (RuntimeException $e) |
||
271 | { |
||
272 | throw new RuntimeException($e->getMessage()); |
||
273 | } |
||
274 | |||
275 | foreach ($data as $key => $value) |
||
276 | { |
||
277 | $id = $value->id; |
||
278 | |||
279 | // Get the localise table |
||
280 | $table = JTable::getInstance('Localise', 'LocaliseTable'); |
||
281 | |||
282 | // Load it before delete. |
||
283 | $table->load($id); |
||
284 | |||
285 | // Delete |
||
286 | try |
||
287 | { |
||
288 | $table->delete($id); |
||
289 | } |
||
290 | catch (RuntimeException $e) |
||
291 | { |
||
292 | throw new RuntimeException($e->getMessage()); |
||
293 | } |
||
294 | } |
||
295 | |||
296 | JFactory::getApplication()->enqueueMessage(JText::_('COM_LOCALISE_PURGE_SUCCESS')); |
||
297 | |||
298 | return true; |
||
299 | } |
||
300 | } |
||
301 |
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.