Subversion Repositories eFlore/Applications.del

Rev

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

Rev 559 Rev 596
1
<?php
1
<?php
2
class Observations extends Del {
2
class Observations extends Del {
3
 
3
 
4
	private $format = 'json';
4
	private $format = 'json';
5
 
5
 
6
	private $debut = 0;
6
	private $debut = 0;
7
	private $limite = 50;
7
	private $limite = 50;
8
 
8
 
9
	private $champ_tri = 'date_observation';
9
	private $champ_tri = 'date_observation';
10
	private $ordre = 'desc';
10
	private $ordre = 'desc';
11
	private $tri_demande = false;
11
	private $tri_demande = false;
12
 
12
 
13
	private $criteres_acceptes = array(
13
	private $criteres_acceptes = array(
14
		"recherche",
14
		"recherche",
15
		"dpt",
15
		"dpt",
16
		"taxon",
16
		"taxon",
17
		"genre",
17
		"genre",
18
		"mot_clef",
18
		"mot_clef",
19
		"date",
19
		"date",
20
		"commune",
20
		"commune",
21
		"famille",
21
		"famille",
22
		"tag",
22
		"tag",
23
		"auteur"
23
		"auteur"
24
		);
24
		);
25
 
25
 
26
		/**
26
		/**
27
		 * Méthode appelée avec une requête de type GET avec une url de la forme
27
		 * Méthode appelée avec une requête de type GET avec une url de la forme
28
		 * http://localhost/jrest/ExempleService/
28
		 * http://localhost/jrest/ExempleService/
29
		 *
29
		 *
30
		 * Sert normalement à renvoyer la description des possibilités du service
30
		 * Sert normalement à renvoyer la description des possibilités du service
31
		 *
31
		 *
32
		 */
32
		 */
33
		public function getRessource() {
33
		public function getRessource() {
34
			return $this->getElement(array());
34
			return $this->getElement(array());
35
		}
35
		}
36
 
36
 
37
		/**
37
		/**
38
		 * Méthode appelée avec une requête de type GET avec une url de la forme
38
		 * Méthode appelée avec une requête de type GET avec une url de la forme
39
		 * http://localhost/jrest/ExempleService/uid[0]/$uid[1]/ etc...
39
		 * http://localhost/jrest/ExempleService/uid[0]/$uid[1]/ etc...
40
		 *
40
		 *
41
		 * Sert normalement à ramener un élément précis indiqué par un identifiant
41
		 * Sert normalement à ramener un élément précis indiqué par un identifiant
42
		 * qui se situe dans l'url après le nom du service
42
		 * qui se situe dans l'url après le nom du service
43
		 * Le filtrage, le format de retour, les paramètres optionnels ... sont normalement indiqués
43
		 * Le filtrage, le format de retour, les paramètres optionnels ... sont normalement indiqués
44
		 * dans le tableau $_GET
44
		 * dans le tableau $_GET
45
		 * Pour obtenir l'élément 2501 dans le format HTML cela pourrait donner
45
		 * Pour obtenir l'élément 2501 dans le format HTML cela pourrait donner
46
		 * http://localhost/jrest/ExempleService/2501?format=HTML
46
		 * http://localhost/jrest/ExempleService/2501?format=HTML
47
		 *
47
		 *
48
		 * @param $uid un tableau contenant les élements passés dans l'url après le nom du service
48
		 * @param $uid un tableau contenant les élements passés dans l'url après le nom du service
49
		 *
49
		 *
50
		 */
50
		 */
51
		public function getElement($uid)
51
		public function getElement($uid)
52
		{
52
		{
53
			$this->collecterCriteresRecherche();
53
			$this->collecterCriteresRecherche();
54
 
54
 
55
			switch ($this->format) {
55
			switch ($this->format) {
56
 
56
 
57
				case 'html':
57
				case 'html':
58
 
58
 
59
				case 'json':
59
				case 'json':
60
 
60
 
61
					$obs = $this->obtenirObservationsAvecImages();
61
					$obs = $this->obtenirObservationsAvecImages();
62
					$total = count($obs);
62
					$total = count($obs);
63
					if(!empty($this->criteres)) {
63
					if(!empty($this->criteres)) {
64
						$obs_filtrees = array();
64
						$obs_filtrees = array();
65
						foreach($obs as $ligne_observation) {
65
						foreach($obs as $ligne_observation) {
66
							if($this->ligneCorrespondAuxCriteres($ligne_observation)) {
66
							if($this->ligneCorrespondAuxCriteres($ligne_observation)) {
67
								$obs_filtrees[] = $ligne_observation;
67
								$obs_filtrees[] = $ligne_observation;
68
							}
68
							}
69
						}
69
						}
70
					} else  {
70
					} else  {
71
						$obs_filtrees = $obs;
71
						$obs_filtrees = $obs;
72
					}
72
					}
73
 
73
 
74
					$total = count($obs_filtrees);
74
					$total = count($obs_filtrees);
75
					if($this->tri_demande) {
75
					if($this->tri_demande) {
76
						usort($obs_filtrees, array($this,'comparerObservations'));
76
						usort($obs_filtrees, array($this,'comparerObservations'));
77
					}
77
					}
78
					$tranche = array_slice($obs_filtrees,$this->debut,$this->limite);
78
					$tranche = array_slice($obs_filtrees,$this->debut,$this->limite);
79
 
79
 
80
					$retour = array('total' => $total,
80
					$retour = array('total' => $total,
81
								'contenu' => $tranche
81
								'contenu' => $tranche
82
					);
82
					);
83
 
83
 
84
					$retour = json_encode($retour);
84
					$retour = json_encode($retour);
85
					$mime = 'application/json';
85
					$mime = 'application/json';
86
					break;
86
					break;
87
 
87
 
88
				case 'xml':
88
				case 'xml':
89
					break;
89
					break;
90
			}
90
			}
91
 
91
 
92
			$this->envoyer($retour,$mime);
92
			$this->envoyer($retour,$mime);
93
		}
93
		}
94
 
94
 
95
		private function collecterCriteresRecherche() {
95
		private function collecterCriteresRecherche() {
96
 
96
 
97
			$this->debut = isset($_GET['debut']) ? $_GET['debut'] : $this->debut;
97
			$this->debut = isset($_GET['debut']) ? $_GET['debut'] : $this->debut;
98
			$this->limite = isset($_GET['limite']) ? $_GET['limite'] : $this->limite;
98
			$this->limite = isset($_GET['limite']) ? $_GET['limite'] : $this->limite;
99
 
99
 
100
			$this->champ_tri = isset($_GET['tri']) ? $_GET['tri'] : $this->champ_tri;
100
			$this->champ_tri = isset($_GET['tri']) ? $_GET['tri'] : $this->champ_tri;
101
			$this->ordre = isset($_GET['ordre']) ? $_GET['ordre'] : $this->ordre;
101
			$this->ordre = isset($_GET['ordre']) ? $_GET['ordre'] : $this->ordre;
102
 
102
 
103
			$this->tri_demande = isset($_GET['tri']) ? true : false;
103
			$this->tri_demande = isset($_GET['tri']) ? true : false;
104
 
104
 
105
			if(isset($_GET['format'])) {
105
			if(isset($_GET['format'])) {
106
				$this->format = strtolower($_GET['format']);
106
				$this->format = strtolower($_GET['format']);
107
			}
107
			}
108
 
108
 
109
			foreach($_GET as $cle => $valeur) {
109
			foreach($_GET as $cle => $valeur) {
110
				if(in_array(strtolower(trim($cle)), $this->criteres_acceptes)) {
110
				if(in_array(strtolower(trim($cle)), $this->criteres_acceptes)) {
111
					$this->criteres[$cle] = $valeur;
111
					$this->criteres[$cle] = $valeur;
112
				}
112
				}
113
			}
113
			}
114
		}
114
		}
115
 
115
 
116
		private function ligneCorrespondAuxCriteres($ligne_observation) {
116
		private function ligneCorrespondAuxCriteres($ligne_observation) {
117
 
117
 
118
			$correspond = true;
118
			$correspond = true;
119
 
119
 
120
			foreach($this->criteres as $critere => $valeur) {
120
			foreach($this->criteres as $critere => $valeur) {
121
				
121
				
122
				$valeur = trim($valeur);
122
				$valeur = trim($valeur);
123
				
123
				
124
				switch($critere) {
124
				switch($critere) {
125
					case "recherche":
125
					case "recherche":
126
						$correspond = $this->correspondAChampDepartement($ligne_observation, $valeur) |
126
						$correspond = $this->correspondAChampDepartement($ligne_observation, $valeur) |
127
						stristr($ligne_observation->nom_sel, $valeur) != '' |
127
						$this->commencePar($ligne_observation->nom_sel, $valeur) |
128
						stristr($ligne_observation->nom_ret, $valeur) != '' |
128
						$this->commencePar($ligne_observation->nom_ret, $valeur) |
129
						stristr($ligne_observation->nom_sel, $valeur) != '' |
129
						$this->commencePar($ligne_observation->nom_sel, $valeur) |
130
						stristr($ligne_observation->nom_ret, $valeur) != '' |
130
						$this->commencePar($ligne_observation->nom_ret, $valeur) |
131
						stristr($ligne_observation->mots_cles_texte, $valeur) != '' |
131
						$this->commencePar($ligne_observation->mots_cles_texte, $valeur) |
132
						stristr($ligne_observation->date_observation, $valeur) != '' |
132
						$this->commencePar($ligne_observation->date_observation, $valeur) |
133
						stristr($ligne_observation->zone_geo, $valeur) != '' |
133
						$this->commencePar($ligne_observation->zone_geo, $valeur) |
134
						stristr($ligne_observation->milieu, $valeur) != '' |
134
						$this->commencePar($ligne_observation->milieu, $valeur) |
135
						stristr($ligne_observation->famille, $valeur) != '' |
135
						$this->commencePar($ligne_observation->famille, $valeur) |
136
						$this->correspondAChampNomOuPrenom($ligne_observation, $valeur);
136
						$this->correspondAChampNomOuPrenom($ligne_observation, $valeur);
137
							
137
							
138
						foreach ($ligne_observation->images as $image){
138
						foreach ($ligne_observation->images as $image){
139
							$correspond =$correspond|stristr($image->date_prise_de_vue, $valeur) != '' |
139
							$correspond =$correspond|stristr($image->date_prise_de_vue, $valeur) != '' |
140
							stristr($image->mots_cles_texte, $valeur) != '';
140
							stristr($image->mots_cles_texte, $valeur) != '';
141
						}
141
						}
142
						break;
142
						break;
143
					case "dpt":
143
					case "dpt":
144
						$correspond = $this->correspondAChampDepartement($ligne_observation, $valeur);
144
						$correspond = $this->correspondAChampDepartement($ligne_observation, $valeur);
145
						break;
145
						break;
146
					case "taxon":
146
					case "taxon":
147
						$correspond = stristr($ligne_observation->nom_sel, $valeur) != '' |
147
						$correspond = stristr($ligne_observation->nom_sel, $valeur) != '' |
148
						stristr($ligne_observation->nom_ret, $valeur) != '';
148
						stristr($ligne_observation->nom_ret, $valeur) != '';
149
						break;
149
						break;
150
					case "genre":
150
					case "genre":
151
						$correspond = stristr($ligne_observation->nom_sel, $valeur) != '' |
151
						$correspond = stristr($ligne_observation->nom_sel, $valeur) != '' |
152
						stristr($ligne_observation->nom_ret, $valeur) != '';
152
						stristr($ligne_observation->nom_ret, $valeur) != '';
153
						break;
153
						break;
154
					case "mot_clef":
154
					case "mot_clef":
155
						$correspond = stristr($ligne_observation->mot_cles_texte, $valeur);
155
						$correspond = stristr($ligne_observation->mot_cles_texte, $valeur);
156
						break;
156
						break;
157
					case "date":
157
					case "date":
158
						$correspond = stristr($ligne_observation->date_observation, $valeur) != '';
158
						$correspond = stristr($ligne_observation->date_observation, $valeur) != '';
159
						foreach ($ligne_observation->images as $image){
159
						foreach ($ligne_observation->images as $image){
160
							$correspond = $correspond|stristr($image->date_prise_de_vue, $valeur) != '';
160
							$correspond = $correspond|stristr($image->date_prise_de_vue, $valeur) != '';
161
						}
161
						}
162
						break;
162
						break;
163
					case "commune":
163
					case "commune":
164
						$correspond = stristr($ligne_observation->zone_geo, $valeur);
164
						$correspond = stristr($ligne_observation->zone_geo, $valeur);
165
						break;
165
						break;
166
					case "famille":
166
					case "famille":
167
						$correspond = stristr($ligne_observation->famille, $valeur);
167
						$correspond = stristr($ligne_observation->famille, $valeur);
168
						break;
168
						break;
169
					case "tag":
169
					case "tag":
170
						$correspond = stristr($ligne_observation->images->mots_cles_texte, $valeur);
170
						$correspond = stristr($ligne_observation->images->mots_cles_texte, $valeur);
171
						break;
171
						break;
172
					case "auteur":
172
					case "auteur":
173
						$correspond = $this->correspondAChampNomOuPrenom($ligne_observation, $valeur);
173
						$correspond = $this->correspondAChampNomOuPrenom($ligne_observation, $valeur);
174
						break;
174
						break;
175
				}
175
				}
176
 
176
 
177
				if(!$correspond) break;
177
				if(!$correspond) break;
178
			}
178
			}
179
			
179
			
180
			return $correspond;
180
			return $correspond;
181
		}
181
		}
-
 
182
		
-
 
183
		private function commencePar($botte, $aiguille) {
-
 
184
			return mb_substr($botte, 0, mb_strlen($aiguille)) == $aiguille;
-
 
185
		}
182
		
186
		
183
		private function correspondAChampDepartement($ligne_observation, $valeur) {
187
		private function correspondAChampDepartement($ligne_observation, $valeur) {
184
			$correspond = false;
188
			$correspond = false;
185
			if(!is_numeric($valeur)) {
189
			if(!is_numeric($valeur)) {
186
				$valeur = $this->obtenirCodeDepartementPourNom($valeur);
190
				$valeur = $this->obtenirCodeDepartementPourNom($valeur);
187
			}
191
			}
188
			
192
			
189
			if($valeur) {		
193
			if($valeur) {		
190
				$correspond = (substr($ligne_observation->ce_zone_geo, 0, 2) == $valeur) |
194
				$correspond = (substr($ligne_observation->ce_zone_geo, 0, 2) == $valeur) |
191
				$ligne_observation->ce_zone_geo == $valeur;
195
				$ligne_observation->ce_zone_geo == $valeur;
192
			}
196
			}
193
		
197
		
194
			return $correspond;
198
			return $correspond;
195
		}
199
		}
196
		
200
		
197
		private function correspondAChampNomOuPrenom($ligne_observation, $valeur) {
201
		private function correspondAChampNomOuPrenom($ligne_observation, $valeur) {
198
		
202
		
199
			while(substr_count($valeur, '  ') > 0) {
203
			while(substr_count($valeur, '  ') > 0) {
200
				$valeur = str_replace('  ',' ', $valeur);
204
				$valeur = str_replace('  ',' ', $valeur);
201
			}
205
			}
202
				
206
				
203
			$chaine_nom_prenom = strtolower($ligne_observation->nom_utilisateur).' '.strtolower($ligne_observation->prenom_utilisateur);
207
			$chaine_nom_prenom = strtolower($ligne_observation->nom_utilisateur).' '.strtolower($ligne_observation->prenom_utilisateur);
204
			$chaine_nom_prenom = trim($chaine_nom_prenom);
208
			$chaine_nom_prenom = trim($chaine_nom_prenom);
205
			$chaine_prenom_nom = strtolower($ligne_observation->prenom_utilisateur).' '.strtolower($ligne_observation->nom_utilisateur);
209
			$chaine_prenom_nom = strtolower($ligne_observation->prenom_utilisateur).' '.strtolower($ligne_observation->nom_utilisateur);
206
			$chaine_prenom_nom = trim($chaine_prenom_nom);
210
			$chaine_prenom_nom = trim($chaine_prenom_nom);
207
			$valeur = strtolower($valeur);
211
			$valeur = strtolower($valeur);
208
		
212
		
209
			$correspond = stristr($ligne_observation->prenom_utilisateur, $valeur) != '' |
213
			$correspond = stristr($ligne_observation->prenom_utilisateur, $valeur) != '' |
210
			stristr($ligne_observation->nom_utilisateur, $valeur) != '' |
214
			stristr($ligne_observation->nom_utilisateur, $valeur) != '' |
211
			stristr($chaine_nom_prenom, $valeur) != '' |
215
			stristr($chaine_nom_prenom, $valeur) != '' |
212
			stristr($chaine_prenom_nom, $valeur) != '';
216
			stristr($chaine_prenom_nom, $valeur) != '';
213
		
217
		
214
			return $correspond;
218
			return $correspond;
215
		}
219
		}
216
		
220
		
217
		private function obtenirCodeDepartementPourNom($nom) {
221
		private function obtenirCodeDepartementPourNom($nom) {
218
			$nom = $this->formaterChaineMinusculeSansAccents($nom);
222
			$nom = $this->formaterChaineMinusculeSansAccents($nom);
219
			$tableau_communes = (array)$this->getTableauCorrespondanceDepartement();
223
			$tableau_communes = (array)$this->getTableauCorrespondanceDepartement();
220
			$code = isset($tableau_communes[$nom]) ? $tableau_communes[$nom] : false;
224
			$code = isset($tableau_communes[$nom]) ? $tableau_communes[$nom] : false;
221
		
225
		
222
			return $code;
226
			return $code;
223
		}
227
		}
224
		
228
		
225
		private function getTableauCorrespondanceDepartement() {
229
		private function getTableauCorrespondanceDepartement() {
226
			return json_decode(file_get_contents(realpath(dirname(__FILE__)).'/mocks/departementsmock.json'));
230
			return json_decode(file_get_contents(realpath(dirname(__FILE__)).'/mocks/departementsmock.json'));
227
		}
231
		}
228
 
232
 
229
		private function comparerObservations($observation_a, $observation_b) {
233
		private function comparerObservations($observation_a, $observation_b) {
230
 
234
 
231
			$champ_tri = $this->champ_tri;
235
			$champ_tri = $this->champ_tri;
232
 
236
 
233
			$observation_a = isset($observation_a->$champ_tri) ? $observation_a->$champ_tri : -1;
237
			$observation_a = isset($observation_a->$champ_tri) ? $observation_a->$champ_tri : -1;
234
			$observation_b = isset($observation_b->$champ_tri) ? $observation_b->$champ_tri : -1;
238
			$observation_b = isset($observation_b->$champ_tri) ? $observation_b->$champ_tri : -1;
235
 
239
 
236
			if($this->ordre == 'asc') {
240
			if($this->ordre == 'asc') {
237
				return $observation_a > $observation_b;
241
				return $observation_a > $observation_b;
238
			} else {
242
			} else {
239
				return $observation_a < $observation_b;
243
				return $observation_a < $observation_b;
240
			}
244
			}
241
		}
245
		}
242
 
246
 
243
		private function obtenirObservationsAvecImages() {
247
		private function obtenirObservationsAvecImages() {
244
			return json_decode(file_get_contents(realpath(dirname(__FILE__)).'/mocks/obsmock.json'));
248
			return json_decode(file_get_contents(realpath(dirname(__FILE__)).'/mocks/obsmock.json'));
245
		}
249
		}
246
}
250
}
247
?>
251
?>