root/g-map/files/lib/page/MapAjaxPage.class.php @ 1441

Revision 1441, 4.1 kB (checked in by Torben Brodt, 2 years ago)

try to fix problems with umlauts and iso encoding, see #539

Line 
1<?php
2// wcf imports
3require_once(WCF_DIR.'lib/page/AbstractPage.class.php');
4
5// map imports
6require_once(WCF_DIR.'lib/data/gmap/GmapCluster.class.php');
7
8/**
9 * Returns the AJAX Content for the Gooogle Map
10 *
11 * @package     de.gmap.wcf.data.page
12 * @author      Torben Brodt
13 * @license     GNU General Public License <http://opensource.org/licenses/gpl-3.0.html>
14 */
15class MapAjaxPage extends AbstractPage {
16        public $action = '';
17        protected $zoom = 0;
18        protected $distance = 35;
19        protected $bounds= array();
20
21        protected $datapoints = array();
22
23        /**
24         * @see Page::readData()
25         */
26        public function readParameters() {
27                parent::readParameters();
28
29                $this->zoom = max(min(21, isset($_GET['zoom']) ? $_GET['zoom'] : 0), 0);
30
31                // ((50.08930948264218, 10.298652648925781), (50.14434619645057, 10.506362915039062))
32                if(isset($_GET['bounds'])) {
33                        if(preg_match('/^\(\((-?\d+\.?\d*), (-?\d+\.?\d*)\), \((-?\d+\.?\d*), (-?\d+\.?\d*)\)\)$/', $_GET['bounds'], $match)) {
34                                $this->bounds = array(
35                                        array(
36                                                'lat' => $match[1],
37                                                'lon' => $match[2]
38                                        ),
39                                        array(
40                                                'lat' => $match[3],
41                                                'lon' => $match[4]
42                                        ),
43                                );
44                        }
45                }
46
47                // just get bounds
48                $this->action = $_GET['action'];
49
50                // load content
51                $this->content = isset($_GET['content']) && $_GET['content'];
52
53                // coordinates
54                $this->lat = isset($_GET['lat']) ? floatval($_GET['lat']) : 0;
55                $this->lon = isset($_GET['lon']) ? floatval($_GET['lon']) : 0;
56               
57                // pick action
58                $this->idx = isset($_GET['idx']) ? intval($_GET['idx']) : 0;
59        }
60
61        /**
62         * @see Page::readData()
63         */
64        public function readData() {
65                parent::readData();
66               
67                if (WCF::getUser()->getPermission('user.profile.gmap.canViewUsers')) {
68                        $this->readUserData();
69                }
70        }
71
72        /**
73         * read user data
74         */
75        public function readUserData() {
76
77                $markers = array();
78
79                $sql = 'SELECT          '.($this->action == 'pick' ? 'userID AS id,' : '').'
80                                        X(pt) AS lon,
81                                        Y(pt) AS lat
82                        FROM            wcf'.WCF_N.'_gmap_user
83                        WHERE           1';
84
85                if($this->bounds) {
86                        $sql .= ' AND X(pt) BETWEEN '.floatval($this->bounds[0]['lon']).' AND '.floatval($this->bounds[1]['lon']).' ';
87                        $sql .= ' AND Y(pt) BETWEEN '.floatval($this->bounds[0]['lat']).' AND '.floatval($this->bounds[1]['lat']).' ';
88                }
89
90                if($this->action == 'initialize') {
91                        $sql = 'SELECT  AVG(lon) AS lon,
92                                        AVG(lat) AS lat
93                                FROM (
94                                        '.$sql.'
95                                ) x';
96                }
97
98                $result = WCF::getDB()->sendQuery($sql);
99                while ($row = WCF::getDB()->fetchArray($result)) {
100                        $markers[] = $row;
101                }
102
103                $cluster = new GmapCluster($this->distance, $this->zoom);
104                if($this->action == 'pick') {
105                        $ids = $cluster->getMarkers($markers, $this->idx);
106                        if($ids === null) {
107                                $this->datapoints = array();   
108                                $this->datapoints[] = array(
109                                        0,
110                                        'data n/a',
111                                        ''
112                                );
113                        } else {
114                                $this->datapoints = $this->getUsers($ids);
115                        }
116                } else {
117                        $this->datapoints = $cluster->getMarkers($markers);
118                }
119        }
120
121        /**
122         * Returns a list of users.
123         *
124         * @param       string                          $userIDs
125         * @return      array<UserProfile>              users
126         */
127        public function getUsers(array $userIDs) {
128                require_once(WCF_DIR.'lib/data/user/UserProfile.class.php');
129
130                $users = array();
131                $sql = "SELECT          avatar_table.*,
132                                        user_table.*
133                        FROM            wcf".WCF_N."_user user_table
134                        LEFT JOIN       wcf".WCF_N."_avatar avatar_table
135                        ON              (avatar_table.avatarID = user_table.avatarID)
136                        WHERE           user_table.userID IN (".implode(',', $userIDs).")
137                        ORDER BY        username";
138                $result = WCF::getDB()->sendQuery($sql);
139                while ($row = WCF::getDB()->fetchArray($result)) {
140
141                        $avatar = '';
142                        $user = new UserProfile(null, $row);
143                        if($user->getAvatar()) {
144                                $user->getAvatar()->setMaxSize(24, 24);
145                                $avatar = $user->getAvatar()->getURL();
146                        }
147                       
148                        $username = $user->username;
149                        if(CHARSET != 'UTF-8') {
150                                $username = StringUtil::encodeHTML($username);
151                        }
152                                       
153                        $users[] = array(
154                                intval($user->userID),
155                                $username,
156                                $avatar
157                        );
158                }
159
160                return $users;
161        }
162
163        /**
164         * @see Page::show()
165         */
166        public function show() {
167                parent::show();
168               
169                // send header for corrent charset
170                @header('Content-Type: application/json; charset='.CHARSET);
171                echo json_encode($this->datapoints);
172        }
173}
174?>
Note: See TracBrowser for help on using the browser.