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