Skip to content

Commit

Permalink
Improve/fix the getExampleFormMarkup of the Member: Timezone field
Browse files Browse the repository at this point in the history
  • Loading branch information
Brendan Abbott committed Apr 3, 2011
1 parent 6aa38d7 commit 4c05dbf
Showing 1 changed file with 56 additions and 26 deletions.
82 changes: 56 additions & 26 deletions fields/field.membertimezone.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ public function getMemberTimezone($member_id) {
));
}

/**
* Creates a list of Timezones for the With Selected dropdown in the backend.
* This list has the limitation that the timezones cannot be grouped as the
* With Selected menu already uses `<optgroup>` to separate the toggling of
* different Field data.
*
* @return array
*/
public function getToggleStates() {
$zones = explode(",", $this->get('available_zones'));

Expand All @@ -76,6 +84,49 @@ public function getToggleStates() {
return $options;
}

/**
* Builds a XMLElement containing a `<select>` with all the available timezone
* options grouped by the different DateTimeZone constants allowed by an instance
* of this field. Developers can select what Timezones are available from the
* Section Editor.
*
* @link http://www.php.net/manual/en/class.datetimezone.php
* @param array $data
* @param string $prefix
* @param string $postfix
* @return XMLElement
*/
public function buildTZSelection(Array $data = array(), $prefix = null, $postfix = null) {
$groups = array();

if ($this->get('required') != 'yes') $groups[] = array(NULL, false, NULL);

$zones = explode(",", $this->get('available_zones'));

foreach($zones as $zone) {
$timezones = DateTimeZone::listIdentifiers(constant('DateTimeZone::' . $zone));

$options = array();
foreach($timezones as $timezone) {
$tz = new DateTime('now', new DateTimeZone($timezone));

$options[] = array($timezone, ($timezone == $data['value']), sprintf("%s %s",
str_replace('_', ' ', substr(strrchr($timezone, '/'),1)),
$tz->format('P')
));
}

$groups[] = array('label' => ucwords(strtolower($zone)), 'options' => $options);
}

$label = new XMLElement('label', $this->get('label'));
$label->appendChild(Widget::Select(
"fields{$prefix}[{$this->get('element_name')}]{$postfix}", $groups
));

return $label;
}

/*-------------------------------------------------------------------------
Settings:
-------------------------------------------------------------------------*/
Expand Down Expand Up @@ -149,32 +200,7 @@ public function tearDown() {
-------------------------------------------------------------------------*/

public function displayPublishPanel(XMLElement &$wrapper, $data = null, $error = null, $prefix = null, $postfix = null, $entry_id = null) {
$groups = array();

if ($this->get('required') != 'yes') $groups[] = array(NULL, false, NULL);

$zones = explode(",", $this->get('available_zones'));

foreach($zones as $zone) {
$timezones = DateTimeZone::listIdentifiers(constant('DateTimeZone::' . $zone));

$options = array();
foreach($timezones as $timezone) {
$tz = new DateTime('now', new DateTimeZone($timezone));

$options[] = array($timezone, ($timezone == $data['value']), sprintf("%s %s",
str_replace('_', ' ', substr(strrchr($timezone, '/'),1)),
$tz->format('P')
));
}

$groups[] = array('label' => ucwords(strtolower($zone)), 'options' => $options);
}

$label = new XMLElement('label', $this->get('label'));
$label->appendChild(Widget::Select(
"fields{$prefix}[{$this->get('element_name')}]{$postfix}", $groups
));
$label = $this->buildTZSelection($data, $prefix, $postfix);

if(!is_null($error)) {
$wrapper->appendChild(Widget::wrapFormElementWithError($label, $error));
Expand Down Expand Up @@ -206,4 +232,8 @@ public function appendFormattedElement(&$wrapper, $data, $encode=false){

$wrapper->appendChild($el);
}

public function getExampleFormMarkup(){
return $this->buildTZSelection();
}
}

0 comments on commit 4c05dbf

Please sign in to comment.