[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Form/Field/ -> UserField.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\Factory;
  13  use Joomla\CMS\Form\FormField;
  14  use Joomla\CMS\Language\Text;
  15  use Joomla\CMS\User\User;
  16  
  17  // phpcs:disable PSR1.Files.SideEffects
  18  \defined('JPATH_PLATFORM') or die;
  19  // phpcs:enable PSR1.Files.SideEffects
  20  
  21  /**
  22   * Field to select a user ID from a modal list.
  23   *
  24   * @since  1.6
  25   */
  26  class UserField extends FormField
  27  {
  28      /**
  29       * The form field type.
  30       *
  31       * @var    string
  32       * @since  1.6
  33       */
  34      public $type = 'User';
  35  
  36      /**
  37       * Filtering groups
  38       *
  39       * @var   array
  40       * @since 3.5
  41       */
  42      protected $groups = null;
  43  
  44      /**
  45       * Users to exclude from the list of users
  46       *
  47       * @var   array
  48       * @since 3.5
  49       */
  50      protected $excluded = null;
  51  
  52      /**
  53       * Layout to render
  54       *
  55       * @var   string
  56       * @since 3.5
  57       */
  58      protected $layout = 'joomla.form.field.user';
  59  
  60      /**
  61       * Method to attach a Form object to the field.
  62       *
  63       * @param   \SimpleXMLElement  $element  The SimpleXMLElement object representing the `<field>` tag for the form field object.
  64       * @param   mixed              $value    The form field value to validate.
  65       * @param   string             $group    The field name group control value. This acts as an array container for the field.
  66       *                                       For example if the field has name="foo" and the group value is set to "bar" then the
  67       *                                       full field name would end up being "bar[foo]".
  68       *
  69       * @return  boolean  True on success.
  70       *
  71       * @since   3.7.0
  72       *
  73       * @see     FormField::setup()
  74       */
  75      public function setup(\SimpleXMLElement $element, $value, $group = null)
  76      {
  77          $return = parent::setup($element, $value, $group);
  78  
  79          // If user can't access com_users the field should be readonly.
  80          if ($return && !$this->readonly) {
  81              $this->readonly = !Factory::getUser()->authorise('core.manage', 'com_users');
  82          }
  83  
  84          return $return;
  85      }
  86  
  87      /**
  88       * Method to get the user field input markup.
  89       *
  90       * @return  string  The field input markup.
  91       *
  92       * @since   1.6
  93       */
  94      protected function getInput()
  95      {
  96          if (empty($this->layout)) {
  97              throw new \UnexpectedValueException(sprintf('%s has no layout assigned.', $this->name));
  98          }
  99  
 100          return $this->getRenderer($this->layout)->render($this->getLayoutData());
 101      }
 102  
 103      /**
 104       * Get the data that is going to be passed to the layout
 105       *
 106       * @return  array
 107       *
 108       * @since   3.5
 109       */
 110      public function getLayoutData()
 111      {
 112          // Get the basic field data
 113          $data = parent::getLayoutData();
 114  
 115          // Initialize value
 116          $name = Text::_('JLIB_FORM_SELECT_USER');
 117  
 118          if (is_numeric($this->value)) {
 119              $name = User::getInstance($this->value)->name;
 120          } elseif (strtoupper($this->value) === 'CURRENT') {
 121              // Handle the special case for "current".
 122              // 'CURRENT' is not a reasonable value to be placed in the html
 123              $current = Factory::getUser();
 124  
 125              $this->value = $current->id;
 126  
 127              $data['value'] = $this->value;
 128  
 129              $name = $current->name;
 130          }
 131  
 132          // User lookup went wrong, we assign the value instead.
 133          if ($name === null && $this->value) {
 134              $name = $this->value;
 135          }
 136  
 137          $extraData = array(
 138              'userName'  => $name,
 139              'groups'    => $this->getGroups(),
 140              'excluded'  => $this->getExcluded(),
 141          );
 142  
 143          return array_merge($data, $extraData);
 144      }
 145  
 146      /**
 147       * Method to get the filtering groups (null means no filtering)
 148       *
 149       * @return  mixed  Array of filtering groups or null.
 150       *
 151       * @since   1.6
 152       */
 153      protected function getGroups()
 154      {
 155          if (isset($this->element['groups'])) {
 156              return explode(',', $this->element['groups']);
 157          }
 158      }
 159  
 160      /**
 161       * Method to get the users to exclude from the list of users
 162       *
 163       * @return  mixed  Array of users to exclude or null to to not exclude them
 164       *
 165       * @since   1.6
 166       */
 167      protected function getExcluded()
 168      {
 169          if (isset($this->element['exclude'])) {
 170              return explode(',', $this->element['exclude']);
 171          }
 172      }
 173  }


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