TimeZone.php
[Pman.Core] / TimeZone.php
1 <?php
2 require_once 'Pman.php';
3
4 class Pman_Core_TimeZone extends Pman
5 {
6     function getAuth()
7     {
8         parent::getAuth();
9         
10         if (!$this->getAuthUser()) {  
11             $this->jerr("Not authenticated", array('authFailure' => true));
12         }
13         
14         return true;
15     }
16
17     function get($base, $opts=array())
18     {
19         self::getTimezones();
20
21         $data = array();
22
23         foreach(self::$tiemzones as $tz => $o) {
24             if(!empty($_REQUEST['region']) && $_REQUEST['region'] != $o['region']) {
25                 continue;
26             }
27
28             if(
29                 !empty($_REQUEST['query']['area_start']) 
30                 && 
31                 substr(strtolower($o['area']), 0, strlen($_REQUEST['query']['area_start'])) != strtolower($_REQUEST['query']['area_start'])
32             ){
33                 continue;
34             }
35             $data[] = array(
36                 'region' => $o['region'],
37                 'displayArea' => $o['displayArea']
38             );
39         }
40
41         echo json_encode(array(
42             'data' => $data,
43             'metaData' => array(
44                 'root' => 'data',
45                 'successProperty' => 'success',
46                 'totalProperty' => 'total',
47                 'fields' => array(
48                     'region',
49                     'displayArea'
50                 )
51             ),
52             'success' => true,
53             'total' => count($data),
54             
55         ));
56         exit;
57     }
58
59     function post($base) {
60         die('Invalid post');
61     }
62
63     static $timezones = array();
64
65     static function getTimezones()
66     {
67         if(!empty(self::$timezones)) {
68             return self::$timezones;
69         }
70
71         $ce = DB_DataObject::factory('core_enum');
72         $ce->query('
73             SELECT
74                 *, TIME_FORMAT(TIMEDIFF(NOW(), CONVERT_TZ(NOW(), Name, "UTC")), "%H:%i") as timeOffset
75             FROM
76                 mysql.time_zone_name
77             ORDER BY
78                 timeoffset DESC,
79                 Name DESC
80         ');
81
82         while($ce->fetch()) {
83             // ignroe timezone such as 'CET' and 'America/Argentina/Buenos_Aires'
84             if(substr_count($ce->Name, '/') != 1) {
85                 continue;
86             }
87
88             $ar = explode('/', $ce->Name);
89             // ignore timezone such as 'Etc/GMT+8'
90             if($ar[0] == 'Etc') {
91                 continue;
92             }
93
94             $displayOffset = '(GMT ' . ((substr($ce->timeOffset, 0, 1) == '-') ? '' : '+') . $ce->timeOffset . ')';
95             $offsetAr = explode(':', $ce->timeOffset);
96             $offsetAr[0] + ($offsetAr[1] / 60)
97             
98
99             self::$timezones[$ce->Name] = array(
100                 'region' => $ar[0],
101                 'area' => $ar[1],
102                 'displayName' => $ce->Name . ' ' . $displayOffset,
103                 'displayArea' => $ar[1] . ' ' . $displayOffset
104             );
105         }
106
107         return self::$timezones;
108     }
109
110     
111 }