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