/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. defined('JPATH_PLATFORM') or die;
  10. /**
  11. * Form Field class for the Joomla Platform.
  12. * Provides a one line text box with up-down handles to set a number in the field.
  13. *
  14. * @link http://www.w3.org/TR/html-markup/input.text.html#input.text
  15. * @since 3.2
  16. */
  17. class JFormFieldNumber extends JFormField
  18. {
  19. /**
  20. * The form field type.
  21. *
  22. * @var string
  23. * @since 3.2
  24. */
  25. protected $type = 'Number';
  26. /**
  27. * The allowable maximum value of the field.
  28. *
  29. * @var float
  30. * @since 3.2
  31. */
  32. protected $max = null;
  33. /**
  34. * The allowable minimum value of the field.
  35. *
  36. * @var float
  37. * @since 3.2
  38. */
  39. protected $min = null;
  40. /**
  41. * The step by which value of the field increased or decreased.
  42. *
  43. * @var float
  44. * @since 3.2
  45. */
  46. protected $step = 0;
  47. /**
  48. * Method to get certain otherwise inaccessible properties from the form field object.
  49. *
  50. * @param string $name The property name for which to the the value.
  51. *
  52. * @return mixed The property value or null.
  53. *
  54. * @since 3.2
  55. */
  56. public function __get($name)
  57. {
  58. switch ($name)
  59. {
  60. case 'max':
  61. case 'min':
  62. case 'step':
  63. return $this->$name;
  64. }
  65. return parent::__get($name);
  66. }
  67. /**
  68. * Method to set certain otherwise inaccessible properties of the form field object.
  69. *
  70. * @param string $name The property name for which to the the value.
  71. * @param mixed $value The value of the property.
  72. *
  73. * @return void
  74. *
  75. * @since 3.2
  76. */
  77. public function __set($name, $value)
  78. {
  79. switch ($name)
  80. {
  81. case 'step':
  82. case 'min':
  83. case 'max':
  84. $this->$name = (float) $value;
  85. break;
  86. default:
  87. parent::__set($name, $value);
  88. }
  89. }
  90. /**
  91. * Method to attach a JForm object to the field.
  92. *
  93. * @param SimpleXMLElement $element The SimpleXMLElement object representing the <field /> tag for the form field object.
  94. * @param mixed $value The form field value to validate.
  95. * @param string $group The field name group control value. This acts as as an array container for the field.
  96. * For example if the field has name="foo" and the group value is set to "bar" then the
  97. * full field name would end up being "bar[foo]".
  98. *
  99. * @return boolean True on success.
  100. *
  101. * @see JFormField::setup()
  102. * @since 3.2
  103. */
  104. public function setup(SimpleXMLElement $element, $value, $group = null)
  105. {
  106. $return = parent::setup($element, $value, $group);
  107. if ($return)
  108. {
  109. // It is better not to force any default limits if none is specified
  110. $this->max = isset($this->element['max']) ? (float) $this->element['max'] : null;
  111. $this->min = isset($this->element['min']) ? (float) $this->element['min'] : null;
  112. $this->step = isset($this->element['step']) ? (float) $this->element['step'] : 1;
  113. }
  114. return $return;
  115. }
  116. /**
  117. * Method to get the field input markup.
  118. *
  119. * @return string The field input markup.
  120. *
  121. * @since 3.2
  122. */
  123. protected function getInput()
  124. {
  125. // Translate placeholder text
  126. $hint = $this->translateHint ? JText::_($this->hint) : $this->hint;
  127. // Initialize some field attributes.
  128. $size = !empty($this->size) ? ' size="' . $this->size . '"' : '';
  129. // Must use isset instead of !empty for max/min because "zero" boundaries are always acceptable
  130. $max = isset($this->max) ? ' max="' . $this->max . '"' : '';
  131. $min = isset($this->min) ? ' min="' . $this->min . '"' : '';
  132. $step = !empty($this->step) ? ' step="' . $this->step . '"' : '';
  133. $class = !empty($this->class) ? ' class="' . $this->class . '"' : '';
  134. $readonly = $this->readonly ? ' readonly' : '';
  135. $disabled = $this->disabled ? ' disabled' : '';
  136. $required = $this->required ? ' required aria-required="true"' : '';
  137. $hint = $hint ? ' placeholder="' . $hint . '"' : '';
  138. $autocomplete = !$this->autocomplete ? ' autocomplete="off"' : ' autocomplete="' . $this->autocomplete . '"';
  139. $autocomplete = $autocomplete == ' autocomplete="on"' ? '' : $autocomplete;
  140. $autofocus = $this->autofocus ? ' autofocus' : '';
  141. $value = (float) $this->value;
  142. $value = empty($value) ? $this->min : $value;
  143. // Initialize JavaScript field attributes.
  144. $onchange = !empty($this->onchange) ? ' onchange="' . $this->onchange . '"' : '';
  145. // Including fallback code for HTML5 non supported browsers.
  146. JHtml::_('jquery.framework');
  147. JHtml::_('script', 'system/html5fallback.js', false, true);
  148. return '<input type="number" name="' . $this->name . '" id="' . $this->id . '"' . ' value="'
  149. . htmlspecialchars($value, ENT_COMPAT, 'UTF-8') . '"' . $class . $size . $disabled . $readonly
  150. . $hint . $onchange . $max . $step . $min . $required . $autocomplete . $autofocus . ' />';
  151. }
  152. }