Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 1111 | Rev 1170 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1111 Rev 1165
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
 
-
 
41
		$retour = null;
40
		$retour = null;
42
		if(preg_match("/^(nt|nn):([0-9]+)$/", $ressources[0], $matches)) {
41
		if(preg_match("/^(nt|nn):([0-9]+)$/", $ressources[0], $matches)) {
43
			$champ_nt_ou_nn = ($matches[1] == "nn") ? "num_nom" : "num_tax";
42
			$champ_nt_ou_nn = ($matches[1] == "nn") ? "num_nom" : "num_tax";
44
 
-
 
45
			if(count($ressources) == 1) {
43
			if(count($ressources) == 1) {
46
				// toutes les infos
44
				// toutes les infos
47
				$infos_espece = $this->getInfosEspece($champ_nt_ou_nn, $matches[2]);
45
				$infos_espece = $this->getInfosEspece($champ_nt_ou_nn, $matches[2]);
48
				$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]));
49
				$statuts_protection = array(
47
				$statuts_protection = array(
50
					'statuts_protection' => $this->getStatutsProtection($champ_nt_ou_nn, $matches[2])
48
					'statuts_protection' => $this->getStatutsProtection($champ_nt_ou_nn, $matches[2])
51
				);
49
				);
52
				$retour = array_merge($retour, $statuts_protection);
50
				$retour = array_merge($retour, $statuts_protection);
53
				$noms_vernaculaires = array(
51
				$noms_vernaculaires = array(
54
					'noms_vernaculaires_fr' => $this->getNomsVernaculaires($champ_nt_ou_nn, $matches[2])
52
					'noms_vernaculaires_fr' => $this->getNomsVernaculaires($champ_nt_ou_nn, $matches[2])
55
				);
53
				);
56
				$retour = array_merge($retour, $noms_vernaculaires);
54
				$retour = array_merge($retour, $noms_vernaculaires);
57
			} else {
55
			} else {
58
				// sous action du service
56
				// sous action du service
59
				$retour = array();
57
				$retour = array();
60
				switch($ressources[1]) {
58
				switch($ressources[1]) {
61
					case "noms-vernaculaires":
59
					case "noms-vernaculaires":
62
						$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]));
63
					break;
61
					break;
64
					case "statuts-protection":
62
					case "statuts-protection":
65
						$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]));
66
					break;
64
					break;
67
					case "presence":
65
					case "presence":
68
						$retour = $this->getInfosPresence($champ_nt_ou_nn, $matches[2]);
66
						$retour = $this->getInfosPresence($champ_nt_ou_nn, $matches[2]);
69
					break;
67
					break;
-
 
68
					case "presence-departement":
-
 
69
						$retour = $this->getInfosPresenceDepartement($champ_nt_ou_nn, $matches[2], $ressources[2]);
-
 
70
					break;
70
					default:
71
					default:
71
						$retour = "Actions possibles: noms-vernaculaires, statuts-protection, presence";
72
						$retour = "Actions possibles: noms-vernaculaires, statuts-protection, presence";
72
				} 	
73
				} 	
73
			}
74
			}
74
		} else {
75
		} else {
75
			// TODO : envoyer message erreur;
76
			// TODO : envoyer message erreur;
76
		}
77
		}
77
		return $retour;
78
		return $retour;
78
	}
79
	}
79
 
80
 
80
	/**
81
	/**
81
	 * Toutes les infos sauf noms vernaculaires et statuts de protection
82
	 * Toutes les infos sauf noms vernaculaires et statuts de protection
82
	 */
83
	 */
83
	protected function getInfosEspece($champ_nt_ou_nn, $nt_ou_nn) {
84
	protected function getInfosEspece($champ_nt_ou_nn, $nt_ou_nn) {
84
		$req = "SELECT DISTINCT num_nom, num_tax, nom_sci"
85
		$req = "SELECT DISTINCT num_nom, num_tax, nom_sci"
85
			. " FROM " . $this->table
86
			. " FROM " . $this->table
86
			. " WHERE $champ_nt_ou_nn = " . $this->getBdd()->proteger($nt_ou_nn);
87
			. " WHERE $champ_nt_ou_nn = " . $this->getBdd()->proteger($nt_ou_nn);
87
 
88
 
88
		$resultat = $this->getBdd()->recuperer($req);
89
		$resultat = $this->getBdd()->recuperer($req);
89
 
90
 
90
		return $resultat;
91
		return $resultat;
91
	}
92
	}
92
 
93
 
93
	/**
94
	/**
94
	 * 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
95
	 * chorodep représentant un département
96
	 * chorodep représentant un département
96
	 */
97
	 */
97
	protected function construireSommeColonnes() {
98
	protected function construireSommeColonnes() {
98
		$colonnes = array();
99
		$colonnes = array();
99
		for ($i=1; $i <= 95; $i++) {
100
		for ($i=1; $i <= 95; $i++) {
100
			$colonnes[] = '`' . ($i > 9 ? $i : "0$i") . '`';
101
			$colonnes[] = '`' . ($i > 9 ? $i : "0$i") . '`';
101
		}
102
		}
102
		$somme = implode('+', $colonnes);
103
		$somme = implode('+', $colonnes);
103
		return $somme;
104
		return $somme;
104
	}
105
	}
-
 
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
	}
105
 
123
 
106
	protected function getInfosPresence($champ_nt_ou_nn, $nt_ou_nn) {
124
	protected function getInfosPresence($champ_nt_ou_nn, $nt_ou_nn) {
107
		$req = "SELECT " . $this->construireSommeColonnes() . " as nb_presence_zones".
125
		$req = "SELECT " . $this->construireSommeColonnes() . " as nb_presence_zones".
108
				" FROM ".$this->table.
126
				" FROM ".$this->table.
109
				" WHERE ".$champ_nt_ou_nn." = ".$this->getBdd()->proteger($nt_ou_nn);
127
				" WHERE ".$champ_nt_ou_nn." = ".$this->getBdd()->proteger($nt_ou_nn);
110
 
128
 
111
		$resultat = $this->getBdd()->recuperer($req);
129
		$resultat = $this->getBdd()->recuperer($req);
112
		return $resultat;
130
		return $resultat;
113
	}
131
	}
114
 
132
 
115
	/**
133
	/**
116
	 * Appelle le WS sptb car la table ne contient pas toutes les infos (il faut
134
	 * Appelle le WS sptb car la table ne contient pas toutes les infos (il faut
117
	 * agréger les taxons supérieurs)
135
	 * agréger les taxons supérieurs)
118
	 */
136
	 */
119
	protected function getStatutsProtection($champ_nt_ou_nn, $nt_ou_nn) {
137
	protected function getStatutsProtection($champ_nt_ou_nn, $nt_ou_nn) {
120
		$num_noms = array();
138
		$num_noms = array();
121
		if ($champ_nt_ou_nn == "num_tax") {
139
		if ($champ_nt_ou_nn == "num_tax") {
122
			// le service sptb n'accepte pas les nt (on croit que si mais en fait
140
			// le service sptb n'accepte pas les nt (on croit que si mais en fait
123
			// non) alors on chope les nn associés à ce nt dans chorodep - c'est
141
			// non) alors on chope les nn associés à ce nt dans chorodep - c'est
124
			// moisi mais c'est toujours mieux que si c'était pire
142
			// moisi mais c'est toujours mieux que si c'était pire
125
			$numTaxP  = $this->getBdd()->proteger($nt_ou_nn);
143
			$numTaxP  = $this->getBdd()->proteger($nt_ou_nn);
126
			$req = "SELECT DISTINCT num_nom"
144
			$req = "SELECT DISTINCT num_nom"
127
				. " FROM " . $this->table
145
				. " FROM " . $this->table
128
				. " WHERE num_tax = $numTaxP";
146
				. " WHERE num_tax = $numTaxP";
129
 
147
 
130
			$resultat = $this->getBdd()->recupererTous($req);
148
			$resultat = $this->getBdd()->recupererTous($req);
131
			foreach($resultat as $res) {
149
			foreach($resultat as $res) {
132
				$num_noms[] = $res['num_nom'];
150
				$num_noms[] = $res['num_nom'];
133
			}
151
			}
134
		} else {
152
		} else {
135
			$num_noms[] = $nt_ou_nn;
153
			$num_noms[] = $nt_ou_nn;
136
		}
154
		}
137
 
155
 
138
		$statuts = array();
156
		$statuts = array();
139
		// récupération des statuts pour chaque num_nom
157
		// récupération des statuts pour chaque num_nom
140
		foreach ($num_noms as $nn) {
158
		foreach ($num_noms as $nn) {
141
			$url = sprintf($this->urlStatutsProtection, $nn);
159
			$url = sprintf($this->urlStatutsProtection, $nn);
142
			$donnees = $this->getRestClient()->consulter($url);
160
			$donnees = $this->getRestClient()->consulter($url);
143
			$donnees = json_decode($donnees, true);
161
			$donnees = json_decode($donnees, true);
144
			foreach ($donnees as $d) {
162
			foreach ($donnees as $d) {
145
				$statuts[] = array(
163
				$statuts[] = array(
146
					'zone' => $d['code_zone_application'],
164
					'zone' => $d['code_zone_application'],
147
					'lien' => $d['hyperlien_legifrance']
165
					'lien' => $d['hyperlien_legifrance']
148
				);
166
				);
149
			}
167
			}
150
		}
168
		}
151
 
169
 
152
		return $statuts;
170
		return $statuts;
153
	}
171
	}
154
 
172
 
155
	// @TODO faire un appel au WS nvjfl "for the sake of non-nodebagging" ?
173
	// @TODO faire un appel au WS nvjfl "for the sake of non-nodebagging" ?
156
	protected function getNomsVernaculaires($champ_nt_ou_nn, $nt_ou_nn) {
174
	protected function getNomsVernaculaires($champ_nt_ou_nn, $nt_ou_nn) {
157
		$numP  = $this->getBdd()->proteger($nt_ou_nn);
175
		$numP  = $this->getBdd()->proteger($nt_ou_nn);
158
		$req = "SELECT DISTINCT nv.nom_vernaculaire"
176
		$req = "SELECT DISTINCT nv.nom_vernaculaire"
159
			. " FROM " . $this->tableNomsVernaculaires . " nv";
177
			. " FROM " . $this->tableNomsVernaculaires . " nv";
160
		if ($champ_nt_ou_nn == "num_nom") {
178
		if ($champ_nt_ou_nn == "num_nom") {
161
			$req .= " LEFT JOIN " . $this->table . " c ON nv.num_taxon = c.num_tax"
179
			$req .= " LEFT JOIN " . $this->table . " c ON nv.num_taxon = c.num_tax"
162
				. " WHERE c.num_nom = $numP";
180
				. " WHERE c.num_nom = $numP";
163
		} else {
181
		} else {
164
			$req .= " WHERE nv.num_taxon = $numP";
182
			$req .= " WHERE nv.num_taxon = $numP";
165
		}
183
		}
166
		$req .= " AND nv.code_langue = 'fra'";
184
		$req .= " AND nv.code_langue = 'fra'";
167
 
185
 
168
		$resultat = $this->getBdd()->recupererTous($req);
186
		$resultat = $this->getBdd()->recupererTous($req);
169
 
187
 
170
		$resultat_fmt = array();
188
		$resultat_fmt = array();
171
		foreach($resultat as $nv) {
189
		foreach($resultat as $nv) {
172
			$resultat_fmt[] = $nv['nom_vernaculaire'];
190
			$resultat_fmt[] = $nv['nom_vernaculaire'];
173
		}
191
		}
174
 
192
 
175
		return $resultat_fmt;
193
		return $resultat_fmt;
176
	}
194
	}
177
}
195
}
178
?>
196
?>