Commit | Line | Data |
---|---|---|
0337d704 | 1 | <?php |
2 | /*************************************************************************** | |
179afa7f | 3 | * Copyright (C) 2003-2008 Polytechnique.org * |
0337d704 | 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 | ||
a7de4ef7 | 22 | // {{{ liste les pays ou les régions d'un pays |
23 | /** donne la liste déroulante des pays | |
24 | * @param $current pays actuellement selectionné | |
0337d704 | 25 | */ |
b536b5ae | 26 | function geoloc_country($current, $avail_only = false) |
27 | { | |
28 | if ($avail_only) { | |
29 | $res = XDB::iterRow('SELECT g.a2, g.pays | |
30 | FROM geoloc_pays AS g | |
31 | INNER JOIN adresses AS a ON(a.country = g.a2) | |
32 | GROUP BY g.a2 | |
33 | ORDER BY g.pays'); | |
34 | } else { | |
35 | $res = XDB::iterRow('SELECT a2,pays FROM geoloc_pays ORDER BY pays'); | |
36 | } | |
0337d704 | 37 | $html = ""; |
38 | while (list($my_id, $my_pays) = $res->next()) { | |
b536b5ae | 39 | $html .= sprintf("<option value=\"%s\" %s>%s</option>\n", |
40 | $my_id, ($current==$my_id?"selected='selected'":""), $my_pays); | |
0337d704 | 41 | } |
42 | return $html; | |
43 | } | |
44 | ||
0337d704 | 45 | /** donne la liste deroulante des regions pour un pays |
46 | * @param $pays le pays dont on veut afficher les regions | |
47 | * @param $current la region actuellement selectionnee | |
48 | */ | |
b536b5ae | 49 | function geoloc_region($country, $current, $avail_only = false) |
50 | { | |
51 | if ($avail_only) { | |
52 | $res = XDB::iterRow('SELECT r.region, r.name | |
53 | FROM geoloc_region AS r | |
54 | INNER JOIN adresses AS a ON (a.country = r.a2 AND a.region = r.region) | |
55 | WHERE r.a2 = {?} | |
56 | GROUP BY r.region | |
57 | ORDER BY r.name', $country); | |
58 | } else { | |
59 | $res = XDB::iterRow('SELECT region,name | |
60 | FROM geoloc_region | |
61 | WHERE a2 = {?} | |
62 | ORDER BY name', $country); | |
63 | } | |
0337d704 | 64 | $html = "<option value=\"\"></option>"; |
65 | while (list($regid, $regname) = $res->next()) { | |
b536b5ae | 66 | $html .= sprintf("<option value=\"%s\" %s>%s</option>\n", |
67 | $regid, ($current==$regid?"selected='selected'":""), $regname); | |
0337d704 | 68 | } |
69 | return $html; | |
0337d704 | 70 | } |
0337d704 | 71 | // }}} |
72 | ||
73 | // {{{ get_address_infos($txt) | |
74 | /** retrieve the infos on a text address | |
75 | * store on the fly the info of the city concerned | |
76 | * @param $txt the raw text of an address | |
77 | */ | |
787bb3d7 | 78 | function get_address_infos($txt) |
8c4a0c30 | 79 | { |
56670b6a | 80 | global $globals; |
3d1a2c7d | 81 | |
8c4a0c30 | 82 | $url = $globals->geoloc->webservice_url."address.php?precise=1&txt=" . urlencode($txt); |
3d1a2c7d FB |
83 | if ($globals->debug & DEBUG_BT) { |
84 | if (!isset(PlBacktrace::$bt['Geoloc'])) { | |
85 | new PlBacktrace('Geoloc'); | |
86 | } | |
87 | PlBacktrace::$bt['Geoloc']->start($url); | |
88 | } | |
89 | $f = @fopen($url, 'r'); | |
90 | if ($f === false) { | |
91 | if ($globals->debug & DEBUG_BT) { | |
92 | PlBacktrace::$bt['Geoloc']->stop(0, 'Can\'t fetch result'); | |
93 | } | |
94 | return false; | |
95 | } | |
0337d704 | 96 | $keys = explode('|',fgets($f)); |
97 | $vals = explode('|',fgets($f)); | |
3d1a2c7d FB |
98 | if ($globals->debug & DEBUG_BT) { |
99 | $data = array(); | |
100 | for ($i = 0 ; $i < count($keys) ; ++$i) { | |
101 | $data[] = array($keys[$i], $vals[$i]); | |
102 | } | |
103 | PlBacktrace::$bt['Geoloc']->stop(count($keys), null, $data); | |
104 | } | |
8eab04f4 | 105 | $infos = empty_address(); |
2b98ac11 | 106 | foreach ($keys as $i=>$key) { |
107 | if($vals[$i]) { | |
108 | if ($key == 'sql') { | |
109 | $infos[$key] = $vals[$i]; | |
110 | } else { | |
111 | $val = strtr($vals[$i], array(chr(197).chr(147) => "œ")); | |
8c4a0c30 | 112 | $infos[$key] = $val; |
2b98ac11 | 113 | } |
114 | } | |
115 | } | |
8eab04f4 FB |
116 | if (empty($infos['country'])) { |
117 | $infos['country'] = '00'; | |
118 | } | |
5ee95138 | 119 | if (isset($infos['sql']) && $infos['sql']) |
de0485fd FB |
120 | XDB::execute("REPLACE INTO geoloc_city |
121 | VALUES ".$infos['sql']); | |
5ee95138 | 122 | if (isset($infos['display']) && $infos['display']) |
de0485fd FB |
123 | XDB::execute("UPDATE geoloc_pays |
124 | SET display = {?} | |
125 | WHERE a2 = {?}", $infos['display'], $infos['country']); | |
126 | if (isset($infos['cityid'])) { | |
b536b5ae | 127 | fix_cities_not_on_map(1, $infos['cityid']); |
de0485fd FB |
128 | if (floatval($infos['precise_lat']) && floatval($infos['precise_lon'])) { |
129 | $res = XDB::query("SELECT c.lat / 100000, c.lon / 100000 | |
130 | FROM geoloc_city AS c | |
131 | WHERE c.id = {?}", $infos['cityid']); | |
132 | if ($res->numRows()) { | |
133 | list($glat, $glng) = $res->fetchOneRow(); | |
134 | $infos['precise_lat'] = $glat; | |
135 | $infos['precise_lon'] = $glng; | |
136 | } | |
137 | } | |
138 | } | |
0337d704 | 139 | return $infos; |
140 | } | |
141 | // }}} | |
142 | ||
56670b6a | 143 | // {{{ get_cities_maps($array) |
144 | /* get all the maps id of the cities contained in an array */ | |
145 | function get_cities_maps($array) | |
146 | { | |
147 | global $globals; | |
148 | implode("\n",$array); | |
8c4a0c30 | 149 | $url = $globals->geoloc->webservice_url."findMaps.php?datatext=".urlencode(implode("\n", $array)); |
56670b6a | 150 | if (!($f = @fopen($url, 'r'))) return false; |
151 | $maps = array(); | |
152 | while (!feof($f)) | |
153 | { | |
154 | $l = trim(fgets($f)); | |
155 | $tab = explode(';', $l); | |
156 | $i = $tab[0]; | |
157 | unset($tab[0]); | |
158 | $maps[$i] = $tab; | |
159 | } | |
160 | return $maps; | |
161 | } | |
162 | // }}} | |
163 | ||
164 | // {{{ get_new_maps($url) | |
165 | /** set new maps from url **/ | |
166 | function get_new_maps($url) | |
167 | { | |
a3a049fc | 168 | if (!($f = @fopen($url, 'r'))) { |
169 | return false; | |
170 | } | |
171 | XDB::query('TRUNCATE TABLE geoloc_maps'); | |
172 | $s = ''; | |
173 | while (!feof($f)) { | |
174 | $l = fgetcsv($f, 1024, ';', '"'); | |
175 | foreach ($l as $i => $val) { | |
176 | if ($val != 'NULL') { | |
177 | $l[$i] = '\''.addslashes($val).'\''; | |
178 | } | |
179 | } | |
180 | $s .= ',('.implode(',',$l).')'; | |
181 | } | |
182 | XDB::execute('INSERT INTO geoloc_maps VALUES '.substr($s, 1)); | |
183 | return true; | |
56670b6a | 184 | } |
80244bbe | 185 | // }}} |
56670b6a | 186 | |
0337d704 | 187 | // {{{ get_address_text($adr) |
188 | /** make the text of an address that can be read by a mailman | |
189 | * @param $adr an array with all the usual fields | |
190 | */ | |
787bb3d7 | 191 | function get_address_text($adr) |
8c4a0c30 | 192 | { |
0337d704 | 193 | $t = ""; |
5ee95138 | 194 | if (isset($adr['adr1']) && $adr['adr1']) $t.= $adr['adr1']; |
195 | if (isset($adr['adr2']) && $adr['adr2']) $t.= "\n".$adr['adr2']; | |
196 | if (isset($adr['adr3']) && $adr['adr3']) $t.= "\n".$adr['adr3']; | |
0337d704 | 197 | $l = ""; |
5ee95138 | 198 | if (isset($adr['display']) && $adr['display']) { |
80ca1b4e | 199 | $keys = explode(' ', $adr['display']); |
200 | foreach ($keys as $key) { | |
8c4a0c30 | 201 | if (isset($adr[$key])) { |
80ca1b4e | 202 | $l .= " ".$adr[$key]; |
8c4a0c30 | 203 | } else { |
80ca1b4e | 204 | $l .= " ".$key; |
8c4a0c30 | 205 | } |
80ca1b4e | 206 | } |
8c4a0c30 | 207 | if ($l) substr($l, 1); |
208 | } elseif ($adr['country'] == 'US' || $adr['country'] == 'CA' || $adr['country'] == 'GB') { | |
209 | if ($adr['city']) $l .= $adr['city'].",\n"; | |
210 | if ($adr['region']) $l .= $adr['region']." "; | |
211 | if ($adr['postcode']) $l .= $adr['postcode']; | |
212 | } else { | |
213 | if (isset($adr['postcode']) && $adr['postcode']) $l .= $adr['postcode']." "; | |
214 | if (isset($adr['city']) && $adr['city']) $l .= $adr['city']; | |
0337d704 | 215 | } |
216 | if ($l) $t .= "\n".trim($l); | |
217 | if ($adr['country'] != '00' && (!$adr['countrytxt'] || $adr['countrytxt'] == strtoupper($adr['countrytxt']))) { | |
08cce2ff | 218 | $res = XDB::query("SELECT pays FROM geoloc_pays WHERE a2 = {?}", $adr['country']); |
0337d704 | 219 | $adr['countrytxt'] = $res->fetchOneCell(); |
220 | } | |
8c4a0c30 | 221 | if (isset($adr['countrytxt']) && $adr['countrytxt']) { |
222 | $t .= "\n".$adr['countrytxt']; | |
223 | } | |
0337d704 | 224 | return trim($t); |
225 | } | |
226 | // }}} | |
227 | ||
228 | // {{{ compare_addresses_text($a, $b) | |
229 | /** compares if two address matches | |
230 | * @param $a the raw text of an address | |
231 | * @param $b the raw text of a complete valid address | |
232 | */ | |
8c4a0c30 | 233 | function compare_addresses_text($a, $b) |
234 | { | |
0337d704 | 235 | $ta = strtoupper(preg_replace(array("/[0-9,\"'#~:;_\- ]/", "/\r\n/"), array("", "\n"), $a)); |
236 | $tb = strtoupper(preg_replace(array("/[0-9,\"'#~:;_\- ]/", "/\r\n/"), array("", "\n"), $b)); | |
787bb3d7 | 237 | |
0337d704 | 238 | $la = explode("\n", $ta); |
239 | $lb = explode("\n", $tb); | |
240 | ||
8c4a0c30 | 241 | if (count($lb) > count($la) + 1) { |
242 | return false; | |
243 | } | |
244 | foreach ($la as $i=>$l) { | |
37d44b3b | 245 | if (levenshtein(trim($l), trim($lb[$i])) > 3) { |
8c4a0c30 | 246 | return false; |
247 | } | |
248 | } | |
0337d704 | 249 | return true; |
250 | } | |
251 | ||
252 | // }}} | |
253 | ||
254 | function empty_address() { | |
255 | return Array( | |
256 | "adr1" => "", | |
257 | "adr2" => "", | |
258 | "adr3" => "", | |
259 | "cityid" => NULL, | |
260 | "city" => "", | |
261 | "postcode" => "", | |
262 | "region" => "", | |
80ca1b4e | 263 | "regiontxt" => "", |
0337d704 | 264 | "country" => "00", |
de0485fd FB |
265 | "countrytxt" => "", |
266 | "precise_lat" => "", | |
267 | "precise_lon" => ""); | |
0337d704 | 268 | } |
269 | ||
270 | // create a simple address from a text without geoloc | |
8c4a0c30 | 271 | function cut_address($txt) |
272 | { | |
0337d704 | 273 | $txt = str_replace("\r\n", "\n", $txt); |
274 | ereg("^([^\n]*)(\n([^\n]*)(\n(.*))?)?$", trim($txt), $a); | |
275 | return array("adr1" => trim($a[1]), "adr2" => trim($a[3]), "adr3" => trim(str_replace("\n", " ", $a[5]))); | |
276 | } | |
277 | ||
278 | // {{{ localize_addresses($uid) | |
279 | /* localize all the address of a user and modify the database | |
280 | * if the new address match with the old one | |
281 | * @param $uid the id of the user | |
282 | */ | |
8c4a0c30 | 283 | function localize_addresses($uid) |
284 | { | |
285 | $res = XDB::iterator("SELECT * | |
286 | FROM adresses | |
287 | WHERE uid = {?} and (cityid IS NULL OR cityid = 0)", $uid); | |
0337d704 | 288 | $erreur = Array(); |
289 | ||
290 | while ($a = $res->next()) { | |
291 | $new = get_address_infos($ta = get_address_text($a)); | |
292 | if (compare_addresses_text($ta, get_address_text($new))) { | |
8c4a0c30 | 293 | XDB::execute("UPDATE adresses |
294 | SET adr1 = {?}, adr2 = {?}, adr3 = {?}, | |
295 | cityid = {?}, city = {?}, postcode = {?}, | |
296 | region = {?}, regiontxt = {?}, country = {?}, | |
297 | glat = {?}, glng = {?} | |
298 | WHERE uid = {?} AND adrid = {?}", | |
299 | $new['adr1'], $new['adr2'], $new['adr3'], | |
300 | $new['cityid'], $new['city'], $new['postcode'], | |
301 | $new['region'], $new['regiontxt'], $new['country'], | |
302 | $new['precise_lat'], $new['precise_lon'], | |
303 | $uid, $a['adrid']); | |
304 | $new['store'] = true; | |
305 | if (!$new['cityid']) { | |
306 | $erreur[$a['adrid']] = $new; | |
307 | } | |
0337d704 | 308 | } else { |
309 | $new['store'] = false; | |
310 | $erreur[$a['adrid']] = $new; | |
311 | } | |
312 | } | |
313 | return $erreur; | |
314 | } | |
315 | // }}} | |
316 | ||
317 | // {{{ synchro_city($id) | |
318 | /** synchronise the local geoloc_city base to geoloc.org | |
319 | * @param $id the id of the city to synchronize | |
320 | */ | |
787bb3d7 | 321 | function synchro_city($id) |
8c4a0c30 | 322 | { |
56670b6a | 323 | global $globals; |
324 | $url = $globals->geoloc->webservice_url."cityFinder.php?method=id&id=".$id."&out=sql"; | |
8c4a0c30 | 325 | if (!($f = @fopen($url, 'r'))) { |
326 | return false; | |
327 | } | |
0337d704 | 328 | $s = fgets($f); |
8c4a0c30 | 329 | if ($s) { |
08cce2ff | 330 | return XDB::execute("REPLACE INTO geoloc_city VALUES ".$s) > 0; |
8c4a0c30 | 331 | } |
332 | } | |
0337d704 | 333 | // }}} |
334 | ||
56670b6a | 335 | // {{{ function fix_cities_not_on_map($limit) |
7f3ac007 | 336 | function fix_cities_not_on_map($limit=false, $cityid=false) |
56670b6a | 337 | { |
8c4a0c30 | 338 | $missing = XDB::query("SELECT c.id |
339 | FROM geoloc_city AS c | |
787bb3d7 | 340 | LEFT JOIN geoloc_city_in_maps AS m ON(c.id = m.city_id) |
8c4a0c30 | 341 | WHERE m.city_id IS NULL" |
342 | . ($cityid ? " AND c.id = '" . $cityid . "'" : "" ) | |
343 | . ($limit ? " LIMIT $limit" : "" )); | |
56670b6a | 344 | $maps = get_cities_maps($missing->fetchColumn()); |
8c4a0c30 | 345 | if ($maps) { |
56670b6a | 346 | $values = ""; |
8c4a0c30 | 347 | foreach ($maps as $cityid => $maps_c) { |
348 | foreach ($maps_c as $map_id) { | |
56670b6a | 349 | $values .= ",($cityid, $map_id, '')"; |
8c4a0c30 | 350 | } |
351 | } | |
22cc3370 FB |
352 | if (strlen($values) > 1) { |
353 | XDB::execute("REPLACE INTO geoloc_city_in_maps | |
354 | VALUES ".substr($values, 1)); | |
355 | } | |
8c4a0c30 | 356 | } else { |
56670b6a | 357 | return false; |
8c4a0c30 | 358 | } |
014c8464 | 359 | return true; |
360 | } | |
56670b6a | 361 | |
8c4a0c30 | 362 | function set_smallest_levels() |
363 | { | |
364 | $maxlengths = XDB::iterRow("SELECT MAX(LENGTH(gm.path)), gcim.city_id | |
365 | FROM geoloc_city_in_maps AS gcim | |
366 | INNER JOIN geoloc_maps AS gm USING ( map_id ) | |
367 | GROUP BY gcim.city_id"); | |
014c8464 | 368 | while (list($length, $id) = $maxlengths->next()) { |
8c4a0c30 | 369 | XDB::execute("UPDATE geoloc_city_in_maps AS gcim |
370 | INNER JOIN geoloc_maps AS gm USING(map_id) | |
371 | SET gcim.infos = IF(LENGTH(gm.path) = {?}, 'smallest', '') | |
372 | WHERE gcim.city_id = {?}", $length, $id); | |
014c8464 | 373 | } |
56670b6a | 374 | return true; |
375 | } | |
376 | // }}} | |
377 | ||
378 | ||
8c4a0c30 | 379 | function geoloc_to_x($lon, $lat) |
380 | { | |
381 | return deg2rad(1) * $lon *100; | |
382 | } | |
383 | ||
384 | function geoloc_to_y($lon, $lat) | |
385 | { | |
386 | if ($lat < -75) { | |
387 | return latToY(-75); | |
388 | } | |
389 | if ($lat > 75) { | |
390 | return latToY(75); | |
391 | } | |
787bb3d7 | 392 | return -100 * log(tan(pi()/4 + deg2rad(1)/2*$lat)); |
8c4a0c30 | 393 | } |
56670b6a | 394 | |
8c4a0c30 | 395 | function size_of_city($nb) |
396 | { | |
397 | $s = round(log($nb + 1)*2,2); | |
398 | if ($s < 1) { | |
399 | return 1; | |
400 | } | |
401 | return $s; | |
56670b6a | 402 | } |
403 | ||
8c4a0c30 | 404 | function size_of_territory($nb) |
405 | { | |
406 | return size_of_city($nb); | |
407 | } | |
56670b6a | 408 | |
8c4a0c30 | 409 | function geoloc_getData_subcities($mapid, $SFields, &$cities, $direct=true) |
410 | { | |
411 | if ($SFields instanceof UserSet) { | |
412 | $set = $SFields; | |
413 | $SFields = array(); | |
414 | } else { | |
415 | $set = new UserSet(); | |
416 | } | |
417 | for ($i_mapfield=0; $i_mapfield < count($SFields) ; $i_mapfield++) { | |
418 | if ($SFields[$i_mapfield]->fieldFormName == 'mapid') { | |
419 | break; | |
420 | } | |
421 | } | |
422 | $SFields[$i_mapfield] = new MapSField('mapid', | |
423 | array('gcim.map_id'), | |
424 | array('adresses','geoloc_city_in_maps'), | |
425 | array('am','gcim'), | |
426 | array(getadr_join('am'), 'am.cityid = gcim.city_id'), | |
427 | $mapid); | |
2b105fb6 | 428 | $fields = new SFieldGroup(true, $SFields); |
429 | $where = $fields->get_where_statement(); | |
86b5c8f0 | 430 | $joins = $fields->get_select_statement(); |
8c4a0c30 | 431 | if ($where) { |
432 | $where .= ' AND '; | |
433 | } | |
434 | $cityres = $set->get('gc.id, | |
435 | gc.lon / 100000 AS x, gc.lat/100000 AS y, | |
436 | gc.name, | |
437 | COUNT(u.user_id) AS pop, | |
438 | SUM(u.promo % 2) AS yellow', | |
86b5c8f0 | 439 | "$joins |
8c4a0c30 | 440 | LEFT JOIN geoloc_city AS gc ON(gcim.city_id = gc.id)", |
441 | $where . ($direct ? "gcim.infos = 'smallest'" : '1'), | |
442 | 'gc.id, gc.alias', | |
443 | 'pop DESC'); | |
06df222b | 444 | foreach($cityres as $c) { |
8c4a0c30 | 445 | if ($c['pop'] > 0) { |
2b105fb6 | 446 | $city = $c; |
447 | $city['x'] = geoloc_to_x($c['x'], $c['y']); | |
448 | $city['y'] = geoloc_to_y($c['x'], $c['y']); | |
449 | $city['size'] = size_of_city($c['pop']); | |
450 | $cities[$c['id']] = $city; | |
451 | } | |
8c4a0c30 | 452 | } |
2b105fb6 | 453 | } |
454 | ||
787bb3d7 | 455 | function geoloc_getData_subcountries($mapid, $sin, $minentities) |
8c4a0c30 | 456 | { |
2b105fb6 | 457 | $countries = array(); |
458 | $cities = array(); | |
787bb3d7 | 459 | |
8c4a0c30 | 460 | if ($mapid === false) { |
b536b5ae | 461 | $wheremapid = "WHERE gm.parent IS NULL"; |
8c4a0c30 | 462 | } else { |
b536b5ae | 463 | $wheremapid = "WHERE gm.parent = {?}"; |
8c4a0c30 | 464 | } |
08cce2ff | 465 | $submapres = XDB::iterator( |
787bb3d7 | 466 | "SELECT gm.map_id AS id, gm.name, gm.x, gm.y, gm.xclip, gm.yclip, |
8c4a0c30 | 467 | gm.width, gm.height, gm.scale, 1 AS rat |
468 | FROM geoloc_maps AS gm | |
469 | ". $wheremapid, Env::v('mapid','')); | |
2b105fb6 | 470 | |
edfc872d | 471 | global $globals; |
472 | ||
8c4a0c30 | 473 | while ($c = $submapres->next()) { |
2b105fb6 | 474 | $country = $c; |
2b105fb6 | 475 | $country['color'] = 0xFFFFFF; |
476 | $country['swf'] = $globals->geoloc->webservice_url."maps/mercator/map_".$c['id'].".swf"; | |
477 | $countries[$c['id']] = $country; | |
478 | } | |
787bb3d7 | 479 | |
8c4a0c30 | 480 | if ($mapid === false) { |
481 | return array($countries, $cities); | |
482 | } | |
2b105fb6 | 483 | |
8c4a0c30 | 484 | geoloc_getData_subcities(Env::i('mapid'), $sin, $cities); |
b536b5ae | 485 | $nbcities = count($cities); |
486 | $nocity = $nbcities == 0; | |
8c4a0c30 | 487 | if ($sin instanceof UserSet) { |
488 | $set = $sin; | |
489 | $SFields = array(); | |
490 | } else { | |
491 | $set = new UserSet(); | |
492 | $SFields = $sin; | |
493 | } | |
2b105fb6 | 494 | |
8c4a0c30 | 495 | for ($i_mapfield=0; $i_mapfield < count($SFields) ; $i_mapfield++) { |
496 | if ($SFields[$i_mapfield]->fieldFormName == 'mapid') { | |
497 | break; | |
498 | } | |
499 | } | |
500 | $SFields[$i_mapfield] = new MapSField('mapid', | |
501 | array('map.parent'), | |
502 | array('adresses','geoloc_city_in_maps','geoloc_maps'), | |
503 | array('am','gcim','map'), | |
787bb3d7 FB |
504 | array(getadr_join('am'), |
505 | 'am.cityid = gcim.city_id', | |
8c4a0c30 | 506 | 'map.map_id = gcim.map_id')); |
b536b5ae | 507 | $fields = new SFieldGroup(true, $SFields); |
86b5c8f0 | 508 | $where = $fields->get_where_statement(); |
509 | $joins = $fields->get_select_statement(); | |
8c4a0c30 | 510 | $countryres = $set->get('map.map_id AS id, |
511 | COUNT(u.user_id) AS nbPop, | |
512 | SUM(u.promo % 2) AS yellow, | |
513 | COUNT(DISTINCT gcim.city_id) AS nbCities, | |
514 | SUM(IF(u.user_id IS NULL,0,am.glng)) AS lonPop, | |
515 | SUM(IF(u.user_id IS NULL, 0,am.glat)) AS latPop', | |
86b5c8f0 | 516 | $joins, |
517 | $where, | |
8c4a0c30 | 518 | 'map.map_id', |
519 | 'NULL'); | |
787bb3d7 | 520 | |
b536b5ae | 521 | $maxpop = 0; |
06df222b FB |
522 | $nbentities = $nbcities + count($countryres); |
523 | foreach ($countryres as $c) { | |
b536b5ae | 524 | $c['latPop'] /= $c['nbPop']; |
525 | $c['lonPop'] /= $c['nbPop']; | |
526 | $c['rad'] = size_of_territory($c['nbPop']); | |
527 | if ($maxpop < $c['nbPop']) $maxpop = $c['nbPop']; | |
528 | $c['xPop'] = geoloc_to_x($c['lonPop'], $c['latPop']); | |
529 | $c['yPop'] = geoloc_to_y($c['lonPop'], $c['latPop']); | |
86b5c8f0 | 530 | @$countries[$c['id']] = array_merge($countries[$c['id']], $c); |
787bb3d7 | 531 | |
b536b5ae | 532 | $nbcities += $c['nbCities']; |
787bb3d7 FB |
533 | } |
534 | ||
8c4a0c30 | 535 | if ($nocity && $nbcities < $minentities){ |
536 | foreach($countries as $i => $c) { | |
b536b5ae | 537 | $countries[$i]['nbPop'] = 0; |
86b5c8f0 | 538 | if (@$c['nbCities'] > 0) { |
8c4a0c30 | 539 | geoloc_getData_subcities($c['id'], $sin, $cities, false); |
787bb3d7 | 540 | } |
8c4a0c30 | 541 | } |
b536b5ae | 542 | } |
787bb3d7 | 543 | |
8c4a0c30 | 544 | foreach ($countries as $i => $c) { |
c1895524 | 545 | if (@$c['nbPop'] > 0) { |
8c4a0c30 | 546 | $lambda = pow($c['nbPop'] / $maxpop,0.3); |
547 | $countries[$i]['color'] = 0x0000FF + round((1-$lambda) * 0xFF)*0x010100; | |
548 | } | |
b536b5ae | 549 | } |
787bb3d7 FB |
550 | |
551 | return array($countries, $cities); | |
552 | } | |
138b3c8e | 553 | // }}} |
554 | ||
a7de4ef7 | 555 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
0337d704 | 556 | ?> |