1 <?php
2 3 4 5 6 7 8
9
10 defined('JPATH_PLATFORM') or die;
11
12 use Joomla\Utilities\ArrayHelper;
13
14 15 16 17 18 19 20 21
22 class JControllerAdmin extends JControllerLegacy
23 {
24 25 26 27 28 29
30 protected $option;
31
32 33 34 35 36 37
38 protected $text_prefix;
39
40 41 42 43 44 45
46 protected $view_list;
47
48 49 50 51 52 53 54 55 56
57 public function __construct($config = array())
58 {
59 parent::__construct($config);
60
61
62
63
64 $this->registerTask('unpublish', 'publish');
65
66
67 $this->registerTask('archive', 'publish');
68
69
70 $this->registerTask('trash', 'publish');
71
72
73 $this->registerTask('report', 'publish');
74 $this->registerTask('orderup', 'reorder');
75 $this->registerTask('orderdown', 'reorder');
76
77
78 if (empty($this->option))
79 {
80 $this->option = 'com_' . strtolower($this->getName());
81 }
82
83
84 if (empty($this->text_prefix))
85 {
86 $this->text_prefix = strtoupper($this->option);
87 }
88
89
90 if (empty($this->view_list))
91 {
92 $r = null;
93
94 if (!preg_match('/(.*)Controller(.*)/i', get_class($this), $r))
95 {
96 throw new Exception(JText::_('JLIB_APPLICATION_ERROR_CONTROLLER_GET_NAME'), 500);
97 }
98
99 $this->view_list = strtolower($r[2]);
100 }
101 }
102
103 104 105 106 107 108 109
110 public function delete()
111 {
112
113 JSession::checkToken() or die(JText::_('JINVALID_TOKEN'));
114
115
116 $cid = $this->input->get('cid', array(), 'array');
117
118 if (!is_array($cid) || count($cid) < 1)
119 {
120 JLog::add(JText::_($this->text_prefix . '_NO_ITEM_SELECTED'), JLog::WARNING, 'jerror');
121 }
122 else
123 {
124
125 $model = $this->getModel();
126
127
128 $cid = ArrayHelper::toInteger($cid);
129
130
131 if ($model->delete($cid))
132 {
133 $this->setMessage(JText::plural($this->text_prefix . '_N_ITEMS_DELETED', count($cid)));
134 }
135 else
136 {
137 $this->setMessage($model->getError(), 'error');
138 }
139
140
141 $this->postDeleteHook($model, $cid);
142 }
143
144 $this->setRedirect(JRoute::_('index.php?option=' . $this->option . '&view=' . $this->view_list, false));
145 }
146
147 148 149 150 151 152 153 154 155 156 157
158 protected function postDeleteHook(JModelLegacy $model, $id = null)
159 {
160 }
161
162 163 164 165 166 167 168 169 170 171
172 public function display($cachable = false, $urlparams = array())
173 {
174 return $this;
175 }
176
177 178 179 180 181 182 183
184 public function publish()
185 {
186
187 JSession::checkToken() or die(JText::_('JINVALID_TOKEN'));
188
189
190 $cid = $this->input->get('cid', array(), 'array');
191 $data = array('publish' => 1, 'unpublish' => 0, 'archive' => 2, 'trash' => -2, 'report' => -3);
192 $task = $this->getTask();
193 $value = ArrayHelper::getValue($data, $task, 0, 'int');
194
195 if (empty($cid))
196 {
197 JLog::add(JText::_($this->text_prefix . '_NO_ITEM_SELECTED'), JLog::WARNING, 'jerror');
198 }
199 else
200 {
201
202 $model = $this->getModel();
203
204
205 $cid = ArrayHelper::toInteger($cid);
206
207
208 try
209 {
210 $model->publish($cid, $value);
211 $errors = $model->getErrors();
212 $ntext = null;
213
214 if ($value == 1)
215 {
216 if ($errors)
217 {
218 JFactory::getApplication()->enqueueMessage(JText::plural($this->text_prefix . '_N_ITEMS_FAILED_PUBLISHING', count($cid)), 'error');
219 }
220 else
221 {
222 $ntext = $this->text_prefix . '_N_ITEMS_PUBLISHED';
223 }
224 }
225 elseif ($value == 0)
226 {
227 $ntext = $this->text_prefix . '_N_ITEMS_UNPUBLISHED';
228 }
229 elseif ($value == 2)
230 {
231 $ntext = $this->text_prefix . '_N_ITEMS_ARCHIVED';
232 }
233 else
234 {
235 $ntext = $this->text_prefix . '_N_ITEMS_TRASHED';
236 }
237
238 if ($ntext !== null)
239 {
240 $this->setMessage(JText::plural($ntext, count($cid)));
241 }
242 }
243 catch (Exception $e)
244 {
245 $this->setMessage($e->getMessage(), 'error');
246 }
247 }
248
249 $extension = $this->input->get('extension');
250 $extensionURL = $extension ? '&extension=' . $extension : '';
251 $this->setRedirect(JRoute::_('index.php?option=' . $this->option . '&view=' . $this->view_list . $extensionURL, false));
252 }
253
254 255 256 257 258 259 260
261 public function reorder()
262 {
263
264 JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN'));
265
266 $ids = $this->input->post->get('cid', array(), 'array');
267 $inc = ($this->getTask() == 'orderup') ? -1 : 1;
268
269 $model = $this->getModel();
270 $return = $model->reorder($ids, $inc);
271
272 if ($return === false)
273 {
274
275 $message = JText::sprintf('JLIB_APPLICATION_ERROR_REORDER_FAILED', $model->getError());
276 $this->setRedirect(JRoute::_('index.php?option=' . $this->option . '&view=' . $this->view_list, false), $message, 'error');
277
278 return false;
279 }
280 else
281 {
282
283 $message = JText::_('JLIB_APPLICATION_SUCCESS_ITEM_REORDERED');
284 $this->setRedirect(JRoute::_('index.php?option=' . $this->option . '&view=' . $this->view_list, false), $message);
285
286 return true;
287 }
288 }
289
290 291 292 293 294 295 296
297 public function saveorder()
298 {
299
300 JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN'));
301
302
303 $pks = $this->input->post->get('cid', array(), 'array');
304 $order = $this->input->post->get('order', array(), 'array');
305
306
307 $pks = ArrayHelper::toInteger($pks);
308 $order = ArrayHelper::toInteger($order);
309
310
311 $model = $this->getModel();
312
313
314 $return = $model->saveorder($pks, $order);
315
316 if ($return === false)
317 {
318
319 $message = JText::sprintf('JLIB_APPLICATION_ERROR_REORDER_FAILED', $model->getError());
320 $this->setRedirect(JRoute::_('index.php?option=' . $this->option . '&view=' . $this->view_list, false), $message, 'error');
321
322 return false;
323 }
324 else
325 {
326
327 $this->setMessage(JText::_('JLIB_APPLICATION_SUCCESS_ORDERING_SAVED'));
328 $this->setRedirect(JRoute::_('index.php?option=' . $this->option . '&view=' . $this->view_list, false));
329
330 return true;
331 }
332 }
333
334 335 336 337 338 339 340
341 public function checkin()
342 {
343
344 JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN'));
345
346 $ids = $this->input->post->get('cid', array(), 'array');
347
348 $model = $this->getModel();
349 $return = $model->checkin($ids);
350
351 if ($return === false)
352 {
353
354 $message = JText::sprintf('JLIB_APPLICATION_ERROR_CHECKIN_FAILED', $model->getError());
355 $this->setRedirect(JRoute::_('index.php?option=' . $this->option . '&view=' . $this->view_list, false), $message, 'error');
356
357 return false;
358 }
359 else
360 {
361
362 $message = JText::plural($this->text_prefix . '_N_ITEMS_CHECKED_IN', count($ids));
363 $this->setRedirect(JRoute::_('index.php?option=' . $this->option . '&view=' . $this->view_list, false), $message);
364
365 return true;
366 }
367 }
368
369 370 371 372 373 374 375
376 public function saveOrderAjax()
377 {
378
379 $pks = $this->input->post->get('cid', array(), 'array');
380 $order = $this->input->post->get('order', array(), 'array');
381
382
383 $pks = ArrayHelper::toInteger($pks);
384 $order = ArrayHelper::toInteger($order);
385
386
387 $model = $this->getModel();
388
389
390 $return = $model->saveorder($pks, $order);
391
392 if ($return)
393 {
394 echo '1';
395 }
396
397
398 JFactory::getApplication()->close();
399 }
400 }
401