Subversion Repositories eFlore/Applications.cel

Rev

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

Rev 1426 Rev 1427
1
<?php
1
<?php
2
/**
2
/**
3
* PHP Version 5
3
* PHP Version 5
4
*
4
*
5
* @category  PHP
5
* @category  PHP
6
* @package   jrest
6
* @package   jrest
7
* @author    David Delon <david@tela-botania.org>
7
* @author    David Delon <david@tela-botania.org>
8
* @author    Aurélien Peronnet <aurelien@tela-botania.org>
8
* @author    Aurélien Peronnet <aurelien@tela-botania.org>
9
* @copyright 2010 Tela-Botanica
9
* @copyright 2010 Tela-Botanica
10
* @license   http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL
10
* @license   http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL
11
* @version   SVN: <svn_id>
11
* @version   SVN: <svn_id>
12
* @link      /doc/jrest/
12
* @link      /doc/jrest/
13
*/
13
*/
14
 
14
 
15
/**
15
/**
16
 * 
16
 * 
17
 * La classe appelle les web service d'eflore pour éviter que le code client 
17
 * La classe appelle les web service d'eflore pour éviter que le code client 
18
 * ne soit dépendant de la version d'eflore
18
 * ne soit dépendant de la version d'eflore
19
 */
19
 */
20
class RechercheInfosTaxonBeta extends Cel {
20
class RechercheInfosTaxonBeta extends Cel {
-
 
21
	
-
 
22
	private $url_service_nom = null;
-
 
23
	private $url_service_taxon = null;
-
 
24
	private $url_service_chorologie = null;
21
	
25
	
22
	public function RechercheInfosTaxonBeta($config) {		
26
	public function RechercheInfosTaxonBeta($config) {		
-
 
27
		parent::__construct($config);
-
 
28
		$this->url_service_nom = $config['eflore']['url_service_nom'];
-
 
29
		$this->url_service_taxon = $config['eflore']['url_service_taxon'];
23
		parent::__construct($config);
30
		$this->url_service_chorologie_obs = $config['eflore']['url_service_chorologie_obs'];
24
	}
31
	}
25
	
32
	
26
	public function rechercherGenreEspeceSurPrefixe($genre = null, $espece = null) {
33
	public function rechercherGenreEspeceSurPrefixe($genre = null, $espece = null) {
27
		
34
		
28
		$liste_genre_espece = array();	
35
		$liste_genre_espece = array();	
29
		$masque = urlencode(trim(trim($genre).' '.trim($espece,' *')));
36
		$masque = urlencode(trim(trim($genre).' '.trim($espece,' *')));
30
		$requete = @file_get_contents('http://localhost/service:eflore:0.1/bdtfx/noms?masque='.$masque.'&recherche=etendue&retour.format=min&navigation.limite=50&ns.structure=au');
37
		$requete = @file_get_contents($this->url_service_nom.'?masque='.$masque.'&recherche=etendue&retour.format=min&navigation.limite=50&ns.structure=au');
31
		
38
		
32
		if($requete != '') {
39
		if($requete != '') {
33
			$requete = json_decode($requete);		
40
			$requete = json_decode($requete);		
34
			if(is_object($requete) && isset($requete->resultat)) {
41
			if(is_object($requete) && isset($requete->resultat)) {
35
				foreach ($requete->resultat as $id => $res) {
42
				foreach ($requete->resultat as $id => $res) {
36
					$retenu = $res->retenu == true ? '3' : '';
43
					$retenu = ($res->retenu == "true") ? '3' : '4';
37
					$liste_genre_espece[] = array($res->nom_sci_complet, $id, $retenu);
44
					$liste_genre_espece[] = array($res->nom_sci_complet, $id, $retenu);
38
				}
45
				}
39
			}
46
			}
40
			usort($liste_genre_espece, array($this, 'comparerParNom'));  
47
			usort($liste_genre_espece, array($this, 'comparerParRetenuPuisNom'));  
41
		}
48
		}
42
		return $liste_genre_espece;
49
		return $liste_genre_espece;
43
	}
50
	}
44
	
51
	
-
 
52
	function comparerParRetenuPuisNom($a, $b) {
-
 
53
		if($a[2] == 3 && $b[2] != 3) {
-
 
54
			return -1;
-
 
55
		} elseif($a[2] != 3 && $b[2] == 3) {
-
 
56
			return 1;
45
	function comparerParNom($a, $b) {
57
		} else {
-
 
58
			return strnatcmp($a[0], $b[0]);
46
		return strnatcmp($a[0], $b[0]); 
59
		}
47
	}
60
	}
48
	
61
	
49
	public function effectuerRequeteInfosComplementairesEtFormaterNom($numNom) {
62
	public function effectuerRequeteInfosComplementairesEtFormaterNom($numNom) {
50
		
63
		
51
		$resultat_infos_complementaires = (array)$this->effectuerRequeteInfosComplementairesSurNumNom($numNom);
64
		$resultat_infos_complementaires = (array)$this->effectuerRequeteInfosComplementairesSurNumNom($numNom);
52
		$retour_infos_complementaires = array();
65
		$retour_infos_complementaires = array();
53
		if (isset($resultat_infos_complementaires['nom_retenu_complet']) && $resultat_infos_complementaires['nom_retenu_complet'] != '') {
66
		if (isset($resultat_infos_complementaires['nom_retenu_complet']) && $resultat_infos_complementaires['nom_retenu_complet'] != '') {
54
           	$retour_infos_complementaires=array(($this->supprimerBiblio($resultat_infos_complementaires['nom_retenu_complet'])));
67
           	$retour_infos_complementaires=array(($this->supprimerBiblio($resultat_infos_complementaires['nom_retenu_complet'])));
55
	    }
68
	    }
56
		
69
		
57
		return $retour_infos_complementaires;
70
		return $retour_infos_complementaires;
58
	}
71
	}
59
	
72
	
60
	public function rechercherInformationsComplementairesSurNom($nom_saisi) {
73
	public function rechercherInformationsComplementairesSurNom($nom_saisi) {
61
		
74
		
62
		$nameparser = new NameParser();
75
		$nameparser = new NameParser();
63
		$nom_decoupe = $nameparser->parse($nom_saisi);
76
		$nom_decoupe = $nameparser->parse($nom_saisi);
64
		
77
		
65
		$masque = ((isset($nom_decoupe['genus']) && $nom_decoupe['genus']!= '') && 
78
		$masque = ((isset($nom_decoupe['genus']) && $nom_decoupe['genus']!= '') && 
66
				  (isset($nom_decoupe['species']) && $nom_decoupe['species']!= ''))
79
				  (isset($nom_decoupe['species']) && $nom_decoupe['species']!= ''))
67
				  ? $nom_decoupe['genus'].' '.$nom_decoupe['species'] : $nom_saisi;
80
				  ? $nom_decoupe['genus'].' '.$nom_decoupe['species'] : $nom_saisi;
68
		
81
		
69
		$liste_genre_espece = array();
82
		$liste_genre_espece = array();
70
		$url = 'http://www.tela-botanica.org/service:eflore:0.1/bdtfx/noms?masque='.urlencode($nom_decoupe['genus'].' '.$nom_decoupe['species']).'&recherche=etendue&ns.format=txt&retour.champs=nom_retenu,nom_retenu.*,num_taxonomique,auteur&navigation.limite=1';
83
		$url = $this->url_service_nom.'?masque='.urlencode($nom_decoupe['genus'].' '.$nom_decoupe['species']).'&recherche=etendue&ns.format=txt&retour.champs=nom_retenu,nom_retenu.*,num_taxonomique,auteur&navigation.limite=1';
71
 
84
 
72
		$resultat = @file_get_contents($url);
85
		$resultat = @file_get_contents($url);
73
		if($resultat != '') {
86
		if($resultat != '') {
74
			$resultat = json_decode($resultat);
87
			$resultat = json_decode($resultat);
75
			if(is_object($resultat) && isset($resultat->resultat)) {
88
			if(is_object($resultat) && isset($resultat->resultat)) {
76
				foreach ($resultat->resultat as $id => $res) {
89
				foreach ($resultat->resultat as $id => $res) {
77
					$nom_complet = $res->{'nom_sci'}.' '.$res->auteur;
90
					$nom_complet = $res->{'nom_sci'}.' '.$res->auteur;
78
					$liste_genre_espece[] = array($res->{'nom_retenu.id'},$nom_complet);
91
					$liste_genre_espece[] = array($res->{'nom_retenu.id'},$nom_complet);
79
				}
92
				}
80
			}
93
			}
81
		}
94
		}
82
		return $liste_genre_espece;
95
		return $liste_genre_espece;
83
	}
96
	}
84
	
97
	
85
	public function rechercherInformationsComplementairesSurNumNom($num_nom) {
98
	public function rechercherInformationsComplementairesSurNumNom($num_nom) {
86
		$infos_formatees = array();
99
		$infos_formatees = array();
87
		$infos = $this->effectuerRequeteInfosComplementairesSurNumNom($num_nom);
100
		$infos = $this->effectuerRequeteInfosComplementairesSurNumNom($num_nom);
88
		if(is_object($infos)) {
101
		if(is_object($infos)) {
89
			$infos_formatees = $this->formaterInfosComplementairesSurNumNom($infos);
102
			$infos_formatees = $this->formaterInfosComplementairesSurNumNom($infos);
90
		}
103
		}
91
		return $infos_formatees;
104
		return $infos_formatees;
92
	}
105
	}
93
	
106
	
94
	public function effectuerRequeteInfosComplementairesSurNumNom($num_nom) {
107
	public function effectuerRequeteInfosComplementairesSurNumNom($num_nom) {
95
		$infos = array();
108
		$infos = array();
96
		//TODO: retourner moins de champs grâce au paramètre retour.champs
109
		//TODO: retourner moins de champs grâce au paramètre retour.champs
97
		$url = "http://www.tela-botanica.org/service:eflore:0.1/bdtfx/noms/".$num_nom;
110
		$url = $this->url_service_nom.'/'.$num_nom;
98
		$resultat = @file_get_contents($url);
111
		$resultat = @file_get_contents($url);
99
		if($resultat != '') {
112
		if($resultat != '') {
100
			$infos = json_decode($resultat);
113
			$infos = json_decode($resultat);
101
		}
114
		}
102
		return $infos;
115
		return $infos;
103
	}
116
	}
104
	
117
	
105
	private function formaterInfosComplementairesSurNumNom($infos) {
118
	private function formaterInfosComplementairesSurNumNom($infos) {
106
		$infos = (array)$infos;
119
		$infos = (array)$infos;
107
		return $infos_formatees = array(
120
		return $infos_formatees = array(
108
			'Nom_Retenu' => $this->supprimerBiblio($infos['nom_retenu_complet']),
121
			'Nom_Retenu' => $this->supprimerBiblio($infos['nom_retenu_complet']),
109
			'Num_Nom_Retenu' => $infos['nom_retenu.id'],
122
			'Num_Nom_Retenu' => $infos['nom_retenu.id'],
110
			'Num_Taxon' => $infos['num_taxonomique'],
123
			'Num_Taxon' => $infos['num_taxonomique'],
111
			'Famille' => $infos['famille']
124
			'Famille' => $infos['famille']
112
		);
125
		);
113
	}
126
	}
114
	
127
	
115
	private function supprimerBiblio($nom) {
128
	private function supprimerBiblio($nom) {
116
		return preg_replace('/ \[.*\]/','',$nom);
129
		return preg_replace('/ \[.*\]/','',$nom);
117
	}
130
	}
118
	
131
	
119
	public function rechercherNumTaxSurNumNom($num_nom) {
132
	public function rechercherNumTaxSurNumNom($num_nom) {
120
	
133
	
121
		$nt = null;
134
		$nt = null;
122
		$url = "http://www.tela-botanica.org/service:eflore:0.1/bdtfx/noms/".$num_nom.'?retour.champs=num_taxonomique';
135
		$url = $this->url_service_nom."/".$num_nom.'?retour.champs=num_taxonomique';
123
		$resultat = @file_get_contents($url);
136
		$resultat = @file_get_contents($url);
124
		if($resultat != '') {
137
		if($resultat != '') {
125
			$infos = json_decode($resultat);
138
			$infos = json_decode($resultat);
126
			$nt = $infos->num_taxonomique;
139
			$nt = $infos->num_taxonomique;
127
		}
140
		}
128
	
141
	
129
		return $nt;
142
		return $nt;
130
	}
143
	}
131
	
144
	
132
	public function taxonEstPresentDansDepartement($num_taxon,$code_departement) {
145
	public function taxonEstPresentDansDepartement($num_taxon,$code_departement) {
133
		$presence_taxon = false;
146
		$presence_taxon = false;
134
		$url = 'http://localhost/service:eflore:0.1/chorodep/observations/?masque.departement='.$code_departement.'&masque.determination.nt='.$num_taxon.'&navigation.limite=1';
147
		$url = $this->url_service_chorologie_obs.'?masque.departement='.$code_departement.'&masque.determination.nt='.$num_taxon.'&navigation.limite=1';
135
		$resultat = @file_get_contents($url);
148
		$resultat = @file_get_contents($url);
136
		if($resultat != '') {
149
		if($resultat != '') {
137
			$resultat = json_decode($resultat);
150
			$resultat = json_decode($resultat);
138
			if(is_object($resultat) && isset($resultat->resultat) && count($resultat->resultat) > 0) {
151
			if(is_object($resultat) && isset($resultat->resultat) && count($resultat->resultat) > 0) {
139
				$presence_taxon = true;
152
				$presence_taxon = true;
140
			}
153
			}
141
		}
154
		}
142
		return $presence_taxon;
155
		return $presence_taxon;
143
	}
156
	}
144
	
157
	
145
	public function effectuerRequeteInfosComplementairesSurNumTax($numTax) {
158
	public function effectuerRequeteInfosComplementairesSurNumTax($numTax) {
146
		
159
		
147
		$infos = array();
160
		$infos = array();
148
		//TODO: retourner moins de champs grâce au paramètre retour.champs
161
		//TODO: retourner moins de champs grâce au paramètre retour.champs
149
		$url = "http://www.tela-botanica.org/service:eflore:0.1/bdtfx/taxons/nt:".$numTax;
162
		$url = $this->url_service_taxon."/nt:".$numTax;
150
		$resultat = @file_get_contents($url);
163
		$resultat = @file_get_contents($url);
151
		if($resultat != '') {
164
		if($resultat != '') {
152
			$infos = json_decode($resultat);
165
			$infos = json_decode($resultat);
153
		}
166
		}
154
 
167
 
155
		return $infos;
168
		return $infos;
156
	}
169
	}
157
	
-
 
158
	private function decouperNomEtRechercheEspeceOuSousEspece($identifiant_espece) {
-
 
159
		$nameparser=new NameParser();
-
 
160
		$nom_latin_decoupe=$nameparser->parse($identifiant_espece);
-
 
161
		// requete sous espece (on privilegie les noms retenu cf tri par esn_ce_statut)
-
 
162
		if (isset($nom_latin_decoupe['infra']) && $nom_latin_decoupe['infra']!="") {
-
 
163
			$requete="SELECT DISTINCT en_id_nom, esn_ce_statut" .
-
 
164
            	" FROM eflore_nom, eflore_nom_rang, eflore_selection_nom " .
-
 
165
            	" WHERE en_id_version_projet_nom = '25' AND en_nom_genre = ".$this->proteger($nom_latin_decoupe['genus'])." " .
-
 
166
                " AND enrg_abreviation_rang = ".$this->proteger($nom_latin_decoupe['infra_type'])." " .
-
 
167
                " AND en_epithete_infra_specifique = ".$this->proteger($nom_latin_decoupe['infra'])." " .
-
 
168
                " AND esn_id_nom= en_id_nom ".
-
 
169
                " AND esn_id_version_projet_taxon=en_id_version_projet_nom " .
-
 
170
                " AND en_epithete_espece =  ".$this->proteger($nom_latin_decoupe['species'])." AND en_ce_rang = enrg_id_rang " .
-
 
171
                " ORDER BY esn_ce_statut ".
-
 
172
                " LIMIT 1";
-
 
173
		}
-
 
174
		else { // espece  (on privilegie les noms retenu cf tri par esn_ce_statut)
-
 
175
			$requete="SELECT DISTINCT en_id_nom, esn_ce_statut" .
-
 
176
				" FROM eflore_nom, eflore_nom_rang, eflore_selection_nom " .
-
 
177
				" WHERE en_id_version_projet_nom = '25' AND en_nom_genre = ".$this->proteger($nom_latin_decoupe['genus'])." " .
-
 
178
				" AND enrg_abreviation_rang = 'sp.' " .
-
 
179
				" AND esn_id_nom= en_id_nom ".
-
 
180
				" AND esn_id_version_projet_taxon=en_id_version_projet_nom " .
-
 
181
				" AND en_epithete_espece =  ".$this->proteger($nom_latin_decoupe['species'])." AND en_ce_rang = enrg_id_rang " .
-
 
182
				" ORDER BY esn_ce_statut ".
-
 
183
				" LIMIT 1";
-
 
184
		
-
 
185
		}
-
 
186
		
-
 
187
		$resultat = $this->executerRequete($requete);
-
 
188
		
-
 
189
		$retour = array();
-
 
190
		if (is_array($resultat) && count($resultat) > 0) {
-
 
191
			$retour = $resultat[0];
-
 
192
		}
-
 
193
		
-
 
194
		return $retour;
-
 
195
	}
-
 
196
	
170
	
197
	function rechercherInfosSurTexteCodeOuNumTax($identifiant_espece) {
171
	function rechercherInfosSurTexteCodeOuNumTax($identifiant_espece) {
198
		// texte libre, nom scientifique, 
172
		// texte libre, nom scientifique, 
199
		// ou code nomenclatural (format BDNFFnn999999) 
173
		// ou code nomenclatural (format BDNFFnn999999) 
200
		// ou code taxonomique (format BDNFFnt999999)
174
		// ou code taxonomique (format BDNFFnt999999)
201
		$identifiant_espece=trim($identifiant_espece);
175
		$identifiant_espece=trim($identifiant_espece);
202
		$identifiant_espece=utf8_encode($identifiant_espece);
176
		$identifiant_espece=utf8_encode($identifiant_espece);
203
		
177
		
204
		$retour = array();
178
		$retour = array();
205
		
179
		
206
		preg_match('/BDNFFnn([0-9][0-9]*)/',$identifiant_espece, $elements);
180
		preg_match('/BDNFFnn([0-9][0-9]*)/',$identifiant_espece, $elements);
207
		if (isset($elements[1])) {
181
		if (isset($elements[1])) {
208
			// Numero nomenclatural
182
			// Numero nomenclatural
209
			$infos_taxon = $this->rechercherInformationsComplementairesSurNumNom($elements[1]);
183
			$infos_taxon = $this->rechercherInformationsComplementairesSurNumNom($elements[1]);
210
			$retour = array("nom_sel" => $infos_taxon['Nom_Retenu'], "en_id_nom" => $elements[1]);
184
			$retour = array("nom_sel" => $infos_taxon['Nom_Retenu'], "en_id_nom" => $elements[1]);
211
		} else { 
185
		} else { 
212
			//  Numero taxonomique ou nom scientifique
186
			//  Numero taxonomique ou nom scientifique
213
			preg_match('/BDNFFnt([0-9][0-9]*)/', $identifiant_espece, $elements);
187
			preg_match('/BDNFFnt([0-9][0-9]*)/', $identifiant_espece, $elements);
214
			if (isset($elements[1])) {
188
			if (isset($elements[1])) {
215
				// Numero taxonomique
189
				// Numero taxonomique
216
				$infos_taxon = (array)$this->effectuerRequeteInfosComplementairesSurNumTax($elements[1]);
190
				$infos_taxon = (array)$this->effectuerRequeteInfosComplementairesSurNumTax($elements[1]);
217
				if(isset($infos_taxon['nom_retenu.libelle']) && isset($infos_taxon['id'])) {
191
				if(isset($infos_taxon['nom_retenu.libelle']) && isset($infos_taxon['id'])) {
218
					$nom = $infos_taxon['nom_retenu.libelle'];
192
					$nom = $infos_taxon['nom_retenu.libelle'];
219
					$nom .= (isset($infos_taxon['auteur'])) ? ' '.$infos_taxon['auteur'] : '';
193
					$nom .= (isset($infos_taxon['auteur'])) ? ' '.$infos_taxon['auteur'] : '';
220
					$retour = array("nom_sel" => $nom, 
194
					$retour = array("nom_sel" => $nom, 
221
									"en_id_nom" => $infos_taxon['id']);
195
									"en_id_nom" => $infos_taxon['id']);
222
				}
196
				}
223
			} else { 
197
			} else { 
224
				// Nom scientifique
198
				// Nom scientifique
225
				$id_nom = $this->rechercherInformationsComplementairesSurNom($identifiant_espece);
199
				$id_nom = $this->rechercherInformationsComplementairesSurNom($identifiant_espece);
226
				// Recherche du nom associe
200
				// Recherche du nom associe
227
				$retour = array("nom_sel" => $identifiant_espece);
201
				$retour = array("nom_sel" => $identifiant_espece);
228
				if(is_array($id_nom) && count($id_nom) > 0 && isset($id_nom[0][0])) {
202
				if(is_array($id_nom) && count($id_nom) > 0 && isset($id_nom[0][0])) {
229
					$nn = $id_nom[0][0];
203
					$nn = $id_nom[0][0];
230
					$infos_nom = (array)$this->effectuerRequeteInfosComplementairesSurNumNom($nn);
204
					$infos_nom = (array)$this->effectuerRequeteInfosComplementairesSurNumNom($nn);
231
					if (is_array($infos_nom) && !empty($infos_nom)) {
205
					if (is_array($infos_nom) && !empty($infos_nom)) {
232
						$retour = array("nom_sel" => $infos_nom['nom_sci'].' '.$infos_nom['auteur'], "en_id_nom" => $infos_nom['id']);
206
						$retour = array("nom_sel" => $infos_nom['nom_sci'].' '.$infos_nom['auteur'], "en_id_nom" => $infos_nom['id']);
233
					}
207
					}
234
				}
208
				}
235
			}
209
			}
236
		}
210
		}
237
		
211
		
238
		return $retour;
212
		return $retour;
239
	}
213
	}
240
}
214
}
241
?>
215
?>