sync
[Pman.Core] / TimeZone.php
index a184e37..e69d856 100644 (file)
@@ -16,30 +16,39 @@ class Pman_Core_TimeZone extends Pman
 
     function get($base, $opts=array())
     {
-        $timezones = self::getTimezones();
+        self::getTimezones();
 
         $data = array();
 
-        foreach($data as $tz => $offset) {
-            $arr = explode('/', $tz);
+        foreach(self::$timezones as $tz => $o) {
+            if(!empty($_REQUEST['region']) && $_REQUEST['region'] != $o['region']) {
+                continue;
+            }
+
+            if(
+                !empty($_REQUEST['query']['area_start']) 
+                && 
+                substr(strtolower($o['area']), 0, strlen($_REQUEST['query']['area_start'])) != strtolower($_REQUEST['query']['area_start'])
+            ){
+                continue;
+            }
             $data[] = array(
-                'region' => $arr[0],
-                'area' => $arr[1],
-                'offset' => $offset
-            )
+                'region' => $o['region'],
+                'area' => $o['area'],
+                'displayArea' => $o['displayArea']
+            );
         }
 
         echo json_encode(array(
             'data' => $data,
             'metaData' => array(
-                'id' => 'id',
                 'root' => 'data',
                 'successProperty' => 'success',
                 'totalProperty' => 'total',
                 'fields' => array(
                     'region',
                     'area',
-                    'offset'
+                    'displayArea'
                 )
             ),
             'success' => true,
@@ -62,48 +71,43 @@ class Pman_Core_TimeZone extends Pman
         }
 
         $ce = DB_DataObject::factory('core_enum');
-        $ce->query('
+        $ce->query("
             SELECT
-                *, TIME_FORMAT(TIMEDIFF(NOW(), CONVERT_TZ(NOW(), Name, "UTC")), "%H:%i") as offset
+                *, TIME_FORMAT(TIMEDIFF(NOW(), CONVERT_TZ(NOW(), Name, 'UTC')), '%H:%i') as timeOffset
             FROM
                 mysql.time_zone_name
+            WHERE
+                Name LIKE '%/%'
+                AND
+                NAME NOT LIKE 'Etc%'
             ORDER BY
-                offset DESC,
+                timeoffset DESC,
                 Name DESC
-        ');
+        ");
 
         while($ce->fetch()) {
             // ignroe timezone such as 'CET' and 'America/Argentina/Buenos_Aires'
-            if(substr_count($ce->Name, '/') != 1) {
-                continue;
-            }
+           
 
             $ar = explode('/', $ce->Name);
             // ignore timezone such as 'Etc/GMT+8'
-            if($ar[0] == 'Etc') {
-                continue;
-            }
+           
 
-            self::$timezones[$ce->Name] = $ce->offset;
-        }
+            $displayArea = str_replace('_', ' ', $ar[1]);
 
-        return self::$timezones;
-    }
+            $timeOffset = ((substr($ce->timeOffset, 0, 1) == '-') ? '' : '+') . $ce->timeOffset;
+            $displayOffset = '(GMT ' . $timeOffset . ')';
 
-    static function getOffset($timezone)
-    {
-        $ce = DB_DataObject::factory('core_enum');
-        $ce->query('
-            SELECT
-                TIME_FORMAT(TIMEDIFF(NOW(), CONVERT_TZ(NOW(), Name, "UTC")), "%H:%i") as offset
-            FROM
-                mysql.time_zone_name
-            WHERE
-                Name = "' . $ce->escape($timezone) . '"
-        ');
-        $ce->fetch();
+            self::$timezones[$ce->Name] = array(
+                'region' => $ar[0],
+                'area' => $ar[1],
+                'displayName' => $ar[0] . '/' . $displayArea . ' ' . $displayOffset,
+                'displayArea' => $displayArea . ' ' . $displayOffset,
+                'timeOffset' => $timeOffset
+            );
+        }
 
-        return empty($ce->offset) ? '' : $ce->offset;
+        return self::$timezones;
     }