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