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 controller |
||
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 | /** |
||
13 | * Packages Controller class for the Localise component |
||
14 | * |
||
15 | * @package Extensions.Components |
||
16 | * @subpackage Localise |
||
17 | * @since 1.0 |
||
18 | */ |
||
19 | class LocaliseControllerPackages extends JControllerAdmin |
||
20 | { |
||
21 | /** |
||
22 | * The prefix to use with controller messages. |
||
23 | * |
||
24 | * @var string |
||
25 | * @since 12.2 |
||
26 | */ |
||
27 | protected $text_prefix = 'COM_LOCALISE_PACKAGES'; |
||
28 | |||
29 | /** |
||
30 | * Proxy for getModel. |
||
31 | * |
||
32 | * @param string $name The name of the model. |
||
33 | * @param string $prefix The prefix for the PHP class name. |
||
34 | * @param array $config The array of possible config values. Optional. |
||
35 | * |
||
36 | * @return object The model. |
||
37 | */ |
||
38 | public function getModel($name = 'Packages', $prefix = 'LocaliseModel', $config = array('ignore_request' => true)) |
||
39 | { |
||
40 | return parent::getModel($name, $prefix, array('ignore_request' => true)); |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * Display View |
||
45 | * |
||
46 | * @param boolean $cachable Enable cache or not. |
||
47 | * @param array $urlparams todo: this params can probably be removed. |
||
48 | * |
||
49 | * @return void Display View |
||
50 | */ |
||
51 | public function display($cachable = false, $urlparams = array()) |
||
52 | { |
||
53 | JFactory::getApplication()->input->set('view', 'packages'); |
||
54 | parent::display($cachable); |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * Delete Packages |
||
59 | * |
||
60 | * @return void |
||
61 | */ |
||
62 | public function delete() |
||
63 | { |
||
64 | // Check for request forgeries. |
||
65 | JSession::checkToken() or die(JText::_('JINVALID_TOKEN')); |
||
66 | |||
67 | // Initialise variables. |
||
68 | $user = JFactory::getUser(); |
||
69 | $ids = JFactory::getApplication()->input->get('cid', array(), 'array'); |
||
70 | |||
71 | // Access checks. |
||
72 | foreach ($ids as $i => $id) |
||
73 | { |
||
74 | $path = LocaliseHelper::getFilePath($id); |
||
75 | $context = LocaliseHelper::isCorePackage($path) ? |
||
76 | 'package' : 'packagefile'; |
||
77 | $model = JModelLegacy::getInstance($context, 'LocaliseModel', array('ignore_request' => true)); |
||
78 | $model->setState("$context.id", $id); |
||
79 | $item = $model->getItem(); |
||
80 | |||
81 | if (!$item->standalone) |
||
82 | { |
||
83 | // Prune items that you can't delete. |
||
84 | unset($ids[$i]); |
||
85 | JError::raiseNotice(403, JText::_('COM_LOCALISE_ERROR_PACKAGES_DELETE')); |
||
86 | } |
||
87 | |||
88 | if (!$user->authorise('core.delete', 'com_localise.' . (int) $id)) |
||
89 | { |
||
90 | // Prune items that you can't delete. |
||
91 | unset($ids[$i]); |
||
92 | JError::raiseNotice(403, JText::_('JERROR_CORE_DELETE_NOT_PERMITTED')); |
||
93 | } |
||
94 | } |
||
95 | |||
96 | View Code Duplication | if (empty($ids)) |
|
0 ignored issues
–
show
|
|||
97 | { |
||
98 | $msg = JText::_('JERROR_NO_ITEMS_SELECTED'); |
||
99 | $type = 'error'; |
||
100 | } |
||
101 | else |
||
102 | { |
||
103 | // Get the model. |
||
104 | $model = $this->getModel(); |
||
105 | |||
106 | // Remove the items. |
||
107 | if (!$model->delete($ids)) |
||
108 | { |
||
109 | $msg = implode("<br />", $model->getErrors()); |
||
110 | $type = 'error'; |
||
111 | } |
||
112 | else |
||
113 | { |
||
114 | $msg = JText::sprintf('JCONTROLLER_N_ITEMS_DELETED', count($ids)); |
||
115 | $type = 'message'; |
||
116 | } |
||
117 | } |
||
118 | |||
119 | $this->setRedirect(JRoute::_('index.php?option=com_localise&view=packages', false), $msg, $type); |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * Export Packages |
||
124 | * |
||
125 | * @return void |
||
126 | */ |
||
127 | public function export() |
||
128 | { |
||
129 | // Check for request forgeries. |
||
130 | JSession::checkToken() or die(JText::_('JINVALID_TOKEN')); |
||
131 | |||
132 | // Initialise variables. |
||
133 | $user = JFactory::getUser(); |
||
134 | $ids = JFactory::getApplication()->input->get('cid', array(), 'array'); |
||
135 | |||
136 | // Access checks. |
||
137 | View Code Duplication | foreach ($ids as $i => $id) |
|
0 ignored issues
–
show
This code seems to be duplicated across your project.
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. ![]() |
|||
138 | { |
||
139 | if (!$user->authorise('core.create', 'com_localise.' . (int) $id)) |
||
140 | { |
||
141 | // Prune items that you can't export. |
||
142 | unset($ids[$i]); |
||
143 | JError::raiseNotice(403, JText::_('COM_LOCALISE_EXPORT_NOT_PERMITTED')); |
||
144 | } |
||
145 | } |
||
146 | |||
147 | if (empty($ids)) |
||
148 | { |
||
149 | $msg = JText::_('JERROR_NO_ITEMS_SELECTED'); |
||
150 | $type = 'error'; |
||
151 | } |
||
152 | else |
||
153 | { |
||
154 | // Get the model. |
||
155 | $model = $this->getModel(); |
||
156 | |||
157 | // Export the packages. |
||
158 | if (!$model->export($ids)) |
||
159 | { |
||
160 | $msg = implode("<br />", $model->getErrors()); |
||
161 | $type = 'error'; |
||
162 | } |
||
163 | } |
||
164 | |||
165 | $this->setRedirect(JRoute::_('index.php?option=com_localise&view=packages', false), $msg, $type); |
||
0 ignored issues
–
show
The variable
$msg does not seem to be defined for all execution paths leading up to this point.
If you define a variable conditionally, it can happen that it is not defined for all execution paths. Let’s take a look at an example: function myFunction($a) {
switch ($a) {
case 'foo':
$x = 1;
break;
case 'bar':
$x = 2;
break;
}
// $x is potentially undefined here.
echo $x;
}
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined. Available Fixes
![]() The variable
$type does not seem to be defined for all execution paths leading up to this point.
If you define a variable conditionally, it can happen that it is not defined for all execution paths. Let’s take a look at an example: function myFunction($a) {
switch ($a) {
case 'foo':
$x = 1;
break;
case 'bar':
$x = 2;
break;
}
// $x is potentially undefined here.
echo $x;
}
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined. Available Fixes
![]() |
|||
166 | } |
||
167 | |||
168 | /** |
||
169 | * Clone an existing package. |
||
170 | * |
||
171 | * @return void |
||
172 | */ |
||
173 | public function duplicate() |
||
174 | { |
||
175 | // Check for request forgeries |
||
176 | JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN')); |
||
177 | |||
178 | // Initialise variables. |
||
179 | $user = JFactory::getUser(); |
||
180 | $ids = JFactory::getApplication()->input->get('cid', array(), 'array'); |
||
181 | |||
182 | // Access checks. |
||
183 | View Code Duplication | foreach ($ids as $i => $id) |
|
0 ignored issues
–
show
This code seems to be duplicated across your project.
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. ![]() |
|||
184 | { |
||
185 | if (!$user->authorise('core.create', 'com_localise.' . (int) $id)) |
||
186 | { |
||
187 | // Prune items that you can't clone. |
||
188 | unset($ids[$i]); |
||
189 | JError::raiseNotice(403, JText::_('COM_LOCALISE_ERROR_PACKAGES_CLONE_NOT_PERMITTED')); |
||
190 | } |
||
191 | } |
||
192 | |||
193 | View Code Duplication | if (empty($ids)) |
|
0 ignored issues
–
show
This code seems to be duplicated across your project.
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. ![]() |
|||
194 | { |
||
195 | $msg = JText::_('JERROR_NO_ITEMS_SELECTED'); |
||
196 | $type = 'error'; |
||
197 | } |
||
198 | else |
||
199 | { |
||
200 | // Get the model. |
||
201 | $model = $this->getModel(); |
||
202 | |||
203 | // Clone the items. |
||
204 | if (!$model->duplicate($ids)) |
||
205 | { |
||
206 | $msg = implode("<br />", $model->getErrors()); |
||
207 | $type = 'error'; |
||
208 | } |
||
209 | else |
||
210 | { |
||
211 | $msg = JText::plural('COM_LOCALISE_N_PACKAGES_DUPLICATED', count($ids)); |
||
212 | $type = 'message'; |
||
213 | } |
||
214 | } |
||
215 | |||
216 | $this->setRedirect(JRoute::_('index.php?option=com_localise&view=packages', false), $msg, $type); |
||
217 | } |
||
218 | |||
219 | /** |
||
220 | * Check in override to checkin one record of either package or packagefile. |
||
221 | * |
||
222 | * @return boolean True on success |
||
223 | * |
||
224 | * @since 12.2 |
||
225 | */ |
||
226 | public function checkin() |
||
227 | { |
||
228 | // Check for request forgeries. |
||
229 | JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN')); |
||
230 | |||
231 | $ids = JFactory::getApplication()->input->post->get('cid', array(), 'array'); |
||
232 | |||
233 | // Checkin only first id if more than one ids are present. |
||
234 | $id = $ids[0]; |
||
235 | |||
236 | $path = LocaliseHelper::getFilePath($id); |
||
237 | $context = LocaliseHelper::isCorePackage($path) ? |
||
238 | 'package' : 'packagefile'; |
||
239 | $model = JModelLegacy::getInstance($context, 'LocaliseModel', array('ignore_request' => true)); |
||
240 | |||
241 | $return = $model->checkin($id); |
||
242 | |||
243 | if ($return === false) |
||
244 | { |
||
245 | // Checkin failed. |
||
246 | $message = JText::sprintf('JLIB_APPLICATION_ERROR_CHECKIN_FAILED', $model->getError()); |
||
247 | $this->setRedirect(JRoute::_('index.php?option=' . $this->option . '&view=' . $this->view_list, false), $message, 'error'); |
||
248 | |||
249 | return false; |
||
250 | } |
||
251 | else |
||
252 | { |
||
253 | // Checkin succeeded. |
||
254 | $message = JText::plural($this->text_prefix . '_N_ITEMS_CHECKED_IN', count($ids)); |
||
255 | $this->setRedirect(JRoute::_('index.php?option=' . $this->option . '&view=' . $this->view_list, false), $message); |
||
256 | |||
257 | return true; |
||
258 | } |
||
259 | } |
||
260 | } |
||
261 |
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.