cfcd673292f97beb5390519ac9eb66f8316f5d59
[platal.git] / modules / geoloc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2006 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
18 * Foundation, Inc., *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
21
22 class GeolocModule extends PLModule
23 {
24 function handlers()
25 {
26 return array(
27 'geoloc' => $this->make_hook('default', AUTH_COOKIE),
28 'geoloc/icon.swf' => $this->make_hook('icon', AUTH_COOKIE),
29 'geoloc/dynamap.swf' => $this->make_hook('dynamap', AUTH_COOKIE),
30 'geoloc/init' => $this->make_hook('init', AUTH_COOKIE),
31 'geoloc/city' => $this->make_hook('city', AUTH_COOKIE),
32 'geoloc/country' => $this->make_hook('country', AUTH_COOKIE),
33 '%grp/geoloc' => $this->make_hook('default',AUTH_COOKIE),
34 '%grp/geoloc/icon.swf' => $this->make_hook('icon', AUTH_COOKIE),
35 '%grp/geoloc/dynamap.swf'=> $this->make_hook('dynamap',AUTH_COOKIE),
36 '%grp/geoloc/init' => $this->make_hook('init', AUTH_COOKIE),
37 '%grp/geoloc/city' => $this->make_hook('city', AUTH_COOKIE),
38 '%grp/geoloc/country' => $this->make_hook('country',AUTH_COOKIE),
39 'admin/geoloc' => $this->make_hook('admin', AUTH_MDP, 'admin'),
40 'admin/geoloc/dynamap' => $this->make_hook('admin_dynamap', AUTH_MDP, 'admin'),
41 );
42 }
43
44 function _make_qs()
45 {
46 $querystring = "";
47
48 foreach ($_GET as $v => $a) {
49 if ($v != 'initfile' && $v != 'n' && $v != 'mapid') {
50 $querystring .= urlencode($v).'='.urlencode($a).'&amp;';
51 }
52 }
53
54 return $querystring;
55 }
56
57 function handler_default(&$page)
58 {
59 global $globals;
60
61 if (!is_file(dirname(__FILE__).'/geoloc/dynamap.swf') ||
62 !is_file(dirname(__FILE__).'/geoloc/icon.swf'))
63 $page->assign('request_geodesix', 1);
64
65 if (!empty($GLOBALS['IS_XNET_SITE'])) {
66 $page->useMenu();
67 $page->setType($globals->asso('cat'));
68 $page->assign('no_annu', 1);
69 }
70
71 require_once 'search.inc.php';
72 $page->changeTpl('geoloc/index.tpl');
73 $fields = new SFieldGroup(true, advancedSearchFromInput());
74 $search = str_replace('&amp;','&',$fields->get_url());
75 if (!Env::has('only_current'))
76 $search .= '&only_current=on';
77 elseif (Env::i('only_current') != 'on')
78 $search .= '&only_current=';
79
80 $search = preg_replace('/(^|&)mapid=([0-9]+)(&)/','\1\3', $search);
81 if ($search)
82 $search = '?'.$search;
83 $page->assign('search',urlencode($search));
84
85 $page->assign('protocole', substr($globals->baseurl,0,strpos($globals->baseurl,':')));
86
87 if (!$search) {
88 $res = XDB::query('SELECT COUNT(DISTINCT uid)
89 FROM adresses WHERE cityid IS NOT NULL');
90 $page->assign('localises', $res->fetchOneCell());
91 }
92 }
93
94 function handler_icon(&$page)
95 {
96 global $globals;
97
98 header("Content-type: application/x-shockwave-flash");
99 header("Pragma:");
100
101 readfile(dirname(__FILE__).'/geoloc/icon.swf');
102 exit;
103
104 return PL_NOT_FOUND;
105 }
106
107 function handler_dynamap(&$page)
108 {
109 global $globals;
110
111 header("Content-type: application/x-shockwave-flash");
112
113 header("Pragma:");
114 readfile(dirname(__FILE__).'/geoloc/dynamap.swf');
115 exit;
116
117 return PL_NOT_FOUND;
118 }
119
120 function handler_init(&$page)
121 {
122 global $globals;
123
124 $page->changeTpl('geoloc/geolocInit.tpl', NO_SKIN);
125
126 header('Content-type: text/xml');
127 header('Pragma:');
128 if(!empty($GLOBALS['IS_XNET_SITE']))
129 $page->assign('background', 0xF2E9D0);
130 $page->assign('querystring', $this->_make_qs());
131 }
132
133 function handler_city(&$page)
134 {
135 global $globals;
136
137 header("Content-type: text/xml");
138 header("Pragma:");
139
140 $page->changeTpl('geoloc/getCityInfos.tpl', NO_SKIN);
141
142 require_once('geoloc.inc.php');
143 require_once('search.inc.php');
144
145 if (empty($GLOBALS['IS_XNET_SITE'])) {
146 $usual_fields = advancedSearchFromInput();
147 $fields = new SFieldGroup(true, $usual_fields);
148 } else {
149 $_REQUEST['asso_id'] = $globals->asso('id');
150 $_REQUEST['only_current'] = 'on';
151 $fields = new SFieldGroup(true, array(
152 new RefSField('asso_id',array('gxm.asso_id'),'groupex.membres','gxm','u.user_id=gxm.uid'),
153 new RefSField('cityid',array('av.cityid'),'adresses','av',getadr_join('av'))));
154 }
155 $where = $fields->get_where_statement();
156 if ($where) $where = "WHERE ".$where;
157
158 $users = XDB::iterator("
159 SELECT u.user_id AS id, u.prenom, u.nom, u.promo
160 FROM adresses AS a
161 INNER JOIN auth_user_md5 AS u ON(u.user_id = a.uid)
162 INNER JOIN auth_user_quick AS q ON(q.user_id = a.uid)
163 ".$fields->get_select_statement()."
164 ".$where."
165 GROUP BY u.user_id LIMIT 11", $id);
166
167 $page->assign('users', $users);
168 }
169
170 function handler_country(&$page)
171 {
172 global $globals;
173
174 // to debug sql use the next line
175 if (Env::has('debug')) {
176 $page->changeTpl('geoloc/getData.tpl', SIMPLE);
177 } else {
178 header("Content-type: text/xml");
179 header("Pragma:");
180 $page->changeTpl('geoloc/getData.tpl', NO_SKIN);
181 }
182
183 require_once 'geoloc.inc.php';
184 require_once 'search.inc.php';
185
186 $querystring = $this->_make_qs();
187 $page->assign('searchvars', $querystring);
188
189 $mapid = Env::has('mapid') ? Env::i('mapid', -2) : false;
190 if (empty($GLOBALS['IS_XNET_SITE'])) {
191 $fields = advancedSearchFromInput();
192 } else {
193 $_REQUEST['asso_id'] = $globals->asso('id');
194 $_REQUEST['only_current'] = 'on';
195 $fields = array(new RefSField('asso_id',array('gxm.asso_id'),'groupex.membres','gxm','u.user_id=gxm.uid'));
196 }
197
198 list($countries, $cities) = geoloc_getData_subcountries($mapid, $fields, 10);
199
200 $page->assign('countries', $countries);
201 $page->assign('cities', $cities);
202 }
203
204 function handler_admin(&$page, $action = false) {
205 $page->changeTpl('geoloc/admin.tpl');
206 require_once("geoloc.inc.php");
207 $page->assign('xorg_title','Polytechnique.org - Administration - Geolocalisation');
208
209 $nb_synchro = 0;
210
211 if (Env::has('id') && is_numeric(Env::v('id'))) {
212 if (synchro_city(Env::v('id'))) $nb_synchro ++;
213 }
214
215 if ($action == 'missinglat') {
216 $res = XDB::iterRow("SELECT id FROM geoloc_city WHERE lat = 0 AND lon = 0");
217 while ($a = $res->next()) if (synchro_city($a[0])) $nb_synchro++;
218 }
219
220 if ($nb_synchro)
221 $page->trig(($nb_synchro > 1)?($nb_synchro." villes ont été synchronisées"):"Une ville a été synchronisée");
222
223 $res = XDB::query("SELECT COUNT(*) FROM geoloc_city WHERE lat = 0 AND lon = 0");
224 $page->assign("nb_missinglat", $res->fetchOneCell());
225 }
226
227 function handler_admin_dynamap(&$page, $action = false) {
228 $page->changeTpl('geoloc/admin_dynamap.tpl');
229
230 if ($action == 'cities_not_on_map') {
231 require_once('geoloc.inc.php');
232 if (!fix_cities_not_on_map(20))
233 $page->trig("Impossible d'accéder au webservice");
234 else
235 $refresh = true;
236 }
237
238 if ($action == 'smallest_maps') {
239 require_once('geoloc.inc.php');
240 set_smallest_levels();
241 }
242
243 if ($action == 'precise_coordinates') {
244 XDB::execute("UPDATE adresses AS a INNER JOIN geoloc_city AS c ON(a.cityid = c.id) SET a.glat = c.lat / 100000, a.glng = c.lon / 100000");
245 }
246
247 if ($action == 'newmaps') {
248 require_once('geoloc.inc.php');
249 if (!get_new_maps(Env::v('url')))
250 $page->trig("Impossible d'accéder aux nouvelles cartes");
251 }
252
253 $countMissing = XDB::query("SELECT COUNT(*) FROM geoloc_city AS c LEFT JOIN geoloc_city_in_maps AS m ON(c.id = m.city_id) WHERE m.city_id IS NULL");
254 $missing = $countMissing->fetchOneCell();
255
256 $countNoSmallest = XDB::query("SELECT SUM(IF(infos = 'smallest',1,0)) AS n FROM geoloc_city_in_maps GROUP BY city_id ORDER BY n");
257 $noSmallest = $countNoSmallest->fetchOneCell() == 0;
258
259 $countNoCoordinates = XDB::query("SELECT COUNT(*) FROM adresses WHERE cityid IS NOT NULL AND glat = 0 AND glng = 0");
260 $noCoordinates = $countNoCoordinates->fetchOneCell();
261
262 if (isset($refresh) && $missing) {
263 $page->assign("xorg_extra_header", "<meta http-equiv='Refresh' content='3'/>");
264 }
265 $page->assign("nb_cities_not_on_map", $missing);
266 $page->assign("no_smallest", $noSmallest);
267 $page->assign("no_coordinates", $noCoordinates);
268 }
269
270 }
271
272 ?>