PageRenderTime 35ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/content/code/trunk/administrator/components/com_artofcontent/libraries/joomla/application/component/controlleradmin.php

https://bitbucket.org/eddieajau/the-art-of-joomla-archive
PHP | 287 lines | 146 code | 37 blank | 104 comment | 28 complexity | 449d6d8878680fc88d9dbe4bdf0b91e9 MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id: controlleradmin.php 484 2010-12-20 23:40:27Z eddieajau $
  4. * @package Joomla.Framework
  5. * @subpackage Application
  6. * @copyright Copyright (C) 2005 - 2010 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE.txt
  8. */
  9. // No direct access.
  10. defined('JPATH_BASE') or die;
  11. juimport('joomla.application.component.controller');
  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. * @abstract
  19. * @package Joomla.Framework
  20. * @subpackage Application
  21. * @since 1.6
  22. */
  23. class JControllerAdmin extends JController
  24. {
  25. /**
  26. * @var string The URL option for the component.
  27. * @since 1.6
  28. */
  29. protected $option;
  30. /**
  31. * @var string The prefix to use with controller messages.
  32. * @since 1.6
  33. */
  34. protected $text_prefix;
  35. /**
  36. * @var string The URL view list variable.
  37. * @since 1.6
  38. */
  39. protected $view_list;
  40. /**
  41. * Constructor.
  42. *
  43. * @param array An optional associative array of configuration settings.
  44. * @see JController
  45. * @since 1.6
  46. */
  47. public function __construct($config = array())
  48. {
  49. parent::__construct($config);
  50. // Define standard task mappings.
  51. $this->registerTask('unpublish', 'publish'); // value = 0
  52. $this->registerTask('archive', 'publish'); // value = 2
  53. $this->registerTask('trash', 'publish'); // value = -2
  54. $this->registerTask('report', 'publish'); // value = -3
  55. $this->registerTask('orderup', 'reorder');
  56. $this->registerTask('orderdown', 'reorder');
  57. // Guess the option as com_NameOfController.
  58. if (empty($this->option)) {
  59. $this->option = 'com_'.strtolower($this->getName());
  60. }
  61. // Guess the JText message prefix. Defaults to the option.
  62. if (empty($this->text_prefix)) {
  63. $this->text_prefix = strtoupper($this->option);
  64. }
  65. // Guess the list view as the suffix, eg: OptionControllerSuffix.
  66. if (empty($this->view_list)) {
  67. $r = null;
  68. if (!preg_match('/(.*)Controller(.*)/i', get_class($this), $r)) {
  69. JError::raiseError(500, JText::_('JLIB_APPLICATION_ERROR_CONTROLLER_GET_NAME'));
  70. }
  71. $this->view_list = strtolower($r[2]);
  72. }
  73. }
  74. /**
  75. * Removes an item.
  76. *
  77. * @since 1.6
  78. */
  79. function delete()
  80. {
  81. // Check for request forgeries
  82. JRequest::checkToken() or die(JText::_('JINVALID_TOKEN'));
  83. // Get items to remove from the request.
  84. $cid = JRequest::getVar('cid', array(), '', 'array');
  85. if (!is_array($cid) || count($cid) < 1) {
  86. JError::raiseWarning(500, JText::_($this->text_prefix.'_NO_ITEM_SELECTED'));
  87. } else {
  88. // Get the model.
  89. $model = $this->getModel();
  90. // Make sure the item ids are integers
  91. jimport('joomla.utilities.arrayhelper');
  92. JArrayHelper::toInteger($cid);
  93. // Remove the items.
  94. if ($model->delete($cid)) {
  95. // $this->setMessage(JText::plural($this->text_prefix.'_N_ITEMS_DELETED', count($cid)));
  96. $this->setMessage(JText::sprintf($this->text_prefix.'_N_ITEMS_DELETED', count($cid)));
  97. } else {
  98. $this->setMessage($model->getError());
  99. }
  100. }
  101. $this->setRedirect(JRoute::_('index.php?option='.$this->option.'&view='.$this->view_list, false));
  102. }
  103. /**
  104. * Display is not supported by this controller.
  105. *
  106. * @param boolean If true, the view output will be cached
  107. * @param array An array of safe url parameters and their variable types, for valid values see {@link JFilterInput::clean()}.
  108. *
  109. * @return JController This object to support chaining.
  110. * @since 1.6
  111. */
  112. public function display($cachable = false, $urlparams = false)
  113. {
  114. return $this;
  115. }
  116. /**
  117. * Method to publish a list of taxa
  118. *
  119. * @since 1.6
  120. */
  121. function publish()
  122. {
  123. // Check for request forgeries
  124. JRequest::checkToken() or die(JText::_('JINVALID_TOKEN'));
  125. $session = JFactory::getSession();
  126. $registry = $session->get('registry');
  127. // Get items to publish from the request.
  128. $cid = JRequest::getVar('cid', array(), '', 'array');
  129. $data = array('publish' => 1, 'unpublish' => 0, 'archive'=> 2, 'trash' => -2, 'report'=>-3);
  130. $task = $this->getTask();
  131. $value = JArrayHelper::getValue($data, $task, 0, 'int');
  132. if (empty($cid)) {
  133. JError::raiseWarning(500, JText::_($this->text_prefix.'_NO_ITEM_SELECTED'));
  134. }
  135. else {
  136. // Get the model.
  137. $model = $this->getModel();
  138. // Make sure the item ids are integers
  139. JArrayHelper::toInteger($cid);
  140. // Publish the items.
  141. if (!$model->publish($cid, $value)) {
  142. JError::raiseWarning(500, $model->getError());
  143. }
  144. else {
  145. if ($value == 1) {
  146. $ntext = $this->text_prefix.'_N_ITEMS_PUBLISHED';
  147. }
  148. else if ($value == 0) {
  149. $ntext = $this->text_prefix.'_N_ITEMS_UNPUBLISHED';
  150. }
  151. else if ($value == 2) {
  152. $ntext = $this->text_prefix.'_N_ITEMS_ARCHIVED';
  153. }
  154. else {
  155. $ntext = $this->text_prefix.'_N_ITEMS_TRASHED';
  156. }
  157. // $this->setMessage(JText::plural($ntext, count($cid)));
  158. $this->setMessage(JText::sprintf($ntext, count($cid)));
  159. }
  160. }
  161. $this->setRedirect(JRoute::_('index.php?option='.$this->option.'&view='.$this->view_list, false));
  162. }
  163. /**
  164. * Changes the order of one or more records.
  165. *
  166. * @since 1.6
  167. */
  168. public function reorder()
  169. {
  170. // Check for request forgeries.
  171. JRequest::checkToken() or jexit(JText::_('JINVALID_TOKEN'));
  172. // Initialise variables.
  173. $user = JFactory::getUser();
  174. $ids = JRequest::getVar('cid', null, 'post', 'array');
  175. $inc = ($this->getTask() == 'orderup') ? -1 : +1;
  176. $model = $this->getModel();
  177. $return = $model->reorder($ids, $inc);
  178. if ($return === false) {
  179. // Reorder failed.
  180. $message = JText::sprintf('JLIB_APPLICATION_ERROR_REORDER_FAILED', $model->getError());
  181. $this->setRedirect(JRoute::_('index.php?option='.$this->option.'&view='.$this->view_list, false), $message, 'error');
  182. return false;
  183. } else {
  184. // Reorder succeeded.
  185. $message = JText::_('JLIB_APPLICATION_SUCCESS_ITEM_REORDERED');
  186. $this->setRedirect(JRoute::_('index.php?option='.$this->option.'&view='.$this->view_list, false), $message);
  187. return true;
  188. }
  189. }
  190. /**
  191. * Method to save the submitted ordering values for records.
  192. *
  193. * @since 1.6
  194. */
  195. public function saveorder()
  196. {
  197. // Check for request forgeries.
  198. JRequest::checkToken() or jexit(JText::_('JINVALID_TOKEN'));
  199. // Get the input
  200. $pks = JRequest::getVar('cid', null, 'post', 'array');
  201. $order = JRequest::getVar('order', null, 'post', 'array');
  202. // Sanitize the input
  203. JArrayHelper::toInteger($pks);
  204. JArrayHelper::toInteger($order);
  205. // Get the model
  206. $model = $this->getModel();
  207. // Save the ordering
  208. $return = $model->saveorder($pks, $order);
  209. if ($return === false)
  210. {
  211. // Reorder failed
  212. $message = JText::sprintf('JLIB_APPLICATION_ERROR_REORDER_FAILED', $model->getError());
  213. $this->setRedirect(JRoute::_('index.php?option='.$this->option.'&view='.$this->view_list, false), $message, 'error');
  214. return false;
  215. } else
  216. {
  217. // Reorder succeeded.
  218. $this->setMessage(JText::_('JLIB_APPLICATION_SUCCESS_ORDERING_SAVED'));
  219. $this->setRedirect(JRoute::_('index.php?option='.$this->option.'&view='.$this->view_list, false));
  220. return true;
  221. }
  222. }
  223. /**
  224. * Check in of one or more records.
  225. *
  226. * @since 1.6
  227. */
  228. public function checkin()
  229. {
  230. // Check for request forgeries.
  231. JRequest::checkToken() or jexit(JText::_('JINVALID_TOKEN'));
  232. // Initialise variables.
  233. $user = JFactory::getUser();
  234. $ids = JRequest::getVar('cid', null, 'post', 'array');
  235. $model = $this->getModel();
  236. $return = $model->checkin($ids);
  237. if ($return === false) {
  238. // Checkin failed.
  239. $message = JText::sprintf('JLIB_APPLICATION_ERROR_CHECKIN_FAILED', $model->getError());
  240. $this->setRedirect(JRoute::_('index.php?option='.$this->option.'&view='.$this->view_list, false), $message, 'error');
  241. return false;
  242. } else {
  243. // Checkin succeeded.
  244. // $message = JText::plural($this->text_prefix.'_N_ITEMS_CHECKED_IN', count($ids));
  245. $message = JText::sprintf($this->text_prefix.'_N_ITEMS_CHECKED_IN', count($ids));
  246. $this->setRedirect(JRoute::_('index.php?option='.$this->option.'&view='.$this->view_list, false), $message);
  247. return true;
  248. }
  249. }
  250. }