Subversion Repositories eFlore/Applications.cel

Rev

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

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