[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_categories/src/Controller/ -> CategoryController.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Administrator
   5   * @subpackage  com_categories
   6   *
   7   * @copyright   (C) 2009 Open Source Matters, Inc. <https://www.joomla.org>
   8   * @license     GNU General Public License version 2 or later; see LICENSE.txt
   9   */
  10  
  11  namespace Joomla\Component\Categories\Administrator\Controller;
  12  
  13  use Joomla\CMS\Application\CMSApplication;
  14  use Joomla\CMS\MVC\Controller\FormController;
  15  use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
  16  use Joomla\CMS\MVC\Model\BaseDatabaseModel;
  17  use Joomla\CMS\Versioning\VersionableControllerTrait;
  18  use Joomla\Input\Input;
  19  use Joomla\Registry\Registry;
  20  
  21  // phpcs:disable PSR1.Files.SideEffects
  22  \defined('_JEXEC') or die;
  23  // phpcs:enable PSR1.Files.SideEffects
  24  
  25  /**
  26   * The Category Controller
  27   *
  28   * @since  1.6
  29   */
  30  class CategoryController extends FormController
  31  {
  32      use VersionableControllerTrait;
  33  
  34      /**
  35       * The extension for which the categories apply.
  36       *
  37       * @var    string
  38       * @since  1.6
  39       */
  40      protected $extension;
  41  
  42      /**
  43       * Constructor.
  44       *
  45       * @param   array                     $config   An optional associative array of configuration settings.
  46       * @param   MVCFactoryInterface|null  $factory  The factory.
  47       * @param   CMSApplication|null       $app      The Application for the dispatcher
  48       * @param   Input|null                $input    Input
  49       *
  50       * @since  1.6
  51       * @throws \Exception
  52       */
  53      public function __construct($config = array(), MVCFactoryInterface $factory = null, CMSApplication $app = null, Input $input = null)
  54      {
  55          parent::__construct($config, $factory, $app, $input);
  56  
  57          if (empty($this->extension)) {
  58              $this->extension = $this->input->get('extension', 'com_content');
  59          }
  60      }
  61  
  62      /**
  63       * Method to check if you can add a new record.
  64       *
  65       * @param   array  $data  An array of input data.
  66       *
  67       * @return  boolean
  68       *
  69       * @since   1.6
  70       */
  71      protected function allowAdd($data = array())
  72      {
  73          $user = $this->app->getIdentity();
  74  
  75          return ($user->authorise('core.create', $this->extension) || \count($user->getAuthorisedCategories($this->extension, 'core.create')));
  76      }
  77  
  78      /**
  79       * Method to check if you can edit a record.
  80       *
  81       * @param   array   $data  An array of input data.
  82       * @param   string  $key   The name of the key for the primary key.
  83       *
  84       * @return  boolean
  85       *
  86       * @since   1.6
  87       */
  88      protected function allowEdit($data = array(), $key = 'parent_id')
  89      {
  90          $recordId = (int) isset($data[$key]) ? $data[$key] : 0;
  91          $user = $this->app->getIdentity();
  92  
  93          // Check "edit" permission on record asset (explicit or inherited)
  94          if ($user->authorise('core.edit', $this->extension . '.category.' . $recordId)) {
  95              return true;
  96          }
  97  
  98          // Check "edit own" permission on record asset (explicit or inherited)
  99          if ($user->authorise('core.edit.own', $this->extension . '.category.' . $recordId)) {
 100              // Need to do a lookup from the model to get the owner
 101              $record = $this->getModel()->getItem($recordId);
 102  
 103              if (empty($record)) {
 104                  return false;
 105              }
 106  
 107              $ownerId = $record->created_user_id;
 108  
 109              // If the owner matches 'me' then do the test.
 110              if ($ownerId == $user->id) {
 111                  return true;
 112              }
 113          }
 114  
 115          return false;
 116      }
 117  
 118      /**
 119       * Override parent save method to store form data with right key as expected by edit category page
 120       *
 121       * @param   string  $key     The name of the primary key of the URL variable.
 122       * @param   string  $urlVar  The name of the URL variable if different from the primary key (sometimes required to avoid router collisions).
 123       *
 124       * @return  boolean  True if successful, false otherwise.
 125       *
 126       * @since   3.10.3
 127       */
 128      public function save($key = null, $urlVar = null)
 129      {
 130          $result = parent::save($key, $urlVar);
 131  
 132          $oldKey = $this->option . '.edit.category.data';
 133          $newKey = $this->option . '.edit.category.' . substr($this->extension, 4) . '.data';
 134          $this->app->setUserState($newKey, $this->app->getUserState($oldKey));
 135  
 136          return $result;
 137      }
 138  
 139      /**
 140       * Override cancel method to clear form data for a failed edit action
 141       *
 142       * @param   string  $key  The name of the primary key of the URL variable.
 143       *
 144       * @return  boolean  True if access level checks pass, false otherwise.
 145       *
 146       * @since   3.10.3
 147       */
 148      public function cancel($key = null)
 149      {
 150          $result = parent::cancel($key);
 151  
 152          $newKey = $this->option . '.edit.category.' . substr($this->extension, 4) . '.data';
 153          $this->app->setUserState($newKey, null);
 154  
 155          return $result;
 156      }
 157  
 158      /**
 159       * Method to run batch operations.
 160       *
 161       * @param   object|null  $model  The model.
 162       *
 163       * @return  boolean  True if successful, false otherwise and internal error is set.
 164       *
 165       * @since   1.6
 166       */
 167      public function batch($model = null)
 168      {
 169          $this->checkToken();
 170  
 171          /** @var \Joomla\Component\Categories\Administrator\Model\CategoryModel $model */
 172          $model = $this->getModel('Category');
 173  
 174          // Preset the redirect
 175          $this->setRedirect('index.php?option=com_categories&view=categories&extension=' . $this->extension);
 176  
 177          return parent::batch($model);
 178      }
 179  
 180      /**
 181       * Gets the URL arguments to append to an item redirect.
 182       *
 183       * @param   integer|null  $recordId  The primary key id for the item.
 184       * @param   string        $urlVar    The name of the URL variable for the id.
 185       *
 186       * @return  string  The arguments to append to the redirect URL.
 187       *
 188       * @since   1.6
 189       */
 190      protected function getRedirectToItemAppend($recordId = null, $urlVar = 'id')
 191      {
 192          $append = parent::getRedirectToItemAppend($recordId);
 193  
 194          // In case extension is not passed in the URL, get it directly from category instead of default to com_content
 195          if (!$this->input->exists('extension') && $recordId > 0) {
 196              $table = $this->getModel('Category')->getTable();
 197  
 198              if ($table->load($recordId)) {
 199                  $this->extension = $table->extension;
 200              }
 201          }
 202  
 203          $append .= '&extension=' . $this->extension;
 204  
 205          return $append;
 206      }
 207  
 208      /**
 209       * Gets the URL arguments to append to a list redirect.
 210       *
 211       * @return  string  The arguments to append to the redirect URL.
 212       *
 213       * @since   1.6
 214       */
 215      protected function getRedirectToListAppend()
 216      {
 217          $append = parent::getRedirectToListAppend();
 218          $append .= '&extension=' . $this->extension;
 219  
 220          return $append;
 221      }
 222  
 223      /**
 224       * Function that allows child controller access to model data after the data has been saved.
 225       *
 226       * @param   \Joomla\CMS\MVC\Model\BaseDatabaseModel  $model      The data model object.
 227       * @param   array                                    $validData  The validated data.
 228       *
 229       * @return  void
 230       *
 231       * @since   3.1
 232       */
 233      protected function postSaveHook(BaseDatabaseModel $model, $validData = array())
 234      {
 235          $item = $model->getItem();
 236  
 237          if (isset($item->params) && \is_array($item->params)) {
 238              $registry = new Registry($item->params);
 239              $item->params = (string) $registry;
 240          }
 241  
 242          if (isset($item->metadata) && \is_array($item->metadata)) {
 243              $registry = new Registry($item->metadata);
 244              $item->metadata = (string) $registry;
 245          }
 246      }
 247  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer