[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_fields/src/Controller/ -> FieldController.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Administrator
   5   * @subpackage  com_fields
   6   *
   7   * @copyright   (C) 2016 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\Fields\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\Component\Fields\Administrator\Helper\FieldsHelper;
  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 Field controller
  27   *
  28   * @since  3.7.0
  29   */
  30  class FieldController extends FormController
  31  {
  32      /**
  33       * @var    string
  34       */
  35      private $internalContext;
  36  
  37      /**
  38       * @var    string
  39       */
  40      private $component;
  41  
  42      /**
  43       * The prefix to use with controller messages.
  44       *
  45       * @var    string
  46  
  47       * @since   3.7.0
  48       */
  49      protected $text_prefix = 'COM_FIELDS_FIELD';
  50  
  51      /**
  52       * Constructor.
  53       *
  54       * @param   array                $config   An optional associative array of configuration settings.
  55       * Recognized key values include 'name', 'default_task', 'model_path', and
  56       * 'view_path' (this list is not meant to be comprehensive).
  57       * @param   MVCFactoryInterface  $factory  The factory.
  58       * @param   CMSApplication       $app      The Application for the dispatcher
  59       * @param   Input                $input    Input
  60       *
  61       * @since   3.7.0
  62       */
  63      public function __construct($config = array(), MVCFactoryInterface $factory = null, $app = null, $input = null)
  64      {
  65          parent::__construct($config, $factory, $app, $input);
  66  
  67          $this->internalContext = $this->app->getUserStateFromRequest('com_fields.fields.context', 'context', 'com_content.article', 'CMD');
  68          $parts = FieldsHelper::extract($this->internalContext);
  69          $this->component = $parts ? $parts[0] : null;
  70      }
  71  
  72      /**
  73       * Method override to check if you can add a new record.
  74       *
  75       * @param   array  $data  An array of input data.
  76       *
  77       * @return  boolean
  78       *
  79       * @since   3.7.0
  80       */
  81      protected function allowAdd($data = array())
  82      {
  83          return $this->app->getIdentity()->authorise('core.create', $this->component);
  84      }
  85  
  86      /**
  87       * Method override to check if you can edit an existing record.
  88       *
  89       * @param   array   $data  An array of input data.
  90       * @param   string  $key   The name of the key for the primary key.
  91       *
  92       * @return  boolean
  93       *
  94       * @since   3.7.0
  95       */
  96      protected function allowEdit($data = array(), $key = 'id')
  97      {
  98          $recordId = (int) isset($data[$key]) ? $data[$key] : 0;
  99          $user     = $this->app->getIdentity();
 100  
 101          // Zero record (id:0), return component edit permission by calling parent controller method
 102          if (!$recordId) {
 103              return parent::allowEdit($data, $key);
 104          }
 105  
 106          // Check edit on the record asset (explicit or inherited)
 107          if ($user->authorise('core.edit', $this->component . '.field.' . $recordId)) {
 108              return true;
 109          }
 110  
 111          // Check edit own on the record asset (explicit or inherited)
 112          if ($user->authorise('core.edit.own', $this->component . '.field.' . $recordId)) {
 113              // Existing record already has an owner, get it
 114              $record = $this->getModel()->getItem($recordId);
 115  
 116              if (empty($record)) {
 117                  return false;
 118              }
 119  
 120              // Grant if current user is owner of the record
 121              return $user->id == $record->created_user_id;
 122          }
 123  
 124          return false;
 125      }
 126  
 127      /**
 128       * Method to run batch operations.
 129       *
 130       * @param   object  $model  The model.
 131       *
 132       * @return  boolean   True if successful, false otherwise and internal error is set.
 133       *
 134       * @since   3.7.0
 135       */
 136      public function batch($model = null)
 137      {
 138          $this->checkToken();
 139  
 140          // Set the model
 141          $model = $this->getModel('Field');
 142  
 143          // Preset the redirect
 144          $this->setRedirect('index.php?option=com_fields&view=fields&context=' . $this->internalContext);
 145  
 146          return parent::batch($model);
 147      }
 148  
 149      /**
 150       * Gets the URL arguments to append to an item redirect.
 151       *
 152       * @param   integer  $recordId  The primary key id for the item.
 153       * @param   string   $urlVar    The name of the URL variable for the id.
 154       *
 155       * @return  string  The arguments to append to the redirect URL.
 156       *
 157       * @since   3.7.0
 158       */
 159      protected function getRedirectToItemAppend($recordId = null, $urlVar = 'id')
 160      {
 161          return parent::getRedirectToItemAppend($recordId) . '&context=' . $this->internalContext;
 162      }
 163  
 164      /**
 165       * Gets the URL arguments to append to a list redirect.
 166       *
 167       * @return  string  The arguments to append to the redirect URL.
 168       *
 169       * @since   3.7.0
 170       */
 171      protected function getRedirectToListAppend()
 172      {
 173          return parent::getRedirectToListAppend() . '&context=' . $this->internalContext;
 174      }
 175  
 176      /**
 177       * Function that allows child controller access to model data after the data has been saved.
 178       *
 179       * @param   BaseDatabaseModel  $model      The data model object.
 180       * @param   array              $validData  The validated data.
 181       *
 182       * @return  void
 183       *
 184       * @since   3.7.0
 185       */
 186      protected function postSaveHook(BaseDatabaseModel $model, $validData = array())
 187      {
 188          $item = $model->getItem();
 189  
 190          if (isset($item->params) && is_array($item->params)) {
 191              $registry = new Registry();
 192              $registry->loadArray($item->params);
 193              $item->params = (string) $registry;
 194          }
 195      }
 196  }


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