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