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