X-Git-Url: http://git.polytechnique.org/?a=blobdiff_plain;ds=sidebyside;f=include%2Fgeoloc.inc.php;h=d736b603218d8d84d67f0daca8ee17cbfb213305;hb=9fc2d0d27274e03806caa10785a78dc4707690e6;hp=915ce5d07fcaf8ace88b102f92f32a128f56977c;hpb=a3a049fc80d3707bcc76903ab89f73974c470c0c;p=platal.git diff --git a/include/geoloc.inc.php b/include/geoloc.inc.php index 915ce5d..d736b60 100644 --- a/include/geoloc.inc.php +++ b/include/geoloc.inc.php @@ -1,6 +1,6 @@ register_function('geoloc_region', '_geoloc_region_smarty'); // }}} +function geoloc_is_utf8($text) +{ + return (iconv('utf-8', 'utf-8', $text) == $text); +} + +function geoloc_utf8_decode($text) +{ + return geoloc_is_utf8($text) ? utf8_decode($text) : $text; +} + // {{{ get_address_infos($txt) /** retrieve the infos on a text address * store on the fly the info of the city concerned @@ -77,11 +86,22 @@ function get_address_infos($txt) { $keys = explode('|',fgets($f)); $vals = explode('|',fgets($f)); $infos = array(); - foreach ($keys as $i=>$key) if($vals[$i]) $infos[$key] = ($key == 'sql')?$vals[$i]:utf8_decode(strtr($vals[$i], array(chr(197).chr(147) => "œ"))); - if ($infos['sql']) + foreach ($keys as $i=>$key) { + if($vals[$i]) { + if ($key == 'sql') { + $infos[$key] = $vals[$i]; + } else { + $val = strtr($vals[$i], array(chr(197).chr(147) => "œ")); + $infos[$key] = geoloc_utf8_decode($val); + } + } + } + if (isset($infos['sql']) && $infos['sql']) XDB::execute("REPLACE INTO geoloc_city VALUES ".$infos['sql']); - if ($infos['display']) - XDB::execute("UPDATE geoloc_pays SET display = {?} WHERE a2 = {?}", $infos['display'], $infos['country']); + if (isset($infos['display']) && $infos['display']) + XDB::execute("UPDATE geoloc_pays SET display = {?} WHERE a2 = {?}", $infos['display'], $infos['country']); + if (isset($infos['cityid'])) + fix_cities_not_on_map(1, $infos['cityid']); return $infos; } // }}} @@ -128,6 +148,7 @@ function get_new_maps($url) XDB::execute('INSERT INTO geoloc_maps VALUES '.substr($s, 1)); return true; } +// }}} // {{{ get_address_text($adr) /** make the text of an address that can be read by a mailman @@ -135,11 +156,11 @@ function get_new_maps($url) */ function get_address_text($adr) { $t = ""; - if ($adr['adr1']) $t.= $adr['adr1']; - if ($adr['adr2']) $t.= "\n".$adr['adr2']; - if ($adr['adr3']) $t.= "\n".$adr['adr3']; + if (isset($adr['adr1']) && $adr['adr1']) $t.= $adr['adr1']; + if (isset($adr['adr2']) && $adr['adr2']) $t.= "\n".$adr['adr2']; + if (isset($adr['adr3']) && $adr['adr3']) $t.= "\n".$adr['adr3']; $l = ""; - if ($adr['display']) { + if (isset($adr['display']) && $adr['display']) { $keys = explode(' ', $adr['display']); foreach ($keys as $key) { if (isset($adr[$key])) @@ -156,8 +177,8 @@ function get_address_text($adr) { if ($adr['region']) $l .= $adr['region']." "; if ($adr['postcode']) $l .= $adr['postcode']; } else { - if ($adr['postcode']) $l .= $adr['postcode']." "; - if ($adr['city']) $l .= $adr['city']; + if (isset($adr['postcode']) && $adr['postcode']) $l .= $adr['postcode']." "; + if (isset($adr['city']) && $adr['city']) $l .= $adr['city']; } } if ($l) $t .= "\n".trim($l); @@ -165,7 +186,7 @@ function get_address_text($adr) { $res = XDB::query("SELECT pays FROM geoloc_pays WHERE a2 = {?}", $adr['country']); $adr['countrytxt'] = $res->fetchOneCell(); } - if ($adr['countrytxt']) $t .= "\n".$adr['countrytxt']; + if (isset($adr['countrytxt']) && $adr['countrytxt']) $t .= "\n".$adr['countrytxt']; return trim($t); } // }}} @@ -259,9 +280,9 @@ function localize_addresses($uid) { // }}} // {{{ function fix_cities_not_on_map($limit) -function fix_cities_not_on_map($limit=false) +function fix_cities_not_on_map($limit=false, $cityid=false) { - $missing = XDB::query("SELECT c.id 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".($limit?" LIMIT $limit":"")); + $missing = XDB::query("SELECT c.id 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".($cityid?(" AND c.id = '".$cityid."'"):"").($limit?" LIMIT $limit":"")); $maps = get_cities_maps($missing->fetchColumn()); if ($maps) { @@ -330,6 +351,10 @@ function geoloc_getData_subcities($mapid, $SFields, &$cities, $direct=true) { if ($c['pop'] > 0) { $city = $c; + // $city['name'] = geoloc_utf8_decode($city['name']); + if (!geoloc_is_utf8($city['name'])) { + $city['name'] = utf8_encode($city['name']); + } $city['x'] = geoloc_to_x($c['x'], $c['y']); $city['y'] = geoloc_to_y($c['x'], $c['y']); $city['size'] = size_of_city($c['pop']); @@ -349,7 +374,9 @@ function geoloc_getData_subcountries($mapid, $SFields, $minentities) { "SELECT gm.map_id AS id, gm.name, gm.x, gm.y, gm.xclip, gm.yclip, gm.width, gm.height, gm.scale, 1 AS rat FROM geoloc_maps AS gm - ".$wheremapid, Env::get('mapid','')); + ".$wheremapid, Env::v('mapid','')); + + global $globals; while ($c = $submapres->next()) { @@ -362,7 +389,7 @@ function geoloc_getData_subcountries($mapid, $SFields, $minentities) { if ($mapid === false) return array($countries, $cities); - geoloc_getData_subcities(Env::getInt('mapid'), $SFields, $cities); + geoloc_getData_subcities(Env::i('mapid'), $SFields, $cities); $nbcities = count($cities); $nocity = $nbcities == 0; @@ -419,5 +446,7 @@ function geoloc_getData_subcountries($mapid, $SFields, $minentities) { return array($countries, $cities); } +// }}} + // vim:set et sw=4 sts=4 sws=4 foldmethod=marker: ?>