Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
meter.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 JFormHelper::loadFieldClass('number');
13 
14 /**
15  * Form Field class for the Joomla Platform.
16  * Provides a meter to show value in a range.
17  *
18  * @package Joomla.Platform
19  * @subpackage Form
20  * @link http://www.w3.org/TR/html-markup/input.text.html#input.text
21  * @since 3.2
22  */
24 {
25  /**
26  * The form field type.
27  *
28  * @var string
29  * @since 3.2
30  */
31  protected $type = 'Meter';
32 
33  /**
34  * The width of the field increased or decreased.
35  *
36  * @var string
37  * @since 3.2
38  */
39  protected $width;
40 
41  /**
42  * Whether the field is active or not.
43  *
44  * @var boolean
45  * @since 3.2
46  */
47  protected $active = false;
48 
49  /**
50  * Whether the field is animated or not.
51  *
52  * @var boolean
53  * @since 3.2
54  */
55  protected $animated = true;
56 
57  /**
58  * The color of the field
59  *
60  * @var boolean
61  * @since 3.2
62  */
63  protected $color;
64 
65  /**
66  * Method to get certain otherwise inaccessible properties from the form field object.
67  *
68  * @param string $name The property name for which to the the value.
69  *
70  * @return mixed The property value or null.
71  *
72  * @since 3.2
73  */
74  public function __get($name)
75  {
76  switch ($name)
77  {
78  case 'active':
79  case 'width':
80  case 'animated':
81  case 'color':
82  return $this->$name;
83  }
84 
85  return parent::__get($name);
86  }
87 
88  /**
89  * Method to set certain otherwise inaccessible properties of the form field object.
90  *
91  * @param string $name The property name for which to the the value.
92  * @param mixed $value The value of the property.
93  *
94  * @return void
95  *
96  * @since 3.2
97  */
98  public function __set($name, $value)
99  {
100  switch ($name)
101  {
102  case 'width':
103  case 'color':
104  $this->$name = (string) $value;
105  break;
106 
107  case 'active':
108  $value = (string) $value;
109  $this->$name = ($value === 'true' || $value === $name || $value === '1');
110  break;
111 
112  case 'animated':
113  $value = (string) $value;
114  $this->$name = !($value === 'false' || $value === 'off' || $value === '0');
115  break;
116 
117  default:
118  parent::__set($name, $value);
119  }
120  }
121 
122  /**
123  * Method to attach a JForm object to the field.
124  *
125  * @param SimpleXMLElement $element The SimpleXMLElement object representing the <field /> tag for the form field object.
126  * @param mixed $value The form field value to validate.
127  * @param string $group The field name group control value. This acts as as an array container for the field.
128  * For example if the field has name="foo" and the group value is set to "bar" then the
129  * full field name would end up being "bar[foo]".
130  *
131  * @return boolean True on success.
132  *
133  * @see JFormField::setup()
134  * @since 3.2
135  */
136  public function setup(SimpleXMLElement $element, $value, $group = null)
137  {
138  $return = parent::setup($element, $value, $group);
139 
140  if ($return)
141  {
142  $this->width = isset($this->element['width']) ? (string) $this->element['width'] : '';
143  $this->color = isset($this->element['color']) ? (string) $this->element['color'] : '';
144 
145  $active = (string) $this->element['active'];
146  $this->active = ($active == 'true' || $active == 'on' || $active == '1');
147 
148  $animated = (string) $this->element['animated'];
149  $this->animated = !($animated == 'false' || $animated == 'off' || $animated == '0');
150  }
151 
152  return $return;
153  }
154 
155  /**
156  * Method to get the field input markup.
157  *
158  * @return string The field input markup.
159  *
160  * @since 3.2
161  */
162  protected function getInput()
163  {
164  // Initialize some field attributes.
165  $width = !empty($this->width) ? ' style="width:' . $this->width . ';"' : '';
166  $color = !empty($this->color) ? ' background-color:' . $this->color . ';' : '';
167 
168  $data = '';
169  $data .= ' data-max="' . $this->max . '"';
170  $data .= ' data-min="' . $this->min . '"';
171  $data .= ' data-step="' . $this->step . '"';
172 
173  $class = 'progress ' . $this->class;
174  $class .= $this->animated ? ' progress-striped' : '';
175  $class .= $this->active ? ' active' : '';
176  $class = ' class="' . $class . '"';
177 
178  $value = (float) $this->value;
179  $value = $value < $this->min ? $this->min : $value;
180  $value = $value > $this->max ? $this->max : $value;
181 
182  $data .= ' data-value="' . $this->value . '"';
183 
184  $value = ((float) ($value - $this->min) * 100) / ($this->max - $this->min);
185 
186  $html[] = '<div ' . $class . $width . $data . ' >';
187  $html[] = ' <div class="bar" style="width: ' . strval($value) . '%;' . $color . '"></div>';
188  $html[] = '</div>';
189 
190  return implode('', $html);
191  }
192 }