Line 34... |
Line 34... |
34 |
private $lon = null;
|
34 |
private $lon = null;
|
35 |
private $zone = null;
|
35 |
private $zone = null;
|
36 |
private $niveaux = null;
|
36 |
private $niveaux = null;
|
Line 37... |
Line 37... |
37 |
|
37 |
|
- |
|
38 |
private $masque = null;
|
Line 38... |
Line 39... |
38 |
private $masque = null;
|
39 |
private $limite = 1;
|
39 |
|
40 |
|
40 |
public function __construct(Bdd $bdd) {
|
41 |
public function __construct(Bdd $bdd) {
|
Line 41... |
Line 42... |
41 |
$this->bdd = $bdd;
|
42 |
$this->bdd = $bdd;
|
42 |
}
|
43 |
}
|
43 |
|
44 |
|
44 |
public function consulter($ressources, $parametres) {
|
45 |
public function consulter($ressources, $parametres) {
|
- |
|
46 |
$this->parametres = $parametres;
|
45 |
$this->parametres = $parametres;
|
47 |
$this->verifierParametres();
|
46 |
$this->verifierParametres();
|
48 |
$this->masque = isset($this->parametres['masque']) ? $this->parametres['masque'] : null;
|
47 |
$this->masque = isset($this->parametres['masque']) ? $this->parametres['masque'] : null;
|
49 |
$this->limite = isset($this->parametres['limite']) ? intval($this->parametres['limite']) : $this->limite;
|
48 |
$this->lat = isset($this->parametres['lat']) ? $this->parametres['lat'] : null;
|
50 |
$this->lat = isset($this->parametres['lat']) ? $this->parametres['lat'] : null;
|
Line 49... |
Line 51... |
49 |
$this->lon = isset($this->parametres['lon']) ? $this->parametres['lon'] : null;
|
51 |
$this->lon = isset($this->parametres['lon']) ? $this->parametres['lon'] : null;
|
50 |
$this->zone = isset($this->parametres['zone']) ? $this->parametres['zone'] : null;
|
52 |
$this->zone = isset($this->parametres['zone']) ? $this->parametres['zone'] : null;
|
51 |
$this->niveaux = isset($this->parametres['niveau']) ? explode(',', $this->parametres['niveau']) : null;
|
53 |
$this->niveaux = isset($this->parametres['niveau']) ? explode(',', $this->parametres['niveau']) : null;
|
52 |
|
54 |
|
53 |
if($this->masque != null) {
|
55 |
if($this->masque != null) {
|
54 |
$corps = $this->rechercherZoneGeoParNom($this->masque);
|
56 |
$corps = $this->rechercherZoneGeoParNom($this->masque, $this->niveaux, $this->limite);
|
Line 170... |
Line 172... |
170 |
$pageEncode = rawurlencode($page);
|
172 |
$pageEncode = rawurlencode($page);
|
171 |
$urlTpl = 'https://%s.wikipedia.org/wiki/%s';
|
173 |
$urlTpl = 'https://%s.wikipedia.org/wiki/%s';
|
172 |
return sprintf($urlTpl, $lang, $pageEncode);
|
174 |
return sprintf($urlTpl, $lang, $pageEncode);
|
173 |
}
|
175 |
}
|
Line 174... |
Line 176... |
174 |
|
176 |
|
175 |
public function rechercherZoneGeoParNom($masque) {
|
177 |
public function rechercherZoneGeoParNom($masque, $niveaux = null, $limite = 1) {
|
- |
|
178 |
$masque_fmt = str_replace(array(' ', '-'), '_', $masque);
|
- |
|
179 |
// Cas d'une recherche pour autocompletion
|
Line 176... |
Line 180... |
176 |
$masque_fmt = str_replace(array(' ', '-'), '_', $masque);
|
180 |
$masque_fmt = ($limite > 1) ? $masque_fmt.'%' : $masque_fmt;
|
177 |
|
181 |
|
178 |
$champs = 'id_zone_geo, osm_id, intitule, centre_lat, centre_lng, '.
|
182 |
$champs = 'id_zone_geo, osm_id, intitule, centre_lat, centre_lng, '.
|
Line -... |
Line 183... |
- |
|
183 |
'zone, niveau, code_iso_3166_1, code_iso_3166_2, code_insee, '.
|
- |
|
184 |
'nom, nom_fr, nom_en, nom_es, wikipedia';
|
- |
|
185 |
|
- |
|
186 |
$champs_tri = 'IF(ISNULL(nom_fr), 0, 1) as nom_fr_present, '.
|
- |
|
187 |
'IF(ISNULL(nom_en), 0, 1) as nom_en_present, '.
|
- |
|
188 |
'IF(ISNULL(nom_es), 0, 1) as nom_es_present, '.
|
- |
|
189 |
'IF(ISNULL(intitule), 0, 1) as intitule_present, '.
|
- |
|
190 |
'IF(ISNULL(nom), 0, 1) as nom_present ';
|
- |
|
191 |
|
- |
|
192 |
$ordre = 'nom_present DESC, nom_fr_present DESC, nom_fr_present DESC, '.
|
- |
|
193 |
'nom_es_present DESC, intitule_present DESC, niveau DESC';
|
- |
|
194 |
|
- |
|
195 |
$niveau_str = array();
|
- |
|
196 |
foreach ($niveaux as $niveau) {
|
- |
|
197 |
$niveau_str[] = $this->bdd->proteger($niveau);
|
- |
|
198 |
}
|
- |
|
199 |
|
- |
|
200 |
$requete = "SELECT $champs, $champs_tri FROM osm_zones_admin ".
|
- |
|
201 |
"WHERE ".
|
- |
|
202 |
(!empty($niveau_str) ? ("niveau IN (".implode(',', $niveau_str).") AND ") : "").
|
- |
|
203 |
"(nom LIKE ".$this->bdd->proteger($masque_fmt).' OR '.
|
179 |
'zone, niveau, code_iso_3166_1, code_iso_3166_2, code_insee, '.
|
204 |
"intitule LIKE ".$this->bdd->proteger($masque_fmt).' OR '.
|
180 |
'nom, nom_fr, nom_en, nom_es, wikipedia';
|
205 |
"nom_fr LIKE ".$this->bdd->proteger($masque_fmt).' OR '.
|
Line 181... |
Line 206... |
181 |
|
206 |
"nom_en LIKE ".$this->bdd->proteger($masque_fmt).' OR '.
|
182 |
$requete = "SELECT $champs FROM osm_zones_admin WHERE nom_fr LIKE ".$this->bdd->proteger($masque_fmt).' '.
|
207 |
"nom_es LIKE ".$this->bdd->proteger($masque_fmt).') '.
|
183 |
"ORDER BY NIVEAU DESC LIMIT 1";
|
208 |
"ORDER BY ".$ordre." LIMIT ".$limite;
|
184 |
|
209 |
|
185 |
$resultat = $this->bdd->recupererTous($requete);
|
210 |
$resultat = $this->bdd->recupererTous($requete);
|
- |
|
211 |
if (empty($resultat)) {
|
- |
|
212 |
$msgTpl = "Service '%s' : aucune zone correspondant au nom : %s .";
|
186 |
if (empty($resultat)) {
|
213 |
$msg = sprintf($msgTpl, get_class($this), $masque);
|
Line 187... |
Line 214... |
187 |
$msgTpl = "Service '%s' : aucune zone correspondant au nom : %s .";
|
214 |
throw new Exception($msg, RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE);
|
188 |
$msg = sprintf($msgTpl, get_class($this), $masque);
|
215 |
} else {
|
189 |
throw new Exception($msg, RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE);
|
216 |
$resultat = ($limite == 1) ? $resultat[0] : $resultat;
|
190 |
}
|
217 |
}
|