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