Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 1111 | Rev 1170 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
6 jpm 1
<?php
2
/**
1111 mathias 3
* Retourne des infos sur une espèce : noms vernaculaires français, statuts de
4
 * protection, num_nom, num_tax, nom_sci, et nombre de zones dans lesquelles elle
5
 * est présente.
6
 * Interrogeable par nn ou nt
7
 *
8
 * Il faudrait appeler les services correspondants pour obtenir les infos sur les
9
 * noms vernaculaires et les statuts de protection, mais c'est pas performant.
10
 * Néanmoins ce serait une arcitecture plus solide en cas de changement - pas le
11
 * temps d'y réfléchir mieux.
6 jpm 12
*
1103 mathias 13
* @package chorodep
1111 mathias 14
* @author Mathias Chouet <mathias@tela-botanica.org>
15
* @author Aurélien Perronnet <aurelien@tela-botanica.org>
6 jpm 16
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
17
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
18
* @version 1.0
1103 mathias 19
* @copyright 1999-2014 Tela Botanica (accueil@tela-botanica.org)
6 jpm 20
*/
21
 
1111 mathias 22
class InfosEspece extends Commun {
6 jpm 23
 
1111 mathias 24
	protected $serviceNom = 'InfosEspece';
25
	protected $masque;
26
	protected $navigation;
1103 mathias 27
	protected $table;
1111 mathias 28
	protected $tableNomsVernaculaires;
29
	protected $urlStatutsProtection;
1103 mathias 30
 
1111 mathias 31
	public function init() {
1103 mathias 32
		$this->masque = array();
33
		$this->traiterVersionProjet();
34
		$this->table = $this->table_version[0];
1111 mathias 35
		$this->tableNomsVernaculaires = $this->config['table_nv'];
36
		$this->urlStatutsProtection = $this->config['url_service_sptb'];
6 jpm 37
	}
1111 mathias 38
 
39
	public function consulter($ressources, $parametres) {
40
		$retour = null;
41
		if(preg_match("/^(nt|nn):([0-9]+)$/", $ressources[0], $matches)) {
42
			$champ_nt_ou_nn = ($matches[1] == "nn") ? "num_nom" : "num_tax";
43
			if(count($ressources) == 1) {
44
				// toutes les infos
45
				$infos_espece = $this->getInfosEspece($champ_nt_ou_nn, $matches[2]);
46
				$retour = array_merge($infos_espece, $this->getInfosPresence($champ_nt_ou_nn, $matches[2]));
47
				$statuts_protection = array(
48
					'statuts_protection' => $this->getStatutsProtection($champ_nt_ou_nn, $matches[2])
49
				);
50
				$retour = array_merge($retour, $statuts_protection);
51
				$noms_vernaculaires = array(
52
					'noms_vernaculaires_fr' => $this->getNomsVernaculaires($champ_nt_ou_nn, $matches[2])
53
				);
54
				$retour = array_merge($retour, $noms_vernaculaires);
55
			} else {
56
				// sous action du service
57
				$retour = array();
58
				switch($ressources[1]) {
59
					case "noms-vernaculaires":
60
						$retour = array('noms_vernaculaires_fr' => $this->getNomsVernaculaires($champ_nt_ou_nn, $matches[2]));
61
					break;
62
					case "statuts-protection":
63
						$retour = array('statuts_protection' => $this->getStatutsProtection($champ_nt_ou_nn, $matches[2]));
64
					break;
65
					case "presence":
66
						$retour = $this->getInfosPresence($champ_nt_ou_nn, $matches[2]);
67
					break;
1165 aurelien 68
					case "presence-departement":
69
						$retour = $this->getInfosPresenceDepartement($champ_nt_ou_nn, $matches[2], $ressources[2]);
70
					break;
1111 mathias 71
					default:
72
						$retour = "Actions possibles: noms-vernaculaires, statuts-protection, presence";
73
				}
74
			}
75
		} else {
76
			// TODO : envoyer message erreur;
6 jpm 77
		}
1111 mathias 78
		return $retour;
6 jpm 79
	}
1103 mathias 80
 
81
	/**
1111 mathias 82
	 * Toutes les infos sauf noms vernaculaires et statuts de protection
6 jpm 83
	 */
1111 mathias 84
	protected function getInfosEspece($champ_nt_ou_nn, $nt_ou_nn) {
85
		$req = "SELECT DISTINCT num_nom, num_tax, nom_sci"
86
			. " FROM " . $this->table
87
			. " WHERE $champ_nt_ou_nn = " . $this->getBdd()->proteger($nt_ou_nn);
88
 
89
		$resultat = $this->getBdd()->recuperer($req);
90
 
91
		return $resultat;
6 jpm 92
	}
1100 mathias 93
 
94
	/**
1111 mathias 95
	 * Construit une opération d'addition entre toutes les colonnes de la table
96
	 * chorodep représentant un département
1100 mathias 97
	 */
1111 mathias 98
	protected function construireSommeColonnes() {
99
		$colonnes = array();
100
		for ($i=1; $i <= 95; $i++) {
101
			$colonnes[] = '`' . ($i > 9 ? $i : "0$i") . '`';
6 jpm 102
		}
1111 mathias 103
		$somme = implode('+', $colonnes);
104
		return $somme;
6 jpm 105
	}
1165 aurelien 106
 
107
	protected function getInfosPresenceDepartement($champ_nt_ou_nn, $nt_ou_nn, $departement) {
108
 
109
		// Afin de s'assurer d'une valeur numérique
110
		$departement = intval($departement);
111
		// Afin de gérer les noms de colonnes qui commencent par 0 sans imposer de format
112
		// de nombre à l'entrée du web service
113
		$departement = $departement < 10 ? "0".$departement : $departement;
114
 
115
		$req = "SELECT count(*) > 0 as present".
116
				" FROM ".$this->table.
117
				" WHERE ".$champ_nt_ou_nn." = ".$this->getBdd()->proteger($nt_ou_nn).' AND '.
118
				"`".$departement."` = 1";
119
		//echo $req;exit;
120
		$resultat = $this->getBdd()->recuperer($req);
121
		return $resultat;
122
	}
1103 mathias 123
 
1111 mathias 124
	protected function getInfosPresence($champ_nt_ou_nn, $nt_ou_nn) {
125
		$req = "SELECT " . $this->construireSommeColonnes() . " as nb_presence_zones".
126
				" FROM ".$this->table.
127
				" WHERE ".$champ_nt_ou_nn." = ".$this->getBdd()->proteger($nt_ou_nn);
1103 mathias 128
 
1111 mathias 129
		$resultat = $this->getBdd()->recuperer($req);
130
		return $resultat;
131
	}
1103 mathias 132
 
1111 mathias 133
	/**
134
	 * Appelle le WS sptb car la table ne contient pas toutes les infos (il faut
135
	 * agréger les taxons supérieurs)
136
	 */
137
	protected function getStatutsProtection($champ_nt_ou_nn, $nt_ou_nn) {
138
		$num_noms = array();
139
		if ($champ_nt_ou_nn == "num_tax") {
140
			// le service sptb n'accepte pas les nt (on croit que si mais en fait
141
			// non) alors on chope les nn associés à ce nt dans chorodep - c'est
142
			// moisi mais c'est toujours mieux que si c'était pire
143
			$numTaxP  = $this->getBdd()->proteger($nt_ou_nn);
144
			$req = "SELECT DISTINCT num_nom"
145
				. " FROM " . $this->table
146
				. " WHERE num_tax = $numTaxP";
1103 mathias 147
 
1111 mathias 148
			$resultat = $this->getBdd()->recupererTous($req);
149
			foreach($resultat as $res) {
150
				$num_noms[] = $res['num_nom'];
151
			}
152
		} else {
153
			$num_noms[] = $nt_ou_nn;
6 jpm 154
		}
1103 mathias 155
 
1111 mathias 156
		$statuts = array();
157
		// récupération des statuts pour chaque num_nom
158
		foreach ($num_noms as $nn) {
159
			$url = sprintf($this->urlStatutsProtection, $nn);
160
			$donnees = $this->getRestClient()->consulter($url);
161
			$donnees = json_decode($donnees, true);
162
			foreach ($donnees as $d) {
163
				$statuts[] = array(
164
					'zone' => $d['code_zone_application'],
165
					'lien' => $d['hyperlien_legifrance']
166
				);
167
			}
1100 mathias 168
		}
1103 mathias 169
 
1111 mathias 170
		return $statuts;
251 delphine 171
	}
1103 mathias 172
 
1111 mathias 173
	// @TODO faire un appel au WS nvjfl "for the sake of non-nodebagging" ?
174
	protected function getNomsVernaculaires($champ_nt_ou_nn, $nt_ou_nn) {
175
		$numP  = $this->getBdd()->proteger($nt_ou_nn);
176
		$req = "SELECT DISTINCT nv.nom_vernaculaire"
177
			. " FROM " . $this->tableNomsVernaculaires . " nv";
178
		if ($champ_nt_ou_nn == "num_nom") {
179
			$req .= " LEFT JOIN " . $this->table . " c ON nv.num_taxon = c.num_tax"
180
				. " WHERE c.num_nom = $numP";
181
		} else {
182
			$req .= " WHERE nv.num_taxon = $numP";
1105 mathias 183
		}
1111 mathias 184
		$req .= " AND nv.code_langue = 'fra'";
1103 mathias 185
 
186
		$resultat = $this->getBdd()->recupererTous($req);
187
 
1111 mathias 188
		$resultat_fmt = array();
189
		foreach($resultat as $nv) {
190
			$resultat_fmt[] = $nv['nom_vernaculaire'];
191
		}
1103 mathias 192
 
1111 mathias 193
		return $resultat_fmt;
6 jpm 194
	}
195
}
196
?>