From ae3aa96898834ce3992790e1622541ce48fd78d3 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Wed, 22 Mar 2017 19:16:35 +0100 Subject: Change timezone data structure send to the templates The goal of this is to be able to adapt the timezone form in template without hacking the HTML already rendered. * there are two arrays available: * `continents` which contains only a list of available continents * `cities` which contains a list of available timezone cities, associated with their continent Note: there are two distinct array because RainTPL doesn't support nested loop very well. --- application/PageBuilder.php | 2 + application/TimeZone.php | 101 +++++++++++++++++++------------------------- index.php | 21 ++++----- tests/TimeZoneTest.php | 83 ++++++++++++++++++++++-------------- 4 files changed, 106 insertions(+), 101 deletions(-) diff --git a/application/PageBuilder.php b/application/PageBuilder.php index b133dee8..8e39455b 100644 --- a/application/PageBuilder.php +++ b/application/PageBuilder.php @@ -1,5 +1,7 @@ 'Europe', + * ], + * [ + * ['continent' => 'America', 'city' => 'Toronto'], + * ['continent' => 'Europe', 'city' => 'Paris'], + * 'selected' => 'Paris', + * ], + * ]; * + * Notes: + * - 'UTC/UTC' is mapped to 'UTC' to form a valid option + * - a few timezone cities includes the country/state, such as Argentina/Buenos_Aires + * - these arrays are designed to build timezone selects in template files with any HTML structure + * + * @param array $installedTimeZones List of installed timezones as string * @param string $preselectedTimezone preselected timezone (optional) * - * @return array containing the generated HTML form and Javascript code + * @return array[] continents and cities **/ -function generateTimeZoneForm($preselectedTimezone='') +function generateTimeZoneData($installedTimeZones, $preselectedTimezone = '') { - // Select the server timezone - if ($preselectedTimezone == '') { - $preselectedTimezone = date_default_timezone_get(); - } - if ($preselectedTimezone == 'UTC') { $pcity = $pcontinent = 'UTC'; } else { @@ -27,62 +46,30 @@ function generateTimeZoneForm($preselectedTimezone='') $pcity = substr($preselectedTimezone, $spos+1); } - // The list is in the form 'Europe/Paris', 'America/Argentina/Buenos_Aires' - // We split the list in continents/cities. - $continents = array(); - $cities = array(); - - // TODO: use a template to generate the HTML/Javascript form - - foreach (timezone_identifiers_list() as $tz) { + $continents = []; + $cities = []; + foreach ($installedTimeZones as $tz) { if ($tz == 'UTC') { $tz = 'UTC/UTC'; } $spos = strpos($tz, '/'); - if ($spos !== false) { - $continent = substr($tz, 0, $spos); - $city = substr($tz, $spos+1); - $continents[$continent] = 1; - - if (!isset($cities[$continent])) { - $cities[$continent] = ''; - } - $cities[$continent] .= '