* @author Aurélien Perronnet * @license GPL v3 * @license CECILL v2 * @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; case "presence-departement": $retour = $this->getInfosPresenceDepartement($champ_nt_ou_nn, $matches[2], $ressources[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 getInfosPresenceDepartement($champ_nt_ou_nn, $nt_ou_nn, $departement) { // Dans le cas où un num nom est demandé on ajoute les synonymes à la recherche if($champ_nt_ou_nn == "num_nom") { $url = $this->ajouterHrefAutreProjet('noms', $nt_ou_nn, '/relations/synonymie', 'bdtfx', 'retour.format=min'); $val = (array)$this->consulterHref($url); if(isset($val['resultat'])) { $nt_ou_nn_tab = array_keys((array)$val['resultat']); foreach($nt_ou_nn_tab as &$nnt) { $nnt = $this->getBdd()->proteger($nnt); } $nt_ou_nn = implode(',', $nt_ou_nn_tab); } else { $nt_ou_nn = $this->getBdd()->proteger($nt_ou_nn); } } else { $nt_ou_nn = $this->getBdd()->proteger($nt_ou_nn); } // Afin de s'assurer d'une valeur numérique $departement = intval($departement); // Afin de gérer les noms de colonnes qui commencent par 0 sans imposer de format // de nombre à l'entrée du web service $departement = $departement < 10 ? "0".$departement : $departement; $req = "SELECT count(*) > 0 as present". " FROM ".$this->table. " WHERE ".$champ_nt_ou_nn." IN (".$nt_ou_nn.") AND ". "`".$departement."` = 1"; $resultat = $this->getBdd()->recuperer($req); return $resultat; } 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; } } ?>