[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Form/Field/ -> MeterField.php (source)

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


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer