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