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