[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2011 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\Factory;
  13  use Joomla\CMS\Form\FormField;
  14  
  15  // phpcs:disable PSR1.Files.SideEffects
  16  \defined('JPATH_PLATFORM') or die;
  17  // phpcs:enable PSR1.Files.SideEffects
  18  
  19  /**
  20   * Color Form Field class for the Joomla Platform.
  21   * This implementation is designed to be compatible with HTML5's `<input type="color">`
  22   *
  23   * @link   https://html.spec.whatwg.org/multipage/input.html#color-state-(type=color)
  24   * @since  1.7.3
  25   */
  26  class ColorField extends FormField
  27  {
  28      /**
  29       * The form field type.
  30       *
  31       * @var    string
  32       * @since  1.7.3
  33       */
  34      protected $type = 'Color';
  35  
  36      /**
  37       * The control.
  38       *
  39       * @var    mixed
  40       * @since  3.2
  41       */
  42      protected $control = 'hue';
  43  
  44      /**
  45       * Default color when there is no value.
  46       *
  47       * @var    string
  48       * @since  4.0.0
  49       */
  50      protected $default;
  51  
  52      /**
  53       * The type of value the slider should display: 'hue', 'saturation' or 'light'.
  54       *
  55       * @var    string
  56       * @since  4.0.0
  57       */
  58      protected $display = 'hue';
  59  
  60      /**
  61       * The format.
  62       *
  63       * @var    string
  64       * @since  3.6.0
  65       */
  66      protected $format = 'hex';
  67  
  68      /**
  69       * The keywords (transparent,initial,inherit).
  70       *
  71       * @var    string
  72       * @since  3.6.0
  73       */
  74      protected $keywords = '';
  75  
  76      /**
  77       * The position.
  78       *
  79       * @var    mixed
  80       * @since  3.2
  81       */
  82      protected $position = 'default';
  83  
  84      /**
  85       * The colors.
  86       *
  87       * @var    mixed
  88       * @since  3.2
  89       */
  90      protected $colors;
  91  
  92      /**
  93       * Shows preview of the selected color
  94       *
  95       * @var    boolean
  96       * @since  4.0.0
  97       */
  98      protected $preview = false;
  99  
 100      /**
 101       * Color format to use when value gets saved
 102       *
 103       * @var    string
 104       * @since  4.0.0
 105       */
 106      protected $saveFormat = 'hex';
 107  
 108      /**
 109       * The split.
 110       *
 111       * @var    integer
 112       * @since  3.2
 113       */
 114      protected $split = 3;
 115  
 116      /**
 117       * Name of the layout being used to render the field
 118       *
 119       * @var    string
 120       * @since  3.5
 121       */
 122      protected $layout = 'joomla.form.field.color';
 123  
 124      /**
 125       * Method to get certain otherwise inaccessible properties from the form field object.
 126       *
 127       * @param   string  $name  The property name for which to get the value.
 128       *
 129       * @return  mixed  The property value or null.
 130       *
 131       * @since   3.2
 132       */
 133      public function __get($name)
 134      {
 135          switch ($name) {
 136              case 'colors':
 137              case 'control':
 138              case 'default':
 139              case 'display':
 140              case 'exclude':
 141              case 'format':
 142              case 'keywords':
 143              case 'preview':
 144              case 'saveFormat':
 145              case 'split':
 146                  return $this->$name;
 147          }
 148  
 149          return parent::__get($name);
 150      }
 151  
 152      /**
 153       * Method to set certain otherwise inaccessible properties of the form field object.
 154       *
 155       * @param   string  $name   The property name for which to set the value.
 156       * @param   mixed   $value  The value of the property.
 157       *
 158       * @return  void
 159       *
 160       * @since   3.2
 161       */
 162      public function __set($name, $value)
 163      {
 164          switch ($name) {
 165              case 'colors':
 166              case 'control':
 167              case 'default':
 168              case 'display':
 169              case 'exclude':
 170              case 'format':
 171              case 'keywords':
 172              case 'saveFormat':
 173                  $this->$name = (string) $value;
 174                  break;
 175              case 'split':
 176                  $this->$name = (int) $value;
 177                  break;
 178              case 'preview':
 179                  $this->$name = (bool) $value;
 180                  break;
 181              default:
 182                  parent::__set($name, $value);
 183          }
 184      }
 185  
 186      /**
 187       * Method to attach a Form object to the field.
 188       *
 189       * @param   \SimpleXMLElement  $element  The SimpleXMLElement object representing the `<field>` tag for the form field object.
 190       * @param   mixed              $value    The form field value to validate.
 191       * @param   string             $group    The field name group control value. This acts as an array container for the field.
 192       *                                      For example if the field has name="foo" and the group value is set to "bar" then the
 193       *                                      full field name would end up being "bar[foo]".
 194       *
 195       * @return  boolean  True on success.
 196       *
 197       * @see     FormField::setup()
 198       * @since   3.2
 199       */
 200      public function setup(\SimpleXMLElement $element, $value, $group = null)
 201      {
 202          $return = parent::setup($element, $value, $group);
 203  
 204          if ($return) {
 205              $this->colors     = (string) $this->element['colors'];
 206              $this->control    = isset($this->element['control']) ? (string) $this->element['control'] : 'hue';
 207              $this->default    = (string) $this->element['default'];
 208              $this->display    = isset($this->element['display']) ? (string) $this->element['display'] : 'hue';
 209              $this->format     = isset($this->element['format']) ? (string) $this->element['format'] : 'hex';
 210              $this->keywords   = (string) $this->element['keywords'];
 211              $this->position   = isset($this->element['position']) ? (string) $this->element['position'] : 'default';
 212              $this->preview    = isset($this->element['preview']) ? (string) $this->element['preview'] : false;
 213              $this->saveFormat = isset($this->element['saveFormat']) ? (string) $this->element['saveFormat'] : 'hex';
 214              $this->split      = isset($this->element['split']) ? (int) $this->element['split'] : 3;
 215          }
 216  
 217          return $return;
 218      }
 219  
 220      /**
 221       * Method to get the field input markup.
 222       *
 223       * @return  string  The field input markup.
 224       *
 225       * @since   1.7.3
 226       */
 227      protected function getInput()
 228      {
 229          // Switch the layouts
 230          if ($this->control === 'simple' || $this->control === 'slider') {
 231              $this->layout .= '.' . $this->control;
 232          } else {
 233              $this->layout .= '.advanced';
 234          }
 235  
 236          // Trim the trailing line in the layout file
 237          return rtrim($this->getRenderer($this->layout)->render($this->getLayoutData()), PHP_EOL);
 238      }
 239  
 240      /**
 241       * Method to get the data to be passed to the layout for rendering.
 242       *
 243       * @return  array
 244       *
 245       * @since 3.5
 246       */
 247      protected function getLayoutData()
 248      {
 249          $lang  = Factory::getApplication()->getLanguage();
 250          $data  = parent::getLayoutData();
 251          $color = strtolower($this->value);
 252          $color = !$color && $color !== '0' ? '' : $color;
 253  
 254          // Position of the panel can be: right (default), left, top or bottom (default RTL is left)
 255          $position = ' data-position="' . (($lang->isRtl() && $this->position === 'default') ? 'left' : $this->position) . '"';
 256  
 257          if ($color === '' || \in_array($color, array('none', 'transparent'))) {
 258              $color = 'none';
 259          } elseif ($color[0] !== '#' && $this->format === 'hex') {
 260              $color = '#' . $color;
 261          }
 262  
 263          switch ($this->control) {
 264              case 'simple':
 265                  $controlModeData = $this->getSimpleModeLayoutData();
 266                  break;
 267              case 'slider':
 268                  $controlModeData = $this->getSliderModeLayoutData();
 269                  break;
 270              case 'advanced':
 271              default:
 272                  $controlModeData = $this->getAdvancedModeLayoutData($lang);
 273                  break;
 274          }
 275  
 276          $extraData = array(
 277              'color'    => $color,
 278              'format'   => $this->format,
 279              'keywords' => $this->keywords,
 280              'position' => $position,
 281              'validate' => $this->validate,
 282          );
 283  
 284          return array_merge($data, $extraData, $controlModeData);
 285      }
 286  
 287      /**
 288       * Method to get the data for the simple mode to be passed to the layout for rendering.
 289       *
 290       * @return  array
 291       *
 292       * @since 3.5
 293       */
 294      protected function getSimpleModeLayoutData()
 295      {
 296          $colors = strtolower($this->colors);
 297  
 298          if (empty($colors)) {
 299              $colors = array(
 300                  'none',
 301                  '#049cdb',
 302                  '#46a546',
 303                  '#9d261d',
 304                  '#ffc40d',
 305                  '#f89406',
 306                  '#c3325f',
 307                  '#7a43b6',
 308                  '#ffffff',
 309                  '#999999',
 310                  '#555555',
 311                  '#000000',
 312              );
 313          } else {
 314              $colors = explode(',', $colors);
 315          }
 316  
 317          if (!$this->split) {
 318              $count = \count($colors);
 319  
 320              if ($count % 5 == 0) {
 321                  $split = 5;
 322              } else {
 323                  if ($count % 4 == 0) {
 324                      $split = 4;
 325                  }
 326              }
 327          }
 328  
 329          $split = $this->split ?: 3;
 330  
 331          return array(
 332              'colors' => $colors,
 333              'split'  => $split,
 334          );
 335      }
 336  
 337      /**
 338       * Method to get the data for the advanced mode to be passed to the layout for rendering.
 339       *
 340       * @param   object  $lang  The language object
 341       *
 342       * @return  array
 343       *
 344       * @since   3.5
 345       */
 346      protected function getAdvancedModeLayoutData($lang)
 347      {
 348          return array(
 349              'colors'  => $this->colors,
 350              'control' => $this->control,
 351              'lang'    => $lang,
 352          );
 353      }
 354  
 355      /**
 356       * Method to get the data for the slider
 357       *
 358       * @return  array
 359       *
 360       * @since   4.0.0
 361       */
 362      protected function getSliderModeLayoutData()
 363      {
 364          return array(
 365              'default'    => $this->default,
 366              'display'    => $this->display,
 367              'preview'    => $this->preview,
 368              'saveFormat' => $this->saveFormat,
 369          );
 370      }
 371  }


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