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