closes #493 fixe le bouton 'Lister les camarades de la carte'
[platal.git] / modules / geoloc.php
CommitLineData
7b14a2a0 1<?php
2/***************************************************************************
5ddeb07c 3 * Copyright (C) 2003-2007 Polytechnique.org *
7b14a2a0 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
22class GeolocModule extends PLModule
23{
24 function handlers()
25 {
26 return array(
0bf274a8 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),
80244bbe 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),
92423144 39 'admin/geoloc' => $this->make_hook('admin', AUTH_MDP, 'admin'),
40 'admin/geoloc/dynamap' => $this->make_hook('admin_dynamap', AUTH_MDP, 'admin'),
7b14a2a0 41 );
42 }
43
44 function _make_qs()
45 {
46 $querystring = "";
47
48 foreach ($_GET as $v => $a) {
27472b85 49 if ($v != 'initfile' && $v != 'n' && $v != 'mapid') {
0bf274a8 50 $querystring .= urlencode($v).'='.urlencode($a).'&amp;';
7b14a2a0 51 }
52 }
53
54 return $querystring;
55 }
56
e336465e 57 function use_map()
58 {
59 return is_file(dirname(__FILE__).'/geoloc/dynamap.swf') &&
60 is_file(dirname(__FILE__).'/geoloc/icon.swf');
61 }
62
7b14a2a0 63 function handler_default(&$page)
64 {
65 global $globals;
66
e336465e 67 if (!$this->use_map())
80244bbe 68 $page->assign('request_geodesix', 1);
7b14a2a0 69
80244bbe 70 if (!empty($GLOBALS['IS_XNET_SITE'])) {
80244bbe 71 $page->assign('no_annu', 1);
d24c8a11 72 new_annu_page('geoloc/index.tpl');
6637dfcc 73 } else {
74 $page->changeTpl('geoloc/index.tpl');
80244bbe 75 }
7b14a2a0 76
abc68084 77 require_once dirname(__FILE__).'/search/search.inc.php';
78
7b14a2a0 79 $fields = new SFieldGroup(true, advancedSearchFromInput());
4a6b8ca3 80 $search = str_replace('&amp;','&',$fields->get_url());
05fec495 81 if ((!Env::has('only_current') && !Env::has('rechercher')) || Env::v('only_current') == 'on')
0bf274a8 82 $search .= '&only_current=on';
4a6b8ca3 83 elseif (Env::i('only_current') != 'on')
7b14a2a0 84 $search .= '&only_current=';
0bf274a8 85
4a6b8ca3 86 $search = preg_replace('/(^|&)mapid=([0-9]+)(&)/','\1\3', $search);
0bf274a8 87 if ($search)
88 $search = '?'.$search;
ea967cbe 89 $page->assign('search_nourlencode',$search);
4a6b8ca3 90 $page->assign('search',urlencode($search));
0bf274a8 91
92 $page->assign('protocole', substr($globals->baseurl,0,strpos($globals->baseurl,':')));
93
94 if (!$search) {
95 $res = XDB::query('SELECT COUNT(DISTINCT uid)
96 FROM adresses WHERE cityid IS NOT NULL');
97 $page->assign('localises', $res->fetchOneCell());
7b14a2a0 98 }
7b14a2a0 99 }
100
101 function handler_icon(&$page)
102 {
103 global $globals;
104
105 header("Content-type: application/x-shockwave-flash");
00d0011e 106 header("Pragma:");
7b14a2a0 107
80244bbe 108 readfile(dirname(__FILE__).'/geoloc/icon.swf');
109 exit;
7b14a2a0 110
111 return PL_NOT_FOUND;
112 }
113
114 function handler_dynamap(&$page)
115 {
116 global $globals;
117
7b14a2a0 118 header("Content-type: application/x-shockwave-flash");
119
80244bbe 120 header("Pragma:");
121 readfile(dirname(__FILE__).'/geoloc/dynamap.swf');
122 exit;
7b14a2a0 123
124 return PL_NOT_FOUND;
125 }
126
127 function handler_init(&$page)
128 {
129 global $globals;
130
e336465e 131 $page->changeTpl('geoloc/init.tpl', NO_SKIN);
7b14a2a0 132
133 header('Content-type: text/xml');
00d0011e 134 header('Pragma:');
80244bbe 135 if(!empty($GLOBALS['IS_XNET_SITE']))
136 $page->assign('background', 0xF2E9D0);
7b14a2a0 137 $page->assign('querystring', $this->_make_qs());
7b14a2a0 138 }
139
140 function handler_city(&$page)
141 {
142 global $globals;
143
144 header("Content-type: text/xml");
00d0011e 145 header("Pragma:");
7b14a2a0 146
e336465e 147 $page->changeTpl('geoloc/city.tpl', NO_SKIN);
7b14a2a0 148
abc68084 149 require_once dirname(__FILE__).'/search/search.inc.php';
7b14a2a0 150 require_once('geoloc.inc.php');
7b14a2a0 151
80244bbe 152 if (empty($GLOBALS['IS_XNET_SITE'])) {
153 $usual_fields = advancedSearchFromInput();
154 $fields = new SFieldGroup(true, $usual_fields);
155 } else {
156 $_REQUEST['asso_id'] = $globals->asso('id');
157 $_REQUEST['only_current'] = 'on';
158 $fields = new SFieldGroup(true, array(
159 new RefSField('asso_id',array('gxm.asso_id'),'groupex.membres','gxm','u.user_id=gxm.uid'),
160 new RefSField('cityid',array('av.cityid'),'adresses','av',getadr_join('av'))));
161 }
7b14a2a0 162 $where = $fields->get_where_statement();
163 if ($where) $where = "WHERE ".$where;
164
08cce2ff 165 $users = XDB::iterator("
509310c2 166 SELECT u.user_id AS id, u.prenom, u.nom, u.promo, alias
7b14a2a0 167 FROM adresses AS a
168 INNER JOIN auth_user_md5 AS u ON(u.user_id = a.uid)
169 INNER JOIN auth_user_quick AS q ON(q.user_id = a.uid)
509310c2 170 LEFT JOIN aliases ON(u.user_id = aliases.id AND FIND_IN_SET(aliases.flags,'bestalias'))
7b14a2a0 171 ".$fields->get_select_statement()."
172 ".$where."
173 GROUP BY u.user_id LIMIT 11", $id);
174
175 $page->assign('users', $users);
7b14a2a0 176 }
177
0bf274a8 178 function handler_country(&$page)
7b14a2a0 179 {
180 global $globals;
181
182 // to debug sql use the next line
183 if (Env::has('debug')) {
e336465e 184 $page->changeTpl('geoloc/country.tpl', SIMPLE);
7b14a2a0 185 } else {
186 header("Content-type: text/xml");
00d0011e 187 header("Pragma:");
e336465e 188 $page->changeTpl('geoloc/country.tpl', NO_SKIN);
7b14a2a0 189 }
190
abc68084 191 require_once dirname(__FILE__).'/search/search.inc.php';
7b14a2a0 192 require_once 'geoloc.inc.php';
7b14a2a0 193
194 $querystring = $this->_make_qs();
195 $page->assign('searchvars', $querystring);
196
5e2307dc 197 $mapid = Env::has('mapid') ? Env::i('mapid', -2) : false;
80244bbe 198 if (empty($GLOBALS['IS_XNET_SITE'])) {
199 $fields = advancedSearchFromInput();
200 } else {
201 $_REQUEST['asso_id'] = $globals->asso('id');
202 $_REQUEST['only_current'] = 'on';
203 $fields = array(new RefSField('asso_id',array('gxm.asso_id'),'groupex.membres','gxm','u.user_id=gxm.uid'));
204 }
7b14a2a0 205
80244bbe 206 list($countries, $cities) = geoloc_getData_subcountries($mapid, $fields, 10);
7b14a2a0 207
208 $page->assign('countries', $countries);
209 $page->assign('cities', $cities);
7b14a2a0 210 }
92423144 211
212 function handler_admin(&$page, $action = false) {
163eddd2 213 $page->changeTpl('geoloc/admin.tpl');
92423144 214 require_once("geoloc.inc.php");
215 $page->assign('xorg_title','Polytechnique.org - Administration - Geolocalisation');
216
217 $nb_synchro = 0;
218
219 if (Env::has('id') && is_numeric(Env::v('id'))) {
220 if (synchro_city(Env::v('id'))) $nb_synchro ++;
221 }
222
223 if ($action == 'missinglat') {
224 $res = XDB::iterRow("SELECT id FROM geoloc_city WHERE lat = 0 AND lon = 0");
225 while ($a = $res->next()) if (synchro_city($a[0])) $nb_synchro++;
226 }
227
228 if ($nb_synchro)
229 $page->trig(($nb_synchro > 1)?($nb_synchro." villes ont été synchronisées"):"Une ville a été synchronisée");
230
231 $res = XDB::query("SELECT COUNT(*) FROM geoloc_city WHERE lat = 0 AND lon = 0");
232 $page->assign("nb_missinglat", $res->fetchOneCell());
233 }
234
235 function handler_admin_dynamap(&$page, $action = false) {
163eddd2 236 $page->changeTpl('geoloc/admin_dynamap.tpl');
92423144 237
238 if ($action == 'cities_not_on_map') {
239 require_once('geoloc.inc.php');
240 if (!fix_cities_not_on_map(20))
241 $page->trig("Impossible d'accéder au webservice");
242 else
243 $refresh = true;
244 }
245
246 if ($action == 'smallest_maps') {
247 require_once('geoloc.inc.php');
248 set_smallest_levels();
249 }
250
251 if ($action == 'precise_coordinates') {
252 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");
253 }
254
255 if ($action == 'newmaps') {
256 require_once('geoloc.inc.php');
257 if (!get_new_maps(Env::v('url')))
258 $page->trig("Impossible d'accéder aux nouvelles cartes");
259 }
260
261 $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");
262 $missing = $countMissing->fetchOneCell();
263
264 $countNoSmallest = XDB::query("SELECT SUM(IF(infos = 'smallest',1,0)) AS n FROM geoloc_city_in_maps GROUP BY city_id ORDER BY n");
265 $noSmallest = $countNoSmallest->fetchOneCell() == 0;
266
267 $countNoCoordinates = XDB::query("SELECT COUNT(*) FROM adresses WHERE cityid IS NOT NULL AND glat = 0 AND glng = 0");
268 $noCoordinates = $countNoCoordinates->fetchOneCell();
269
270 if (isset($refresh) && $missing) {
271 $page->assign("xorg_extra_header", "<meta http-equiv='Refresh' content='3'/>");
272 }
273 $page->assign("nb_cities_not_on_map", $missing);
274 $page->assign("no_smallest", $noSmallest);
275 $page->assign("no_coordinates", $noCoordinates);
276 }
277
7b14a2a0 278}
279
280?>