Subversion Repositories eFlore/Applications.del

Rev

Rev 548 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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