Source for file admin.php

Documentation is available at admin.php

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

Documentation generated on Tue, 19 Nov 2013 14:53:27 +0100 by phpDocumentor 1.4.3