Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
admin.php
Aller à la documentation de ce fichier.
1 <?php
2 /**
3  * @package Joomla.Legacy
4  * @subpackage Controller
5  *
6  * @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
7  * @license GNU General Public License version 2 or later; see LICENSE
8  */
9 
10 defined('JPATH_PLATFORM') or die;
11 
12 /**
13  * Base class for a Joomla Administrator Controller
14  *
15  * Controller (controllers are where you put all the actual code) Provides basic
16  * functionality, such as rendering views (aka displaying templates).
17  *
18  * @package Joomla.Legacy
19  * @subpackage Controller
20  * @since 12.2
21  */
23 {
24  /**
25  * The URL option for the component.
26  *
27  * @var string
28  * @since 12.2
29  */
30  protected $option;
31 
32  /**
33  * The prefix to use with controller messages.
34  *
35  * @var string
36  * @since 12.2
37  */
38  protected $text_prefix;
39 
40  /**
41  * The URL view list variable.
42  *
43  * @var string
44  * @since 12.2
45  */
46  protected $view_list;
47 
48  /**
49  * Constructor.
50  *
51  * @param array $config An optional associative array of configuration settings.
52  *
53  * @see JControllerLegacy
54  * @since 12.2
55  * @throws Exception
56  */
57  public function __construct($config = array())
58  {
59  parent::__construct($config);
60 
61  // Define standard task mappings.
62 
63  // Value = 0
64  $this->registerTask('unpublish', 'publish');
65 
66  // Value = 2
67  $this->registerTask('archive', 'publish');
68 
69  // Value = -2
70  $this->registerTask('trash', 'publish');
71 
72  // Value = -3
73  $this->registerTask('report', 'publish');
74  $this->registerTask('orderup', 'reorder');
75  $this->registerTask('orderdown', 'reorder');
76 
77  // Guess the option as com_NameOfController.
78  if (empty($this->option))
79  {
80  $this->option = 'com_' . strtolower($this->getName());
81  }
82 
83  // Guess the JText message prefix. Defaults to the option.
84  if (empty($this->text_prefix))
85  {
86  $this->text_prefix = strtoupper($this->option);
87  }
88 
89  // Guess the list view as the suffix, eg: OptionControllerSuffix.
90  if (empty($this->view_list))
91  {
92  $r = null;
93  if (!preg_match('/(.*)Controller(.*)/i', get_class($this), $r))
94  {
95  throw new Exception(JText::_('JLIB_APPLICATION_ERROR_CONTROLLER_GET_NAME'), 500);
96  }
97  $this->view_list = strtolower($r[2]);
98  }
99  }
100 
101  /**
102  * Removes an item.
103  *
104  * @return void
105  *
106  * @since 12.2
107  */
108  public function delete()
109  {
110  // Check for request forgeries
111  JSession::checkToken() or die(JText::_('JINVALID_TOKEN'));
112 
113  // Get items to remove from the request.
114  $cid = JFactory::getApplication()->input->get('cid', array(), 'array');
115 
116  if (!is_array($cid) || count($cid) < 1)
117  {
118  JLog::add(JText::_($this->text_prefix . '_NO_ITEM_SELECTED'), JLog::WARNING, 'jerror');
119  }
120  else
121  {
122  // Get the model.
123  $model = $this->getModel();
124 
125  // Make sure the item ids are integers
126  jimport('joomla.utilities.arrayhelper');
128 
129  // Remove the items.
130  if ($model->delete($cid))
131  {
132  $this->setMessage(JText::plural($this->text_prefix . '_N_ITEMS_DELETED', count($cid)));
133  }
134  else
135  {
136  $this->setMessage($model->getError());
137  }
138  }
139  // Invoke the postDelete method to allow for the child class to access the model.
140  $this->postDeleteHook($model, $cid);
141 
142  $this->setRedirect(JRoute::_('index.php?option=' . $this->option . '&view=' . $this->view_list, false));
143  }
144 
145  /**
146  * Function that allows child controller access to model data
147  * after the item has been deleted.
148  *
149  * @param JModelLegacy $model The data model object.
150  * @param integer $id The validated data.
151  *
152  * @return void
153  *
154  * @since 12.2
155  */
156  protected function postDeleteHook(JModelLegacy $model, $id = null)
157  {
158  }
159 
160  /**
161  * Display is not supported by this controller.
162  *
163  * @param boolean $cachable If true, the view output will be cached
164  * @param array $urlparams An array of safe url parameters and their variable types, for valid values see {@link JFilterInput::clean()}.
165  *
166  * @return JControllerLegacy A JControllerLegacy object to support chaining.
167  *
168  * @since 12.2
169  */
170  public function display($cachable = false, $urlparams = array())
171  {
172  return $this;
173  }
174 
175  /**
176  * Method to publish a list of items
177  *
178  * @return void
179  *
180  * @since 12.2
181  */
182  public function publish()
183  {
184  // Check for request forgeries
185  JSession::checkToken() or die(JText::_('JINVALID_TOKEN'));
186 
187  // Get items to publish from the request.
188  $cid = JFactory::getApplication()->input->get('cid', array(), 'array');
189  $data = array('publish' => 1, 'unpublish' => 0, 'archive' => 2, 'trash' => -2, 'report' => -3);
190  $task = $this->getTask();
191  $value = JArrayHelper::getValue($data, $task, 0, 'int');
192 
193  if (empty($cid))
194  {
195  JLog::add(JText::_($this->text_prefix . '_NO_ITEM_SELECTED'), JLog::WARNING, 'jerror');
196  }
197  else
198  {
199  // Get the model.
200  $model = $this->getModel();
201 
202  // Make sure the item ids are integers
204 
205  // Publish the items.
206  try
207  {
208  $model->publish($cid, $value);
209 
210  if ($value == 1)
211  {
212  $ntext = $this->text_prefix . '_N_ITEMS_PUBLISHED';
213  }
214  elseif ($value == 0)
215  {
216  $ntext = $this->text_prefix . '_N_ITEMS_UNPUBLISHED';
217  }
218  elseif ($value == 2)
219  {
220  $ntext = $this->text_prefix . '_N_ITEMS_ARCHIVED';
221  }
222  else
223  {
224  $ntext = $this->text_prefix . '_N_ITEMS_TRASHED';
225  }
226  $this->setMessage(JText::plural($ntext, count($cid)));
227  }
228  catch (Exception $e)
229  {
230  $this->setMessage(JText::_('JLIB_DATABASE_ERROR_ANCESTOR_NODES_LOWER_STATE'), 'error');
231  }
232 
233  }
234  $extension = $this->input->get('extension');
235  $extensionURL = ($extension) ? '&extension=' . $extension : '';
236  $this->setRedirect(JRoute::_('index.php?option=' . $this->option . '&view=' . $this->view_list . $extensionURL, false));
237  }
238 
239  /**
240  * Changes the order of one or more records.
241  *
242  * @return boolean True on success
243  *
244  * @since 12.2
245  */
246  public function reorder()
247  {
248  // Check for request forgeries.
249  JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN'));
250 
251  $ids = JFactory::getApplication()->input->post->get('cid', array(), 'array');
252  $inc = ($this->getTask() == 'orderup') ? -1 : 1;
253 
254  $model = $this->getModel();
255  $return = $model->reorder($ids, $inc);
256  if ($return === false)
257  {
258  // Reorder failed.
259  $message = JText::sprintf('JLIB_APPLICATION_ERROR_REORDER_FAILED', $model->getError());
260  $this->setRedirect(JRoute::_('index.php?option=' . $this->option . '&view=' . $this->view_list, false), $message, 'error');
261  return false;
262  }
263  else
264  {
265  // Reorder succeeded.
266  $message = JText::_('JLIB_APPLICATION_SUCCESS_ITEM_REORDERED');
267  $this->setRedirect(JRoute::_('index.php?option=' . $this->option . '&view=' . $this->view_list, false), $message);
268  return true;
269  }
270  }
271 
272  /**
273  * Method to save the submitted ordering values for records.
274  *
275  * @return boolean True on success
276  *
277  * @since 12.2
278  */
279  public function saveorder()
280  {
281  // Check for request forgeries.
282  JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN'));
283 
284  // Get the input
285  $pks = $this->input->post->get('cid', array(), 'array');
286  $order = $this->input->post->get('order', array(), 'array');
287 
288  // Sanitize the input
290  JArrayHelper::toInteger($order);
291 
292  // Get the model
293  $model = $this->getModel();
294 
295  // Save the ordering
296  $return = $model->saveorder($pks, $order);
297 
298  if ($return === false)
299  {
300  // Reorder failed
301  $message = JText::sprintf('JLIB_APPLICATION_ERROR_REORDER_FAILED', $model->getError());
302  $this->setRedirect(JRoute::_('index.php?option=' . $this->option . '&view=' . $this->view_list, false), $message, 'error');
303  return false;
304  }
305  else
306  {
307  // Reorder succeeded.
308  $this->setMessage(JText::_('JLIB_APPLICATION_SUCCESS_ORDERING_SAVED'));
309  $this->setRedirect(JRoute::_('index.php?option=' . $this->option . '&view=' . $this->view_list, false));
310  return true;
311  }
312  }
313 
314  /**
315  * Check in of one or more records.
316  *
317  * @return boolean True on success
318  *
319  * @since 12.2
320  */
321  public function checkin()
322  {
323  // Check for request forgeries.
324  JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN'));
325 
326  $ids = JFactory::getApplication()->input->post->get('cid', array(), 'array');
327 
328  $model = $this->getModel();
329  $return = $model->checkin($ids);
330  if ($return === false)
331  {
332  // Checkin failed.
333  $message = JText::sprintf('JLIB_APPLICATION_ERROR_CHECKIN_FAILED', $model->getError());
334  $this->setRedirect(JRoute::_('index.php?option=' . $this->option . '&view=' . $this->view_list, false), $message, 'error');
335  return false;
336  }
337  else
338  {
339  // Checkin succeeded.
340  $message = JText::plural($this->text_prefix . '_N_ITEMS_CHECKED_IN', count($ids));
341  $this->setRedirect(JRoute::_('index.php?option=' . $this->option . '&view=' . $this->view_list, false), $message);
342  return true;
343  }
344  }
345 
346  /**
347  * Method to save the submitted ordering values for records via AJAX.
348  *
349  * @return void
350  *
351  * @since 3.0
352  */
353  public function saveOrderAjax()
354  {
355  // Get the input
356  $pks = $this->input->post->get('cid', array(), 'array');
357  $order = $this->input->post->get('order', array(), 'array');
358 
359  // Sanitize the input
361  JArrayHelper::toInteger($order);
362 
363  // Get the model
364  $model = $this->getModel();
365 
366  // Save the ordering
367  $return = $model->saveorder($pks, $order);
368 
369  if ($return)
370  {
371  echo "1";
372  }
373 
374  // Close the application
375  JFactory::getApplication()->close();
376  }
377 }