Subversion Repositories eFlore/Applications.moissonnage

Rev

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

Rev Author Line No. Line
17 delphine 1
<?php
2
 
3
 
4
 
5
class Observations {
6
 
7
private $longitude = null;
8
	private $latitude = null;
9
 
10
	private $utilisateur = '';
11
	private $taxon = null;
12
	private $referentiel = '';
13
	private $sousTaxons = array();
14
 
15
	private $bdd;
16
 
17
 
18
	public function __construct() {}
19
 
20
 
21
	public function consulter($ressources, $parametres) {
22
		extract($parametres);
23
		$this->utilisateur = $utilisateur != '*' ? $utilisateur : '';
24
		$this->departement = $dept != '*' ? $dept : '';
25
		$this->traiterParametresCoordonnees($parametres);
26
		if (is_null($this->longitude) || is_null($this->latitude)) {
27
			$message = "Recherche des observations impossible : coordonnees de la station hors du plan";
28
			return new Exception($message, RestServeur::HTTP_CODE_MAUVAISE_REQUETE);
29
		}
30
 
31
		// recherche du taxon dans les referentiels bdtfx et bdtxa et verification de sa validite
32
		if ($referentiel != '*') {
33
			$this->referentiel = $this->verifierExistenceReferentiel($referentiel);
34
			if (is_null($this->referentiel)) {
35
				$message = "Recherche des stations impossible : referentiel inconnu\n"
36
					+ "Voici les referentiels disponibles : "
37
					+ implode(', ', Referentiel::listeReferentielsDisponibles());
38
				return new Exception($message, RestServeur::HTTP_CODE_MAUVAISE_REQUETE);
39
			}
40
		}
41
 
42
		if ($num_taxon != '*') {
43
			$this->traiterParametresTaxon($num_taxon);
44
			if (is_null($this->taxon)) {
45
				$message = "Recherche des stations impossible : taxon non trouve dans les referentiels";
46
				return new Exception($message, RestServeur::HTTP_CODE_MAUVAISE_REQUETE);
47
			}
48
			// recuperer les sous-taxons
49
			$referentiel = new Referentiel($this->referentiel);
50
			$this->sousTaxons = $referentiel->recupererSousTaxons();
51
		}
52
 
53
		// recuperer les informations sur les stations repondant a ces parametres
54
		$observations = $this->recupererObservations();
55
		$nomSite = $this->obtenirNomsStations();
56
 
57
		// mettre en forme les informations au format JSON
58
		$formateurJSON = new FormateurJson();
59
		$donneesFormatees = $formateurJSON->formaterObservations($observations, $nomSite);
60
		return $donneesFormatees;
61
	}
62
 
63
 
64
 
65
 
66
	//-------------------------------------------------------------------------------
67
	//    Recuperation et verification des parametres
68
	//-------------------------------------------------------------------------------
69
 
70
private function traiterParametresCoordonnees($parametres) {
71
		$this->longitude = $this->verifierCoordonnees($parametres['longitude'], 'lng');
72
		$this->latitude  = $this->verifierCoordonnees($parametres['latitude'],  'lat');
73
	}
74
 
75
	private function verifierCoordonnees($valeur, $axe) {
76
		$limites = array(
77
			'ouest' => floatval(Config::get('carte.limite_ouest')),
78
			'est'   => floatval(Config::get('carte.limite_est')),
79
			'sud'   => floatval(Config::get('carte.limite_sud')),
80
			'nord'  => floatval(Config::get('carte.limite_nord'))
81
		);
82
		if (($axe=='lng' && floatval($valeur) >= $limites['ouest'] && floatval($valeur) <= $limites['est'])
83
			|| ($axe=='lat' && floatval($valeur) >= $limites['sud'] && floatval($valeur) <= $limites['nord'])) {
84
			return $valeur;
85
		}
86
 
87
		return null;
88
	}
89
 
90
	private function traiterParametresTaxon($numeroTaxon) {
91
		if ($numeroTaxon != '*') {
92
			if ($this->referentiel != '') {
93
				$referentiel = new Referentiel($this->referentiel);
94
				$this->taxon = $referentiel->recupererTaxon($numeroTaxon);
95
			} else {
96
				$this->taxon = $this->verifierExistenceTaxon($numeroTaxon);
97
			}
98
		}
99
	}
100
 
101
	private function verifierExistenceReferentiel($referentiel) {
102
		if ($referentiel == '*') {
103
			return '';
104
		}
105
		$listeReferentiels = Referentiel::listeReferentielsDisponibles();
106
		foreach ($listeReferentiels as $nomReferentiel) {
107
			if (strstr($nomReferentiel, $referentiel) !== false) {
108
				$this->referentiel = $nomReferentiel;
109
				break;
110
			}
111
		}
112
		return null;
113
	}
114
 
115
	private function verifierExistenceTaxon($numTaxon) {
116
		$listeReferentiels = Referentiel::listeReferentielsDisponibles();
117
		// tester chaque referentiel jusqu'a trouver un taxon correspondant
118
		// a tous les criteres demandes dans les masques
119
		foreach ($listeReferentiels as $nomReferentiel) {
120
			$referentiel = new Referentiel($nomReferentiel);
121
			$taxon = $referentiel->recupererTaxon($numTaxon);
122
			if (!is_null($taxon)) {
123
				$this->referentiel = $nomReferentiel;
124
				return $taxon;
125
			}
126
		}
127
		return null;
128
	}
129
 
130
 
131
 
132
	//-------------------------------------------------------------------------------
133
	//    Recuperation des donnees dans la base de donnees
134
	//    et mise sous forme d'objet geographique en fonction du niveau de zoom
135
	//-------------------------------------------------------------------------------
136
 
137
	private function getBdd() {
138
		if (!isset($this->bdd)) {
139
			$this->bdd = new Bdd();
140
		}
141
		$this->bdd->requeter("USE tb_eflore");
142
		return $this->bdd;
143
	}
144
 
145
 
146
	private function recupererObservations() {
147
		$requete = 'SELECT ' . $this->construireColonnesSelection() . ' FROM sophy_tapir WHERE '
148
			. $this->construireCriteresTaxon() . ' lieu_station_latitude=' . $this->latitude
149
			. ' AND lieu_station_longitude=' . $this->longitude;
150
		$observations = $this->getBdd()->recupererTous($requete);
151
 
152
		foreach ($observations as $obs) {
153
			// recuperer le numero du taxon en parcourant les referentiels
154
			// jusqu'a retrouver le taxon
155
			$nn = null;
156
			if (!is_null($this->taxon)) {
157
				$nn = $this->taxon['nn'];
158
			} elseif (strlen(trim($obs['nomSci'])) > 0) {
159
				// recuperer le nom du taxon
160
				$taxon = $this->obtenirNumeroTaxon($obs['nomSci']);
161
				$nn = $taxon['nn'];
162
			}
163
			$obs['nn'] = $nn;
164
			if (!is_null($nn)) {
165
				$obs['urlEflore'] = "http://www.tela-botanica.org/bdtfx-nn-{$nn}";
166
			}
167
		}
168
 
169
		return $observations;
170
	}
171
 
172
	private function construireColonnesSelection() {
173
		$colonnes = array(
174
			'idObs' => 'observation_id',
175
			'nomSci' => 'nom_scientifique_complet',
176
			'date' => 'observation_date',
177
			'lieu' => 'lieu_station_nom',
178
			'observateur' => "observateur_nom_complet",
179
		);
180
		$selectSql = '';
181
		foreach ($colonnes as $renvoi => $colonne) {
182
			$selectSql .= (strlen($selectSql) == 0 ? '' : ', ') . "{$colonne} AS {$renvoi}";
183
		}
184
		return $selectSql;
185
	}
186
 
187
	private function construireCriteresTaxon() {
188
		if (is_null($this->taxon)) {
189
			return '';
190
		}
191
		$criteres = '(nom_scientifique_complet LIKE \'' . addslashes($this->taxon['nom']) . '%\'';
192
		foreach ($this->sousTaxons as $sousTaxon) {
193
			$criteres .= ' OR nom_scientifique_complet LIKE \'' . addslashes($sousTaxon['nom']) . '%\'';
194
		}
195
		$criteres .= ') AND ';
196
		return $criteres;
197
	}
198
 
199
	private function obtenirNomsStations() {
200
		$critereTaxon = '';
201
		if (!is_null($this->taxon)) {
202
			$critereTaxon = 'nom_scientifique_complet LIKE \'' . addslashes($this->taxon['nom_sci'])
203
			. '%\' AND ';
204
		}
205
		$requete = 'SELECT DISTINCTROW lieu_station_nom FROM sophy_tapir WHERE ' . $critereTaxon
206
			. ' lieu_station_latitude=' . $this->latitude . ' AND lieu_station_longitude='
207
			. $this->longitude;
208
		$stations = $this->getBdd()->recupererTous($requete);
209
		$nomsStations = array();
210
		foreach ($stations as $station) {
211
			$nomsStations[] = $station['lieu_station_nom'];
212
		}
213
		return implode(', ', $nomsStations);
214
	}
215
 
216
 
217
 
218
	private function obtenirNumeroTaxon($nomScientifique) {
219
		$masque = array('masque.ns' => $nomScientifique);
220
		$listeReferentiels = Referentiel::listeReferentielsDisponibles();
221
		// tester chaque referentiel jusqu'a trouver un taxon correspondant
222
		// a tous les criteres demandes dans les masques
223
		foreach ($listeReferentiels as $nomReferentiel) {
224
			$referentiel = new Referentiel($nomReferentiel);
225
			$taxon = $referentiel->recupererTaxon($masque);
226
			if (!is_null($taxon)) {
227
				return $taxon;
228
			}
229
		}
230
	}
231
 
232
}
233
 
234
?>