| 1 | <?php |
| 2 | /*************************************************************************** |
| 3 | * Copyright (C) 2003-2004 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 | |
| 23 | require_once('xorg.inc.php'); |
| 24 | |
| 25 | // to debug sql use the next line |
| 26 | if (Env::has('debug')) |
| 27 | new_simple_page('geoloc/getData.tpl', AUTH_COOKIE); |
| 28 | else |
| 29 | { |
| 30 | header("Content-type: text/xml"); |
| 31 | new_nonhtml_page('geoloc/getData.tpl', AUTH_COOKIE); |
| 32 | } |
| 33 | |
| 34 | require_once('search.inc.php'); |
| 35 | require_once('geoloc.inc.php'); |
| 36 | |
| 37 | $usual_fields = advancedSearchFromInput(); |
| 38 | foreach ($usual_fields as $i_mapfield => $field) if ($field->fieldFormName == 'mapid') break; |
| 39 | |
| 40 | $querystring = ""; |
| 41 | foreach ($_GET as $v => $a) |
| 42 | if ($v != 'mapid') |
| 43 | $querystring .= urlencode($v).'='.urlencode($a).'&'; |
| 44 | $page->assign('searchvars', $querystring); |
| 45 | |
| 46 | $maxentities = 100; |
| 47 | $minentities = 5; |
| 48 | |
| 49 | function get_cities_in_territory($t, $usual_fields, $direct=true) |
| 50 | { |
| 51 | global $cities, $globals, $i_mapfield; |
| 52 | $usual_fields[$i_mapfield] = new MapSField('mapid', array('gcim.map_id'), array('adresses','geoloc_city_in_maps'), array('am','gcim'), array(getadr_join('am'), 'am.cityid = gcim.city_id'), $t); |
| 53 | $fields = new SFieldGroup(true, $usual_fields); |
| 54 | $where = $fields->get_where_statement(); |
| 55 | if ($where) $where = " AND ".$where; |
| 56 | $cityres = $globals->xdb->iterator(" |
| 57 | SELECT gc.id, |
| 58 | gc.lon / 100000 AS x, gc.lat/100000 AS y, |
| 59 | gc.name, |
| 60 | COUNT(u.user_id) AS pop, |
| 61 | SUM(u.promo % 2) AS yellow |
| 62 | FROM auth_user_md5 AS u |
| 63 | INNER JOIN auth_user_quick AS q ON(u.user_id = q.user_id) |
| 64 | ".$fields->get_select_statement()." |
| 65 | LEFT JOIN geoloc_city AS gc ON(gcim.city_id = gc.id) |
| 66 | WHERE ".($direct?"gcim.infos = 'smallest'":"1")." |
| 67 | $where |
| 68 | GROUP BY gc.id,gc.alias ORDER BY pop DESC"); |
| 69 | while ($c = $cityres->next()) |
| 70 | if ($c['pop'] > 0) |
| 71 | { |
| 72 | $city = $c; |
| 73 | $city['x'] = geoloc_to_x($c['x'], $c['y']); |
| 74 | $city['y'] = geoloc_to_y($c['x'], $c['y']); |
| 75 | $city['size'] = size_of_city($c['pop']); |
| 76 | $cities[$c['id']] = $city; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | if (Env::has('mapid')) |
| 81 | $wheremapid = "WHERE gm.parent = {?}"; |
| 82 | else |
| 83 | $wheremapid = "WHERE gm.parent IS NULL"; |
| 84 | |
| 85 | $submapres = $globals->xdb->iterator( |
| 86 | "SELECT gm.map_id AS id, gm.name, gm.x, gm.y, gm.xclip, gm.yclip, |
| 87 | gm.width, gm.height, gm.scale, 1 AS rat |
| 88 | FROM geoloc_maps AS gm |
| 89 | ".$wheremapid, Env::get('mapid','')); |
| 90 | |
| 91 | $countries = array(); |
| 92 | while ($c = $submapres->next()) |
| 93 | { |
| 94 | $country = $c; |
| 95 | $country['name'] = utf8_decode($country['name']); |
| 96 | $country['color'] = 0xFFFFFF; |
| 97 | $country['swf'] = $globals->geoloc->webservice_url."maps/mercator/map_".$c['id'].".swf"; |
| 98 | $countries[$c['id']] = $country; |
| 99 | } |
| 100 | |
| 101 | $cities = array(); |
| 102 | if (Env::has('mapid')) |
| 103 | { |
| 104 | get_cities_in_territory(Env::getInt('mapid'), $usual_fields); |
| 105 | $nbcities = count($cities); |
| 106 | $nocity = $nbcities == 0; |
| 107 | |
| 108 | $usual_fields[$i_mapfield] = new MapSField('mapid', array('map.parent'), array('adresses','geoloc_city_in_maps','geoloc_maps'), array('am','gcim','map'), array(getadr_join('am'), 'am.cityid = gcim.city_id', 'map.map_id = gcim.map_id')); |
| 109 | $fields = new SFieldGroup(true, $usual_fields); |
| 110 | $where = $fields->get_where_statement(); |
| 111 | if ($where) $where = " WHERE ".$where; |
| 112 | |
| 113 | $countryres = $globals->xdb->iterator(" |
| 114 | SELECT map.map_id AS id, |
| 115 | COUNT(u.user_id) AS nbPop, |
| 116 | SUM(u.promo % 2) AS yellow, |
| 117 | COUNT(DISTINCT gcim.city_id) AS nbCities, |
| 118 | SUM(IF(u.user_id IS NULL,0,am.glng)) AS lonPop, |
| 119 | SUM(IF(u.user_id IS NULL, 0,am.glat)) AS latPop |
| 120 | FROM auth_user_md5 AS u |
| 121 | INNER JOIN auth_user_quick AS q ON(u.user_id = q.user_id) |
| 122 | ".$fields->get_select_statement()." |
| 123 | $where |
| 124 | GROUP BY map.map_id ORDER BY NULL", $hierarchy); |
| 125 | |
| 126 | $maxpop = 0; |
| 127 | $nbentities = $nbcities + $countryres->total(); |
| 128 | while ($c = $countryres->next()) |
| 129 | { |
| 130 | $c['latPop'] /= $c['nbPop']; |
| 131 | $c['lonPop'] /= $c['nbPop']; |
| 132 | $c['rad'] = size_of_territory($c['nbPop']); |
| 133 | if ($maxpop < $c['nbPop']) $maxpop = $c['nbPop']; |
| 134 | $c['xPop'] = geoloc_to_x($c['lonPop'], $c['latPop']); |
| 135 | $c['yPop'] = geoloc_to_y($c['lonPop'], $c['latPop']); |
| 136 | $countries[$c['id']] = array_merge($countries[$c['id']], $c); |
| 137 | |
| 138 | $nbcities += $c['nbCities']; |
| 139 | } |
| 140 | |
| 141 | if ($nocity && $nbcities < 10) |
| 142 | { |
| 143 | foreach($countries as $i => $c) |
| 144 | { |
| 145 | $countries[$i]['nbPop'] = 0; |
| 146 | if ($c['nbCities'] > 0) |
| 147 | get_cities_in_territory($c['id'], $usual_fields, false); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | foreach ($countries as $i => $c) if ($c['nbPop'] > 0) |
| 152 | { |
| 153 | $lambda = pow($c['nbPop'] / $maxpop,0.3); |
| 154 | $countries[$i]['color'] = 0x0000FF + round((1-$lambda) * 0xFF)*0x010100; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | $page->assign('countries', $countries); |
| 159 | $page->assign('cities', $cities); |
| 160 | |
| 161 | $page->run(); |
| 162 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker: |
| 163 | ?> |