Subversion Repositories eFlore/Applications.cel

Rev

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

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