PageRenderTime 25ms CodeModel.GetById 10ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 1ms

/libraries/joomla/form/fields/color.php

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