Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
color.php
Aller à la documentation de ce fichier.
1 <?php
2 /**
3  * @package Joomla.Platform
4  * @subpackage Form
5  *
6  * @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
7  * @license GNU General Public License version 2 or later; see LICENSE
8  */
9 
10 defined('JPATH_PLATFORM') or die;
11 
12 /**
13  * Color Form Field class for the Joomla Platform.
14  * This implementation is designed to be compatible with HTML5's <input type="color">
15  *
16  * @package Joomla.Platform
17  * @subpackage Form
18  * @link http://www.w3.org/TR/html-markup/input.color.html
19  * @since 11.3
20  */
22 {
23  /**
24  * The form field type.
25  *
26  * @var string
27  * @since 11.3
28  */
29  protected $type = 'Color';
30 
31  /**
32  * The control.
33  *
34  * @var mixed
35  * @since 3.2
36  */
37  protected $control = 'hue';
38 
39  /**
40  * The position.
41  *
42  * @var mixed
43  * @since 3.2
44  */
45  protected $position = 'right';
46 
47  /**
48  * The colors.
49  *
50  * @var mixed
51  * @since 3.2
52  */
53  protected $colors;
54 
55  /**
56  * The split.
57  *
58  * @var integer
59  * @since 3.2
60  */
61  protected $split = 3;
62 
63  /**
64  * Method to get certain otherwise inaccessible properties from the form field object.
65  *
66  * @param string $name The property name for which to the the value.
67  *
68  * @return mixed The property value or null.
69  *
70  * @since 3.2
71  */
72  public function __get($name)
73  {
74  switch ($name)
75  {
76  case 'control':
77  case 'exclude':
78  case 'colors':
79  case 'split':
80  return $this->$name;
81  }
82 
83  return parent::__get($name);
84  }
85 
86  /**
87  * Method to set certain otherwise inaccessible properties of the form field object.
88  *
89  * @param string $name The property name for which to the the value.
90  * @param mixed $value The value of the property.
91  *
92  * @return void
93  *
94  * @since 3.2
95  */
96  public function __set($name, $value)
97  {
98  switch ($name)
99  {
100  case 'split':
101  $value = (int) $value;
102  case 'control':
103  case 'exclude':
104  case 'colors':
105  $this->$name = (string) $value;
106  break;
107 
108  default:
109  parent::__set($name, $value);
110  }
111  }
112 
113  /**
114  * Method to attach a JForm object to the field.
115  *
116  * @param SimpleXMLElement $element The SimpleXMLElement object representing the <field /> tag for the form field object.
117  * @param mixed $value The form field value to validate.
118  * @param string $group The field name group control value. This acts as as an array container for the field.
119  * For example if the field has name="foo" and the group value is set to "bar" then the
120  * full field name would end up being "bar[foo]".
121  *
122  * @return boolean True on success.
123  *
124  * @see JFormField::setup()
125  * @since 3.2
126  */
127  public function setup(SimpleXMLElement $element, $value, $group = null)
128  {
129  $return = parent::setup($element, $value, $group);
130 
131  if ($return)
132  {
133  $this->control = isset($this->element['control']) ? (string) $this->element['control'] : 'hue';
134  $this->position = isset($this->element['position']) ? (string) $this->element['position'] : 'right';
135  $this->colors = (string) $this->element['colors'];
136  $this->split = isset($this->element['split']) ? (int) $this->element['split'] : 3;
137  }
138 
139  return $return;
140  }
141 
142  /**
143  * Method to get the field input markup.
144  *
145  * @return string The field input markup.
146  *
147  * @since 11.3
148  */
149  protected function getInput()
150  {
151  // Translate placeholder text
152  $hint = $this->translateHint ? JText::_($this->hint) : $this->hint;
153 
154  // Control value can be: hue (default), saturation, brightness, wheel or simple
155  $control = $this->control;
156 
157  // Position of the panel can be: right (default), left, top or bottom
158  $position = ' data-position="' . $this->position . '"';
159 
160  $onchange = !empty($this->onchange) ? ' onchange="' . $this->onchange . '"' : '';
161  $class = $this->class;
162  $required = $this->required ? ' required aria-required="true"' : '';
163  $disabled = $this->disabled ? ' disabled' : '';
164  $autofocus = $this->autofocus ? ' autofocus' : '';
165 
166  $color = strtolower($this->value);
167 
168  if (!$color || in_array($color, array('none', 'transparent')))
169  {
170  $color = 'none';
171  }
172  elseif ($color['0'] != '#')
173  {
174  $color = '#' . $color;
175  }
176 
177  if ($control == 'simple')
178  {
179  $class = ' class="' . trim('simplecolors chzn-done ' . $class) . '"';
180  JHtml::_('behavior.simplecolorpicker');
181 
182  $colors = strtolower($this->colors);
183 
184  if (empty($colors))
185  {
186  $colors = array(
187  'none',
188  '#049cdb',
189  '#46a546',
190  '#9d261d',
191  '#ffc40d',
192  '#f89406',
193  '#c3325f',
194  '#7a43b6',
195  '#ffffff',
196  '#999999',
197  '#555555',
198  '#000000'
199  );
200  }
201  else
202  {
203  $colors = explode(',', $colors);
204  }
205 
206  $split = $this->split;
207 
208  if (!$split)
209  {
210  $count = count($colors);
211 
212  if ($count % 5 == 0)
213  {
214  $split = 5;
215  }
216  else
217  {
218  if ($count % 4 == 0)
219  {
220  $split = 4;
221  }
222  }
223  }
224 
225  $split = $split ? $split : 3;
226 
227  $html = array();
228  $html[] = '<select name="' . $this->name . '" id="' . $this->id . '"' . $disabled . $required
229  . $class . $position . $onchange . $autofocus . ' style="visibility:hidden;width:22px;height:1px">';
230 
231  foreach ($colors as $i => $c)
232  {
233  $html[] = '<option' . ($c == $color ? ' selected="selected"' : '') . '>' . $c . '</option>';
234 
235  if (($i + 1) % $split == 0)
236  {
237  $html[] = '<option>-</option>';
238  }
239  }
240 
241  $html[] = '</select>';
242 
243  return implode('', $html);
244  }
245  else
246  {
247  $class = ' class="' . trim('minicolors ' . $class) . '"';
248  $control = $control ? ' data-control="' . $control . '"' : '';
249  $readonly = $this->readonly ? ' readonly' : '';
250  $hint = $hint ? ' placeholder="' . $hint . '"' : ' placeholder="#rrggbb"';
251  $autocomplete = !$this->autocomplete ? ' autocomplete="off"' : '';
252 
253  // Including fallback code for HTML5 non supported browsers.
254  JHtml::_('jquery.framework');
255  JHtml::_('script', 'system/html5fallback.js', false, true);
256 
257  JHtml::_('behavior.colorpicker');
258 
259  return '<input type="text" name="' . $this->name . '" id="' . $this->id . '"' . ' value="'
260  . htmlspecialchars($color, ENT_COMPAT, 'UTF-8') . '"' . $hint . $class . $position . $control
261  . $readonly . $disabled . $required . $onchange . $autocomplete . $autofocus . '/>';
262  }
263  }
264 }