PageRenderTime 41ms CodeModel.GetById 18ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 0ms

/libraries/joomla/form/fields/number.php

https://gitlab.com/vitaliylukin91/text
PHP | 174 lines | 68 code | 22 blank | 84 comment | 4 complexity | a526bcddfe15a13e2905a73efd9b781b 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 * Form Field class for the Joomla Platform.
 14 * Provides a one line text box with up-down handles to set a number in the field.
 15 *
 16 * @link   http://www.w3.org/TR/html-markup/input.text.html#input.text
 17 * @since  3.2
 18 */
 19class JFormFieldNumber extends JFormField
 20{
 21	/**
 22	 * The form field type.
 23	 *
 24	 * @var    string
 25	 * @since  3.2
 26	 */
 27	protected $type = 'Number';
 28
 29	/**
 30	 * The allowable maximum value of the field.
 31	 *
 32	 * @var    float
 33	 * @since  3.2
 34	 */
 35	protected $max = null;
 36
 37	/**
 38	 * The allowable minimum value of the field.
 39	 *
 40	 * @var    float
 41	 * @since  3.2
 42	 */
 43	protected $min = null;
 44
 45	/**
 46	 * The step by which value of the field increased or decreased.
 47	 *
 48	 * @var    float
 49	 * @since  3.2
 50	 */
 51	protected $step = 0;
 52
 53	/**
 54	 * Method to get certain otherwise inaccessible properties from the form field object.
 55	 *
 56	 * @param   string  $name  The property name for which to the the value.
 57	 *
 58	 * @return  mixed  The property value or null.
 59	 *
 60	 * @since   3.2
 61	 */
 62	public function __get($name)
 63	{
 64		switch ($name)
 65		{
 66			case 'max':
 67			case 'min':
 68			case 'step':
 69				return $this->$name;
 70		}
 71
 72		return parent::__get($name);
 73	}
 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 'step':
 90			case 'min':
 91			case 'max':
 92				$this->$name = (float) $value;
 93				break;
 94
 95			default:
 96				parent::__set($name, $value);
 97		}
 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
118		if ($return)
119		{
120			// It is better not to force any default limits if none is specified
121			$this->max  = isset($this->element['max']) ? (float) $this->element['max'] : null;
122			$this->min  = isset($this->element['min']) ? (float) $this->element['min'] : null;
123			$this->step = isset($this->element['step']) ? (float) $this->element['step'] : 1;
124		}
125
126		return $return;
127	}
128
129	/**
130	 * Method to get the field input markup.
131	 *
132	 * @return  string  The field input markup.
133	 *
134	 * @since   3.2
135	 */
136	protected function getInput()
137	{
138		// Translate placeholder text
139		$hint = $this->translateHint ? JText::_($this->hint) : $this->hint;
140
141		// Initialize some field attributes.
142		$size     = !empty($this->size) ? ' size="' . $this->size . '"' : '';
143
144		// Must use isset instead of !empty for max/min because "zero" boundaries are always acceptable
145		$max      = isset($this->max) ? ' max="' . $this->max . '"' : '';
146		$min      = isset($this->min) ? ' min="' . $this->min . '"' : '';
147
148		$step     = !empty($this->step) ? ' step="' . $this->step . '"' : '';
149		$class    = !empty($this->class) ? ' class="' . $this->class . '"' : '';
150		$readonly = $this->readonly ? ' readonly' : '';
151		$disabled = $this->disabled ? ' disabled' : '';
152		$required = $this->required ? ' required aria-required="true"' : '';
153		$hint     = $hint ? ' placeholder="' . $hint . '"' : '';
154
155		$autocomplete = !$this->autocomplete ? ' autocomplete="off"' : ' autocomplete="' . $this->autocomplete . '"';
156		$autocomplete = $autocomplete == ' autocomplete="on"' ? '' : $autocomplete;
157
158		$autofocus = $this->autofocus ? ' autofocus' : '';
159
160		$value = (float) $this->value;
161		$value = empty($value) ? $this->min : $value;
162
163		// Initialize JavaScript field attributes.
164		$onchange = !empty($this->onchange) ? ' onchange="' . $this->onchange . '"' : '';
165
166		// Including fallback code for HTML5 non supported browsers.
167		JHtml::_('jquery.framework');
168		JHtml::_('script', 'system/html5fallback.js', false, true);
169
170		return '<input type="number" name="' . $this->name . '" id="' . $this->id . '"' . ' value="'
171			. htmlspecialchars($value, ENT_COMPAT, 'UTF-8') . '"' . $class . $size . $disabled . $readonly
172			. $hint . $onchange . $max . $step . $min . $required . $autocomplete . $autofocus . ' />';
173	}
174}