Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
timezone.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('groupedlist');
13 
14 /**
15  * Form Field class for the Joomla Platform.
16  *
17  * @package Joomla.Platform
18  * @subpackage Form
19  * @since 11.1
20  */
22 {
23  /**
24  * The form field type.
25  *
26  * @var string
27  * @since 11.1
28  */
29  protected $type = 'Timezone';
30 
31  /**
32  * The list of available timezone groups to use.
33  *
34  * @var array
35  * @since 11.1
36  */
37  protected static $zones = array('Africa', 'America', 'Antarctica', 'Arctic', 'Asia', 'Atlantic', 'Australia', 'Europe', 'Indian', 'Pacific');
38 
39  /**
40  * The keyField of timezone field.
41  *
42  * @var integer
43  * @since 3.2
44  */
45  protected $keyField;
46 
47  /**
48  * Method to get certain otherwise inaccessible properties from the form field object.
49  *
50  * @param string $name The property name for which to the the value.
51  *
52  * @return mixed The property value or null.
53  *
54  * @since 3.2
55  */
56  public function __get($name)
57  {
58  switch ($name)
59  {
60  case 'keyField':
61  return $this->$name;
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 the 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  {
81  case 'keyField':
82  $this->keyField = (string) $value;
83  break;
84 
85  default:
86  parent::__set($name, $value);
87  }
88  }
89 
90  /**
91  * Method to attach a JForm object to the field.
92  *
93  * @param SimpleXMLElement $element The SimpleXMLElement object representing the <field /> tag for the form field object.
94  * @param mixed $value The form field value to validate.
95  * @param string $group The field name group control value. This acts as as an array container for the field.
96  * For example if the field has name="foo" and the group value is set to "bar" then the
97  * full field name would end up being "bar[foo]".
98  *
99  * @return boolean True on success.
100  *
101  * @see JFormField::setup()
102  * @since 3.2
103  */
104  public function setup(SimpleXMLElement $element, $value, $group = null)
105  {
106  $return = parent::setup($element, $value, $group);
107 
108  if ($return)
109  {
110  $this->keyField = (string) $this->element['key_field'];
111  }
112 
113  return $return;
114  }
115 
116  /**
117  * Method to get the time zone field option groups.
118  *
119  * @return array The field option objects as a nested array in groups.
120  *
121  * @since 11.1
122  */
123  protected function getGroups()
124  {
125  $groups = array();
126 
127  $keyField = !empty($this->keyField) ? $this->keyField : 'id';
128  $keyValue = $this->form->getValue($keyField);
129 
130  // If the timezone is not set use the server setting.
131  if (strlen($this->value) == 0 && empty($keyValue))
132  {
133  $this->value = JFactory::getConfig()->get('offset');
134  }
135 
136  // Get the list of time zones from the server.
137  $zones = DateTimeZone::listIdentifiers();
138 
139  // Build the group lists.
140  foreach ($zones as $zone)
141  {
142  // Time zones not in a group we will ignore.
143  if (strpos($zone, '/') === false)
144  {
145  continue;
146  }
147 
148  // Get the group/locale from the timezone.
149  list ($group, $locale) = explode('/', $zone, 2);
150 
151  // Only use known groups.
152  if (in_array($group, self::$zones))
153  {
154  // Initialize the group if necessary.
155  if (!isset($groups[$group]))
156  {
157  $groups[$group] = array();
158  }
159 
160  // Only add options where a locale exists.
161  if (!empty($locale))
162  {
163  $groups[$group][$zone] = JHtml::_('select.option', $zone, str_replace('_', ' ', $locale), 'value', 'text', false);
164  }
165  }
166  }
167 
168  // Sort the group lists.
169  ksort($groups);
170 
171  foreach ($groups as &$location)
172  {
173  sort($location);
174  }
175 
176  // Merge any additional groups in the XML definition.
177  $groups = array_merge(parent::getGroups(), $groups);
178 
179  return $groups;
180  }
181 }