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::$timezones 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                 'area' => $o['area'],
38                 'displayArea' => $o['displayArea']
39             );
40         }
41
42         echo json_encode(array(
43             'data' => $data,
44             'metaData' => array(
45                 'root' => 'data',
46                 'successProperty' => 'success',
47                 'totalProperty' => 'total',
48                 'fields' => array(
49                     'region',
50                     'area',
51                     'displayArea'
52                 )
53             ),
54             'success' => true,
55             'total' => count($data),
56             
57         ));
58         exit;
59     }
60
61     function post($base) {
62         die('Invalid post');
63     }
64
65     static $timezones = array();
66
67     static function getTimezones()
68     {
69         if(!empty(self::$timezones)) {
70             return self::$timezones;
71         }
72
73         $ce = DB_DataObject::factory('core_enum');
74         $ce->query('
75             SELECT
76                 *, TIME_FORMAT(TIMEDIFF(NOW(), CONVERT_TZ(NOW(), Name, "UTC")), "%H:%i") as timeOffset
77             FROM
78                 mysql.time_zone_name
79             ORDER BY
80                 timeoffset DESC,
81                 Name DESC
82         ');
83
84         while($ce->fetch()) {
85             // ignroe timezone such as 'CET' and 'America/Argentina/Buenos_Aires'
86             if(substr_count($ce->Name, '/') != 1) {
87                 continue;
88             }
89
90             $ar = explode('/', $ce->Name);
91             // ignore timezone such as 'Etc/GMT+8'
92             if($ar[0] == 'Etc') {
93                 continue;
94             }
95
96             $timeOffset = ((substr($ce->timeOffset, 0, 1) == '-') ? '' : '+') . $ce->timeOffset;
97             $displayOffset = '(GMT ' . ((substr($ce->timeOffset, 0, 1) == '-') ? '' : '+') . $ce->timeOffset . ')';
98             $offsetAr = explode(':', $ce->timeOffset);
99
100             self::$timezones[$ce->Name] = array(
101                 'region' => $ar[0],
102                 'area' => $ar[1],
103                 'displayName' => $ce->Name . ' ' . $displayOffset,
104                 'displayArea' => $ar[1] . ' ' . $displayOffset,
105                 'timeOffset' => 
106                 'decimalOffset' => $offsetAr[0] + (($offsetAr[0] < 0) ? (-1 * $offsetAr[1] / 60) : ($offsetAr[1] / 60))
107             );
108         }
109
110         return self::$timezones;
111     }
112
113     
114 }