Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 1168 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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