Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 1165 | 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
 
1170 aurelien 109
		// Dans le cas où un num nom est demandé on ajoute les synonymes à la recherche
110
		if($champ_nt_ou_nn == "num_nom") {
111
			$url = $this->ajouterHrefAutreProjet('noms', $nt_ou_nn, '/relations/synonymie', 'bdtfx', 'retour.format=min');
112
			$val = (array)$this->consulterHref($url);
113
 
114
			if(isset($val['resultat'])) {
115
				$nt_ou_nn_tab = array_keys((array)$val['resultat']);
116
				//print_r($nt_ou_nn_tab);exit;
117
				foreach($nt_ou_nn_tab as &$nnt) {
118
					$nnt = $this->getBdd()->proteger($nnt);
119
				}
120
				$nt_ou_nn = implode(',', $nt_ou_nn_tab);
121
			} else {
122
				$nt_ou_nn = $this->getBdd()->proteger($nt_ou_nn);
123
			}
124
		} else {
125
			$nt_ou_nn = $this->getBdd()->proteger($nt_ou_nn);
126
		}
127
 
1165 aurelien 128
		// Afin de s'assurer d'une valeur numérique
129
		$departement = intval($departement);
130
		// Afin de gérer les noms de colonnes qui commencent par 0 sans imposer de format
131
		// de nombre à l'entrée du web service
132
		$departement = $departement < 10 ? "0".$departement : $departement;
133
 
134
		$req = "SELECT count(*) > 0 as present".
135
				" FROM ".$this->table.
1170 aurelien 136
				" WHERE ".$champ_nt_ou_nn." IN (".$nt_ou_nn.") AND ".
1165 aurelien 137
				"`".$departement."` = 1";
1170 aurelien 138
 
1165 aurelien 139
		$resultat = $this->getBdd()->recuperer($req);
140
		return $resultat;
141
	}
1103 mathias 142
 
1111 mathias 143
	protected function getInfosPresence($champ_nt_ou_nn, $nt_ou_nn) {
144
		$req = "SELECT " . $this->construireSommeColonnes() . " as nb_presence_zones".
145
				" FROM ".$this->table.
146
				" WHERE ".$champ_nt_ou_nn." = ".$this->getBdd()->proteger($nt_ou_nn);
1103 mathias 147
 
1111 mathias 148
		$resultat = $this->getBdd()->recuperer($req);
149
		return $resultat;
150
	}
1103 mathias 151
 
1111 mathias 152
	/**
153
	 * Appelle le WS sptb car la table ne contient pas toutes les infos (il faut
154
	 * agréger les taxons supérieurs)
155
	 */
156
	protected function getStatutsProtection($champ_nt_ou_nn, $nt_ou_nn) {
157
		$num_noms = array();
158
		if ($champ_nt_ou_nn == "num_tax") {
159
			// le service sptb n'accepte pas les nt (on croit que si mais en fait
160
			// non) alors on chope les nn associés à ce nt dans chorodep - c'est
161
			// moisi mais c'est toujours mieux que si c'était pire
162
			$numTaxP  = $this->getBdd()->proteger($nt_ou_nn);
163
			$req = "SELECT DISTINCT num_nom"
164
				. " FROM " . $this->table
165
				. " WHERE num_tax = $numTaxP";
1103 mathias 166
 
1111 mathias 167
			$resultat = $this->getBdd()->recupererTous($req);
168
			foreach($resultat as $res) {
169
				$num_noms[] = $res['num_nom'];
170
			}
171
		} else {
172
			$num_noms[] = $nt_ou_nn;
6 jpm 173
		}
1103 mathias 174
 
1111 mathias 175
		$statuts = array();
176
		// récupération des statuts pour chaque num_nom
177
		foreach ($num_noms as $nn) {
178
			$url = sprintf($this->urlStatutsProtection, $nn);
179
			$donnees = $this->getRestClient()->consulter($url);
180
			$donnees = json_decode($donnees, true);
181
			foreach ($donnees as $d) {
182
				$statuts[] = array(
183
					'zone' => $d['code_zone_application'],
184
					'lien' => $d['hyperlien_legifrance']
185
				);
186
			}
1100 mathias 187
		}
1103 mathias 188
 
1111 mathias 189
		return $statuts;
251 delphine 190
	}
1103 mathias 191
 
1111 mathias 192
	// @TODO faire un appel au WS nvjfl "for the sake of non-nodebagging" ?
193
	protected function getNomsVernaculaires($champ_nt_ou_nn, $nt_ou_nn) {
194
		$numP  = $this->getBdd()->proteger($nt_ou_nn);
195
		$req = "SELECT DISTINCT nv.nom_vernaculaire"
196
			. " FROM " . $this->tableNomsVernaculaires . " nv";
197
		if ($champ_nt_ou_nn == "num_nom") {
198
			$req .= " LEFT JOIN " . $this->table . " c ON nv.num_taxon = c.num_tax"
199
				. " WHERE c.num_nom = $numP";
200
		} else {
201
			$req .= " WHERE nv.num_taxon = $numP";
1105 mathias 202
		}
1111 mathias 203
		$req .= " AND nv.code_langue = 'fra'";
1103 mathias 204
 
205
		$resultat = $this->getBdd()->recupererTous($req);
206
 
1111 mathias 207
		$resultat_fmt = array();
208
		foreach($resultat as $nv) {
209
			$resultat_fmt[] = $nv['nom_vernaculaire'];
210
		}
1103 mathias 211
 
1111 mathias 212
		return $resultat_fmt;
6 jpm 213
	}
214
}
215
?>