Subversion Repositories eFlore/Applications.cel

Rev

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

Rev 2170 Rev 2282
1
<?php
1
<?php
2
// declare(encoding='UTF-8');
2
// declare(encoding='UTF-8');
3
/**
3
/**
4
 * Service fournissant une carte dynamique des obsertions publiques du CEL.
4
 * Service fournissant une carte dynamique des obsertions publiques du CEL.
5
 * Encodage en entrée : utf8
5
 * Encodage en entrée : utf8
6
 * Encodage en sortie : utf8
6
 * Encodage en sortie : utf8
7
 *
7
 *
8
 * Cas d'utilisation :
8
 * Cas d'utilisation :
9
 * /CelWidgetMap/Carte/Utilisateur : carte des observations publiques d'un utilisateur.
9
 * /CelWidgetMap/Carte/Utilisateur : carte des observations publiques d'un utilisateur.
10
 * /CelWidgetMap/Carte/Utilisateur/Projet : carte des observations publiques d'un utilisateur pour un projet.
10
 * /CelWidgetMap/Carte/Utilisateur/Projet : carte des observations publiques d'un utilisateur pour un projet.
11
 * /CelWidgetMap/Carte/Utilisateur/Projet/dept : carte des observations publiques d'un utilisateur pour un projet sur un département.
11
 * /CelWidgetMap/Carte/Utilisateur/Projet/dept : carte des observations publiques d'un utilisateur pour un projet sur un département.
12
 * /CelWidgetMap/Carte/Utilisateur/Projet/dept/nt : carte des observations publiques d'un utilisateur pour un projet sur un département pour un taxon.
12
 * /CelWidgetMap/Carte/Utilisateur/Projet/dept/nt : carte des observations publiques d'un utilisateur pour un projet sur un département pour un taxon.
13
 *
13
 *
14
 * Carte = Type de carte. Valeurs possible : defaut,
14
 * Carte = Type de carte. Valeurs possible : defaut,
15
 * Utilisateur = identifiant (= courriel) de l'utilisateur ou * pour tous les utilisateurs.
15
 * Utilisateur = identifiant (= courriel) de l'utilisateur ou * pour tous les utilisateurs.
16
 * Projet = mot-clé du projet
16
 * Projet = mot-clé du projet
-
 
17
 * 		Plusieurs mots-clés peuvent être spécifiés:
-
 
18
 * 			machin ET bidule ET chose => observations ayant tous les mots-clés (intersection) 
-
 
19
 * 			machin OU bildule OU chose => observations ayant au moins un des mots-clés (union)
-
 
20
 * 		ATTENTION
-
 
21
 * 			machin ET bidule OU chose donne un résultat indéterminé pour l'instant
17
 *
22
 *
18
 * @author Jean-Pascal MILCENT <jpm@clapas.org>
23
 * @author Jean-Pascal MILCENT <jpm@clapas.org>
19
 * @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
24
 * @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
20
 * @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
25
 * @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
21
 * @version $Id$
26
 * @version $Id$
22
 * @copyright © 2010, Jean-Pascal MILCENT
27
 * @copyright © 2010, Jean-Pascal MILCENT
23
 */
28
 */
24
// TODO : supprimer le TRIM quand les obs seront reliées correctements aux localisations (sur le code INSEE par exemple)
29
// TODO : supprimer le TRIM quand les obs seront reliées correctements aux localisations (sur le code INSEE par exemple)
25
class CelWidgetMapPoint extends Cel {
30
class CelWidgetMapPoint extends Cel {
26
	const MARQUEUR_GROUPE = 'GROUPE';
31
	const MARQUEUR_GROUPE = 'GROUPE';
27
	const MARQUEUR_COMMUNE = 'COMMUNE';
32
	const MARQUEUR_COMMUNE = 'COMMUNE';
28
	const MARQUEUR_STATION = 'STATION';
33
	const MARQUEUR_STATION = 'STATION';
29
	/**
34
	/**
30
	 * Méthode appelée avec une requête de type GET.
35
	 * Méthode appelée avec une requête de type GET.
31
	 */
36
	 */
32
	public function getElement($ressources) {
37
	public function getElement($ressources) {
33
		$retour = null;
38
		$retour = null;
34
		if($this->parametres == null) {
39
		if($this->parametres == null) {
35
			$this->parametres = array();
40
			$this->parametres = array();
36
		}
41
		}
37
		extract($this->parametres);
42
		extract($this->parametres);
38
		//Chronometre::chrono("Avant groupage");
43
		//Chronometre::chrono("Avant groupage");
39
 
44
 
40
		$action = array_shift($ressources);
45
		$action = array_shift($ressources);
41
		if (isset($action)) {
46
		if (isset($action)) {
42
			$methode = $this->traiterNomMethodeGet($action);
47
			$methode = $this->traiterNomMethodeGet($action);
43
			if (method_exists($this, $methode)) {
48
			if (method_exists($this, $methode)) {
44
				$retour = $this->$methode($ressources);
49
				$retour = $this->$methode($ressources);
45
			} else {
50
			} else {
46
				$this->messages[] = "Ce type de ressource '$methode' n'est pas disponible.";
51
				$this->messages[] = "Ce type de ressource '$methode' n'est pas disponible.";
47
			}
52
			}
48
		} else {
53
		} else {
49
			$this->messages[] = "Vous devez indiquer le type de ressource.";
54
			$this->messages[] = "Vous devez indiquer le type de ressource.";
50
		}
55
		}
51
 
56
 
52
		//Chronometre::chrono("Apres traitement");
57
		//Chronometre::chrono("Apres traitement");
53
		//echo Chronometre::afficherChrono();
58
		//echo Chronometre::afficherChrono();
54
		if (is_null($retour)) {
59
		if (is_null($retour)) {
55
			$info = 'Un problème est survenu : '.print_r($this->messages, true);
60
			$info = 'Un problème est survenu : '.print_r($this->messages, true);
56
			$this->envoyer($info);
61
			$this->envoyer($info);
57
		} else if (isset($retour['type']) && $retour['type'] == 'jsonVar') {
62
		} else if (isset($retour['type']) && $retour['type'] == 'jsonVar') {
58
			$this->envoyerJsonVar($retour['variable_js'], $retour['donnees']);
63
			$this->envoyerJsonVar($retour['variable_js'], $retour['donnees']);
59
		} else if (isset($retour['type']) && $retour['type'] == 'jsonP') {
64
		} else if (isset($retour['type']) && $retour['type'] == 'jsonP') {
60
			$this->envoyerJsonp($retour['donnees']);
65
			$this->envoyerJsonp($retour['donnees']);
61
		} else if (isset($retour['type']) && $retour['type'] == 'png') {
66
		} else if (isset($retour['type']) && $retour['type'] == 'png') {
62
			header("Content-type: image/png");
67
			header("Content-type: image/png");
63
			imagepng($retour['img']);
68
			imagepng($retour['img']);
64
			imagedestroy($retour['img']);
69
			imagedestroy($retour['img']);
65
		} else {
70
		} else {
66
			$this->envoyerJson($retour);
71
			$this->envoyerJson($retour);
67
		}
72
		}
68
	}
73
	}
69
 
74
 
70
	/**
75
	/**
71
	 * Les icones des groupes de stations
76
	 * Les icones des groupes de stations
72
	 */
77
	 */
73
	public function getIconeGroupe($params) {
78
	public function getIconeGroupe($params) {
74
		extract($this->parametres);
79
		extract($this->parametres);
75
 
80
 
76
		$chemin_marqueur = sprintf($this->config['settings']['cheminCelMarkerObsTpl'], $type);
81
		$chemin_marqueur = sprintf($this->config['settings']['cheminCelMarkerObsTpl'], $type);
77
		$img = imagecreatefrompng($chemin_marqueur);
82
		$img = imagecreatefrompng($chemin_marqueur);
78
 
83
 
79
		$noir = imagecolorallocate($img, 0, 0, 0);
84
		$noir = imagecolorallocate($img, 0, 0, 0);
80
		$texte = (String) $nbre;
85
		$texte = (String) $nbre;
81
		$x = (imagesx($img) - 6.0 * strlen($texte)) / 2;
86
		$x = (imagesx($img) - 6.0 * strlen($texte)) / 2;
82
		$y = (imagesy($img) - 16) / 2;
87
		$y = (imagesy($img) - 16) / 2;
83
 
88
 
84
		imagestring($img, 3, $x, $y, $texte, $noir);
89
		imagestring($img, 3, $x, $y, $texte, $noir);
85
 
90
 
86
		imagealphablending($img, false);
91
		imagealphablending($img, false);
87
		imagesavealpha($img, true);
92
		imagesavealpha($img, true);
88
 
93
 
89
		return array('type' => 'png', 'img' => $img);
94
		return array('type' => 'png', 'img' => $img);
90
	}
95
	}
91
 
96
 
92
	public function getTout($params) {
97
	public function getTout($params) {
93
 
98
 
94
		$emplacements = null;
99
		$emplacements = null;
95
		$concatenation_id = "CONCAT(IFNULL(latitude,''),IFNULL(longitude,''), IFNULL(wgs84_latitude,''),IFNULL(wgs84_longitude,'')) ";
100
		$concatenation_id = "CONCAT(IFNULL(latitude,''),IFNULL(longitude,''), IFNULL(wgs84_latitude,''),IFNULL(wgs84_longitude,'')) ";
96
 
101
 
97
		$requete =  'SELECT ce_zone_geo, zone_geo, station, '.
102
		$requete =  'SELECT ce_zone_geo, zone_geo, station, '.
98
								"mots_cles_texte, ".
103
								"mots_cles_texte, ".
99
								"latitude, ".
104
								"latitude, ".
100
								"wgs84_latitude, ".
105
								"wgs84_latitude, ".
101
								"longitude, ".
106
								"longitude, ".
102
								"wgs84_longitude, ".
107
								"wgs84_longitude, ".
103
								$concatenation_id." as id_coord ".
108
								$concatenation_id." as id_coord ".
104
					'FROM cel_obs AS co '.
109
					'FROM cel_obs AS co '.
105
                    '   LEFT JOIN cel_zones_geo AS l '.
110
                    '   LEFT JOIN cel_zones_geo AS l '.
106
                    '       ON (l.nom = co.zone_geo AND l.id_zone_geo = co.ce_zone_geo) '.
111
                    '       ON (l.nom = co.zone_geo AND l.id_zone_geo = co.ce_zone_geo) '.
107
                    "WHERE transmission = '1' ".
112
                    "WHERE transmission = '1' ".
108
					" AND (".
113
					" AND (".
109
					$this->construireWhereRectangleStationOR()." OR ".
114
					$this->construireWhereRectangleStationOR()." OR ".
110
					$this->construireWhereRectangleCommuneOR().") ".
115
					$this->construireWhereRectangleCommuneOR().") ".
111
					$this->construireWhereDept().
116
					$this->construireWhereDept().
112
                    $this->construireWhereCommune().
117
                    $this->construireWhereCommune().
113
                    $this->construireWhereUtilisateur().
118
                    $this->construireWhereUtilisateur().
114
					$this->construireWhereNumTaxonAvecSousTaxons().
119
					$this->construireWhereNumTaxonAvecSousTaxons().
115
					$this->construireWhereNomTaxon().
120
					$this->construireWhereNomTaxon().
116
					$this->construireWhereReferentiel().
121
					$this->construireWhereReferentiel().
117
					$this->construireWhereDate().
122
					$this->construireWhereDate().
118
					$this->construireWhereCommentaire().
123
					$this->construireWhereCommentaire().
119
					$this->construireWherePhotosSeulement().
124
					$this->construireWherePhotosSeulement().
120
					$this->construireWhereProjet().
125
					$this->construireWhereProjet().
121
					$this->construireWhereTag().
126
					$this->construireWhereTag().
122
					$this->construireWhereNombreDeJours().
127
					$this->construireWhereNombreDeJours().
123
					' GROUP BY id_coord';
128
					' GROUP BY id_coord';
124
 
129
 
125
		$resultats_emplacements = Cel::db()->requeter($requete);
130
		$resultats_emplacements = Cel::db()->requeter($requete);
126
		$emplacements = $this->traiterEmplacements($resultats_emplacements, $this->compterObservations($params));
131
		$emplacements = $this->traiterEmplacements($resultats_emplacements, $this->compterObservations($params));
127
		return $emplacements;
132
		return $emplacements;
128
	}
133
	}
129
 
134
 
130
	private function afficherRequeteFormatee($requete) {
135
	private function afficherRequeteFormatee($requete) {
131
		$requete = str_replace(')',')<br />',$requete);
136
		$requete = str_replace(')',')<br />',$requete);
132
		$requete = str_replace('(','	<br />	(',$requete);
137
		$requete = str_replace('(','	<br />	(',$requete);
133
		echo '<pre>'.$requete.'</pre>';
138
		echo '<pre>'.$requete.'</pre>';
134
		exit;
139
		exit;
135
	}
140
	}
136
 
141
 
137
	private $nb_obs = 0;
142
	private $nb_obs = 0;
138
 
143
 
139
	private function compterObservations($params) {
144
	private function compterObservations($params) {
140
		$requete =  'SELECT COUNT(*) as nb '.
145
		$requete =  'SELECT COUNT(*) as nb '.
141
					'FROM cel_obs AS co '.
146
					'FROM cel_obs AS co '.
142
                    '   LEFT JOIN cel_zones_geo AS l '.
147
                    '   LEFT JOIN cel_zones_geo AS l '.
143
                    '       ON (l.nom = co.zone_geo AND l.id_zone_geo = co.ce_zone_geo) '.
148
                    '       ON (l.nom = co.zone_geo AND l.id_zone_geo = co.ce_zone_geo) '.
144
                    "WHERE transmission = '1' ".
149
                    "WHERE transmission = '1' ".
145
					" AND (".
150
					" AND (".
146
					$this->construireWhereRectangleStationOR()." OR ".
151
					$this->construireWhereRectangleStationOR()." OR ".
147
					$this->construireWhereRectangleCommuneOR().") ".
152
					$this->construireWhereRectangleCommuneOR().") ".
148
					$this->construireWhereDept().
153
					$this->construireWhereDept().
149
                    $this->construireWhereCommune().
154
                    $this->construireWhereCommune().
150
                    $this->construireWhereUtilisateur().
155
                    $this->construireWhereUtilisateur().
151
					$this->construireWhereNumTaxonAvecSousTaxons().
156
					$this->construireWhereNumTaxonAvecSousTaxons().
152
					$this->construireWhereNomTaxon().
157
					$this->construireWhereNomTaxon().
153
					$this->construireWhereReferentiel().
158
					$this->construireWhereReferentiel().
154
					$this->construireWhereDate().
159
					$this->construireWhereDate().
155
					$this->construireWhereCommentaire().
160
					$this->construireWhereCommentaire().
156
					$this->construireWherePhotosSeulement().
161
					$this->construireWherePhotosSeulement().
157
					$this->construireWhereProjet().
162
					$this->construireWhereProjet().
158
					$this->construireWhereTag().
163
					$this->construireWhereTag().
159
					$this->construireWhereNombreDeJours();
164
					$this->construireWhereNombreDeJours();
160
 
165
 
161
		$resultats_nb_obs = Cel::db()->requeter($requete);
166
		$resultats_nb_obs = Cel::db()->requeter($requete);
162
		return $resultats_nb_obs[0]['nb'];
167
		return $resultats_nb_obs[0]['nb'];
163
	}
168
	}
164
 
169
 
165
	private function traiterEmplacements(&$emplacements, $nb_total_observation) {
170
	private function traiterEmplacements(&$emplacements, $nb_total_observation) {
166
		$zoom = (int) array_key_exists('zoom', $this->parametres) ? $this->parametres['zoom'] : 11;
171
		$zoom = (int) array_key_exists('zoom', $this->parametres) ? $this->parametres['zoom'] : 11;
167
		$distance = (int) array_key_exists('distance', $this->parametres) ? $this->parametres['distance'] : 20;
172
		$distance = (int) array_key_exists('distance', $this->parametres) ? $this->parametres['distance'] : 20;
168
 
173
 
169
		$marqueurs = array(
174
		$marqueurs = array(
170
			'stats' => array('stations' => 0, 'communes' => 0, 'observations' => 0),
175
			'stats' => array('stations' => 0, 'communes' => 0, 'observations' => 0),
171
			'points' => null
176
			'points' => null
172
		);
177
		);
173
 
178
 
174
		if (isset($this->parametres['ne']) && $this->parametres['sw'] && ! $this->etreNull($this->parametres['ne']) && ! $this->etreNull($this->parametres['sw'])) {
179
		if (isset($this->parametres['ne']) && $this->parametres['sw'] && ! $this->etreNull($this->parametres['ne']) && ! $this->etreNull($this->parametres['sw'])) {
175
			$ne = $this->decomposerLatLng($this->parametres['ne']);
180
			$ne = $this->decomposerLatLng($this->parametres['ne']);
176
			$sw = $this->decomposerLatLng($this->parametres['sw']);
181
			$sw = $this->decomposerLatLng($this->parametres['sw']);
177
			$marqueurs['points'] = CartoGroupage::creerGroupesQuadtree($emplacements, $ne['lat'], $ne['lng'], $sw['lat'], $sw['lng'], $zoom);
182
			$marqueurs['points'] = CartoGroupage::creerGroupesQuadtree($emplacements, $ne['lat'], $ne['lng'], $sw['lat'], $sw['lng'], $zoom);
178
 
183
 
179
			// laisser la classe cartoGroupage compter les élements simplifie le comptage
184
			// laisser la classe cartoGroupage compter les élements simplifie le comptage
180
			// et permet de ne pas reparser le tableau pour compter les différents éléments
185
			// et permet de ne pas reparser le tableau pour compter les différents éléments
181
			$nb_elements = CartoGroupage::getNbElements();
186
			$nb_elements = CartoGroupage::getNbElements();
182
			// les bornes servent à centrer la carte dans le cas ou l'on demande des paramètres précis
187
			// les bornes servent à centrer la carte dans le cas ou l'on demande des paramètres précis
183
			$marqueurs['stats']['coordmax'] = CartoGroupage::getBornes();
188
			$marqueurs['stats']['coordmax'] = CartoGroupage::getBornes();
184
			$marqueurs['stats']['stations'] = $nb_elements['stations'];
189
			$marqueurs['stats']['stations'] = $nb_elements['stations'];
185
			$marqueurs['stats']['communes'] = $nb_elements['communes'];
190
			$marqueurs['stats']['communes'] = $nb_elements['communes'];
186
			$marqueurs['stats']['observations'] = (int)$nb_total_observation;
191
			$marqueurs['stats']['observations'] = (int)$nb_total_observation;
187
		} else {
192
		} else {
188
			$marqueurs['points'] = $emplacements;
193
			$marqueurs['points'] = $emplacements;
189
		}
194
		}
190
 
195
 
191
		return $marqueurs;
196
		return $marqueurs;
192
	}
197
	}
193
 
198
 
194
	private function traiterStations($communes, $stations) {
199
	private function traiterStations($communes, $stations) {
195
		$zoom = (int) array_key_exists('zoom', $this->parametres) ? $this->parametres['zoom'] : 11;
200
		$zoom = (int) array_key_exists('zoom', $this->parametres) ? $this->parametres['zoom'] : 11;
196
		$distance = (int) array_key_exists('distance', $this->parametres) ? $this->parametres['distance'] : 20;
201
		$distance = (int) array_key_exists('distance', $this->parametres) ? $this->parametres['distance'] : 20;
197
 
202
 
198
		$marqueurs = array(
203
		$marqueurs = array(
199
			'stats' => array('stations' => 0, 'communes' => 0, 'observations' => 0),
204
			'stats' => array('stations' => 0, 'communes' => 0, 'observations' => 0),
200
			'points' => null
205
			'points' => null
201
		);
206
		);
202
		$marqueurs['stats']['observations'] = $this->traiterNbreObs($communes) + $this->traiterNbreObs($stations);
207
		$marqueurs['stats']['observations'] = $this->traiterNbreObs($communes) + $this->traiterNbreObs($stations);
203
 
208
 
204
		$points = array();
209
		$points = array();
205
		if ($communes !== false) {
210
		if ($communes !== false) {
206
			foreach ($communes as $commune) {
211
			foreach ($communes as $commune) {
207
				if (is_numeric($commune['lat']) && is_numeric($commune['lng'])) {
212
				if (is_numeric($commune['lat']) && is_numeric($commune['lng'])) {
208
					extract($commune);
213
					extract($commune);
209
					$id = self::MARQUEUR_COMMUNE.':'.$lat.'|'.$lng;
214
					$id = self::MARQUEUR_COMMUNE.':'.$lat.'|'.$lng;
210
					$lata = round($lat, 5);
215
					$lata = round($lat, 5);
211
					$lnga = round($lng, 5);
216
					$lnga = round($lng, 5);
212
 
217
 
213
					if (!isset($points[$id])) {
218
					if (!isset($points[$id])) {
214
						$points[$id]['id'] = $id;
219
						$points[$id]['id'] = $id;
215
						$points[$id]['nom'] = $nom;
220
						$points[$id]['nom'] = $nom;
216
						$points[$id]['lat'] = $lata;
221
						$points[$id]['lat'] = $lata;
217
						$points[$id]['lng'] = $lnga;
222
						$points[$id]['lng'] = $lnga;
218
						$points[$id]['nbre'] = 1;
223
						$points[$id]['nbre'] = 1;
219
						$marqueurs['stats']['communes']++;
224
						$marqueurs['stats']['communes']++;
220
					} else {
225
					} else {
221
						$points[$id]['nbre']++;
226
						$points[$id]['nbre']++;
222
					}
227
					}
223
				}
228
				}
224
			}
229
			}
225
		}
230
		}
226
		if ($stations !== false) {
231
		if ($stations !== false) {
227
			foreach ($stations as $station) {
232
			foreach ($stations as $station) {
228
				if (is_numeric($station['lat']) && is_numeric($station['lng'])) {
233
				if (is_numeric($station['lat']) && is_numeric($station['lng'])) {
229
					extract($station);
234
					extract($station);
230
					$id = self::MARQUEUR_STATION.':'.$lat.'|'.$lng;
235
					$id = self::MARQUEUR_STATION.':'.$lat.'|'.$lng;
231
					$lata = round($lat, 5);
236
					$lata = round($lat, 5);
232
					$lnga = round($lng, 5);
237
					$lnga = round($lng, 5);
233
					$nom = $this->etreNull($nom) ? $lata.','.$lnga : $nom;
238
					$nom = $this->etreNull($nom) ? $lata.','.$lnga : $nom;
234
 
239
 
235
					if (!isset($points[$id])) {
240
					if (!isset($points[$id])) {
236
						$points[$id]['id'] = $id;
241
						$points[$id]['id'] = $id;
237
						$points[$id]['nom'] = $nom;
242
						$points[$id]['nom'] = $nom;
238
						$points[$id]['lat'] = $lata;
243
						$points[$id]['lat'] = $lata;
239
						$points[$id]['lng'] = $lnga;
244
						$points[$id]['lng'] = $lnga;
240
						$points[$id]['nbre'] = 1;
245
						$points[$id]['nbre'] = 1;
241
						$marqueurs['stats']['stations']++;
246
						$marqueurs['stats']['stations']++;
242
					} else {
247
					} else {
243
						$points[$id]['nbre']++;
248
						$points[$id]['nbre']++;
244
					}
249
					}
245
				}
250
				}
246
			}
251
			}
247
		}
252
		}
248
 
253
 
249
		if (isset($this->parametres['ne']) && $this->parametres['sw'] && ! $this->etreNull($this->parametres['ne']) && ! $this->etreNull($this->parametres['ne']) && ! $this->etreNull($this->parametres['sw'])) {
254
		if (isset($this->parametres['ne']) && $this->parametres['sw'] && ! $this->etreNull($this->parametres['ne']) && ! $this->etreNull($this->parametres['ne']) && ! $this->etreNull($this->parametres['sw'])) {
250
			$ne = $this->decomposerLatLng($this->parametres['ne']);
255
			$ne = $this->decomposerLatLng($this->parametres['ne']);
251
			$sw = $this->decomposerLatLng($this->parametres['sw']);
256
			$sw = $this->decomposerLatLng($this->parametres['sw']);
252
			$marqueurs['points'] = CartoGroupage::creerGroupesQuadtree($points, $ne['lat'], $ne['lng'], $sw['lat'], $sw['lng'], $zoom);
257
			$marqueurs['points'] = CartoGroupage::creerGroupesQuadtree($points, $ne['lat'], $ne['lng'], $sw['lat'], $sw['lng'], $zoom);
253
		} else {
258
		} else {
254
			$marqueurs['points'] = $points;
259
			$marqueurs['points'] = $points;
255
		}
260
		}
256
		//$marqueurs['stats']['latDiff'] = abs($marqueurs['stats']['latMin'] - $marqueurs['stats']['latMax']);
261
		//$marqueurs['stats']['latDiff'] = abs($marqueurs['stats']['latMin'] - $marqueurs['stats']['latMax']);
257
		//$marqueurs['stats']['lngDiff'] = abs($marqueurs['stats']['lngMin'] - $marqueurs['stats']['lngMax']);
262
		//$marqueurs['stats']['lngDiff'] = abs($marqueurs['stats']['lngMin'] - $marqueurs['stats']['lngMax']);
258
 
263
 
259
		return $marqueurs;
264
		return $marqueurs;
260
	}
265
	}
261
 
266
 
262
	private function definirLatLngMaxMin(&$marqueurs, $lat, $lng) {
267
	private function definirLatLngMaxMin(&$marqueurs, $lat, $lng) {
263
		if ($lat != null && $lng != null) {
268
		if ($lat != null && $lng != null) {
264
			$marqueurs['stats']['latMin'] = $marqueurs['stats']['latMin'] > $lat ? $lat : $marqueurs['stats']['latMin'];
269
			$marqueurs['stats']['latMin'] = $marqueurs['stats']['latMin'] > $lat ? $lat : $marqueurs['stats']['latMin'];
265
			$marqueurs['stats']['lngMin'] = $marqueurs['stats']['lngMin'] > $lng ? $lng : $marqueurs['stats']['lngMin'];
270
			$marqueurs['stats']['lngMin'] = $marqueurs['stats']['lngMin'] > $lng ? $lng : $marqueurs['stats']['lngMin'];
266
			$marqueurs['stats']['latMax'] = $marqueurs['stats']['latMax'] < $lat ? $lat : $marqueurs['stats']['latMax'];
271
			$marqueurs['stats']['latMax'] = $marqueurs['stats']['latMax'] < $lat ? $lat : $marqueurs['stats']['latMax'];
267
			$marqueurs['stats']['lngMax'] = $marqueurs['stats']['lngMax'] < $lng ? $lng : $marqueurs['stats']['lngMax'];
272
			$marqueurs['stats']['lngMax'] = $marqueurs['stats']['lngMax'] < $lng ? $lng : $marqueurs['stats']['lngMax'];
268
		}
273
		}
269
	}
274
	}
270
 
275
 
271
	private function traiterNbreObs($resultats) {
276
	private function traiterNbreObs($resultats) {
272
		$obs_nbre = 0;
277
		$obs_nbre = 0;
273
		if ($resultats !== false) {
278
		if ($resultats !== false) {
274
			$obs_nbre = count($resultats);
279
			$obs_nbre = count($resultats);
275
		}
280
		}
276
		return $obs_nbre;
281
		return $obs_nbre;
277
	}
282
	}
278
 
283
 
279
	private function verifierLatLng($lat, $lng) {
284
	private function verifierLatLng($lat, $lng) {
280
		$ok_lat = $this->etreLatitude($lat) ? true : false;
285
		$ok_lat = $this->etreLatitude($lat) ? true : false;
281
		$ok_lng = $this->etreLongitude($lng) ? true : false;
286
		$ok_lng = $this->etreLongitude($lng) ? true : false;
282
		$ok = $ok_lat && $ok_lng;
287
		$ok = $ok_lat && $ok_lng;
283
		return $ok;
288
		return $ok;
284
	}
289
	}
285
 
290
 
286
	private function etreLatitude($lat) {
291
	private function etreLatitude($lat) {
287
		$ok = false;
292
		$ok = false;
288
		//$format =  preg_match('/^[-]?[0-9]+(?:[.][0-9]+|)$/', $lat) ? true : false;
293
		//$format =  preg_match('/^[-]?[0-9]+(?:[.][0-9]+|)$/', $lat) ? true : false;
289
		$ok = ($lat >= -90 && $lat <= 90) ? true : false;
294
		$ok = ($lat >= -90 && $lat <= 90) ? true : false;
290
		return $ok;
295
		return $ok;
291
	}
296
	}
292
 
297
 
293
	private function etreLongitude($lng) {
298
	private function etreLongitude($lng) {
294
		$ok = false;
299
		$ok = false;
295
		//$format =  preg_match('/^[-]?[0-9]+(?:[.][0-9]+|)$/', $lng) ? true : false;
300
		//$format =  preg_match('/^[-]?[0-9]+(?:[.][0-9]+|)$/', $lng) ? true : false;
296
		$ok = ($lng >= -180 && $lng <= 180) ? true : false;
301
		$ok = ($lng >= -180 && $lng <= 180) ? true : false;
297
		return $ok;
302
		return $ok;
298
	}
303
	}
299
 
304
 
300
	private function etreObsSensible($tags) {
305
	private function etreObsSensible($tags) {
301
		$sensible = true;
306
		$sensible = true;
302
		if (stristr($tags, 'sensible') === FALSE) {
307
		if (stristr($tags, 'sensible') === FALSE) {
303
			$sensible = false;
308
			$sensible = false;
304
		}
309
		}
305
		return $sensible;
310
		return $sensible;
306
	}
311
	}
307
 
312
 
308
	private function communeEstDemandee() {
313
	private function communeEstDemandee() {
309
	    $station_infos = $this->decomposerParametreStation();
314
	    $station_infos = $this->decomposerParametreStation();
310
	    $commune_demandee = true;
315
	    $commune_demandee = true;
311
	    if($station_infos['type'] == self::MARQUEUR_STATION) {
316
	    if($station_infos['type'] == self::MARQUEUR_STATION) {
312
			$commune_demandee = false;
317
			$commune_demandee = false;
313
	    }
318
	    }
314
	    return $commune_demandee;
319
	    return $commune_demandee;
315
	}
320
	}
316
 
321
 
317
	/**
322
	/**
318
	 * Données pour l'affichage des obs d'une station
323
	 * Données pour l'affichage des obs d'une station
319
	 */
324
	 */
320
	public function getObservations($params) {
325
	public function getObservations($params) {
321
		$resultats = array();
326
		$resultats = array();
322
		$total = 0;
327
		$total = 0;
323
		if (isset($this->parametres['station']) && !$this->etreNull($this->parametres['station'])) {
328
		if (isset($this->parametres['station']) && !$this->etreNull($this->parametres['station'])) {
324
			$requete = 	'SELECT SQL_CALC_FOUND_ROWS id_observation, ce_utilisateur, courriel_utilisateur, nom_utilisateur, prenom_utilisateur, '.
329
			$requete = 	'SELECT SQL_CALC_FOUND_ROWS id_observation, ce_utilisateur, courriel_utilisateur, nom_utilisateur, prenom_utilisateur, '.
325
						'	nom_sel, nom_ret, nom_sel_nn, nom_ret_nn, nt, famille, '.
330
						'	nom_sel, nom_ret, nom_sel_nn, nom_ret_nn, nt, famille, '.
326
						'	lieudit, zone_geo, date_observation, milieu, commentaire, '.
331
						'	lieudit, zone_geo, date_observation, milieu, commentaire, '.
327
						'	utm_secteur, utm_x, utm_y, code, date_transmission, nom_referentiel '.
332
						'	utm_secteur, utm_x, utm_y, code, date_transmission, nom_referentiel '.
328
						'FROM cel_obs AS co '.
333
						'FROM cel_obs AS co '.
329
						'	LEFT JOIN cel_zones_geo AS l '.
334
						'	LEFT JOIN cel_zones_geo AS l '.
330
						"		ON (l.nom = co.zone_geo AND l.id_zone_geo = co.ce_zone_geo) ".
335
						"		ON (l.nom = co.zone_geo AND l.id_zone_geo = co.ce_zone_geo) ".
331
						"WHERE transmission = '1' ".
336
						"WHERE transmission = '1' ".
332
						(($this->communeEstDemandee()) ? $this->construireWhereCommuneSansCoordonneesAvecSensibles() : $this->construireWhereCoordonneesSansSensibles()).
337
						(($this->communeEstDemandee()) ? $this->construireWhereCommuneSansCoordonneesAvecSensibles() : $this->construireWhereCoordonneesSansSensibles()).
333
						$this->construireWhereDept().
338
						$this->construireWhereDept().
334
						$this->construireWhereUtilisateur().
339
						$this->construireWhereUtilisateur().
335
						$this->construireWhereNumTaxonAvecSousTaxons().
340
						$this->construireWhereNumTaxonAvecSousTaxons().
336
						$this->construireWhereNomTaxon().
341
						$this->construireWhereNomTaxon().
337
						$this->construireWhereReferentiel().
342
						$this->construireWhereReferentiel().
338
						$this->construireWhereDate().
343
						$this->construireWhereDate().
339
						$this->construireWhereCommentaire().
344
						$this->construireWhereCommentaire().
340
						$this->construireWherePhotosSeulement().
345
						$this->construireWherePhotosSeulement().
341
						$this->construireWhereProjet().
346
						$this->construireWhereProjet().
342
						$this->construireWhereTag().
347
						$this->construireWhereTag().
343
						$this->construireWhereNombreDeJours().
348
						$this->construireWhereNombreDeJours().
344
	                    'ORDER BY nom_sel ASC '.
349
	                    'ORDER BY nom_sel ASC '.
345
	                    "LIMIT {$this->start},{$this->limit} ";
350
	                    "LIMIT {$this->start},{$this->limit} ";
346
			//echo $requete;exit;
351
			//echo $requete;exit;
347
			$resultats = Cel::db()->requeter($requete, self::SQL_RETOUR_COMPLET, self::SQL_MODE_OBJET);
352
			$resultats = Cel::db()->requeter($requete, self::SQL_RETOUR_COMPLET, self::SQL_MODE_OBJET);
348
			$requete = 'SELECT FOUND_ROWS()';
353
			$requete = 'SELECT FOUND_ROWS()';
349
			$total = (int) Cel::db()->requeter($requete, self::SQL_RETOUR_COLONNE);
354
			$total = (int) Cel::db()->requeter($requete, self::SQL_RETOUR_COLONNE);
350
		}
355
		}
351
 
356
 
352
		// Post-traitement
357
		// Post-traitement
353
		$observations = $this->traiterObservations($resultats, $total);
358
		$observations = $this->traiterObservations($resultats, $total);
354
		$observations = $this->ajouterImagesAuxObs($observations);
359
		$observations = $this->ajouterImagesAuxObs($observations);
355
		$observations = $this->ajouterAuteursAuxObs($observations);
360
		$observations = $this->ajouterAuteursAuxObs($observations);
356
		$observations = $this->supprimerIdDesObs($observations);
361
		$observations = $this->supprimerIdDesObs($observations);
357
 
362
 
358
		return $observations;
363
		return $observations;
359
	}
364
	}
360
 
365
 
361
	private function traiterObservations($donnees, $total) {
366
	private function traiterObservations($donnees, $total) {
362
		$observations = array('commune' => '', 'observations' => array(), 'observateurs' => array());
367
		$observations = array('commune' => '', 'observations' => array(), 'observateurs' => array());
363
		$observations['total'] = (isset($total)) ? $total : 0;
368
		$observations['total'] = (isset($total)) ? $total : 0;
364
		if (is_array($donnees) && count($donnees) > 0) {
369
		if (is_array($donnees) && count($donnees) > 0) {
365
			foreach ($donnees as $donnee) {
370
			foreach ($donnees as $donnee) {
366
				//echo '<pre>'.print_r($donnee,true).'</pre>';exit;
371
				//echo '<pre>'.print_r($donnee,true).'</pre>';exit;
367
				$observation = array();
372
				$observation = array();
368
				$observation['idObs'] = $donnee->id_observation;
373
				$observation['idObs'] = $donnee->id_observation;
369
				$observation['nn'] = $this->etreNull($donnee->nom_sel_nn) ? null : $donnee->nom_sel_nn;
374
				$observation['nn'] = $this->etreNull($donnee->nom_sel_nn) ? null : $donnee->nom_sel_nn;
370
				$observation['nomSci'] = $this->nettoyerTexte($donnee->nom_sel);
375
				$observation['nomSci'] = $this->nettoyerTexte($donnee->nom_sel);
371
				$observation['date'] = ($donnee->date_observation != '0000-00-00 00:00:00') ? $this->formaterDate($donnee->date_observation, '%d/%m/%Y') : '';
376
				$observation['date'] = ($donnee->date_observation != '0000-00-00 00:00:00') ? $this->formaterDate($donnee->date_observation, '%d/%m/%Y') : '';
372
				$observation['datePubli'] = $this->formaterDate($donnee->date_transmission);
377
				$observation['datePubli'] = $this->formaterDate($donnee->date_transmission);
373
				$observation['lieu'] = $this->traiterLieu($donnee);
378
				$observation['lieu'] = $this->traiterLieu($donnee);
374
				$observation['observateur'] = $donnee->courriel_utilisateur;
379
				$observation['observateur'] = $donnee->courriel_utilisateur;
375
				$observation['observateurId'] = $donnee->ce_utilisateur;
380
				$observation['observateurId'] = $donnee->ce_utilisateur;
376
				$observation['urlEflore'] = $this->getUrlEflore($donnee->nom_referentiel, $donnee->nom_sel_nn);
381
				$observation['urlEflore'] = $this->getUrlEflore($donnee->nom_referentiel, $donnee->nom_sel_nn);
377
 
382
 
378
				if (isset($donnee->zone_geo)) {
383
				if (isset($donnee->zone_geo)) {
379
					$observations['commune'] = $this->nettoyerTexte($donnee->zone_geo);
384
					$observations['commune'] = $this->nettoyerTexte($donnee->zone_geo);
380
				}
385
				}
381
				$observations['observations'][$donnee->id_observation] = $observation;
386
				$observations['observations'][$donnee->id_observation] = $observation;
382
 
387
 
383
				if (! array_key_exists($donnee->ce_utilisateur, $observations['observateurs'])) {
388
				if (! array_key_exists($donnee->ce_utilisateur, $observations['observateurs'])) {
384
					$observations['observateurs'][$donnee->courriel_utilisateur] = $donnee->courriel_utilisateur;
389
					$observations['observateurs'][$donnee->courriel_utilisateur] = $donnee->courriel_utilisateur;
385
				}
390
				}
386
			}
391
			}
387
		}
392
		}
388
		return $observations;
393
		return $observations;
389
	}
394
	}
390
 
395
 
391
	private function traiterLieu($donnee) {
396
	private function traiterLieu($donnee) {
392
		$lieu = array();
397
		$lieu = array();
393
		if (!$this->etreNull($donnee->lieudit)) {
398
		if (!$this->etreNull($donnee->lieudit)) {
394
			$lieu[] = $donnee->lieudit;
399
			$lieu[] = $donnee->lieudit;
395
		}
400
		}
396
		if (!$this->etreNull($donnee->milieu)) {
401
		if (!$this->etreNull($donnee->milieu)) {
397
			$lieu[] = $donnee->milieu;
402
			$lieu[] = $donnee->milieu;
398
		}
403
		}
399
		return implode(', ', $lieu);
404
		return implode(', ', $lieu);
400
	}
405
	}
401
 
406
 
402
	private function chargerImages(Array $obs_ids) {
407
	private function chargerImages(Array $obs_ids) {
403
		// Récupération des données au format Json
408
		// Récupération des données au format Json
404
		$service = 'CelImage/liste-ids?obsId='.implode(',', $obs_ids);
409
		$service = 'CelImage/liste-ids?obsId='.implode(',', $obs_ids);
405
		$url = sprintf($this->config['settings']['baseURLServicesCelTpl'], $service);
410
		$url = sprintf($this->config['settings']['baseURLServicesCelTpl'], $service);
406
		$json = $this->getRestClient()->consulter($url);
411
		$json = $this->getRestClient()->consulter($url);
407
		$donnees = json_decode($json);
412
		$donnees = json_decode($json);
408
 
413
 
409
		// Post-traitement des données
414
		// Post-traitement des données
410
		$images = $this->traiterImages($donnees);
415
		$images = $this->traiterImages($donnees);
411
 
416
 
412
		return $images;
417
		return $images;
413
	}
418
	}
414
 
419
 
415
	private function traiterImages($donnees) {
420
	private function traiterImages($donnees) {
416
		$images = array();
421
		$images = array();
417
		if (count($donnees) > 0) {
422
		if (count($donnees) > 0) {
418
			foreach ($donnees as $id_obs => $id_images) {
423
			foreach ($donnees as $id_obs => $id_images) {
419
				foreach ($id_images as $id_img) {
424
				foreach ($id_images as $id_img) {
420
					$urls['idImg'] = $id_img;
425
					$urls['idImg'] = $id_img;
421
					$urls['guid'] = sprintf($this->config['settings']['guidImgTpl'], $id_img);
426
					$urls['guid'] = sprintf($this->config['settings']['guidImgTpl'], $id_img);
422
					$urls['miniature'] = $this->getUrlImage($id_img, 'CXS');
427
					$urls['miniature'] = $this->getUrlImage($id_img, 'CXS');
423
					$urls['normale'] = $this->getUrlImage($id_img, 'XL');
428
					$urls['normale'] = $this->getUrlImage($id_img, 'XL');
424
					$images[$id_obs][] = $urls;
429
					$images[$id_obs][] = $urls;
425
				}
430
				}
426
			}
431
			}
427
		}
432
		}
428
		return $images;
433
		return $images;
429
	}
434
	}
430
 
435
 
431
	private function ajouterImagesAuxObs($observations) {
436
	private function ajouterImagesAuxObs($observations) {
432
		$images = $this->chargerImages(array_keys($observations['observations']));
437
		$images = $this->chargerImages(array_keys($observations['observations']));
433
		foreach ($observations['observations'] as $id => $infos) {
438
		foreach ($observations['observations'] as $id => $infos) {
434
			if(isset($images[$id])) {
439
			if(isset($images[$id])) {
435
				$infos['images'] = $images[$id];
440
				$infos['images'] = $images[$id];
436
				$observations['observations'][$id] = $infos;
441
				$observations['observations'][$id] = $infos;
437
			}
442
			}
438
		}
443
		}
439
		return $observations;
444
		return $observations;
440
	}
445
	}
441
 
446
 
442
	private function ajouterAuteursAuxObs($observations) {
447
	private function ajouterAuteursAuxObs($observations) {
443
		$observateurs = $this->recupererUtilisateursIdentite(array_keys($observations['observateurs']));
448
		$observateurs = $this->recupererUtilisateursIdentite(array_keys($observations['observateurs']));
444
		unset($observations['observateurs']);
449
		unset($observations['observateurs']);
445
		foreach ($observations['observations'] as $id => $infos) {
450
		foreach ($observations['observations'] as $id => $infos) {
446
			$courriel = strtolower($infos['observateur']);
451
			$courriel = strtolower($infos['observateur']);
447
			if(isset($observateurs[$courriel])) {
452
			if(isset($observateurs[$courriel])) {
448
				$infos['observateur'] = $observateurs[$courriel]['intitule'];
453
				$infos['observateur'] = $observateurs[$courriel]['intitule'];
449
				$infos['observateurId'] = $observateurs[$courriel]['id'];
454
				$infos['observateurId'] = $observateurs[$courriel]['id'];
450
			}
455
			}
451
			$observations['observations'][$id] = $infos;
456
			$observations['observations'][$id] = $infos;
452
		}
457
		}
453
		return $observations;
458
		return $observations;
454
	}
459
	}
455
 
460
 
456
	private function supprimerIdDesObs($observations) {
461
	private function supprimerIdDesObs($observations) {
457
		// Le tableau de sortie ne doit pas avoir les id des obs en clé car sinon Jquery Template ne fonctionne pas
462
		// Le tableau de sortie ne doit pas avoir les id des obs en clé car sinon Jquery Template ne fonctionne pas
458
		$observationSansId = $observations;
463
		$observationSansId = $observations;
459
		unset($observationSansId['observations']);
464
		unset($observationSansId['observations']);
460
		foreach ($observations['observations'] as $id => $infos) {
465
		foreach ($observations['observations'] as $id => $infos) {
461
			$observationSansId['observations'][] = $infos;
466
			$observationSansId['observations'][] = $infos;
462
		}
467
		}
463
		return $observationSansId;
468
		return $observationSansId;
464
	}
469
	}
465
 
470
 
466
	/**
471
	/**
467
	 * Liste des taxons présents sur la carte
472
	 * Liste des taxons présents sur la carte
468
	 */
473
	 */
469
	public function getTaxons($params) {
474
	public function getTaxons($params) {
470
		$json = null;
475
		$json = null;
471
 
476
 
472
		$requete = 	'SELECT SQL_CALC_FOUND_ROWS DISTINCT nom_ret, nom_ret_nn, nt, famille '.
477
		$requete = 	'SELECT SQL_CALC_FOUND_ROWS DISTINCT nom_ret, nom_ret_nn, nt, famille '.
473
					'FROM cel_obs AS co '.
478
					'FROM cel_obs AS co '.
474
					'	LEFT JOIN cel_zones_geo AS l '.
479
					'	LEFT JOIN cel_zones_geo AS l '.
475
					'		ON (l.nom = co.zone_geo AND l.id_zone_geo = co.ce_zone_geo) '.
480
					'		ON (l.nom = co.zone_geo AND l.id_zone_geo = co.ce_zone_geo) '.
476
					"WHERE transmission = '1' ".
481
					"WHERE transmission = '1' ".
477
					"	AND nom_ret != '' ".
482
					"	AND nom_ret != '' ".
478
					$this->construireWhereDept().
483
					$this->construireWhereDept().
479
                    $this->construireWhereCommune().
484
                    $this->construireWhereCommune().
480
                    $this->construireWhereUtilisateur().
485
                    $this->construireWhereUtilisateur().
481
					$this->construireWhereNumTaxon().
486
					$this->construireWhereNumTaxon().
482
					$this->construireWhereNomTaxon().
487
					$this->construireWhereNomTaxon().
483
					$this->construireWhereReferentiel().
488
					$this->construireWhereReferentiel().
484
					$this->construireWhereDate().
489
					$this->construireWhereDate().
485
					$this->construireWhereCommentaire().
490
					$this->construireWhereCommentaire().
486
					$this->construireWherePhotosSeulement().
491
					$this->construireWherePhotosSeulement().
487
					$this->construireWhereProjet().
492
					$this->construireWhereProjet().
488
					$this->construireWhereTag().
493
					$this->construireWhereTag().
489
					$this->construireWhereNombreDeJours().
494
					$this->construireWhereNombreDeJours().
490
					'ORDER BY nom_ret ASC '.
495
					'ORDER BY nom_ret ASC '.
491
                    "LIMIT {$this->start},{$this->limit} ";
496
                    "LIMIT {$this->start},{$this->limit} ";
492
		//$this->debug[] = $requete;
497
		//$this->debug[] = $requete;
493
		$resultats = Cel::db()->requeter($requete, self::SQL_RETOUR_COMPLET, self::SQL_MODE_OBJET);
498
		$resultats = Cel::db()->requeter($requete, self::SQL_RETOUR_COMPLET, self::SQL_MODE_OBJET);
494
		//echo $requete;exit;
499
		//echo $requete;exit;
495
		$requete = 'SELECT FOUND_ROWS()';
500
		$requete = 'SELECT FOUND_ROWS()';
496
		$taxons['total'] = (int) Cel::db()->requeter($requete, self::SQL_RETOUR_COLONNE);
501
		$taxons['total'] = (int) Cel::db()->requeter($requete, self::SQL_RETOUR_COLONNE);
497
 
502
 
498
		// Post-traitement
503
		// Post-traitement
499
		$taxons['taxons'] = $this->traiterTaxons($resultats);
504
		$taxons['taxons'] = $this->traiterTaxons($resultats);
500
 
505
 
501
		return $taxons;
506
		return $taxons;
502
	}
507
	}
503
 
508
 
504
	private function traiterTaxons($donnees) {
509
	private function traiterTaxons($donnees) {
505
		$taxons = array();
510
		$taxons = array();
506
		if (is_array($donnees) && count($donnees) > 0) {
511
		if (is_array($donnees) && count($donnees) > 0) {
507
			foreach ($donnees as $donnee) {
512
			foreach ($donnees as $donnee) {
508
				if (!isset($taxons[$donnee->nt]) && ! $this->etreNull($donnee->nom_ret)) {
513
				if (!isset($taxons[$donnee->nt]) && ! $this->etreNull($donnee->nom_ret)) {
509
					$taxon = array();
514
					$taxon = array();
510
					$taxon['nn'] = $donnee->nom_ret_nn;
515
					$taxon['nn'] = $donnee->nom_ret_nn;
511
					$taxon['nt'] = $donnee->nt;
516
					$taxon['nt'] = $donnee->nt;
512
					$taxon['nom'] = $this->nettoyerTexte($donnee->nom_ret);
517
					$taxon['nom'] = $this->nettoyerTexte($donnee->nom_ret);
513
					$taxon['famille'] = $this->nettoyerTexte($donnee->famille);
518
					$taxon['famille'] = $this->nettoyerTexte($donnee->famille);
514
					$taxons[$donnee->nt] = $taxon;
519
					$taxons[$donnee->nt] = $taxon;
515
				}
520
				}
516
			}
521
			}
517
		}
522
		}
518
		$taxons = array_values($taxons);
523
		$taxons = array_values($taxons);
519
		return $taxons;
524
		return $taxons;
520
	}
525
	}
521
 
526
 
522
	private function construireWhereCoordonnees() {
527
	private function construireWhereCoordonnees() {
523
		$sql = '';
528
		$sql = '';
524
		// Récupération des coordonnées depuis l'id station
529
		// Récupération des coordonnées depuis l'id station
525
		extract($this->decomposerParametreStation());
530
		extract($this->decomposerParametreStation());
526
		if (isset($type)) {
531
		if (isset($type)) {
527
			if ($type == self::MARQUEUR_COMMUNE) {
532
			if ($type == self::MARQUEUR_COMMUNE) {
528
				$lat = Cel::db()->proteger($lat.'%');
533
				$lat = Cel::db()->proteger($lat.'%');
529
				$lng = Cel::db()->proteger($lng.'%');
534
				$lng = Cel::db()->proteger($lng.'%');
530
				$sql = " AND wgs84_latitude LIKE $lat AND wgs84_longitude LIKE $lng ";
535
				$sql = " AND wgs84_latitude LIKE $lat AND wgs84_longitude LIKE $lng ";
531
			}  else if ($type == self::MARQUEUR_STATION) {
536
			}  else if ($type == self::MARQUEUR_STATION) {
532
				$lat = Cel::db()->proteger($lat.'%');
537
				$lat = Cel::db()->proteger($lat.'%');
533
				$lng = Cel::db()->proteger($lng.'%');
538
				$lng = Cel::db()->proteger($lng.'%');
534
				$sql = " AND (latitude LIKE $lat AND longitude LIKE $lng) ";
539
				$sql = " AND (latitude LIKE $lat AND longitude LIKE $lng) ";
535
			}
540
			}
536
		}
541
		}
537
		return $sql;
542
		return $sql;
538
	}
543
	}
539
 
544
 
540
	private function construireWhereCoordonneesSansSensibles() {
545
	private function construireWhereCoordonneesSansSensibles() {
541
		$sql = '(';
546
		$sql = '(';
542
		// Récupération des coordonnées depuis l'id station
547
		// Récupération des coordonnées depuis l'id station
543
		extract($this->decomposerParametreStation());
548
		extract($this->decomposerParametreStation());
544
		if (isset($type)) {
549
		if (isset($type)) {
545
			if ($type == self::MARQUEUR_COMMUNE) {
550
			if ($type == self::MARQUEUR_COMMUNE) {
546
				$lat = Cel::db()->proteger($lat);
551
				$lat = Cel::db()->proteger($lat);
547
				$lng = Cel::db()->proteger($lng);
552
				$lng = Cel::db()->proteger($lng);
548
				$sql = " AND wgs84_latitude LIKE $lat AND wgs84_longitude LIKE $lng ";
553
				$sql = " AND wgs84_latitude LIKE $lat AND wgs84_longitude LIKE $lng ";
549
			}  else if ($type == self::MARQUEUR_STATION) {
554
			}  else if ($type == self::MARQUEUR_STATION) {
550
				$lat = Cel::db()->proteger($lat.'%');
555
				$lat = Cel::db()->proteger($lat.'%');
551
				$lng = Cel::db()->proteger($lng.'%');
556
				$lng = Cel::db()->proteger($lng.'%');
552
				$sql = " AND (latitude LIKE $lat AND longitude LIKE $lng) ";
557
				$sql = " AND (latitude LIKE $lat AND longitude LIKE $lng) ";
553
			}
558
			}
554
		}
559
		}
555
		$sql .= ' AND (mots_cles_texte IS NULL OR mots_cles_texte NOT LIKE "%sensible%" ) ';
560
		$sql .= ' AND (mots_cles_texte IS NULL OR mots_cles_texte NOT LIKE "%sensible%" ) ';
556
		return $sql;
561
		return $sql;
557
	}
562
	}
558
 
563
 
559
	private function construireWhereCommentaire() {
564
	private function construireWhereCommentaire() {
560
		$sql = '';
565
		$sql = '';
561
		list($type, $commentaire) = $this->decomposerParametreCommentaire();
566
		list($type, $commentaire) = $this->decomposerParametreCommentaire();
562
		if (!$this->etreNull($commentaire)) {
567
		if (!$this->etreNull($commentaire)) {
563
			$commentaire = Cel::db()->proteger('%'.$commentaire.'%');
568
			$commentaire = Cel::db()->proteger('%'.$commentaire.'%');
564
			switch ($type) {
569
			switch ($type) {
565
				case '*' :
570
				case '*' :
566
					$sql = $this->obtenirConditionPourCommentaires($commentaire);
571
					$sql = $this->obtenirConditionPourCommentaires($commentaire);
567
					$sql = " AND (commentaire LIKE $commentaire OR ($sql)) ";
572
					$sql = " AND (commentaire LIKE $commentaire OR ($sql)) ";
568
					break;
573
					break;
569
				case 'observation' :
574
				case 'observation' :
570
					$sql = " AND commentaire LIKE $commentaire ";
575
					$sql = " AND commentaire LIKE $commentaire ";
571
					break;
576
					break;
572
				case 'photo' :
577
				case 'photo' :
573
					$sql = ' AND '.$this->obtenirConditionPourCommentaires($commentaire).' ';
578
					$sql = ' AND '.$this->obtenirConditionPourCommentaires($commentaire).' ';
574
					break;
579
					break;
575
				case 'photo.meta' :
580
				case 'photo.meta' :
576
					$sql = ' AND '.$this->obtenirConditionPourCommentaireMeta($commentaire).' ';
581
					$sql = ' AND '.$this->obtenirConditionPourCommentaireMeta($commentaire).' ';
577
					break;
582
					break;
578
				case 'photo.utilisateur' :
583
				case 'photo.utilisateur' :
579
					$sql = ' AND '.$this->obtenirConditionPourCommentaireUtilisateur($commentaire).' ';
584
					$sql = ' AND '.$this->obtenirConditionPourCommentaireUtilisateur($commentaire).' ';
580
					break;
585
					break;
581
				default:
586
				default:
582
					$sql = " AND commentaire LIKE $commentaire ";
587
					$sql = " AND commentaire LIKE $commentaire ";
583
			}
588
			}
584
		}
589
		}
585
		return $sql;
590
		return $sql;
586
	}
591
	}
587
 
592
 
588
 
593
 
589
	private function construireWhereNomTaxon() {
594
	private function construireWhereNomTaxon() {
590
		$sql = '';
595
		$sql = '';
591
		list($type, $nom) = $this->decomposerParametreTaxon();
596
		list($type, $nom) = $this->decomposerParametreTaxon();
592
		if (!$this->etreNull($nom)) {
597
		if (!$this->etreNull($nom)) {
593
			$nom = Cel::db()->proteger($nom.'%');
598
			$nom = Cel::db()->proteger($nom.'%');
594
			switch ($type) {
599
			switch ($type) {
595
				case '*' :
600
				case '*' :
596
					$sql = " AND (nom_ret LIKE $nom OR nom_sel LIKE $nom OR famille LIKE $nom) ";
601
					$sql = " AND (nom_ret LIKE $nom OR nom_sel LIKE $nom OR famille LIKE $nom) ";
597
					break;
602
					break;
598
				case 'retenu' :
603
				case 'retenu' :
599
					$sql = " AND nom_ret LIKE $nom ";
604
					$sql = " AND nom_ret LIKE $nom ";
600
					break;
605
					break;
601
				case 'selectionne' :
606
				case 'selectionne' :
602
					$sql = " AND nom_sel LIKE $nom ";
607
					$sql = " AND nom_sel LIKE $nom ";
603
					break;
608
					break;
604
				case 'famille' :
609
				case 'famille' :
605
					$sql = " AND famille LIKE $nom ";
610
					$sql = " AND famille LIKE $nom ";
606
					break;
611
					break;
607
				default:
612
				default:
608
					$sql = " AND nom_ret LIKE $nom ";
613
					$sql = " AND nom_ret LIKE $nom ";
609
			}
614
			}
610
		}
615
		}
611
		return $sql;
616
		return $sql;
612
	}
617
	}
613
 
618
 
614
	private function construireWhereReferentiel() {
619
	private function construireWhereReferentiel() {
615
		$sql = '';
620
		$sql = '';
616
		extract($this->parametres);
621
		extract($this->parametres);
617
		if (isset($referentiel) && !$this->etreNull($referentiel)) {
622
		if (isset($referentiel) && !$this->etreNull($referentiel)) {
618
			$referentiel = Cel::db()->proteger($referentiel.'%');
623
			$referentiel = Cel::db()->proteger($referentiel.'%');
619
			$sql = ' AND co.nom_referentiel LIKE '.$referentiel.' ';
624
			$sql = ' AND co.nom_referentiel LIKE '.$referentiel.' ';
620
		}
625
		}
621
		return $sql;
626
		return $sql;
622
	}
627
	}
623
 
628
 
624
	private function construireWhereDate() {
629
	private function construireWhereDate() {
625
		$sql = '';
630
		$sql = '';
626
		// Récupération des coordonnées depuis l'id station
631
		// Récupération des coordonnées depuis l'id station
627
		list($type, $date) = $this->decomposerParametreDate();
632
		list($type, $date) = $this->decomposerParametreDate();
628
 
633
 
629
		if (!$this->etreNull($date)) {
634
		if (!$this->etreNull($date)) {
630
			$date = Cel::db()->proteger($date.'%');
635
			$date = Cel::db()->proteger($date.'%');
631
			switch ($type) {
636
			switch ($type) {
632
				case '*' :
637
				case '*' :
633
					$sql = " AND (
638
					$sql = " AND (
634
						date_observation LIKE $date
639
						date_observation LIKE $date
635
						OR date_creation LIKE $date
640
						OR date_creation LIKE $date
636
						OR date_modification LIKE $date
641
						OR date_modification LIKE $date
637
						OR date_transmission LIKE $date) ";
642
						OR date_transmission LIKE $date) ";
638
					break;
643
					break;
639
				case 'observation' :
644
				case 'observation' :
640
					$sql = " AND date_observation LIKE $date ";
645
					$sql = " AND date_observation LIKE $date ";
641
					break;
646
					break;
642
				case 'creation' :
647
				case 'creation' :
643
					$sql = " AND date_creation LIKE $date ";
648
					$sql = " AND date_creation LIKE $date ";
644
					break;
649
					break;
645
				case 'modification' :
650
				case 'modification' :
646
					$sql = " AND date_modification LIKE $date ";
651
					$sql = " AND date_modification LIKE $date ";
647
					break;
652
					break;
648
				case 'transmission' :
653
				case 'transmission' :
649
					$sql = " AND date_transmission LIKE $date ";
654
					$sql = " AND date_transmission LIKE $date ";
650
					break;
655
					break;
651
				case 'photo' :
656
				case 'photo' :
652
					$sql = $this->obtenirConditionPourDatePhoto($date);
657
					$sql = $this->obtenirConditionPourDatePhoto($date);
653
					break;
658
					break;
654
				case 'ajout' :
659
				case 'ajout' :
655
					$sql = $this->obtenirConditionPourDateAjout($date);
660
					$sql = $this->obtenirConditionPourDateAjout($date);
656
					break;
661
					break;
657
				case 'liaison' :
662
				case 'liaison' :
658
					$sql = $this->obtenirConditionPourDateLiaison($date);
663
					$sql = $this->obtenirConditionPourDateLiaison($date);
659
					break;
664
					break;
660
				default:
665
				default:
661
					$sql = " AND date_observation LIKE $date ";
666
					$sql = " AND date_observation LIKE $date ";
662
			}
667
			}
663
		}
668
		}
664
		return $sql;
669
		return $sql;
665
	}
670
	}
666
 
671
 
667
	private function obtenirConditionPourDatePhoto($date) {
672
	private function obtenirConditionPourDatePhoto($date) {
668
		$observations = $this->obtenirObsLieesImg('date.photo', $date);
673
		$observations = $this->obtenirObsLieesImg('date.photo', $date);
669
		if (is_null($observations)) {
674
		if (is_null($observations)) {
670
			$this->debug[] = "Aucune observation n'est liée à une photo prise à la date : $date";
675
			$this->debug[] = "Aucune observation n'est liée à une photo prise à la date : $date";
671
		}
676
		}
672
		$sql = $this->assemblerObsEnConditionSql($observations);
677
		$sql = $this->assemblerObsEnConditionSql($observations);
673
		return $sql;
678
		return $sql;
674
	}
679
	}
675
 
680
 
676
	private function obtenirConditionPourDateLiaison($date) {
681
	private function obtenirConditionPourDateLiaison($date) {
677
		$observations = $this->obtenirObsLieesImg('date.liaison', $date);
682
		$observations = $this->obtenirObsLieesImg('date.liaison', $date);
678
		if (is_null($observations)) {
683
		if (is_null($observations)) {
679
			$this->debug[] = "Aucune observation n'a été liée à une image à à la date : $date";
684
			$this->debug[] = "Aucune observation n'a été liée à une image à à la date : $date";
680
		}
685
		}
681
		$sql = $this->assemblerObsEnConditionSql($observations);
686
		$sql = $this->assemblerObsEnConditionSql($observations);
682
		return $sql;
687
		return $sql;
683
	}
688
	}
684
 
689
 
685
	private function obtenirConditionPourDateAjout($date) {
690
	private function obtenirConditionPourDateAjout($date) {
686
		$observations = $this->obtenirObsLieesImg('date.ajout', $date);
691
		$observations = $this->obtenirObsLieesImg('date.ajout', $date);
687
		if (is_null($observations)) {
692
		if (is_null($observations)) {
688
			$this->debug[] = "Aucune observation n'est liée à une image ajoutée à la date : $date";
693
			$this->debug[] = "Aucune observation n'est liée à une image ajoutée à la date : $date";
689
		}
694
		}
690
		$sql = $this->assemblerObsEnConditionSql($observations);
695
		$sql = $this->assemblerObsEnConditionSql($observations);
691
		return $sql;
696
		return $sql;
692
	}
697
	}
693
 
698
 
694
	private function obtenirConditionPourCommentaireMeta($commentaire) {
699
	private function obtenirConditionPourCommentaireMeta($commentaire) {
695
		$observations = $this->obtenirObsLieesImg('commentaire.meta', $commentaire);
700
		$observations = $this->obtenirObsLieesImg('commentaire.meta', $commentaire);
696
		if (is_null($observations)) {
701
		if (is_null($observations)) {
697
			$this->debug[] = "Aucune observation n'est liée à une image dont le commentaire des méta-données correspond à : $commmentaire";
702
			$this->debug[] = "Aucune observation n'est liée à une image dont le commentaire des méta-données correspond à : $commmentaire";
698
		}
703
		}
699
		$operateur = '';
704
		$operateur = '';
700
		$sql = $this->assemblerObsEnConditionSql($observations, $operateur);
705
		$sql = $this->assemblerObsEnConditionSql($observations, $operateur);
701
		return $sql;
706
		return $sql;
702
	}
707
	}
703
 
708
 
704
	private function obtenirConditionPourCommentaireUtilisateur($commentaire) {
709
	private function obtenirConditionPourCommentaireUtilisateur($commentaire) {
705
		$observations = $this->obtenirObsLieesImg('commentaire.utilisateur', $commentaire);
710
		$observations = $this->obtenirObsLieesImg('commentaire.utilisateur', $commentaire);
706
		if (is_null($observations)) {
711
		if (is_null($observations)) {
707
			$this->debug[] = "Aucune observation n'est liée à une image dont le commentaire des utilisateur correspond à : $commmentaire";
712
			$this->debug[] = "Aucune observation n'est liée à une image dont le commentaire des utilisateur correspond à : $commmentaire";
708
		}
713
		}
709
		$operateur = '';
714
		$operateur = '';
710
		$sql = $this->assemblerObsEnConditionSql($observations, $operateur);
715
		$sql = $this->assemblerObsEnConditionSql($observations, $operateur);
711
		return $sql;
716
		return $sql;
712
	}
717
	}
713
 
718
 
714
	private function obtenirConditionPourCommentaires($commentaire) {
719
	private function obtenirConditionPourCommentaires($commentaire) {
715
		$observations = $this->obtenirObsLieesImg('commentaire.*', $commentaire);
720
		$observations = $this->obtenirObsLieesImg('commentaire.*', $commentaire);
716
		if (is_null($observations)) {
721
		if (is_null($observations)) {
717
			$this->debug[] = "Aucune observation n'est liée à une image dont un des commentaires correspond à : $commmentaire";
722
			$this->debug[] = "Aucune observation n'est liée à une image dont un des commentaires correspond à : $commmentaire";
718
		}
723
		}
719
		$operateur = '';
724
		$operateur = '';
720
		$sql = $this->assemblerObsEnConditionSql($observations, $operateur);
725
		$sql = $this->assemblerObsEnConditionSql($observations, $operateur);
721
		return $sql;
726
		return $sql;
722
	}
727
	}
723
 
728
 
724
	/**
729
	/**
725
	 * Récupération des identifiant d'utilisateur et des ordres des observations correspondant à une date.
730
	 * Récupération des identifiant d'utilisateur et des ordres des observations correspondant à une date.
726
	 * Retour sous forme de tableau : array[identifiant] = array(ordre, ordre...);
731
	 * Retour sous forme de tableau : array[identifiant] = array(ordre, ordre...);
727
	 */
732
	 */
728
	private function obtenirObsLieesImg($type, $param) {
733
	private function obtenirObsLieesImg($type, $param) {
729
		// Construction de la requête
734
		// Construction de la requête
730
		$requete = 	'SELECT DISTINCT co.id_obs,  ci.ce_utilisateur AS utilisateur '.
735
		$requete = 	'SELECT DISTINCT co.id_obs,  ci.ce_utilisateur AS utilisateur '.
731
			'FROM  cel_images '.
736
			'FROM  cel_images '.
732
			'	LEFT JOIN cel_obs_images coi '.
737
			'	LEFT JOIN cel_obs_images coi '.
733
			'		ON (ci.id_image = coi.id_image) '.
738
			'		ON (ci.id_image = coi.id_image) '.
734
			'	LEFT JOIN cel_obs AS co '.
739
			'	LEFT JOIN cel_obs AS co '.
735
			'		ON (coi.id_observation = co.id_observation) '.
740
			'		ON (coi.id_observation = co.id_observation) '.
736
			'	LEFT JOIN cel_zones_geo AS l '.
741
			'	LEFT JOIN cel_zones_geo AS l '.
737
			'		ON (l.nom = co.zone_geo AND l.id_zone_geo = co.ce_zone_geo) '.
742
			'		ON (l.nom = co.zone_geo AND l.id_zone_geo = co.ce_zone_geo) '.
738
			"WHERE  transmission = '1' ".
743
			"WHERE  transmission = '1' ".
739
			($type == 'date.photo' ? " AND (ci_meta_date_time LIKE ".str_replace('-', ':', $param)." OR ci_meta_date LIKE $param) " : '').
744
			($type == 'date.photo' ? " AND (ci_meta_date_time LIKE ".str_replace('-', ':', $param)." OR ci_meta_date LIKE $param) " : '').
740
			($type == 'date.ajout' ? " AND ci_meta_date_ajout LIKE $param " : '').
745
			($type == 'date.ajout' ? " AND ci_meta_date_ajout LIKE $param " : '').
741
			($type == 'date.liaison' ? " AND coi_date_liaison LIKE $param " : '').
746
			($type == 'date.liaison' ? " AND coi_date_liaison LIKE $param " : '').
742
			($type == 'commentaire.meta' ? " AND ci_meta_comment LIKE $param " : '').
747
			($type == 'commentaire.meta' ? " AND ci_meta_comment LIKE $param " : '').
743
			($type == 'commentaire.utilisateur' ? "	AND ci_meta_user_comment LIKE $param " : '').
748
			($type == 'commentaire.utilisateur' ? "	AND ci_meta_user_comment LIKE $param " : '').
744
			($type == 'commentaire.*' ? " AND (ci_meta_comment LIKE $param OR ci_meta_user_comment LIKE $param) " : '').
749
			($type == 'commentaire.*' ? " AND (ci_meta_comment LIKE $param OR ci_meta_user_comment LIKE $param) " : '').
745
			$this->construireWhereCoordonnees().
750
			$this->construireWhereCoordonnees().
746
			$this->construireWhereDept().
751
			$this->construireWhereDept().
747
			$this->construireWhereCommune().
752
			$this->construireWhereCommune().
748
			$this->construireWhereUtilisateur().
753
			$this->construireWhereUtilisateur().
749
			$this->construireWhereNumTaxon().
754
			$this->construireWhereNumTaxon().
750
			$this->construireWhereNomTaxon().
755
			$this->construireWhereNomTaxon().
751
			$this->construireWhereReferentiel().
756
			$this->construireWhereReferentiel().
752
			$this->construireWhereProjet().
757
			$this->construireWhereProjet().
753
			$this->construireWhereTag().
758
			$this->construireWhereTag().
754
			'ORDER BY utilisateur ASC, ordre ASC';
759
			'ORDER BY utilisateur ASC, ordre ASC';
755
		//$this->debug[] = $requete;
760
		//$this->debug[] = $requete;
756
		//die($requete);
761
		//die($requete);
757
		$resultats = Cel::db()->requeter($requete);
762
		$resultats = Cel::db()->requeter($requete);
758
 
763
 
759
		$observations = null;
764
		$observations = null;
760
		if ($resultats != false) {
765
		if ($resultats != false) {
761
			$observations = array();
766
			$observations = array();
762
			foreach ($resultats as $occurence) {
767
			foreach ($resultats as $occurence) {
763
				$utilisateur = $occurence['utilisateur'];
768
				$utilisateur = $occurence['utilisateur'];
764
				$ordre = $occurence['ordre'];
769
				$ordre = $occurence['ordre'];
765
				if (!array_key_exists($utilisateur, $observations)) {
770
				if (!array_key_exists($utilisateur, $observations)) {
766
					$observations[$utilisateur] = array();
771
					$observations[$utilisateur] = array();
767
				}
772
				}
768
				if (!array_key_exists($ordre, $observations[$utilisateur])) {
773
				if (!array_key_exists($ordre, $observations[$utilisateur])) {
769
					$observations[$utilisateur][$ordre] = $ordre;
774
					$observations[$utilisateur][$ordre] = $ordre;
770
				}
775
				}
771
			}
776
			}
772
		}
777
		}
773
		return $observations;
778
		return $observations;
774
	}
779
	}
775
 
780
 
776
	private function assemblerObsEnConditionSql($observations, $operateur = 'AND') {
781
	private function assemblerObsEnConditionSql($observations, $operateur = 'AND') {
777
		$sql = '';
782
		$sql = '';
778
		if ($observations != null) {
783
		if ($observations != null) {
779
			// Pré-construction du where de la requête
784
			// Pré-construction du where de la requête
780
			$tpl_where = "(identifiant = '%s' AND ordre IN (%s))";
785
			$tpl_where = "(identifiant = '%s' AND ordre IN (%s))";
781
			foreach ($observations as $utilisateur => $ordres) {
786
			foreach ($observations as $utilisateur => $ordres) {
782
				$morceaux_requete[] = sprintf($tpl_where, $utilisateur, implode(',', $ordres));
787
				$morceaux_requete[] = sprintf($tpl_where, $utilisateur, implode(',', $ordres));
783
			}
788
			}
784
			if (count($morceaux_requete) > 0) {
789
			if (count($morceaux_requete) > 0) {
785
				$sql = implode(" \nOR ", $morceaux_requete);
790
				$sql = implode(" \nOR ", $morceaux_requete);
786
			}
791
			}
787
		} else {
792
		} else {
788
			// Nous voulons que la requête ne retourne rien
793
			// Nous voulons que la requête ne retourne rien
789
			$sql = "identifiant = '' AND ordre = ''";
794
			$sql = "identifiant = '' AND ordre = ''";
790
		}
795
		}
791
		$sql = " $operateur ($sql) ";
796
		$sql = " $operateur ($sql) ";
792
		return $sql;
797
		return $sql;
793
	}
798
	}
794
 
799
 
795
	private function construireWhereRectangleStation() {
800
	private function construireWhereRectangleStation() {
796
		$sql = '';
801
		$sql = '';
797
		if (isset($this->parametres['ne']) && isset($this->parametres['sw']) && ! $this->etreNull($this->parametres['ne']) && ! $this->etreNull($this->parametres['sw'])) {
802
		if (isset($this->parametres['ne']) && isset($this->parametres['sw']) && ! $this->etreNull($this->parametres['ne']) && ! $this->etreNull($this->parametres['sw'])) {
798
			$ne = $this->decomposerLatLng($this->parametres['ne']);
803
			$ne = $this->decomposerLatLng($this->parametres['ne']);
799
			$sw = $this->decomposerLatLng($this->parametres['sw']);
804
			$sw = $this->decomposerLatLng($this->parametres['sw']);
800
 
805
 
801
			$latMin = $sw['lat'];
806
			$latMin = $sw['lat'];
802
			$lngMin = $sw['lng'];
807
			$lngMin = $sw['lng'];
803
 
808
 
804
			$latMax = $ne['lat'];
809
			$latMax = $ne['lat'];
805
			$lngMax = $ne['lng'];
810
			$lngMax = $ne['lng'];
806
 
811
 
807
			// ATTENTION : latitude correspond bien à la LATITUDE!
812
			// ATTENTION : latitude correspond bien à la LATITUDE!
808
			$sql = " AND (latitude != 0 AND longitude != 0) ".
813
			$sql = " AND (latitude != 0 AND longitude != 0) ".
809
				" AND latitude > $latMin ".
814
				" AND latitude > $latMin ".
810
				" AND latitude < $latMax ".
815
				" AND latitude < $latMax ".
811
				" AND longitude > $lngMin ".
816
				" AND longitude > $lngMin ".
812
				" AND longitude < $lngMax ";
817
				" AND longitude < $lngMax ";
813
		}
818
		}
814
		return $sql;
819
		return $sql;
815
	}
820
	}
816
 
821
 
817
	private function construireWhereRectangleStationOR() {
822
	private function construireWhereRectangleStationOR() {
818
		$sql = '';
823
		$sql = '';
819
		if (isset($this->parametres['ne']) && isset($this->parametres['sw']) && ! $this->etreNull($this->parametres['ne']) && ! $this->etreNull($this->parametres['sw'])) {
824
		if (isset($this->parametres['ne']) && isset($this->parametres['sw']) && ! $this->etreNull($this->parametres['ne']) && ! $this->etreNull($this->parametres['sw'])) {
820
			$ne = $this->decomposerLatLng($this->parametres['ne']);
825
			$ne = $this->decomposerLatLng($this->parametres['ne']);
821
			$sw = $this->decomposerLatLng($this->parametres['sw']);
826
			$sw = $this->decomposerLatLng($this->parametres['sw']);
822
 
827
 
823
			$latMin = $sw['lat'];
828
			$latMin = $sw['lat'];
824
			$lngMin = $sw['lng'];
829
			$lngMin = $sw['lng'];
825
 
830
 
826
			$latMax = $ne['lat'];
831
			$latMax = $ne['lat'];
827
			$lngMax = $ne['lng'];
832
			$lngMax = $ne['lng'];
828
 
833
 
829
			$sql = "( (latitude != 0 AND longitude != 0) ".
834
			$sql = "( (latitude != 0 AND longitude != 0) ".
830
				" AND latitude BETWEEN $latMin AND $latMax ".
835
				" AND latitude BETWEEN $latMin AND $latMax ".
831
				" AND longitude BETWEEN $lngMin AND $lngMax )";
836
				" AND longitude BETWEEN $lngMin AND $lngMax )";
832
 
837
 
833
			/*$sql = " MBRWithin(mon_point, GeomFromText('POLYGON((".$latMin.' '.$lngMin.','.
838
			/*$sql = " MBRWithin(mon_point, GeomFromText('POLYGON((".$latMin.' '.$lngMin.','.
834
			$latMax.' '.$lngMin.','.
839
			$latMax.' '.$lngMin.','.
835
			$latMax.' '.$lngMax.','.
840
			$latMax.' '.$lngMax.','.
836
			$latMax.' '.$lngMin.','.
841
			$latMax.' '.$lngMin.','.
837
			$latMin.' '.$lngMin."))')) ";		*/
842
			$latMin.' '.$lngMin."))')) ";		*/
838
		}
843
		}
839
		return $sql;
844
		return $sql;
840
	}
845
	}
841
 
846
 
842
	private function construireWhereRectangleCommune() {
847
	private function construireWhereRectangleCommune() {
843
		$sql = '';
848
		$sql = '';
844
		if (isset($this->parametres['ne']) && isset($this->parametres['sw']) && ! $this->etreNull($this->parametres['ne']) && ! $this->etreNull($this->parametres['sw'])) {
849
		if (isset($this->parametres['ne']) && isset($this->parametres['sw']) && ! $this->etreNull($this->parametres['ne']) && ! $this->etreNull($this->parametres['sw'])) {
845
			$ne = $this->decomposerLatLng($this->parametres['ne']);
850
			$ne = $this->decomposerLatLng($this->parametres['ne']);
846
			$sw = $this->decomposerLatLng($this->parametres['sw']);
851
			$sw = $this->decomposerLatLng($this->parametres['sw']);
847
 
852
 
848
			$latMin = $sw['lat'];
853
			$latMin = $sw['lat'];
849
			$lngMin = $sw['lng'];
854
			$lngMin = $sw['lng'];
850
 
855
 
851
			$latMax = $ne['lat'];
856
			$latMax = $ne['lat'];
852
			$lngMax = $ne['lng'];
857
			$lngMax = $ne['lng'];
853
 
858
 
854
			$sql = "AND wgs84_longitude != 0 AND wgs84_latitude != 0 ".
859
			$sql = "AND wgs84_longitude != 0 AND wgs84_latitude != 0 ".
855
				" AND wgs84_latitude BETWEEN $latMin AND $latMax ".
860
				" AND wgs84_latitude BETWEEN $latMin AND $latMax ".
856
				" AND wgs84_longitude BETWEEN $lngMin AND $lngMax ";
861
				" AND wgs84_longitude BETWEEN $lngMin AND $lngMax ";
857
		}
862
		}
858
		return $sql;
863
		return $sql;
859
	}
864
	}
860
 
865
 
861
	private function construireWhereRectangleCommuneOR() {
866
	private function construireWhereRectangleCommuneOR() {
862
		$sql = '';
867
		$sql = '';
863
		if (isset($this->parametres['ne']) && isset($this->parametres['sw']) && ! $this->etreNull($this->parametres['ne']) && ! $this->etreNull($this->parametres['sw'])) {
868
		if (isset($this->parametres['ne']) && isset($this->parametres['sw']) && ! $this->etreNull($this->parametres['ne']) && ! $this->etreNull($this->parametres['sw'])) {
864
			$ne = $this->decomposerLatLng($this->parametres['ne']);
869
			$ne = $this->decomposerLatLng($this->parametres['ne']);
865
			$sw = $this->decomposerLatLng($this->parametres['sw']);
870
			$sw = $this->decomposerLatLng($this->parametres['sw']);
866
 
871
 
867
			$latMin = $sw['lat'];
872
			$latMin = $sw['lat'];
868
			$lngMin = $sw['lng'];
873
			$lngMin = $sw['lng'];
869
 
874
 
870
			$latMax = $ne['lat'];
875
			$latMax = $ne['lat'];
871
			$lngMax = $ne['lng'];
876
			$lngMax = $ne['lng'];
872
 
877
 
873
			$sql = "( wgs84_longitude != 0 AND wgs84_latitude != 0 ".
878
			$sql = "( wgs84_longitude != 0 AND wgs84_latitude != 0 ".
874
				" AND wgs84_latitude BETWEEN $latMin AND $latMax ".
879
				" AND wgs84_latitude BETWEEN $latMin AND $latMax ".
875
				" AND wgs84_longitude BETWEEN $lngMin AND $lngMax )";
880
				" AND wgs84_longitude BETWEEN $lngMin AND $lngMax )";
876
 
881
 
877
			/*$sql = " MBRWithin(point_commune, GeomFromText('POLYGON((".$latMin.' '.$lngMin.','.
882
			/*$sql = " MBRWithin(point_commune, GeomFromText('POLYGON((".$latMin.' '.$lngMin.','.
878
			$latMax.' '.$lngMin.','.
883
			$latMax.' '.$lngMin.','.
879
			$latMax.' '.$lngMax.','.
884
			$latMax.' '.$lngMax.','.
880
			$latMax.' '.$lngMin.','.
885
			$latMax.' '.$lngMin.','.
881
			$latMin.' '.$lngMin."))')) ";*/
886
			$latMin.' '.$lngMin."))')) ";*/
882
		}
887
		}
883
		return $sql;
888
		return $sql;
884
	}
889
	}
885
 
890
 
886
	private function construireWhereDept() {
891
	private function construireWhereDept() {
887
		$sql = '';
892
		$sql = '';
888
		// Récupération des coordonnées depuis l'id station
893
		// Récupération des coordonnées depuis l'id station
889
		extract($this->parametres);
894
		extract($this->parametres);
890
		if (isset($dept) && !$this->etreNull($dept)) {
895
		if (isset($dept) && !$this->etreNull($dept)) {
891
			$valeurs_a_proteger = explode(',',trim($dept));
896
			$valeurs_a_proteger = explode(',',trim($dept));
892
			foreach ($valeurs_a_proteger as $valeur) {
897
			foreach ($valeurs_a_proteger as $valeur) {
893
				$valeurs_protegees[] = '(ce_zone_geo LIKE '.Cel::db()->quote('INSEE-C:'.$valeur.'%').') ';
898
				$valeurs_protegees[] = '(ce_zone_geo LIKE '.Cel::db()->quote('INSEE-C:'.$valeur.'%').') ';
894
			}
899
			}
895
			$valeurs = implode(' OR ', $valeurs_protegees);
900
			$valeurs = implode(' OR ', $valeurs_protegees);
896
			$sql = " AND ($valeurs) ";
901
			$sql = " AND ($valeurs) ";
897
		}
902
		}
898
		return $sql;
903
		return $sql;
899
	}
904
	}
900
 
905
 
901
	private function construireWhereCommune() {
906
	private function construireWhereCommune() {
902
		$sql = '';
907
		$sql = '';
903
		// Récupération des coordonnées depuis l'id station
908
		// Récupération des coordonnées depuis l'id station
904
		extract($this->parametres);
909
		extract($this->parametres);
905
		if (isset($this->parametres['commune']) && !$this->etreNull($commune)) {
910
		if (isset($this->parametres['commune']) && !$this->etreNull($commune)) {
906
			$commune = Cel::db()->proteger($commune);
911
			$commune = Cel::db()->proteger($commune);
907
			$sql = " AND zone_geo LIKE $commune";
912
			$sql = " AND zone_geo LIKE $commune";
908
		}
913
		}
909
		if (isset($this->parametres['zonegeo']) && !$this->etreNull($zonegeo)) {
914
		if (isset($this->parametres['zonegeo']) && !$this->etreNull($zonegeo)) {
910
			$zonegeo = Cel::db()->proteger($zonegeo);
915
			$zonegeo = Cel::db()->proteger($zonegeo);
911
			$sql = " AND ce_zone_geo = $zonegeo";
916
			$sql = " AND ce_zone_geo = $zonegeo";
912
		}
917
		}
913
		return $sql;
918
		return $sql;
914
	}
919
	}
915
 
920
 
916
	private function construireWhereCommuneSansCoordonneesAvecSensibles() {
921
	private function construireWhereCommuneSansCoordonneesAvecSensibles() {
917
		$sql = '';
922
		$sql = '';
918
		// Récupération des coordonnées depuis l'id station
923
		// Récupération des coordonnées depuis l'id station
919
		extract($this->parametres);
924
		extract($this->parametres);
920
		if (isset($this->parametres['commune']) && !$this->etreNull($commune)) {
925
		if (isset($this->parametres['commune']) && !$this->etreNull($commune)) {
921
			$commune = Cel::db()->proteger($commune);
926
			$commune = Cel::db()->proteger($commune);
922
			$sql = " AND zone_geo LIKE $commune";
927
			$sql = " AND zone_geo LIKE $commune";
923
			$sql .= " AND (
928
			$sql .= " AND (
924
			           		(
929
			           		(
925
			           			(latitude = '000null' OR latitude = '' OR latitude = 0 OR latitude IS NULL) ".
930
			           			(latitude = '000null' OR latitude = '' OR latitude = 0 OR latitude IS NULL) ".
926
								" AND (longitude = '000null' OR longitude = '' OR longitude = 0 OR longitude IS NULL)".
931
								" AND (longitude = '000null' OR longitude = '' OR longitude = 0 OR longitude IS NULL)".
927
							')'.
932
							')'.
928
							' OR mots_cles_texte LIKE "%sensible%"'.
933
							' OR mots_cles_texte LIKE "%sensible%"'.
929
						') ';
934
						') ';
930
 
935
 
931
		}
936
		}
932
		if (isset($this->parametres['zonegeo']) && !$this->etreNull($zonegeo)) {
937
		if (isset($this->parametres['zonegeo']) && !$this->etreNull($zonegeo)) {
933
			$zonegeo = Cel::db()->proteger($zonegeo);
938
			$zonegeo = Cel::db()->proteger($zonegeo);
934
			$sql = " AND ce_zone_geo = $zonegeo";
939
			$sql = " AND ce_zone_geo = $zonegeo";
935
		}
940
		}
936
		return $sql;
941
		return $sql;
937
	}
942
	}
938
 
943
 
939
 
944
 
940
	private function construireWherePhotosSeulement() {
945
	private function construireWherePhotosSeulement() {
941
		$sql = '';
946
		$sql = '';
942
		if (isset($this->parametres['photos']) && $this->parametres['photos'] == 1) {
947
		if (isset($this->parametres['photos']) && $this->parametres['photos'] == 1) {
943
			$sql = 'AND co.id_observation IN (SELECT DISTINCT id_observation FROM cel_obs_images) ';
948
			$sql = 'AND co.id_observation IN (SELECT DISTINCT id_observation FROM cel_obs_images) ';
944
		}
949
		}
945
		return $sql;
950
		return $sql;
946
	}
951
	}
947
 
952
 
948
	private function construireWhereUtilisateur() {
953
	private function construireWhereUtilisateur() {
949
		$sql = '';
954
		$sql = '';
950
		// TODO tester si l'on recoit un id, un mail ou bien un nom ou prenom
955
		// TODO tester si l'on recoit un id, un mail ou bien un nom ou prenom
951
		// pour en faire une fonction polyvalente
956
		// pour en faire une fonction polyvalente
952
		extract($this->parametres);
957
		extract($this->parametres);
953
		if (isset($this->parametres['utilisateur']) && !$this->etreNull($utilisateur)) {
958
		if (isset($this->parametres['utilisateur']) && !$this->etreNull($utilisateur)) {
954
			$utilisateur = Cel::db()->proteger($utilisateur);
959
			$utilisateur = Cel::db()->proteger($utilisateur);
955
			$sql = " AND co.courriel_utilisateur = $utilisateur ";
960
			$sql = " AND co.courriel_utilisateur = $utilisateur ";
956
		}
961
		}
957
		return $sql;
962
		return $sql;
958
	}
963
	}
959
 
964
 
960
 
965
 
961
	private function construireWhereNumTaxon() {
966
	private function construireWhereNumTaxon() {
962
		$sql = '';
967
		$sql = '';
963
		// Récupération des coordonnées depuis l'id station
968
		// Récupération des coordonnées depuis l'id station
964
		extract($this->parametres);
969
		extract($this->parametres);
965
		if (isset($this->parametres['num_taxon']) && !$this->etreNull($num_taxon)) {
970
		if (isset($this->parametres['num_taxon']) && !$this->etreNull($num_taxon)) {
966
		$num_taxon = Cel::db()->proteger($num_taxon);
971
		$num_taxon = Cel::db()->proteger($num_taxon);
967
		$sql = " AND nt = $num_taxon ";
972
		$sql = " AND nt = $num_taxon ";
968
		}
973
		}
969
		return $sql;
974
		return $sql;
970
	}
975
	}
971
 
976
 
972
	private function construireWhereNumTaxonAvecSousTaxons() {
977
	private function construireWhereNumTaxonAvecSousTaxons() {
973
		$sql = '';
978
		$sql = '';
974
		// Récupération des coordonnées depuis l'id station
979
		// Récupération des coordonnées depuis l'id station
975
		extract($this->parametres);
980
		extract($this->parametres);
976
		if (isset($this->parametres['num_taxon']) && !$this->etreNull($num_taxon)) {
981
		if (isset($this->parametres['num_taxon']) && !$this->etreNull($num_taxon)) {
977
			$sous_taxons = $this->obtenirSousTaxons($this->parametres['num_taxon']);
982
			$sous_taxons = $this->obtenirSousTaxons($this->parametres['num_taxon']);
978
			$num_taxon = Cel::db()->proteger($num_taxon);
983
			$num_taxon = Cel::db()->proteger($num_taxon);
979
			if(!empty($sous_taxons)) {
984
			if(!empty($sous_taxons)) {
980
				$sql_in_sous_tax = implode(',', $sous_taxons);
985
				$sql_in_sous_tax = implode(',', $sous_taxons);
981
				$sql = " AND (nt = $num_taxon OR ".
986
				$sql = " AND (nt = $num_taxon OR ".
982
							"nom_sel_nn IN (".$sql_in_sous_tax.") OR ".
987
							"nom_sel_nn IN (".$sql_in_sous_tax.") OR ".
983
							"nom_ret_nn IN (".$sql_in_sous_tax.") ".
988
							"nom_ret_nn IN (".$sql_in_sous_tax.") ".
984
							") ";
989
							") ";
985
			}
990
			}
986
			else {
991
			else {
987
				$sql = " AND nt = $num_taxon ";
992
				$sql = " AND nt = $num_taxon ";
988
			}
993
			}
989
		}
994
		}
990
		return $sql;
995
		return $sql;
991
	}
996
	}
992
 
997
 
993
	private function obtenirSousTaxons($nt) {
998
	private function obtenirSousTaxons($nt) {
994
		$referentiel = 'bdtfx';
999
		$referentiel = 'bdtfx';
995
		if(isset($this->parametres['referentiel']) && $this->parametres['referentiel'] != "" && $this->parametres['referentiel'] != '*') {
1000
		if(isset($this->parametres['referentiel']) && $this->parametres['referentiel'] != "" && $this->parametres['referentiel'] != '*') {
996
			$referentiel = $this->parametres['referentiel'];
1001
			$referentiel = $this->parametres['referentiel'];
997
		}
1002
		}
998
		$nn_sous_taxons = array();
1003
		$nn_sous_taxons = array();
999
		$sous_taxons = $this->obtenirSousTaxonsPourNt($referentiel, $nt);
1004
		$sous_taxons = $this->obtenirSousTaxonsPourNt($referentiel, $nt);
1000
		foreach($sous_taxons as $sous_tax) {
1005
		foreach($sous_taxons as $sous_tax) {
1001
			$nn_sous_taxons[] = $sous_tax['num_nom'];
1006
			$nn_sous_taxons[] = $sous_tax['num_nom'];
1002
		}
1007
		}
1003
		return $nn_sous_taxons;
1008
		return $nn_sous_taxons;
1004
	}
1009
	}
1005
 
1010
 
1006
	private function construireWhereProjet() {
1011
	private function construireWhereProjet() {
1007
		$sql = '';
1012
		$sql = '';
1008
		// Récupération des coordonnées depuis l'id station
-
 
1009
		extract($this->parametres);
1013
		extract($this->parametres);
1010
		$projet_sql = isset($projet) ? $this->getSqlWhereProjet($projet) : null;
1014
		$projet_sql = isset($projet) ? $this->getSqlWhereProjet($projet) : null;
1011
		if (!$this->etreNull($projet_sql)) {
1015
		if (!$this->etreNull($projet_sql)) {
1012
			$sql = " AND ($projet_sql) ";
1016
			$sql = " AND ($projet_sql) ";
1013
		}
1017
		}
1014
		return $sql;
1018
		return $sql;
1015
	}
1019
	}
1016
 
1020
 
1017
	/**
1021
	/**
1018
	 * Traitement de $projet pour construction du filtre dans la requête
1022
	 * Traitement de $projet pour construction du filtre dans la requête
-
 
1023
	 * 
-
 
1024
	 * projet1 ET projet2 ET projet3 => intersection
-
 
1025
	 * projet1 OU projet2 OU projet3 => union
-
 
1026
	 * projet1 ET projet2 OU projet3 => ATTENTION indéfini 
1019
	 */
1027
	 */
1020
	private function getSqlWhereProjet($projet) {
1028
	private function getSqlWhereProjet($projet) {
1021
		$sql = null;
1029
		$sql = null;
1022
		if (isset($projet) && !$this->etreNull($projet)) {
1030
		if (isset($projet) && !$this->etreNull($projet)) {			
-
 
1031
			if (strpos($projet, ' ET ')) {
-
 
1032
				// intersection
-
 
1033
				$projets = explode(' ET ', $projet);
-
 
1034
				$clauses = array();
-
 
1035
				foreach ($projets as $proj) {
-
 
1036
					if ($proj != '') {
-
 
1037
						$clauses[] = 'co.mots_cles_texte LIKE '.Cel::db()->proteger('%'.$proj.'%');
-
 
1038
					}
-
 
1039
				}
-
 
1040
				$sql = implode($clauses, ' AND ');
-
 
1041
			} else if (strpos($projet, ' OU ')) {
-
 
1042
				// union
-
 
1043
				$projets = explode(' OU ', $projet);
-
 
1044
				$clauses = array();
-
 
1045
				foreach ($projets as $proj) {
-
 
1046
					if ($proj != '') {
-
 
1047
						$clauses[] = 'co.mots_cles_texte LIKE '.Cel::db()->proteger('%'.$proj.'%');
-
 
1048
					}
-
 
1049
				}
-
 
1050
				$sql = implode($clauses, ' OR ');
-
 
1051
			} else {
-
 
1052
				// simple
1023
			$sql = 'co.mots_cles_texte LIKE '.Cel::db()->proteger('%'.$projet.'%');
1053
				$sql = 'co.mots_cles_texte LIKE '.Cel::db()->proteger('%'.$projet.'%');
-
 
1054
			}
1024
		}
1055
		}
1025
		return $sql;
1056
		return $sql;
1026
	}
1057
	}
1027
 
1058
 
1028
	private function construireWhereTag() {
1059
	private function construireWhereTag() {
1029
		$sql = '';
1060
		$sql = '';
1030
		extract($this->parametres);
1061
		extract($this->parametres);
1031
		$tag_sql = isset($tag) ? $this->getSqlWhereObsAvecImagesTaguees($tag) : null;
1062
		$tag_sql = isset($tag) ? $this->getSqlWhereObsAvecImagesTaguees($tag) : null;
1032
		if (!$this->etreNull($tag_sql)) {
1063
		if (!$this->etreNull($tag_sql)) {
1033
			$sql = " AND ($tag_sql) ";
1064
			$sql = " AND ($tag_sql) ";
1034
		}
1065
		}
1035
		return $sql;
1066
		return $sql;
1036
	}
1067
	}
1037
 
1068
 
1038
	private function construireWhereNombreDeJours() {
1069
	private function construireWhereNombreDeJours() {
1039
		$sql = null;
1070
		$sql = null;
1040
		extract($this->parametres);
1071
		extract($this->parametres);
1041
		if (isset($nbjours) && !$this->etreNull($nbjours)) {
1072
		if (isset($nbjours) && !$this->etreNull($nbjours)) {
1042
			$sql = ' AND DATEDIFF(CURDATE(),co.date_creation) <= '.Cel::db()->proteger($nbjours).' ';
1073
			$sql = ' AND DATEDIFF(CURDATE(),co.date_creation) <= '.Cel::db()->proteger($nbjours).' ';
1043
		}
1074
		}
1044
		return $sql;
1075
		return $sql;
1045
	}
1076
	}
1046
 
1077
 
1047
	/**
1078
	/**
1048
	 * Traitement de $tag pour construction du filtre dans la requête
1079
	 * Traitement de $tag pour construction du filtre dans la requête
1049
	 */
1080
	 */
1050
	private function getSqlWhereObsAvecImagesTaguees($tag) {
1081
	private function getSqlWhereObsAvecImagesTaguees($tag) {
1051
		$sql = null;
1082
		$sql = null;
1052
		if (isset($tag) && !$this->etreNull($tag)) {
1083
		if (isset($tag) && !$this->etreNull($tag)) {
1053
			$tag_sql = $this->getSqlWhereMotsCles($tag);
1084
			$tag_sql = $this->getSqlWhereMotsCles($tag);
1054
			// Construction de la requête
1085
			// Construction de la requête
1055
			$requete = 	'SELECT DISTINCT coi.id_observation AS id_obs,  ci.ce_utilisateur AS utilisateur '.
1086
			$requete = 	'SELECT DISTINCT coi.id_observation AS id_obs,  ci.ce_utilisateur AS utilisateur '.
1056
						'FROM cel_images ci'.
1087
						'FROM cel_images ci'.
1057
						'	LEFT JOIN cel_obs_images coi'.
1088
						'	LEFT JOIN cel_obs_images coi'.
1058
						'		ON (ci.id_image = coi.id_image) '.
1089
						'		ON (ci.id_image = coi.id_image) '.
1059
						'	LEFT JOIN cel_obs AS co '.
1090
						'	LEFT JOIN cel_obs AS co '.
1060
						'		ON (coi.id_observation = co.id_observation) '.
1091
						'		ON (coi.id_observation = co.id_observation) '.
1061
						'	LEFT JOIN cel_zones_geo AS l '.
1092
						'	LEFT JOIN cel_zones_geo AS l '.
1062
						"		ON (l.nom = co.zone_geo AND l.id_zone_geo = co.ce_zone_geo) ".
1093
						"		ON (l.nom = co.zone_geo AND l.id_zone_geo = co.ce_zone_geo) ".
1063
						"WHERE transmission = '1' ".
1094
						"WHERE transmission = '1' ".
1064
						$this->construireWhereCoordonnees().
1095
						$this->construireWhereCoordonnees().
1065
						$this->construireWhereUtilisateur().
1096
						$this->construireWhereUtilisateur().
1066
						$this->construireWhereNumTaxon().
1097
						$this->construireWhereNumTaxon().
1067
						$this->construireWhereNomTaxon().
1098
						$this->construireWhereNomTaxon().
1068
						$this->construireWhereReferentiel().
1099
						$this->construireWhereReferentiel().
1069
						$this->construireWhereProjet().
1100
						$this->construireWhereProjet().
1070
						(!$this->etreNull($tag_sql) ? "AND ($tag_sql) " : '').
1101
						(!$this->etreNull($tag_sql) ? "AND ($tag_sql) " : '').
1071
						'ORDER BY utilisateur ASC, ci.ordre ASC';
1102
						'ORDER BY utilisateur ASC, ci.ordre ASC';
1072
			//$this->debug[] = $requete;
1103
			//$this->debug[] = $requete;
1073
			//die($requete);
1104
			//die($requete);
1074
			$elements_tag = Cel::db()->requeter($requete);
1105
			$elements_tag = Cel::db()->requeter($requete);
1075
 
1106
 
1076
			$requete_tag = array();
1107
			$requete_tag = array();
1077
			if ($elements_tag != false && count($elements_tag) > 0) {
1108
			if ($elements_tag != false && count($elements_tag) > 0) {
1078
 
1109
 
1079
				$filtres = array();
1110
				$filtres = array();
1080
				foreach ($elements_tag as $occurence) {
1111
				foreach ($elements_tag as $occurence) {
1081
					$utilisateur = $occurence['utilisateur'];
1112
					$utilisateur = $occurence['utilisateur'];
1082
					$id_obs = $occurence['id_obs'];
1113
					$id_obs = $occurence['id_obs'];
1083
					if (!array_key_exists($utilisateur, $filtres)) {
1114
					if (!array_key_exists($utilisateur, $filtres)) {
1084
						$filtres[$utilisateur] = array();
1115
						$filtres[$utilisateur] = array();
1085
					}
1116
					}
1086
					if (!array_key_exists($id_obs, $filtres[$utilisateur])) {
1117
					if (!array_key_exists($id_obs, $filtres[$utilisateur])) {
1087
						$filtres[$utilisateur][$id_obs] = $id_obs;
1118
						$filtres[$utilisateur][$id_obs] = $id_obs;
1088
					}
1119
					}
1089
				}
1120
				}
1090
 
1121
 
1091
				// Pré-construction du where de la requête
1122
				// Pré-construction du where de la requête
1092
				$tpl_where = "(id_observation IN (%s))";
1123
				$tpl_where = "(id_observation IN (%s))";
1093
				foreach ($filtres as $utilisateur => $id_obs) {
1124
				foreach ($filtres as $utilisateur => $id_obs) {
1094
					$requete_tag[] = sprintf($tpl_where, implode(',', $id_obs));
1125
					$requete_tag[] = sprintf($tpl_where, implode(',', $id_obs));
1095
				}
1126
				}
1096
 
1127
 
1097
			} else {
1128
			} else {
1098
				$this->messages[] = "Aucune observation ne possède d'images avec ce mot-clé.";
1129
				$this->messages[] = "Aucune observation ne possède d'images avec ce mot-clé.";
1099
			}
1130
			}
1100
			if (count($requete_tag) > 0) {
1131
			if (count($requete_tag) > 0) {
1101
				$sql = implode(" \nOR ", $requete_tag);
1132
				$sql = implode(" \nOR ", $requete_tag);
1102
			}
1133
			}
1103
		}
1134
		}
1104
		return $sql;
1135
		return $sql;
1105
	}
1136
	}
1106
 
1137
 
1107
	/**
1138
	/**
1108
	 * Traitement de $tag pour construction du filtre dans la requête
1139
	 * Traitement de $tag pour construction du filtre dans la requête
1109
	 */
1140
	 */
1110
	private function getSqlWhereMotsCles($tag) {
1141
	private function getSqlWhereMotsCles($tag) {
1111
		$sql = null;
1142
		$sql = null;
1112
		$mots_cles = $this->decomposerParametreTag($tag);
1143
		$mots_cles = $this->decomposerParametreTag($tag);
1113
		$requete_projet = $this->getSqlWhereMotsClesImages($mots_cles);
1144
		$requete_projet = $this->getSqlWhereMotsClesImages($mots_cles);
1114
		$sql = $requete_projet;
1145
		$sql = $requete_projet;
1115
		//$this->debug[] = $sql;
1146
		//$this->debug[] = $sql;
1116
		return $sql;
1147
		return $sql;
1117
	}
1148
	}
1118
 
1149
 
1119
	/**
1150
	/**
1120
	 * Traitement de $tag pour construction du filtre dans la requête
1151
	 * Traitement de $tag pour construction du filtre dans la requête
1121
	 */
1152
	 */
1122
	private function getSqlWhereMotsClesImages($mots_cles_encodes) {
1153
	private function getSqlWhereMotsClesImages($mots_cles_encodes) {
1123
		$where_mots_cles_images = array();
1154
		$where_mots_cles_images = array();
1124
		foreach ($mots_cles_encodes['motsClesEncodesProteges'] as $mot_cle_encode) {
1155
		foreach ($mots_cles_encodes['motsClesEncodesProteges'] as $mot_cle_encode) {
1125
			$where_mots_cles_images[] = "ci.mots_cles_texte LIKE $mot_cle_encode";
1156
			$where_mots_cles_images[] = "ci.mots_cles_texte LIKE $mot_cle_encode";
1126
		}
1157
		}
1127
		$where_mots_cles_images = implode(' '.$mots_cles_encodes['type'].' ', $where_mots_cles_images);
1158
		$where_mots_cles_images = implode(' '.$mots_cles_encodes['type'].' ', $where_mots_cles_images);
1128
		return $where_mots_cles_images;
1159
		return $where_mots_cles_images;
1129
	}
1160
	}
1130
 
1161
 
1131
	private function decomposerParametreTag($tags) {
1162
	private function decomposerParametreTag($tags) {
1132
 
1163
 
1133
		$mots_cles = array('type' => null, 'motsCles' => null, 'motsClesEncodesProteges' => null);
1164
		$mots_cles = array('type' => null, 'motsCles' => null, 'motsClesEncodesProteges' => null);
1134
		if (preg_match('/.+OU.+/', $tags)) {
1165
		if (preg_match('/.+OU.+/', $tags)) {
1135
			$mots_cles['type'] = 'OR';
1166
			$mots_cles['type'] = 'OR';
1136
			$mots_cles['motsCles'] = explode('OU', $tags);
1167
			$mots_cles['motsCles'] = explode('OU', $tags);
1137
		} else if (preg_match('/.+ET.+/', $tags)) {
1168
		} else if (preg_match('/.+ET.+/', $tags)) {
1138
			$mots_cles['type'] = 'AND';
1169
			$mots_cles['type'] = 'AND';
1139
			$mots_cles['motsCles'] = explode('ET', $tags);
1170
			$mots_cles['motsCles'] = explode('ET', $tags);
1140
		} else {
1171
		} else {
1141
			$mots_cles['motsCles'][] = $tags;
1172
			$mots_cles['motsCles'][] = $tags;
1142
		}
1173
		}
1143
 
1174
 
1144
		foreach ($mots_cles['motsCles'] as $mot) {
1175
		foreach ($mots_cles['motsCles'] as $mot) {
1145
			$mots_cles['motsClesEncodesProteges'][] = Cel::db()->quote('%'.$mot.'%');
1176
			$mots_cles['motsClesEncodesProteges'][] = Cel::db()->quote('%'.$mot.'%');
1146
		}
1177
		}
1147
		$this->debug[] = $mots_cles;
1178
		$this->debug[] = $mots_cles;
1148
		return $mots_cles;
1179
		return $mots_cles;
1149
	}
1180
	}
1150
 
1181
 
1151
	private function decomposerLatLng($coord) {
1182
	private function decomposerLatLng($coord) {
1152
		$lat_lng = array();
1183
		$lat_lng = array();
1153
		if (isset($coord)) {
1184
		if (isset($coord)) {
1154
			list($lat, $lng) = explode('|', $coord);
1185
			list($lat, $lng) = explode('|', $coord);
1155
			$lat_lng = array('lat' => $lat, 'lng' => $lng);
1186
			$lat_lng = array('lat' => $lat, 'lng' => $lng);
1156
		}
1187
		}
1157
		return $lat_lng;
1188
		return $lat_lng;
1158
	}
1189
	}
1159
 
1190
 
1160
	private function decomposerParametreStation() {
1191
	private function decomposerParametreStation() {
1161
		$station_infos = array();
1192
		$station_infos = array();
1162
		if (isset($this->parametres['station'])) {
1193
		if (isset($this->parametres['station'])) {
1163
			$station = $this->parametres['station'];
1194
			$station = $this->parametres['station'];
1164
			$this->debug[] = $station;
1195
			$this->debug[] = $station;
1165
			@list($type, $coord) = explode(':', $station);
1196
			@list($type, $coord) = explode(':', $station);
1166
			@list($lat, $lng) = explode('|', $coord);
1197
			@list($lat, $lng) = explode('|', $coord);
1167
 
1198
 
1168
			$station_infos = array('type' => $type, 'lat' => $lat, 'lng' => $lng);
1199
			$station_infos = array('type' => $type, 'lat' => $lat, 'lng' => $lng);
1169
		}
1200
		}
1170
		return $station_infos;
1201
		return $station_infos;
1171
	}
1202
	}
1172
 
1203
 
1173
	private function decomposerParametreDate() {
1204
	private function decomposerParametreDate() {
1174
		$date_infos = array(null,null);
1205
		$date_infos = array(null,null);
1175
		if (isset($this->parametres['date'])) {
1206
		if (isset($this->parametres['date'])) {
1176
			$date = $this->parametres['date'];
1207
			$date = $this->parametres['date'];
1177
			if (strpos($date, ':')) {
1208
			if (strpos($date, ':')) {
1178
				list($type, $date) = explode(':', $date);
1209
				list($type, $date) = explode(':', $date);
1179
			} else {
1210
			} else {
1180
				$type = 'observation';
1211
				$type = 'observation';
1181
			}
1212
			}
1182
 
1213
 
1183
			$date = str_replace('/', '-', $date);
1214
			$date = str_replace('/', '-', $date);
1184
			if (preg_match('/(^[0-9]{2})-([0-9]{2})-([0-9]{4}$)/', $date, $matches)) {
1215
			if (preg_match('/(^[0-9]{2})-([0-9]{2})-([0-9]{4}$)/', $date, $matches)) {
1185
				$date = $matches[3].'-'.$matches[2].'-'.$matches[1];
1216
				$date = $matches[3].'-'.$matches[2].'-'.$matches[1];
1186
			}
1217
			}
1187
 
1218
 
1188
			$date_infos = array($type, $date);
1219
			$date_infos = array($type, $date);
1189
		}
1220
		}
1190
		return $date_infos;
1221
		return $date_infos;
1191
	}
1222
	}
1192
 
1223
 
1193
	private function decomposerParametreTaxon() {
1224
	private function decomposerParametreTaxon() {
1194
		$nom_infos = array(null,null);
1225
		$nom_infos = array(null,null);
1195
		if (isset($this->parametres['taxon'])) {
1226
		if (isset($this->parametres['taxon'])) {
1196
			$taxon = $this->parametres['taxon'];
1227
			$taxon = $this->parametres['taxon'];
1197
			if (strpos($taxon, ':')) {
1228
			if (strpos($taxon, ':')) {
1198
				$nom_infos = explode(':', $taxon);
1229
				$nom_infos = explode(':', $taxon);
1199
			} else {
1230
			} else {
1200
				$nom_infos = array('retenu', $taxon);
1231
				$nom_infos = array('retenu', $taxon);
1201
			}
1232
			}
1202
		}
1233
		}
1203
		return $nom_infos;
1234
		return $nom_infos;
1204
	}
1235
	}
1205
 
1236
 
1206
	private function decomposerParametreCommentaire() {
1237
	private function decomposerParametreCommentaire() {
1207
		$commentaire_infos = array(null,null);
1238
		$commentaire_infos = array(null,null);
1208
		if (isset($this->parametres['commentaire'])) {
1239
		if (isset($this->parametres['commentaire'])) {
1209
			$commentaire = $this->parametres['commentaire'];
1240
			$commentaire = $this->parametres['commentaire'];
1210
			if (strpos($commentaire, ':')) {
1241
			if (strpos($commentaire, ':')) {
1211
				$commentaire_infos = explode(':', $commentaire);
1242
				$commentaire_infos = explode(':', $commentaire);
1212
			} else {
1243
			} else {
1213
				$commentaire_infos = array('observation', $commentaire);
1244
				$commentaire_infos = array('observation', $commentaire);
1214
			}
1245
			}
1215
		}
1246
		}
1216
		return $commentaire_infos;
1247
		return $commentaire_infos;
1217
	}
1248
	}
1218
}
1249
}
1219
?>
1250
?>