[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Form/Field/ -> RulesField.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2009 Open Source Matters, Inc. <https://www.joomla.org>
   7   * @license    GNU General Public License version 2 or later; see LICENSE.txt
   8   */
   9  
  10  namespace Joomla\CMS\Form\Field;
  11  
  12  use Joomla\CMS\Access\Access;
  13  use Joomla\CMS\Form\FormField;
  14  use Joomla\CMS\Helper\UserGroupsHelper;
  15  use Joomla\Database\ParameterType;
  16  
  17  // phpcs:disable PSR1.Files.SideEffects
  18  \defined('JPATH_PLATFORM') or die;
  19  // phpcs:enable PSR1.Files.SideEffects
  20  
  21  /**
  22   * Form Field class for the Joomla Platform.
  23   * Field for assigning permissions to groups for a given asset
  24   *
  25   * @see    JAccess
  26   * @since  1.7.0
  27   */
  28  class RulesField extends FormField
  29  {
  30      /**
  31       * The form field type.
  32       *
  33       * @var    string
  34       * @since  1.7.0
  35       */
  36      protected $type = 'Rules';
  37  
  38      /**
  39       * Name of the layout being used to render the field
  40       *
  41       * @var    string
  42       * @since  4.0.0
  43       */
  44      protected $layout = 'joomla.form.field.rules';
  45  
  46      /**
  47       * The section.
  48       *
  49       * @var    string
  50       * @since  3.2
  51       */
  52      protected $section;
  53  
  54      /**
  55       * The component.
  56       *
  57       * @var    string
  58       * @since  3.2
  59       */
  60      protected $component;
  61  
  62      /**
  63       * The assetField.
  64       *
  65       * @var    string
  66       * @since  3.2
  67       */
  68      protected $assetField;
  69  
  70      /**
  71       * The parent class of the field
  72       *
  73       * @var  string
  74       * @since 4.0.0
  75       */
  76      protected $parentclass;
  77  
  78      /**
  79       * Method to get certain otherwise inaccessible properties from the form field object.
  80       *
  81       * @param   string  $name  The property name for which to get the value.
  82       *
  83       * @return  mixed  The property value or null.
  84       *
  85       * @since   3.2
  86       */
  87      public function __get($name)
  88      {
  89          switch ($name) {
  90              case 'section':
  91              case 'component':
  92              case 'assetField':
  93                  return $this->$name;
  94          }
  95  
  96          return parent::__get($name);
  97      }
  98  
  99      /**
 100       * Method to set certain otherwise inaccessible properties of the form field object.
 101       *
 102       * @param   string  $name   The property name for which to set the value.
 103       * @param   mixed   $value  The value of the property.
 104       *
 105       * @return  void
 106       *
 107       * @since   3.2
 108       */
 109      public function __set($name, $value)
 110      {
 111          switch ($name) {
 112              case 'section':
 113              case 'component':
 114              case 'assetField':
 115                  $this->$name = (string) $value;
 116                  break;
 117  
 118              default:
 119                  parent::__set($name, $value);
 120          }
 121      }
 122  
 123      /**
 124       * Method to attach a Form object to the field.
 125       *
 126       * @param   \SimpleXMLElement  $element  The SimpleXMLElement object representing the `<field>` tag for the form field object.
 127       * @param   mixed              $value    The form field value to validate.
 128       * @param   string             $group    The field name group control value. This acts as an array container for the field.
 129       *                                       For example if the field has name="foo" and the group value is set to "bar" then the
 130       *                                       full field name would end up being "bar[foo]".
 131       *
 132       * @return  boolean  True on success.
 133       *
 134       * @see     FormField::setup()
 135       * @since   3.2
 136       */
 137      public function setup(\SimpleXMLElement $element, $value, $group = null)
 138      {
 139          $return = parent::setup($element, $value, $group);
 140  
 141          if ($return) {
 142              $this->section    = $this->element['section'] ? (string) $this->element['section'] : '';
 143              $this->component  = $this->element['component'] ? (string) $this->element['component'] : '';
 144              $this->assetField = $this->element['asset_field'] ? (string) $this->element['asset_field'] : 'asset_id';
 145          }
 146  
 147          return $return;
 148      }
 149  
 150      /**
 151       * Method to get the field input markup for Access Control Lists.
 152       * Optionally can be associated with a specific component and section.
 153       *
 154       * @return  string  The field input markup.
 155       *
 156       * @since   1.7.0
 157       * @todo:   Add access check.
 158       */
 159      protected function getInput()
 160      {
 161          // Initialise some field attributes.
 162          $section    = $this->section;
 163          $assetField = $this->assetField;
 164          $component  = empty($this->component) ? 'root.1' : $this->component;
 165  
 166          // Current view is global config?
 167          $this->isGlobalConfig = $component === 'root.1';
 168  
 169          // Get the actions for the asset.
 170          $this->actions = Access::getActionsFromFile(
 171              JPATH_ADMINISTRATOR . '/components/' . $component . '/access.xml',
 172              "/access/section[@name='" . $section . "']/"
 173          );
 174  
 175          if ($this->actions === false) {
 176              $this->actions = [];
 177          }
 178  
 179          // Iterate over the children and add to the actions.
 180          foreach ($this->element->children() as $el) {
 181              if ($el->getName() === 'action') {
 182                  $this->actions[] = (object) array(
 183                      'name' => (string) $el['name'],
 184                      'title' => (string) $el['title'],
 185                      'description' => (string) $el['description'],
 186                  );
 187              }
 188          }
 189  
 190          // Get the asset id.
 191          // Note that for global configuration, com_config injects asset_id = 1 into the form.
 192          $this->assetId = (int) $this->form->getValue($assetField);
 193          $this->newItem = empty($this->assetId) && $this->isGlobalConfig === false && $section !== 'component';
 194  
 195          // If the asset id is empty (component or new item).
 196          if (empty($this->assetId)) {
 197              // Get the component asset id as fallback.
 198              $db = $this->getDatabase();
 199              $query = $db->getQuery(true)
 200                  ->select($db->quoteName('id'))
 201                  ->from($db->quoteName('#__assets'))
 202                  ->where($db->quoteName('name') . ' = :component')
 203                  ->bind(':component', $component);
 204  
 205              $db->setQuery($query);
 206  
 207              $this->assetId = (int) $db->loadResult();
 208  
 209              /**
 210               * @todo: incorrect info
 211               * When creating a new item (not saving) it uses the calculated permissions from the component (item <-> component <-> global config).
 212               * But if we have a section too (item <-> section(s) <-> component <-> global config) this is not correct.
 213               * Also, currently it uses the component permission, but should use the calculated permissions for achild of the component/section.
 214               */
 215          }
 216  
 217          // If not in global config we need the parent_id asset to calculate permissions.
 218          if (!$this->isGlobalConfig) {
 219              // In this case we need to get the component rules too.
 220              $db = $this->getDatabase();
 221  
 222              $query = $db->getQuery(true)
 223                  ->select($db->quoteName('parent_id'))
 224                  ->from($db->quoteName('#__assets'))
 225                  ->where($db->quoteName('id') . ' = :assetId')
 226                  ->bind(':assetId', $this->assetId, ParameterType::INTEGER);
 227  
 228              $db->setQuery($query);
 229  
 230              $this->parentAssetId = (int) $db->loadResult();
 231          }
 232  
 233          // Get the rules for just this asset (non-recursive).
 234          $this->assetRules = Access::getAssetRules($this->assetId, false, false);
 235  
 236          // Get the available user groups.
 237          $this->groups = $this->getUserGroups();
 238  
 239          // Trim the trailing line in the layout file
 240          return trim($this->getRenderer($this->layout)->render($this->getLayoutData()));
 241      }
 242  
 243      /**
 244       * Method to get the data to be passed to the layout for rendering.
 245       *
 246       * @return  array
 247       *
 248       * @since   4.0.0
 249       */
 250      protected function getLayoutData()
 251      {
 252          $data = parent::getLayoutData();
 253  
 254          $extraData = array(
 255              'groups'         => $this->groups,
 256              'section'        => $this->section,
 257              'actions'        => $this->actions,
 258              'assetId'        => $this->assetId,
 259              'newItem'        => $this->newItem,
 260              'assetRules'     => $this->assetRules,
 261              'isGlobalConfig' => $this->isGlobalConfig,
 262              'parentAssetId'  => $this->parentAssetId,
 263              'component'      => $this->component,
 264          );
 265  
 266          return array_merge($data, $extraData);
 267      }
 268  
 269      /**
 270       * Get a list of the user groups.
 271       *
 272       * @return  array
 273       *
 274       * @since   1.7.0
 275       */
 276      protected function getUserGroups()
 277      {
 278          $options = UserGroupsHelper::getInstance()->getAll();
 279  
 280          foreach ($options as &$option) {
 281              $option->value = $option->id;
 282              $option->text  = $option->title;
 283          }
 284  
 285          return array_values($options);
 286      }
 287  }


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