1 <?php
  2 /**
  3  * @package     Joomla.Platform
  4  * @subpackage  Form
  5  *
  6  * @copyright   Copyright (C) 2005 - 2017 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  * @link   http://www.w3.org/TR/html-markup/input.text.html#input.text
 19  * @since  3.2
 20  */
 21 class JFormFieldMeter extends JFormFieldNumber
 22 {
 23     /**
 24      * The form field type.
 25      *
 26      * @var    string
 27      * @since  3.2
 28      */
 29     protected $type = 'Meter';
 30 
 31     /**
 32      * The width of the field increased or decreased.
 33      *
 34      * @var    string
 35      * @since  3.2
 36      */
 37     protected $width;
 38 
 39     /**
 40      * Whether the field is active or not.
 41      *
 42      * @var    boolean
 43      * @since  3.2
 44      */
 45     protected $active = false;
 46 
 47     /**
 48      * Whether the field is animated or not.
 49      *
 50      * @var    boolean
 51      * @since  3.2
 52      */
 53     protected $animated = true;
 54 
 55     /**
 56      * The color of the field
 57      *
 58      * @var    boolean
 59      * @since  3.2
 60      */
 61     protected $color;
 62 
 63     /**
 64      * Name of the layout being used to render the field
 65      *
 66      * @var    string
 67      * @since  3.7
 68      */
 69     protected $layout = 'joomla.form.field.meter';
 70 
 71     /**
 72      * Method to get certain otherwise inaccessible properties from the form field object.
 73      *
 74      * @param   string  $name  The property name for which to the the value.
 75      *
 76      * @return  mixed  The property value or null.
 77      *
 78      * @since   3.2
 79      */
 80     public function __get($name)
 81     {
 82         switch ($name)
 83         {
 84             case 'active':
 85             case 'width':
 86             case 'animated':
 87             case 'color':
 88                 return $this->$name;
 89         }
 90 
 91         return parent::__get($name);
 92     }
 93 
 94     /**
 95      * Method to set certain otherwise inaccessible properties of the form field object.
 96      *
 97      * @param   string  $name   The property name for which to the the value.
 98      * @param   mixed   $value  The value of the property.
 99      *
100      * @return  void
101      *
102      * @since   3.2
103      */
104     public function __set($name, $value)
105     {
106         switch ($name)
107         {
108             case 'width':
109             case 'color':
110                 $this->$name = (string) $value;
111                 break;
112 
113             case 'active':
114                 $value = (string) $value;
115                 $this->active = ($value === 'true' || $value === $name || $value === '1');
116                 break;
117 
118             case 'animated':
119                 $value = (string) $value;
120                 $this->animated = !($value === 'false' || $value === 'off' || $value === '0');
121                 break;
122 
123             default:
124                 parent::__set($name, $value);
125         }
126     }
127 
128     /**
129      * Method to attach a JForm object to the field.
130      *
131      * @param   SimpleXMLElement  $element  The SimpleXMLElement object representing the `<field>` tag for the form field object.
132      * @param   mixed             $value    The form field value to validate.
133      * @param   string            $group    The field name group control value. This acts as an array container for the field.
134      *                                      For example if the field has name="foo" and the group value is set to "bar" then the
135      *                                      full field name would end up being "bar[foo]".
136      *
137      * @return  boolean  True on success.
138      *
139      * @see     JFormField::setup()
140      * @since   3.2
141      */
142     public function setup(SimpleXMLElement $element, $value, $group = null)
143     {
144         $return = parent::setup($element, $value, $group);
145 
146         if ($return)
147         {
148             $this->width = isset($this->element['width']) ? (string) $this->element['width'] : '';
149             $this->color = isset($this->element['color']) ? (string) $this->element['color'] : '';
150 
151             $active       = (string) $this->element['active'];
152             $this->active = ($active == 'true' || $active == 'on' || $active == '1');
153 
154             $animated       = (string) $this->element['animated'];
155             $this->animated = !($animated == 'false' || $animated == 'off' || $animated == '0');
156         }
157 
158         return $return;
159     }
160 
161     /**
162      * Method to get the field input markup.
163      *
164      * @return  string  The field input markup.
165      *
166      * @since   3.2
167      */
168     protected function getInput()
169     {
170         // Trim the trailing line in the layout file
171         return rtrim($this->getRenderer($this->layout)->render($this->getLayoutData()), PHP_EOL);
172     }
173 
174     /**
175      * Method to get the data to be passed to the layout for rendering.
176      *
177      * @return  array
178      *
179      * @since 3.5
180      */
181     protected function getLayoutData()
182     {
183         $data = parent::getLayoutData();
184 
185         // Initialize some field attributes.
186         $extraData = array(
187             'width'    => $this->width,
188             'color'    => $this->color,
189             'animated' => $this->animated,
190             'active'   => $this->active,
191             'max'      => $this->max,
192             'min'      => $this->min,
193             'step'     => $this->step,
194         );
195 
196         return array_merge($data, $extraData);
197     }
198 }
199