Subversion Repositories eFlore/Applications.cel

Rev

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

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