Subversion Repositories eFlore/Applications.cel

Rev

Rev 1426 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1425 aurelien 1
<?php
2
/**
3
* PHP Version 5
4
*
5
* @category  PHP
6
* @package   jrest
7
* @author    David Delon <david@tela-botania.org>
8
* @author    Aurélien Peronnet <aurelien@tela-botania.org>
9
* @copyright 2010 Tela-Botanica
10
* @license   http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL
11
* @version   SVN: <svn_id>
12
* @link      /doc/jrest/
13
*/
14
 
15
/**
16
 *
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
19
 */
20
class RechercheInfosTaxonBeta extends Cel {
21
 
22
	public function RechercheInfosTaxonBeta($config) {
23
		parent::__construct($config);
24
	}
25
 
26
	public function rechercherGenreEspeceSurPrefixe($genre = null, $espece = null) {
27
 
28
		$liste_genre_espece = array();
29
		$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');
31
 
32
		if($requete != '') {
33
			$requete = json_decode($requete);
34
			if(is_object($requete) && isset($requete->resultat)) {
35
				foreach ($requete->resultat as $id => $res) {
36
					$retenu = $res->retenu == true ? '3' : '';
37
					$liste_genre_espece[] = array($res->nom_sci_complet, $id, $retenu);
38
				}
39
			}
40
			usort($liste_genre_espece, array($this, 'comparerParNom'));
41
		}
42
		return $liste_genre_espece;
43
	}
44
 
45
	function comparerParNom($a, $b) {
46
		return strnatcmp($a[0], $b[0]);
47
	}
48
 
49
	public function effectuerRequeteInfosComplementairesEtFormaterNom($numNom) {
50
 
51
		$resultat_infos_complementaires = (array)$this->effectuerRequeteInfosComplementairesSurNumNom($numNom);
52
		$retour_infos_complementaires = array();
53
		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'])));
55
	    }
56
 
57
		return $retour_infos_complementaires;
58
	}
59
 
60
	public function rechercherInformationsComplementairesSurNom($nom_saisi) {
61
 
62
		$nameparser = new NameParser();
63
		$nom_decoupe = $nameparser->parse($nom_saisi);
64
 
65
		$masque = ((isset($nom_decoupe['genus']) && $nom_decoupe['genus']!= '') &&
66
				  (isset($nom_decoupe['species']) && $nom_decoupe['species']!= ''))
67
				  ? $nom_decoupe['genus'].' '.$nom_decoupe['species'] : $nom_saisi;
68
 
69
		$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=3000';
71
		$resultat = @file_get_contents($url);
72
		if($resultat != '') {
73
			$resultat = json_decode($resultat);
74
			if(is_object($resultat) && isset($resultat->resultat)) {
75
				foreach ($resultat->resultat as $id => $res) {
76
					$nom_complet = $res->{'nom_sci'}.' '.$res->auteur;
77
					$liste_genre_espece[] = array($res->{'nom_retenu.id'},$nom_complet);
78
				}
79
			}
80
		}
81
		return $liste_genre_espece;
82
	}
83
 
84
	public function rechercherInformationsComplementairesSurNumNom($num_nom) {
85
		$infos_formatees = array();
86
		$infos = $this->effectuerRequeteInfosComplementairesSurNumNom($num_nom);
87
		if(is_object($infos)) {
88
			$infos_formatees = $this->formaterInfosComplementairesSurNumNom($infos);
89
		}
90
		return $infos_formatees;
91
	}
92
 
93
	public function effectuerRequeteInfosComplementairesSurNumNom($num_nom) {
94
		$infos = array();
95
		//TODO: retourner moins de champs grâce au paramètre retour.champs
96
		$url = "http://www.tela-botanica.org/service:eflore:0.1/bdtfx/noms/".$num_nom;
97
		$resultat = @file_get_contents($url);
98
		if($resultat != '') {
99
			$infos = json_decode($resultat);
100
		}
101
		return $infos;
102
	}
103
 
104
	private function formaterInfosComplementairesSurNumNom($infos) {
105
		$infos = (array)$infos;
106
		return $infos_formatees = array(
107
			'Nom_Retenu' => $this->supprimerBiblio($infos['nom_retenu_complet']),
108
			'Num_Nom_Retenu' => $infos['nom_retenu.id'],
109
			'Num_Taxon' => $infos['num_taxonomique'],
110
			'Famille' => $infos['famille']
111
		);
112
	}
113
 
114
	private function supprimerBiblio($nom) {
115
		return preg_replace('/ \[.*\]/','',$nom);
116
	}
117
 
118
	public function rechercherNumTaxSurNumNom($num_nom) {
119
 
120
		$nt = null;
121
		$url = "http://www.tela-botanica.org/service:eflore:0.1/bdtfx/noms/".$num_nom.'?retour.champs=num_taxonomique';
122
		$resultat = @file_get_contents($url);
123
		if($resultat != '') {
124
			$infos = json_decode($resultat);
125
			$nt = $infos->num_taxonomique;
126
		}
127
 
128
		return $nt;
129
	}
130
 
131
	public function taxonEstPresentDansDepartement($num_taxon,$code_departement) {
132
		$presence_taxon = false;
133
		$url = 'http://localhost/service:eflore:0.1/chorodep/observations/?masque.departement='.$code_departement.'&masque.determination.nt='.$num_taxon.'&navigation.limite=1';
134
		$resultat = @file_get_contents($url);
135
		if($resultat != '') {
136
			$resultat = json_decode($resultat);
137
			if(is_object($resultat) && isset($resultat->resultat) && count($resultat->resultat) > 0) {
138
				$presence_taxon = true;
139
			}
140
		}
141
		return $presence_taxon;
142
	}
143
 
144
	public function effectuerRequeteInfosComplementairesSurNumTax($numTax) {
145
 
146
		$requete_infos_complementaires = "SELECT DISTINCT en_nom_genre, en_epithete_espece, en_nom_supra_generique, en_epithete_infra_generique,".
147
                    "   auteur_bex.enaia_intitule_abrege AS abreviation_auteur_basio_ex ".
148
                    " , auteur_b.enaia_intitule_abrege AS abreviation_auteur_basio ".
149
                    " , auteur_mex.enaia_intitule_abrege AS abreviation_auteur_modif_ex ".
150
                    " , auteur_m.enaia_intitule_abrege AS abreviation_auteur_modif ".
151
                    " , en_epithete_espece, en_epithete_infra_specifique, enrg_abreviation_rang, en_id_nom" .
152
                    " FROM eflore_nom, eflore_nom_rang," .
153
                    "     eflore_naturaliste_intitule_abreviation AS auteur_bex ".
154
                    "   , eflore_naturaliste_intitule_abreviation AS auteur_b ".
155
                    "   , eflore_naturaliste_intitule_abreviation AS auteur_mex ".
156
                    "   , eflore_naturaliste_intitule_abreviation AS auteur_m ".
157
                    " , eflore_selection_nom ".
158
                    " WHERE esn_id_taxon = '".$numTax. "'".
159
                    " AND esn_id_version_projet_taxon = 25 ".
160
                    " AND esn_ce_statut=3 ".
161
                    " AND en_id_nom = esn_id_nom" .
162
                    " AND en_ce_rang = enrg_id_rang" .
163
                    " AND en_ce_auteur_basio_ex = auteur_bex.enaia_id_intitule_naturaliste_abrege ".
164
                    " AND en_ce_auteur_basio = auteur_b.enaia_id_intitule_naturaliste_abrege  ".
165
                    " AND en_ce_auteur_modif_ex = auteur_mex.enaia_id_intitule_naturaliste_abrege ".
166
                    " AND en_ce_auteur_modif = auteur_m.enaia_id_intitule_naturaliste_abrege ".
167
                    " AND esn_id_version_projet_taxon=en_id_version_projet_nom ";
168
 
169
		$resultat_infos_complementaires = $this->executerRequete($requete_infos_complementaires);
170
 
171
		return $resultat_infos_complementaires;
172
	}
173
 
174
	public function rechercherFamille($taxon) {
175
 
176
		$row = array();
177
 
178
		$requete_famille = "SELECT DISTINCT en_ce_rang, etr_id_taxon_2, en_id_nom, en_nom_supra_generique ".
179
		" FROM eflore_taxon_relation, ".
180
		" eflore_selection_nom, ".
181
		" eflore_nom ".
182
		" WHERE etr_id_taxon_1 = ".$taxon.
183
		" AND etr_id_version_projet_taxon_1 = 25 ".
184
		" AND etr_id_categorie_taxon = 3 ".
185
		" AND etr_id_valeur_taxon = 3 ".
186
		" AND esn_id_taxon =  etr_id_taxon_2 ".
187
		" AND esn_ce_statut = 3 ".
188
		" AND esn_id_version_projet_taxon = etr_id_version_projet_taxon_1 ".
189
		" AND en_id_nom = esn_id_nom ".
190
		" AND esn_id_version_projet_taxon=en_id_version_projet_nom  ";
191
 
192
		$resultat_recherche_famille = $this->executerRequete($requete_famille);
193
 
194
		if (!is_array($resultat_recherche_famille) || count($resultat_recherche_famille) == 0) {
195
	    	$resultat_recherche_famille = array('en_ce_rang' => 'fin');
196
	    } else {
197
	    	$resultat_recherche_famille = $resultat_recherche_famille[0];
198
	    }
199
 
200
		return $resultat_recherche_famille;
201
	}
202
 
203
	private function decouperNomEtRechercheEspeceOuSousEspece($identifiant_espece) {
204
		$nameparser=new NameParser();
205
		$nom_latin_decoupe=$nameparser->parse($identifiant_espece);
206
		// requete sous espece (on privilegie les noms retenu cf tri par esn_ce_statut)
207
		if (isset($nom_latin_decoupe['infra']) && $nom_latin_decoupe['infra']!="") {
208
			$requete="SELECT DISTINCT en_id_nom, esn_ce_statut" .
209
            	" FROM eflore_nom, eflore_nom_rang, eflore_selection_nom " .
210
            	" WHERE en_id_version_projet_nom = '25' AND en_nom_genre = ".$this->proteger($nom_latin_decoupe['genus'])." " .
211
                " AND enrg_abreviation_rang = ".$this->proteger($nom_latin_decoupe['infra_type'])." " .
212
                " AND en_epithete_infra_specifique = ".$this->proteger($nom_latin_decoupe['infra'])." " .
213
                " AND esn_id_nom= en_id_nom ".
214
                " AND esn_id_version_projet_taxon=en_id_version_projet_nom " .
215
                " AND en_epithete_espece =  ".$this->proteger($nom_latin_decoupe['species'])." AND en_ce_rang = enrg_id_rang " .
216
                " ORDER BY esn_ce_statut ".
217
                " LIMIT 1";
218
		}
219
		else { // espece  (on privilegie les noms retenu cf tri par esn_ce_statut)
220
			$requete="SELECT DISTINCT en_id_nom, esn_ce_statut" .
221
				" FROM eflore_nom, eflore_nom_rang, eflore_selection_nom " .
222
				" WHERE en_id_version_projet_nom = '25' AND en_nom_genre = ".$this->proteger($nom_latin_decoupe['genus'])." " .
223
				" AND enrg_abreviation_rang = 'sp.' " .
224
				" AND esn_id_nom= en_id_nom ".
225
				" AND esn_id_version_projet_taxon=en_id_version_projet_nom " .
226
				" AND en_epithete_espece =  ".$this->proteger($nom_latin_decoupe['species'])." AND en_ce_rang = enrg_id_rang " .
227
				" ORDER BY esn_ce_statut ".
228
				" LIMIT 1";
229
 
230
		}
231
 
232
		$resultat = $this->executerRequete($requete);
233
 
234
		$retour = array();
235
		if (is_array($resultat) && count($resultat) > 0) {
236
			$retour = $resultat[0];
237
		}
238
 
239
		return $retour;
240
	}
241
 
242
	private function formaterNom($rawnom) {
243
 
244
        // Constitution du nom:
245
        $nom = '';
246
 
247
        if (isset($rawnom['en_nom_supra_generique']) && $rawnom['en_nom_supra_generique'] != '') {
248
            $nom .= $rawnom['en_nom_supra_generique'];
249
        } else if (isset($rawnom['en_epithete_infra_generique']) && $rawnom['en_epithete_infra_generique'] != '') {
250
            $nom .= $rawnom['en_epithete_infra_generique'];
251
        } else {
252
            if (isset($rawnom['en_nom_genre']) &&  $rawnom['en_nom_genre'] != '') {
253
                $nom .=  $rawnom['en_nom_genre'];
254
            }
255
            if (isset($rawnom['en_epithete_espece']) &&  $rawnom['en_epithete_espece']!= '') {
256
                $nom .= ' '.$rawnom['en_epithete_espece'];
257
            }
258
            if (isset($rawnom['en_epithete_infra_specifique']) &&  $rawnom['en_epithete_infra_specifique'] != '') {
259
                if (!empty($rawnom['enrg_abreviation_rang'])) {
260
                        $nom .= ' '.$rawnom['enrg_abreviation_rang'].'';
261
                }
262
                $nom .= ' '.$rawnom['en_epithete_infra_specifique'];
263
            }
264
        }
265
 
266
        return $nom.$this->retournerAuteur($rawnom) ;
267
 	}
268
 
269
	private function retournerAuteur($rawnom) {
270
		$auteurs = '';
271
		$auteur_basio = '';
272
		$auteur_modif = '';
273
		if (!empty($rawnom['abreviation_auteur_basio_ex']) && $rawnom['abreviation_auteur_basio_ex']!='-' )  {
274
		    $auteur_basio .= $rawnom['abreviation_auteur_basio_ex'];
275
		    if (!empty($rawnom['abreviation_auteur_basio']) && $rawnom['abreviation_auteur_basio']!='-') {
276
		        $auteur_basio .= ' ex '.$rawnom['abreviation_auteur_basio'];
277
		    }
278
		} else if (!empty($rawnom['abreviation_auteur_basio']) && $rawnom['abreviation_auteur_basio']!='-') {
279
		    $auteur_basio .= $rawnom['abreviation_auteur_basio'];
280
		}
281
 
282
		if (!empty($rawnom['abreviation_auteur_modif_ex']) && $rawnom['abreviation_auteur_modif_ex']!='-') {
283
		    $auteur_modif .= $rawnom['abreviation_auteur_modif_ex'];
284
		    if (!empty($rawnom['abreviation_auteur_modif']) && $rawnom['abreviation_auteur_modif']!='-') {
285
		        $auteur_modif .= ' ex '.$rawnom['abreviation_auteur_modif'];
286
		    }
287
		} else if (!empty($rawnom['abreviation_auteur_modif']) && $rawnom['abreviation_auteur_modif']!='-')  {
288
		    $auteur_modif .= $rawnom['abreviation_auteur_modif'];
289
		}
290
 
291
		if (!empty($auteur_modif)) {
292
		    $auteurs = ' ('.$auteur_basio.') '.$auteur_modif;
293
		} elseif (!empty($auteur_basio)) {
294
		    $auteurs = ' '.$auteur_basio;
295
		}
296
 
297
		return $auteurs ;
298
	}
299
 
300
	function rechercherInfosSurTexteCodeOuNumTax($identifiant_espece) {
301
		// texte libre, nom scientifique,
302
		// ou code nomenclatural (format BDNFFnn999999)
303
		// ou code taxonomique (format BDNFFnt999999)
304
		$identifiant_espece=trim($identifiant_espece);
305
		$identifiant_espece=utf8_encode($identifiant_espece);
306
 
307
		$retour = array();
308
 
309
		preg_match('/BDNFFnn([0-9][0-9]*)/',$identifiant_espece, $elements);
310
		if (isset($elements[1])) {
311
			// Numero nomenclatural
312
			$infos_taxon = $this->rechercherInformationsComplementairesSurNumNom($elements[1]);
313
			$retour = array("nom_sel" => $this->formaterNom($infos_taxon), "en_id_nom" => $elements[1]);
314
		} else {
315
			//  Numero taxonomique ou nom scientifique
316
			preg_match('/BDNFFnt([0-9][0-9]*)/', $identifiant_espece, $elements);
317
 
318
			if (isset($elements[1])) {
319
				// Numero taxonomique
320
				$infos_taxon = $this->effectuerRequeteInfosComplementairesSurNumTax($elements[1]);
321
				$infos_taxon = $infos_taxon[0];
322
				$retour = array("nom_sel" => $this->formaterNom($infos_taxon), "en_id_nom" => $infos_taxon['en_id_nom']);
323
			} else {
324
				// Nom scientifique
325
				$id_nom = $this->decouperNomEtRechercheEspeceOuSousEspece($identifiant_espece);
326
				// Recherche du nom associe
327
				$retour = array("nom_sel" => $identifiant_espece);
328
				if(is_array($id_nom) && isset($id_nom['en_id_nom'])) {
329
					$infos_nom = $this->effectuerRequeteInfosComplementairesSurNumNom($id_nom['en_id_nom']);
330
					if (is_array($infos_nom) && !empty($infos_nom)) {
331
						$infos_nom = $infos_nom[0];
332
						$retour = array("nom_sel" => $this->formaterNom($infos_nom), "en_id_nom" => $id_nom['en_id_nom']);
333
					}
334
				}
335
			}
336
		}
337
 
338
		return $retour;
339
	}
340
}
341
?>