Subversion Repositories eFlore/Applications.cel

Rev

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

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