New file |
0,0 → 1,178 |
<?php |
/** |
* Retourne des infos sur une espèce : noms vernaculaires français, statuts de |
* protection, num_nom, num_tax, nom_sci, et nombre de zones dans lesquelles elle |
* est présente. |
* Interrogeable par nn ou nt |
* |
* Il faudrait appeler les services correspondants pour obtenir les infos sur les |
* noms vernaculaires et les statuts de protection, mais c'est pas performant. |
* Néanmoins ce serait une arcitecture plus solide en cas de changement - pas le |
* temps d'y réfléchir mieux. |
* |
* @package chorodep |
* @author Mathias Chouet <mathias@tela-botanica.org> |
* @author Aurélien Perronnet <aurelien@tela-botanica.org> |
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt> |
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt> |
* @version 1.0 |
* @copyright 1999-2014 Tela Botanica (accueil@tela-botanica.org) |
*/ |
|
class InfosEspece extends Commun { |
|
protected $serviceNom = 'InfosEspece'; |
protected $masque; |
protected $navigation; |
protected $table; |
protected $tableNomsVernaculaires; |
protected $urlStatutsProtection; |
|
public function init() { |
$this->masque = array(); |
$this->traiterVersionProjet(); |
$this->table = $this->table_version[0]; |
$this->tableNomsVernaculaires = $this->config['table_nv']; |
$this->urlStatutsProtection = $this->config['url_service_sptb']; |
} |
|
public function consulter($ressources, $parametres) { |
|
$retour = null; |
if(preg_match("/^(nt|nn):([0-9]+)$/", $ressources[0], $matches)) { |
$champ_nt_ou_nn = ($matches[1] == "nn") ? "num_nom" : "num_tax"; |
|
if(count($ressources) == 1) { |
// toutes les infos |
$infos_espece = $this->getInfosEspece($champ_nt_ou_nn, $matches[2]); |
$retour = array_merge($infos_espece, $this->getInfosPresence($champ_nt_ou_nn, $matches[2])); |
$statuts_protection = array( |
'statuts_protection' => $this->getStatutsProtection($champ_nt_ou_nn, $matches[2]) |
); |
$retour = array_merge($retour, $statuts_protection); |
$noms_vernaculaires = array( |
'noms_vernaculaires_fr' => $this->getNomsVernaculaires($champ_nt_ou_nn, $matches[2]) |
); |
$retour = array_merge($retour, $noms_vernaculaires); |
} else { |
// sous action du service |
$retour = array(); |
switch($ressources[1]) { |
case "noms-vernaculaires": |
$retour = array('noms_vernaculaires_fr' => $this->getNomsVernaculaires($champ_nt_ou_nn, $matches[2])); |
break; |
case "statuts-protection": |
$retour = array('statuts_protection' => $this->getStatutsProtection($champ_nt_ou_nn, $matches[2])); |
break; |
case "presence": |
$retour = $this->getInfosPresence($champ_nt_ou_nn, $matches[2]); |
break; |
default: |
$retour = "Actions possibles: noms-vernaculaires, statuts-protection, presence"; |
} |
} |
} else { |
// TODO : envoyer message erreur; |
} |
return $retour; |
} |
|
/** |
* Toutes les infos sauf noms vernaculaires et statuts de protection |
*/ |
protected function getInfosEspece($champ_nt_ou_nn, $nt_ou_nn) { |
$req = "SELECT DISTINCT num_nom, num_tax, nom_sci" |
. " FROM " . $this->table |
. " WHERE $champ_nt_ou_nn = " . $this->getBdd()->proteger($nt_ou_nn); |
|
$resultat = $this->getBdd()->recuperer($req); |
|
return $resultat; |
} |
|
/** |
* Construit une opération d'addition entre toutes les colonnes de la table |
* chorodep représentant un département |
*/ |
protected function construireSommeColonnes() { |
$colonnes = array(); |
for ($i=1; $i <= 95; $i++) { |
$colonnes[] = '`' . ($i > 9 ? $i : "0$i") . '`'; |
} |
$somme = implode('+', $colonnes); |
return $somme; |
} |
|
protected function getInfosPresence($champ_nt_ou_nn, $nt_ou_nn) { |
$req = "SELECT " . $this->construireSommeColonnes() . " as nb_presence_zones". |
" FROM ".$this->table. |
" WHERE ".$champ_nt_ou_nn." = ".$this->getBdd()->proteger($nt_ou_nn); |
|
$resultat = $this->getBdd()->recuperer($req); |
return $resultat; |
} |
|
/** |
* Appelle le WS sptb car la table ne contient pas toutes les infos (il faut |
* agréger les taxons supérieurs) |
*/ |
protected function getStatutsProtection($champ_nt_ou_nn, $nt_ou_nn) { |
$num_noms = array(); |
if ($champ_nt_ou_nn == "num_tax") { |
// le service sptb n'accepte pas les nt (on croit que si mais en fait |
// non) alors on chope les nn associés à ce nt dans chorodep - c'est |
// moisi mais c'est toujours mieux que si c'était pire |
$numTaxP = $this->getBdd()->proteger($nt_ou_nn); |
$req = "SELECT DISTINCT num_nom" |
. " FROM " . $this->table |
. " WHERE num_tax = $numTaxP"; |
|
$resultat = $this->getBdd()->recupererTous($req); |
foreach($resultat as $res) { |
$num_noms[] = $res['num_nom']; |
} |
} else { |
$num_noms[] = $nt_ou_nn; |
} |
|
$statuts = array(); |
// récupération des statuts pour chaque num_nom |
foreach ($num_noms as $nn) { |
$url = sprintf($this->urlStatutsProtection, $nn); |
$donnees = $this->getRestClient()->consulter($url); |
$donnees = json_decode($donnees, true); |
foreach ($donnees as $d) { |
$statuts[] = array( |
'zone' => $d['code_zone_application'], |
'lien' => $d['hyperlien_legifrance'] |
); |
} |
} |
|
return $statuts; |
} |
|
// @TODO faire un appel au WS nvjfl "for the sake of non-nodebagging" ? |
protected function getNomsVernaculaires($champ_nt_ou_nn, $nt_ou_nn) { |
$numP = $this->getBdd()->proteger($nt_ou_nn); |
$req = "SELECT DISTINCT nv.nom_vernaculaire" |
. " FROM " . $this->tableNomsVernaculaires . " nv"; |
if ($champ_nt_ou_nn == "num_nom") { |
$req .= " LEFT JOIN " . $this->table . " c ON nv.num_taxon = c.num_tax" |
. " WHERE c.num_nom = $numP"; |
} else { |
$req .= " WHERE nv.num_taxon = $numP"; |
} |
$req .= " AND nv.code_langue = 'fra'"; |
|
$resultat = $this->getBdd()->recupererTous($req); |
|
$resultat_fmt = array(); |
foreach($resultat as $nv) { |
$resultat_fmt[] = $nv['nom_vernaculaire']; |
} |
|
return $resultat_fmt; |
} |
} |
?> |