[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2009 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\HTML\HTMLHelper;
  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   *
  21   * @since  1.7.0
  22   */
  23  class TimezoneField extends GroupedlistField
  24  {
  25      /**
  26       * The form field type.
  27       *
  28       * @var    string
  29       * @since  1.7.0
  30       */
  31      protected $type = 'Timezone';
  32  
  33      /**
  34       * The list of available timezone groups to use.
  35       *
  36       * @var    array
  37       * @since  1.7.0
  38       */
  39      protected static $zones = array('Africa', 'America', 'Antarctica', 'Arctic', 'Asia', 'Atlantic', 'Australia', 'Europe', 'Indian', 'Pacific');
  40  
  41      /**
  42       * The keyField of timezone field.
  43       *
  44       * @var    integer
  45       * @since  3.2
  46       */
  47      protected $keyField;
  48  
  49      /**
  50       * Method to get certain otherwise inaccessible properties from the form field object.
  51       *
  52       * @param   string  $name  The property name for which to get the value.
  53       *
  54       * @return  mixed  The property value or null.
  55       *
  56       * @since   3.2
  57       */
  58      public function __get($name)
  59      {
  60          if ($name === 'keyField') {
  61              return $this->keyField;
  62          }
  63  
  64          return parent::__get($name);
  65      }
  66  
  67      /**
  68       * Method to set certain otherwise inaccessible properties of the form field object.
  69       *
  70       * @param   string  $name   The property name for which to set the value.
  71       * @param   mixed   $value  The value of the property.
  72       *
  73       * @return  void
  74       *
  75       * @since   3.2
  76       */
  77      public function __set($name, $value)
  78      {
  79          switch ($name) {
  80              case 'keyField':
  81                  $this->keyField = (string) $value;
  82                  break;
  83  
  84              default:
  85                  parent::__set($name, $value);
  86          }
  87      }
  88  
  89      /**
  90       * Method to attach a Form object to the field.
  91       *
  92       * @param   \SimpleXMLElement  $element  The SimpleXMLElement object representing the `<field>` tag for the form field object.
  93       * @param   mixed              $value    The form field value to validate.
  94       * @param   string             $group    The field name group control value. This acts as an array container for the field.
  95       *                                       For example if the field has name="foo" and the group value is set to "bar" then the
  96       *                                       full field name would end up being "bar[foo]".
  97       *
  98       * @return  boolean  True on success.
  99       *
 100       * @see     FormField::setup()
 101       * @since   3.2
 102       */
 103      public function setup(\SimpleXMLElement $element, $value, $group = null)
 104      {
 105          $return = parent::setup($element, $value, $group);
 106  
 107          if ($return) {
 108              $this->keyField = (string) $this->element['key_field'];
 109          }
 110  
 111          return $return;
 112      }
 113  
 114      /**
 115       * Method to get the time zone field option groups.
 116       *
 117       * @return  array  The field option objects as a nested array in groups.
 118       *
 119       * @since   1.7.0
 120       */
 121      protected function getGroups()
 122      {
 123          $groups = array();
 124  
 125          // Get the list of time zones from the server.
 126          $zones = \DateTimeZone::listIdentifiers();
 127  
 128          // Build the group lists.
 129          foreach ($zones as $zone) {
 130              // Time zones not in a group we will ignore.
 131              if (strpos($zone, '/') === false) {
 132                  continue;
 133              }
 134  
 135              // Get the group/locale from the timezone.
 136              list ($group, $locale) = explode('/', $zone, 2);
 137  
 138              // Only use known groups.
 139              if (\in_array($group, self::$zones)) {
 140                  // Initialize the group if necessary.
 141                  if (!isset($groups[$group])) {
 142                      $groups[$group] = array();
 143                  }
 144  
 145                  // Only add options where a locale exists.
 146                  if (!empty($locale)) {
 147                      $groups[$group][$zone] = HTMLHelper::_('select.option', $zone, str_replace('_', ' ', $locale), 'value', 'text', false);
 148                  }
 149              }
 150          }
 151  
 152          // Sort the group lists.
 153          ksort($groups);
 154  
 155          foreach ($groups as &$location) {
 156              sort($location);
 157          }
 158  
 159          // Merge any additional groups in the XML definition.
 160          $groups = array_merge(parent::getGroups(), $groups);
 161  
 162          return $groups;
 163      }
 164  }


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