1 <?php
  2   3   4   5   6   7   8 
  9 
 10 defined('JPATH_PLATFORM') or die;
 11 
 12  13  14  15  16  17 
 18 class JFormFieldList extends JFormField
 19 {
 20      21  22  23  24  25 
 26     protected $type = 'List';
 27 
 28      29  30  31  32  33  34  35 
 36     protected function getInput()
 37     {
 38         $html = array();
 39         $attr = '';
 40 
 41         
 42         $attr .= !empty($this->class) ? ' class="' . $this->class . '"' : '';
 43         $attr .= !empty($this->size) ? ' size="' . $this->size . '"' : '';
 44         $attr .= $this->multiple ? ' multiple' : '';
 45         $attr .= $this->required ? ' required aria-required="true"' : '';
 46         $attr .= $this->autofocus ? ' autofocus' : '';
 47 
 48         
 49         if ((string) $this->readonly == '1' || (string) $this->readonly == 'true' || (string) $this->disabled == '1'|| (string) $this->disabled == 'true')
 50         {
 51             $attr .= ' disabled="disabled"';
 52         }
 53 
 54         
 55         $attr .= $this->onchange ? ' onchange="' . $this->onchange . '"' : '';
 56 
 57         
 58         $options = (array) $this->getOptions();
 59 
 60         
 61         if ((string) $this->readonly == '1' || (string) $this->readonly == 'true')
 62         {
 63             $html[] = JHtml::_('select.genericlist', $options, '', trim($attr), 'value', 'text', $this->value, $this->id);
 64 
 65             
 66             if ($this->multiple && is_array($this->value))
 67             {
 68                 if (!count($this->value))
 69                 {
 70                     $this->value[] = '';
 71                 }
 72 
 73                 foreach ($this->value as $value)
 74                 {
 75                     $html[] = '<input type="hidden" name="' . $this->name . '" value="' . htmlspecialchars($value, ENT_COMPAT, 'UTF-8') . '"/>';
 76                 }
 77             }
 78             else
 79             {
 80                 $html[] = '<input type="hidden" name="' . $this->name . '" value="' . htmlspecialchars($this->value, ENT_COMPAT, 'UTF-8') . '"/>';
 81             }
 82         }
 83         else
 84         
 85         {
 86             $html[] = JHtml::_('select.genericlist', $options, $this->name, trim($attr), 'value', 'text', $this->value, $this->id);
 87         }
 88 
 89         return implode($html);
 90     }
 91 
 92      93  94  95  96  97  98 
 99     protected function getOptions()
100     {
101         $fieldname = preg_replace('/[^a-zA-Z0-9_\-]/', '_', $this->fieldname);
102         $options   = array();
103 
104         foreach ($this->element->xpath('option') as $option)
105         {
106             
107             if ($requires = explode(',', (string) $option['requires']))
108             {
109                 
110                 if (in_array('multilanguage', $requires) && !JLanguageMultilang::isEnabled())
111                 {
112                     continue;
113                 }
114 
115                 
116                 if (in_array('associations', $requires) && !JLanguageAssociations::isEnabled())
117                 {
118                     continue;
119                 }
120             }
121 
122             $value = (string) $option['value'];
123             $text  = trim((string) $option) != '' ? trim((string) $option) : $value;
124 
125             $disabled = (string) $option['disabled'];
126             $disabled = ($disabled == 'true' || $disabled == 'disabled' || $disabled == '1');
127             $disabled = $disabled || ($this->readonly && $value != $this->value);
128 
129             $checked = (string) $option['checked'];
130             $checked = ($checked == 'true' || $checked == 'checked' || $checked == '1');
131 
132             $selected = (string) $option['selected'];
133             $selected = ($selected == 'true' || $selected == 'selected' || $selected == '1');
134 
135             $tmp = array(
136                     'value'    => $value,
137                     'text'     => JText::alt($text, $fieldname),
138                     'disable'  => $disabled,
139                     'class'    => (string) $option['class'],
140                     'selected' => ($checked || $selected),
141                     'checked'  => ($checked || $selected),
142             );
143 
144             
145             $tmp['onclick']  = (string) $option['onclick'];
146             $tmp['onchange'] = (string) $option['onchange'];
147 
148             
149             $options[] = (object) $tmp;
150         }
151 
152         if ($this->element['useglobal'])
153         {
154             $tmp        = new stdClass;
155             $tmp->value = '';
156             $tmp->text  = JText::_('JGLOBAL_USE_GLOBAL');
157             $component  = JFactory::getApplication()->input->getCmd('option');
158 
159             
160             if ($component == 'com_menus')
161             {
162                 $link      = $this->form->getData()->get('link');
163                 $uri       = new JUri($link);
164                 $component = $uri->getVar('option', 'com_menus');
165             }
166 
167             $params = JComponentHelper::getParams($component);
168             $value  = $params->get($this->fieldname);
169 
170             
171             if (is_null($value))
172             {
173                 $value = JFactory::getConfig()->get($this->fieldname);
174             }
175 
176             
177             if (is_null($value) && JFactory::getApplication()->input->getCmd('option') == 'com_menus')
178             {
179                 $value = JComponentHelper::getParams('com_menus')->get($this->fieldname);
180             }
181 
182             if (!is_null($value))
183             {
184                 $value = (string) $value;
185 
186                 foreach ($options as $option)
187                 {
188                     if ($option->value === $value)
189                     {
190                         $value = $option->text;
191 
192                         break;
193                     }
194                 }
195 
196                 $tmp->text = JText::sprintf('JGLOBAL_USE_GLOBAL_VALUE', $value);
197             }
198 
199             array_unshift($options, $tmp);
200         }
201 
202         reset($options);
203 
204         return $options;
205     }
206 
207     208 209 210 211 212 213 214 215 216 
217     public function addOption($text, $attributes = array())
218     {
219         if ($text && $this->element instanceof SimpleXMLElement)
220         {
221             $child = $this->element->addChild('option', $text);
222 
223             foreach ($attributes as $name => $value)
224             {
225                 $child->addAttribute($name, $value);
226             }
227         }
228 
229         return $this;
230     }
231 
232     233 234 235 236 237 238 239 240 
241     public function __get($name)
242     {
243         if ($name == 'options')
244         {
245             return $this->getOptions();
246         }
247 
248         return parent::__get($name);
249     }
250 }
251