Completed
Push — develop ( 499afc...0823ec )
by Luis
20s queued 14s
created

RControllerAdminBase::validateRequestCids()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 6
c 2
b 0
f 0
dl 0
loc 16
rs 10
cc 4
nc 4
nop 0
1
<?php
2
/**
3
 * @package     Redcore
4
 * @subpackage  Controller
5
 *
6
 * @copyright   Copyright (C) 2008 - 2016 redCOMPONENT.com. All rights reserved.
7
 * @license     GNU General Public License version 2 or later, see LICENSE.
8
 */
9
10
defined('JPATH_REDCORE') or die;
11
12
JLoader::import('joomla.application.component.controlleradmin');
13
14
use Joomla\Utilities\ArrayHelper;
15
16
/**
17
 * Base Controller Admin class.
18
 *
19
 * @package     Redcore
20
 * @subpackage  Controller
21
 * @since       1.0.0
22
 */
23
abstract class RControllerAdminBase extends JControllerAdmin
24
{
25
	/**
26
	 * The method => state map.
27
	 *
28
	 * @var    array
29
	 * @since  1.0.0
30
	 */
31
	protected $states = array(
32
		'publish' => 1,
33
		'unpublish' => 0,
34
		'archive' => 2,
35
		'trash' => -2,
36
		'report' => -3
37
	);
38
39
	/**
40
	 * Constructor.
41
	 *
42
	 * @param   array  $config  An optional associative array of configuration settings.
43
	 * @since   1.0.0
44
	 * @throws  \Exception
45
	 */
46
	public function __construct($config = array())
47
	{
48
		parent::__construct($config);
49
50
		// J2.5 compatibility
51
		if (empty($this->input))
52
		{
53
			$this->input = JFactory::getApplication()->input;
54
		}
55
	}
56
57
	/**
58
	 * Method to get a model object, loading it if required.
59
	 *
60
	 * @param   string  $name    The model name. Optional.
61
	 * @param   string  $prefix  The class prefix. Optional.
62
	 * @param   array   $config  Configuration array for model. Optional.
63
	 *
64
	 * @return  \RModel|false  The model.
65
	 * @since   1.0.0
66
	 */
67
	public function getModel($name = '', $prefix = '', $config = array('ignore_request' => true))
68
	{
69
		$class = get_class($this);
70
71
		if (empty($name))
72
		{
73
			$name = strstr($class, 'Controller');
74
			$name = str_replace('Controller', '', $name);
75
			$name = RInflector::singularize($name);
76
		}
77
78
		if (empty($prefix))
79
		{
80
			$prefix = strstr($class, 'Controller', true) . 'Model';
81
		}
82
83
		return parent::getModel($name, $prefix, $config);
0 ignored issues
show
Bug Best Practice introduced by
The expression return parent::getModel($name, $prefix, $config) returns the type boolean which is incompatible with the documented return type false|RModel.
Loading history...
84
	}
85
86
	/**
87
	 * Validate request and cid parameter
88
	 *
89
	 * @return   array|false
90
	 * @since    __DEPLOY_VERSION__
91
	 * @throws   \Exception
92
	 */
93
	protected function validateRequestCids()
94
	{
95
		// Check for request forgeries
96
		JSession::checkToken() or die(JText::_('JINVALID_TOKEN'));
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
97
98
		// Get items to remove from the request.
99
		$cid = JFactory::getApplication()->input->get('cid', array(), 'array');
100
101
		if (!is_array($cid) || count($cid) < 1)
102
		{
103
			JLog::add(JText::_($this->text_prefix . '_NO_ITEM_SELECTED'), JLog::WARNING, 'jerror');
104
105
			return false;
106
		}
107
108
		return $cid;
109
	}
110
111
	/**
112
	 * Removes an item.
113
	 *
114
	 * @return  void
115
	 * @since   1.0.0
116
	 * @throws  \Exception
117
	 */
118
	public function delete()
119
	{
120
		$cid = $this->validateRequestCids();
121
122
		if (!$cid)
0 ignored issues
show
introduced by
$cid is of type false|array, thus it always evaluated to false.
Loading history...
123
		{
124
			return;
125
		}
126
127
		// Get the model.
128
		/** @var \RModelList $model */
129
		$model = $this->getModel();
130
131
		// Make sure the item ids are integers
132
		jimport('joomla.utilities.arrayhelper');
133
		ArrayHelper::toInteger($cid);
134
135
		// Remove the items.
136
		if ($model->delete($cid))
137
		{
138
			$this->setMessage(JText::plural($this->text_prefix . '_N_ITEMS_DELETED', count($cid)));
139
		}
140
		else
141
		{
142
			$this->setMessage($model->getError(), 'error');
143
		}
144
145
		// Invoke the postDelete method to allow for the child class to access the model.
146
		$this->postDeleteHook($model, $cid);
147
148
		// Set redirect
149
		$this->setRedirect($this->getRedirectToListRoute());
150
	}
151
152
	/**
153
	 * Method to publish a list of items
154
	 *
155
	 * @return  void
156
	 * @since   1.0.0
157
	 * @throws  \Exception
158
	 */
159
	public function publish()
160
	{
161
		$cid = $this->validateRequestCids();
162
163
		if (!$cid)
0 ignored issues
show
introduced by
$cid is of type false|array, thus it always evaluated to false.
Loading history...
164
		{
165
			return;
166
		}
167
168
		$value = ArrayHelper::getValue($this->states, $this->getTask(), 0, 'int');
169
170
		// Get the model.
171
		/** @var \RModelList $model */
172
		$model = $this->getModel();
173
174
		// Make sure the item ids are integers
175
		ArrayHelper::toInteger($cid);
176
177
		// Publish the items.
178
		try
179
		{
180
			if ($model->publish($cid, $value))
181
			{
182
				switch ($this->getTask())
183
				{
184
					case 'publish':
185
						$ntext = $this->text_prefix . '_N_ITEMS_PUBLISHED';
186
						break;
187
188
					case 'unpublish':
189
						$ntext = $this->text_prefix . '_N_ITEMS_UNPUBLISHED';
190
						break;
191
192
					case 'archive':
193
						$ntext = $this->text_prefix . '_N_ITEMS_ARCHIVED';
194
						break;
195
196
					case 'trash':
197
						$ntext = $this->text_prefix . '_N_ITEMS_TRASHED';
198
						break;
199
200
					case 'report':
201
						$ntext = $this->text_prefix . '_N_ITEMS_REPORTED';
202
						break;
203
				}
204
205
				$this->setMessage(JText::plural($ntext, count($cid)));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $ntext does not seem to be defined for all execution paths leading up to this point.
Loading history...
206
			}
207
			else
208
			{
209
				$this->setMessage($model->getError(), 'error');
210
			}
211
		}
212
		catch (Exception $e)
213
		{
214
			$this->setMessage(JText::_('JLIB_DATABASE_ERROR_ANCESTOR_NODES_LOWER_STATE'), 'error');
215
		}
216
217
		$extension    = $this->input->get('extension');
218
		$extensionURL = ($extension) ? '&extension=' . $extension : '';
219
220
		// Set redirect
221
		$this->setRedirect($this->getRedirectToListRoute($extensionURL));
222
	}
223
224
	/**
225
	 * Changes the order of one or more records.
226
	 *
227
	 * @return  boolean  True on success
228
	 * @since   1.0.0
229
	 * @throws  \Exception
230
	 */
231
	public function reorder()
232
	{
233
		$cid = $this->validateRequestCids();
234
235
		if (!$cid)
0 ignored issues
show
introduced by
$cid is of type false|array, thus it always evaluated to false.
Loading history...
236
		{
237
			return false;
238
		}
239
240
		$inc = ($this->getTask() == 'orderup') ? -1 : 1;
241
242
		/** @var \RModelAdmin $model */
243
		$model  = $this->getModel();
244
		$return = $model->reorder($cid, $inc);
245
246
		if ($return === false)
247
		{
248
			// Reorder failed.
249
			$message = JText::sprintf('JLIB_APPLICATION_ERROR_REORDER_FAILED', $model->getError());
250
251
			// Set redirect
252
			$this->setRedirect($this->getRedirectToListRoute(), $message, 'error');
253
254
			return false;
255
		}
256
257
		else
258
		{
259
			// Reorder succeeded.
260
			$message = JText::_('JLIB_APPLICATION_SUCCESS_ITEM_REORDERED');
261
262
			// Set redirect
263
			$this->setRedirect($this->getRedirectToListRoute(), $message);
264
265
			return true;
266
		}
267
	}
268
269
	/**
270
	 * Method to save the submitted ordering values for records.
271
	 *
272
	 * @return  boolean  True on success
273
	 * @since   1.0.0
274
	 * @throws  \Exception
275
	 */
276
	public function saveorder()
277
	{
278
		$cid = $this->validateRequestCids();
279
280
		if (!$cid)
0 ignored issues
show
introduced by
$cid is of type false|array, thus it always evaluated to false.
Loading history...
281
		{
282
			return false;
283
		}
284
285
		$order = $this->input->post->get('order', array(), 'array');
286
287
		// Sanitize the input
288
		ArrayHelper::toInteger($cid);
289
		ArrayHelper::toInteger($order);
290
291
		// Get the model
292
		/** @var \RModelAdmin $model */
293
		$model = $this->getModel();
294
295
		// Save the ordering
296
		$return = $model->saveorder($cid, $order);
297
298
		if ($return === false)
299
		{
300
			// Reorder failed
301
			$message = JText::sprintf('JLIB_APPLICATION_ERROR_REORDER_FAILED', $model->getError());
302
303
			// Set redirect
304
			$this->setRedirect($this->getRedirectToListRoute(), $message, 'error');
305
306
			return false;
307
		}
308
309
		else
310
		{
311
			// Reorder succeeded.
312
			$this->setMessage(JText::_('JLIB_APPLICATION_SUCCESS_ORDERING_SAVED'));
313
314
			// Set redirect
315
			$this->setRedirect($this->getRedirectToListRoute());
316
317
			return true;
318
		}
319
	}
320
321
	/**
322
	 * Get the JRoute object for a redirect to list.
323
	 *
324
	 * @param   string  $append  An optional string to append to the route
325
	 *
326
	 * @return  string
327
	 * @since   1.0.0
328
	 */
329
	protected function getRedirectToListRoute($append = null)
330
	{
331
		$returnUrl = $this->input->get('return', '', 'Base64');
332
333
		if ($returnUrl)
334
		{
335
			$returnUrl = base64_decode($returnUrl);
336
337
			return JRoute::_($returnUrl . $append, false);
338
		}
339
		else
340
		{
341
			return JRoute::_('index.php?option=' . $this->option . '&view=' . $this->view_list . $append, false);
342
		}
343
	}
344
345
	/**
346
	 * Check in of one or more records.
347
	 *
348
	 * @return  boolean  True on success
349
	 * @since   1.0.0
350
	 * @throws  \Exception
351
	 */
352
	public function checkin()
353
	{
354
		$cid = $this->validateRequestCids();
355
356
		if (!$cid)
0 ignored issues
show
introduced by
$cid is of type false|array, thus it always evaluated to false.
Loading history...
357
		{
358
			return false;
359
		}
360
361
		/** @var \RModelAdmin $model */
362
		$model  = $this->getModel();
363
		$return = $model->checkin($cid);
364
365
		if ($return === false)
366
		{
367
			// Checkin failed.
368
			$message = JText::sprintf('JLIB_APPLICATION_ERROR_CHECKIN_FAILED', $model->getError());
369
370
			// Set redirect
371
			$this->setRedirect($this->getRedirectToListRoute(), $message, 'error');
372
373
			return false;
374
		}
375
		else
376
		{
377
			// Checkin succeeded.
378
			$message = JText::plural($this->text_prefix . '_N_ITEMS_CHECKED_IN', count($cid));
379
380
			// Set redirect
381
			$this->setRedirect($this->getRedirectToListRoute(), $message);
382
383
			return true;
384
		}
385
	}
386
387
	/**
388
	 * Method to save the submitted ordering values for records via AJAX.
389
	 *
390
	 * @return	void
391
	 * @since   1.0.0
392
	 * @throws  \Exception
393
	 */
394
	public function saveOrderAjax()
395
	{
396
		$cid = $this->validateRequestCids();
397
398
		if (!$cid)
0 ignored issues
show
introduced by
$cid is of type false|array, thus it always evaluated to false.
Loading history...
399
		{
400
			return;
401
		}
402
403
		// Get the input
404
		$order = $this->input->post->get('order', array(), 'array');
405
406
		// Sanitize the input
407
		ArrayHelper::toInteger($cid);
408
		ArrayHelper::toInteger($order);
409
410
		// Get the model
411
		/** @var \RModelAdmin $model */
412
		$model = $this->getModel();
413
414
		// Save the ordering
415
		$return = $model->saveorder($cid, $order);
416
417
		if ($return)
418
		{
419
			echo "1";
420
		}
421
422
		// Close the application
423
		JFactory::getApplication()->close();
424
	}
425
}
426