[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_fields/src/Model/ -> GroupModel.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\Model;
  12  
  13  use Joomla\CMS\Factory;
  14  use Joomla\CMS\Filesystem\Path;
  15  use Joomla\CMS\Form\Form;
  16  use Joomla\CMS\Language\Text;
  17  use Joomla\CMS\MVC\Model\AdminModel;
  18  use Joomla\CMS\Table\Table;
  19  use Joomla\Component\Fields\Administrator\Helper\FieldsHelper;
  20  use Joomla\Registry\Registry;
  21  
  22  // phpcs:disable PSR1.Files.SideEffects
  23  \defined('_JEXEC') or die;
  24  // phpcs:enable PSR1.Files.SideEffects
  25  
  26  /**
  27   * Group Model
  28   *
  29   * @since  3.7.0
  30   */
  31  class GroupModel extends AdminModel
  32  {
  33      /**
  34       * @var null|string
  35       *
  36       * @since   3.7.0
  37       */
  38      public $typeAlias = null;
  39  
  40      /**
  41       * Allowed batch commands
  42       *
  43       * @var array
  44       */
  45      protected $batch_commands = array(
  46          'assetgroup_id' => 'batchAccess',
  47          'language_id'   => 'batchLanguage'
  48      );
  49  
  50      /**
  51       * Method to save the form data.
  52       *
  53       * @param   array  $data  The form data.
  54       *
  55       * @return  boolean  True on success, False on error.
  56       *
  57       * @since   3.7.0
  58       */
  59      public function save($data)
  60      {
  61          // Alter the title for save as copy
  62          $input = Factory::getApplication()->input;
  63  
  64          // Save new group as unpublished
  65          if ($input->get('task') == 'save2copy') {
  66              $data['state'] = 0;
  67          }
  68  
  69          return parent::save($data);
  70      }
  71  
  72      /**
  73       * Method to get a table object, load it if necessary.
  74       *
  75       * @param   string  $name     The table name. Optional.
  76       * @param   string  $prefix   The class prefix. Optional.
  77       * @param   array   $options  Configuration array for model. Optional.
  78       *
  79       * @return  Table  A Table object
  80       *
  81       * @since   3.7.0
  82       * @throws  \Exception
  83       */
  84      public function getTable($name = 'Group', $prefix = 'Administrator', $options = array())
  85      {
  86          return parent::getTable($name, $prefix, $options);
  87      }
  88  
  89      /**
  90       * Abstract method for getting the form from the model.
  91       *
  92       * @param   array    $data      Data for the form.
  93       * @param   boolean  $loadData  True if the form is to load its own data (default case), false if not.
  94       *
  95       * @return  mixed  A Form object on success, false on failure
  96       *
  97       * @since   3.7.0
  98       */
  99      public function getForm($data = array(), $loadData = true)
 100      {
 101          $context = $this->getState('filter.context');
 102          $jinput = Factory::getApplication()->input;
 103  
 104          if (empty($context) && isset($data['context'])) {
 105              $context = $data['context'];
 106              $this->setState('filter.context', $context);
 107          }
 108  
 109          // Get the form.
 110          $form = $this->loadForm(
 111              'com_fields.group.' . $context,
 112              'group',
 113              array(
 114                  'control'   => 'jform',
 115                  'load_data' => $loadData,
 116              )
 117          );
 118  
 119          if (empty($form)) {
 120              return false;
 121          }
 122  
 123          // Modify the form based on Edit State access controls.
 124          if (empty($data['context'])) {
 125              $data['context'] = $context;
 126          }
 127  
 128          $user = Factory::getUser();
 129  
 130          if (!$user->authorise('core.edit.state', $context . '.fieldgroup.' . $jinput->get('id'))) {
 131              // Disable fields for display.
 132              $form->setFieldAttribute('ordering', 'disabled', 'true');
 133              $form->setFieldAttribute('state', 'disabled', 'true');
 134  
 135              // Disable fields while saving. The controller has already verified this is a record you can edit.
 136              $form->setFieldAttribute('ordering', 'filter', 'unset');
 137              $form->setFieldAttribute('state', 'filter', 'unset');
 138          }
 139  
 140          // Don't allow to change the created_by user if not allowed to access com_users.
 141          if (!$user->authorise('core.manage', 'com_users')) {
 142              $form->setFieldAttribute('created_by', 'filter', 'unset');
 143          }
 144  
 145          return $form;
 146      }
 147  
 148      /**
 149       * Method to test whether a record can be deleted.
 150       *
 151       * @param   object  $record  A record object.
 152       *
 153       * @return  boolean  True if allowed to delete the record. Defaults to the permission for the component.
 154       *
 155       * @since   3.7.0
 156       */
 157      protected function canDelete($record)
 158      {
 159          if (empty($record->id) || $record->state != -2) {
 160              return false;
 161          }
 162  
 163          return Factory::getUser()->authorise('core.delete', $record->context . '.fieldgroup.' . (int) $record->id);
 164      }
 165  
 166      /**
 167       * Method to test whether a record can have its state changed.
 168       *
 169       * @param   object  $record  A record object.
 170       *
 171       * @return  boolean  True if allowed to change the state of the record. Defaults to the permission for the
 172       *                   component.
 173       *
 174       * @since   3.7.0
 175       */
 176      protected function canEditState($record)
 177      {
 178          $user = Factory::getUser();
 179  
 180          // Check for existing fieldgroup.
 181          if (!empty($record->id)) {
 182              return $user->authorise('core.edit.state', $record->context . '.fieldgroup.' . (int) $record->id);
 183          }
 184  
 185          // Default to component settings.
 186          return $user->authorise('core.edit.state', $record->context);
 187      }
 188  
 189      /**
 190       * Auto-populate the model state.
 191       *
 192       * Note. Calling getState in this method will result in recursion.
 193       *
 194       * @return  void
 195       *
 196       * @since   3.7.0
 197       */
 198      protected function populateState()
 199      {
 200          parent::populateState();
 201  
 202          $context = Factory::getApplication()->getUserStateFromRequest('com_fields.groups.context', 'context', 'com_fields', 'CMD');
 203          $this->setState('filter.context', $context);
 204      }
 205  
 206      /**
 207       * A protected method to get a set of ordering conditions.
 208       *
 209       * @param   Table  $table  A Table object.
 210       *
 211       * @return  array  An array of conditions to add to ordering queries.
 212       *
 213       * @since   3.7.0
 214       */
 215      protected function getReorderConditions($table)
 216      {
 217          $db = $this->getDatabase();
 218  
 219          return [
 220              $db->quoteName('context') . ' = ' . $db->quote($table->context),
 221          ];
 222      }
 223  
 224      /**
 225       * Method to preprocess the form.
 226       *
 227       * @param   Form    $form   A Form object.
 228       * @param   mixed   $data   The data expected for the form.
 229       * @param   string  $group  The name of the plugin group to import (defaults to "content").
 230       *
 231       * @return  void
 232       *
 233       * @see     \Joomla\CMS\Form\FormField
 234       * @since   3.7.0
 235       * @throws  \Exception if there is an error in the form event.
 236       */
 237      protected function preprocessForm(Form $form, $data, $group = 'content')
 238      {
 239          parent::preprocessForm($form, $data, $group);
 240  
 241          $parts = FieldsHelper::extract($this->state->get('filter.context'));
 242  
 243          // Extract the component name
 244          $component = $parts[0];
 245  
 246          // Extract the optional section name
 247          $section = (count($parts) > 1) ? $parts[1] : null;
 248  
 249          if ($parts) {
 250              // Set the access control rules field component value.
 251              $form->setFieldAttribute('rules', 'component', $component);
 252          }
 253  
 254          if ($section !== null) {
 255              // Looking first in the component models/forms folder
 256              $path = Path::clean(JPATH_ADMINISTRATOR . '/components/' . $component . '/models/forms/fieldgroup/' . $section . '.xml');
 257  
 258              if (file_exists($path)) {
 259                  $lang = Factory::getLanguage();
 260                  $lang->load($component, JPATH_BASE);
 261                  $lang->load($component, JPATH_BASE . '/components/' . $component);
 262  
 263                  if (!$form->loadFile($path, false)) {
 264                      throw new \Exception(Text::_('JERROR_LOADFILE_FAILED'));
 265                  }
 266              }
 267          }
 268      }
 269  
 270      /**
 271       * Method to validate the form data.
 272       *
 273       * @param   Form    $form   The form to validate against.
 274       * @param   array   $data   The data to validate.
 275       * @param   string  $group  The name of the field group to validate.
 276       *
 277       * @return  array|boolean  Array of filtered data if valid, false otherwise.
 278       *
 279       * @see     JFormRule
 280       * @see     JFilterInput
 281       * @since   3.9.23
 282       */
 283      public function validate($form, $data, $group = null)
 284      {
 285          if (!Factory::getUser()->authorise('core.admin', 'com_fields')) {
 286              if (isset($data['rules'])) {
 287                  unset($data['rules']);
 288              }
 289          }
 290  
 291          return parent::validate($form, $data, $group);
 292      }
 293  
 294      /**
 295       * Method to get the data that should be injected in the form.
 296       *
 297       * @return  array    The default data is an empty array.
 298       *
 299       * @since   3.7.0
 300       */
 301      protected function loadFormData()
 302      {
 303          // Check the session for previously entered form data.
 304          $app = Factory::getApplication();
 305          $data = $app->getUserState('com_fields.edit.group.data', array());
 306  
 307          if (empty($data)) {
 308              $data = $this->getItem();
 309  
 310              // Pre-select some filters (Status, Language, Access) in edit form if those have been selected in Field Group Manager
 311              if (!$data->id) {
 312                  // Check for which context the Field Group Manager is used and get selected fields
 313                  $context = substr($app->getUserState('com_fields.groups.filter.context', ''), 4);
 314                  $filters = (array) $app->getUserState('com_fields.groups.' . $context . '.filter');
 315  
 316                  $data->set(
 317                      'state',
 318                      $app->input->getInt('state', (!empty($filters['state']) ? $filters['state'] : null))
 319                  );
 320                  $data->set(
 321                      'language',
 322                      $app->input->getString('language', (!empty($filters['language']) ? $filters['language'] : null))
 323                  );
 324                  $data->set(
 325                      'access',
 326                      $app->input->getInt('access', (!empty($filters['access']) ? $filters['access'] : $app->get('access')))
 327                  );
 328              }
 329          }
 330  
 331          $this->preprocessData('com_fields.group', $data);
 332  
 333          return $data;
 334      }
 335  
 336      /**
 337       * Method to get a single record.
 338       *
 339       * @param   integer  $pk  The id of the primary key.
 340       *
 341       * @return  mixed    Object on success, false on failure.
 342       *
 343       * @since   3.7.0
 344       */
 345      public function getItem($pk = null)
 346      {
 347          if ($item = parent::getItem($pk)) {
 348              // Prime required properties.
 349              if (empty($item->id)) {
 350                  $item->context = $this->getState('filter.context');
 351              }
 352  
 353              if (property_exists($item, 'params')) {
 354                  $item->params = new Registry($item->params);
 355              }
 356          }
 357  
 358          return $item;
 359      }
 360  
 361      /**
 362       * Clean the cache
 363       *
 364       * @param   string   $group     The cache group
 365       * @param   integer  $clientId  @deprecated   5.0   No longer used.
 366       *
 367       * @return  void
 368       *
 369       * @since   3.7.0
 370       */
 371      protected function cleanCache($group = null, $clientId = 0)
 372      {
 373          $context = Factory::getApplication()->input->get('context');
 374  
 375          parent::cleanCache($context);
 376      }
 377  }


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