Subversion Repositories eFlore/Applications.cel

Rev

Rev 1427 | Go to most recent revision | Details | Compare with Previous | 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
 
1427 aurelien 22
	private $url_service_nom = null;
23
	private $url_service_taxon = null;
24
	private $url_service_chorologie = null;
25
 
1425 aurelien 26
	public function RechercheInfosTaxonBeta($config) {
27
		parent::__construct($config);
1427 aurelien 28
		$this->url_service_nom = $config['eflore']['url_service_nom'];
29
		$this->url_service_taxon = $config['eflore']['url_service_taxon'];
30
		$this->url_service_chorologie_obs = $config['eflore']['url_service_chorologie_obs'];
1425 aurelien 31
	}
32
 
33
	public function rechercherGenreEspeceSurPrefixe($genre = null, $espece = null) {
34
 
35
		$liste_genre_espece = array();
36
		$masque = urlencode(trim(trim($genre).' '.trim($espece,' *')));
1427 aurelien 37
		$requete = @file_get_contents($this->url_service_nom.'?masque='.$masque.'&recherche=etendue&retour.format=min&navigation.limite=50&ns.structure=au');
1425 aurelien 38
 
39
		if($requete != '') {
40
			$requete = json_decode($requete);
41
			if(is_object($requete) && isset($requete->resultat)) {
42
				foreach ($requete->resultat as $id => $res) {
1427 aurelien 43
					$retenu = ($res->retenu == "true") ? '3' : '4';
1425 aurelien 44
					$liste_genre_espece[] = array($res->nom_sci_complet, $id, $retenu);
45
				}
46
			}
1427 aurelien 47
			usort($liste_genre_espece, array($this, 'comparerParRetenuPuisNom'));
1425 aurelien 48
		}
49
		return $liste_genre_espece;
50
	}
51
 
1427 aurelien 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;
57
		} else {
58
			return strnatcmp($a[0], $b[0]);
59
		}
1425 aurelien 60
	}
61
 
62
	public function effectuerRequeteInfosComplementairesEtFormaterNom($numNom) {
63
 
64
		$resultat_infos_complementaires = (array)$this->effectuerRequeteInfosComplementairesSurNumNom($numNom);
65
		$retour_infos_complementaires = array();
66
		if (isset($resultat_infos_complementaires['nom_retenu_complet']) && $resultat_infos_complementaires['nom_retenu_complet'] != '') {
67
           	$retour_infos_complementaires=array(($this->supprimerBiblio($resultat_infos_complementaires['nom_retenu_complet'])));
68
	    }
69
 
70
		return $retour_infos_complementaires;
71
	}
72
 
73
	public function rechercherInformationsComplementairesSurNom($nom_saisi) {
74
 
75
		$nameparser = new NameParser();
76
		$nom_decoupe = $nameparser->parse($nom_saisi);
77
 
78
		$masque = ((isset($nom_decoupe['genus']) && $nom_decoupe['genus']!= '') &&
79
				  (isset($nom_decoupe['species']) && $nom_decoupe['species']!= ''))
80
				  ? $nom_decoupe['genus'].' '.$nom_decoupe['species'] : $nom_saisi;
81
 
82
		$liste_genre_espece = array();
1432 aurelien 83
		$url = $this->url_service_nom.'?masque='.urlencode($nom_decoupe['genus'].' '.$nom_decoupe['species']).'&recherche=etendue&ns.format=txt&retour.champs=nom_sci,auteur,nom_retenu.id&navigation.limite=1';
1425 aurelien 84
		$resultat = @file_get_contents($url);
85
		if($resultat != '') {
86
			$resultat = json_decode($resultat);
87
			if(is_object($resultat) && isset($resultat->resultat)) {
88
				foreach ($resultat->resultat as $id => $res) {
89
					$nom_complet = $res->{'nom_sci'}.' '.$res->auteur;
1432 aurelien 90
					$liste_genre_espece[] = array($id,$nom_complet);
1425 aurelien 91
				}
92
			}
1432 aurelien 93
		}
1425 aurelien 94
		return $liste_genre_espece;
95
	}
96
 
97
	public function rechercherInformationsComplementairesSurNumNom($num_nom) {
98
		$infos_formatees = array();
99
		$infos = $this->effectuerRequeteInfosComplementairesSurNumNom($num_nom);
100
		if(is_object($infos)) {
101
			$infos_formatees = $this->formaterInfosComplementairesSurNumNom($infos);
102
		}
103
		return $infos_formatees;
104
	}
105
 
106
	public function effectuerRequeteInfosComplementairesSurNumNom($num_nom) {
107
		$infos = array();
1432 aurelien 108
		$url = $this->url_service_nom.'/'.$num_nom.'?retour.champs=nom_sci,auteur,id,nom_retenu_complet,nom_retenu.id,num_taxonomique,famille';
1425 aurelien 109
		$resultat = @file_get_contents($url);
110
		if($resultat != '') {
111
			$infos = json_decode($resultat);
112
		}
113
		return $infos;
114
	}
115
 
116
	private function formaterInfosComplementairesSurNumNom($infos) {
117
		$infos = (array)$infos;
118
		return $infos_formatees = array(
119
			'Nom_Retenu' => $this->supprimerBiblio($infos['nom_retenu_complet']),
120
			'Num_Nom_Retenu' => $infos['nom_retenu.id'],
121
			'Num_Taxon' => $infos['num_taxonomique'],
122
			'Famille' => $infos['famille']
123
		);
124
	}
125
 
126
	private function supprimerBiblio($nom) {
127
		return preg_replace('/ \[.*\]/','',$nom);
128
	}
129
 
130
	public function rechercherNumTaxSurNumNom($num_nom) {
131
 
132
		$nt = null;
1427 aurelien 133
		$url = $this->url_service_nom."/".$num_nom.'?retour.champs=num_taxonomique';
1425 aurelien 134
		$resultat = @file_get_contents($url);
135
		if($resultat != '') {
136
			$infos = json_decode($resultat);
137
			$nt = $infos->num_taxonomique;
138
		}
139
 
140
		return $nt;
141
	}
142
 
143
	public function taxonEstPresentDansDepartement($num_taxon,$code_departement) {
144
		$presence_taxon = false;
1427 aurelien 145
		$url = $this->url_service_chorologie_obs.'?masque.departement='.$code_departement.'&masque.determination.nt='.$num_taxon.'&navigation.limite=1';
1425 aurelien 146
		$resultat = @file_get_contents($url);
147
		if($resultat != '') {
148
			$resultat = json_decode($resultat);
149
			if(is_object($resultat) && isset($resultat->resultat) && count($resultat->resultat) > 0) {
150
				$presence_taxon = true;
151
			}
152
		}
153
		return $presence_taxon;
154
	}
155
 
156
	public function effectuerRequeteInfosComplementairesSurNumTax($numTax) {
157
 
1426 aurelien 158
		$infos = array();
159
		//TODO: retourner moins de champs grâce au paramètre retour.champs
1427 aurelien 160
		$url = $this->url_service_taxon."/nt:".$numTax;
1426 aurelien 161
		$resultat = @file_get_contents($url);
162
		if($resultat != '') {
163
			$infos = json_decode($resultat);
164
		}
165
 
166
		return $infos;
1425 aurelien 167
	}
168
 
169
	function rechercherInfosSurTexteCodeOuNumTax($identifiant_espece) {
170
		// texte libre, nom scientifique,
171
		// ou code nomenclatural (format BDNFFnn999999)
172
		// ou code taxonomique (format BDNFFnt999999)
173
		$identifiant_espece=trim($identifiant_espece);
174
		$identifiant_espece=utf8_encode($identifiant_espece);
1426 aurelien 175
 
1425 aurelien 176
		$retour = array();
177
 
178
		preg_match('/BDNFFnn([0-9][0-9]*)/',$identifiant_espece, $elements);
179
		if (isset($elements[1])) {
180
			// Numero nomenclatural
181
			$infos_taxon = $this->rechercherInformationsComplementairesSurNumNom($elements[1]);
1426 aurelien 182
			$retour = array("nom_sel" => $infos_taxon['Nom_Retenu'], "en_id_nom" => $elements[1]);
1425 aurelien 183
		} else {
184
			//  Numero taxonomique ou nom scientifique
185
			preg_match('/BDNFFnt([0-9][0-9]*)/', $identifiant_espece, $elements);
186
			if (isset($elements[1])) {
187
				// Numero taxonomique
1426 aurelien 188
				$infos_taxon = (array)$this->effectuerRequeteInfosComplementairesSurNumTax($elements[1]);
189
				if(isset($infos_taxon['nom_retenu.libelle']) && isset($infos_taxon['id'])) {
190
					$nom = $infos_taxon['nom_retenu.libelle'];
191
					$nom .= (isset($infos_taxon['auteur'])) ? ' '.$infos_taxon['auteur'] : '';
192
					$retour = array("nom_sel" => $nom,
193
									"en_id_nom" => $infos_taxon['id']);
194
				}
1425 aurelien 195
			} else {
196
				// Nom scientifique
1426 aurelien 197
				$id_nom = $this->rechercherInformationsComplementairesSurNom($identifiant_espece);
1425 aurelien 198
				// Recherche du nom associe
199
				$retour = array("nom_sel" => $identifiant_espece);
1432 aurelien 200
				if(is_array($id_nom) && count($id_nom) > 0 && isset($id_nom[0][0]) && isset($id_nom[0][1])) {
1426 aurelien 201
					$nn = $id_nom[0][0];
1432 aurelien 202
					$retour = array("nom_sel" => $id_nom[0][1], "en_id_nom" => $id_nom[0][0]);
1425 aurelien 203
				}
204
			}
205
		}
206
 
207
		return $retour;
208
	}
209
}
210
?>