Subversion Repositories eFlore/Applications.del

Rev

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

Rev 505 Rev 509
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 $ordre = 'desc';
11
	private $ordre = 'desc';
11
	private $tri_demande = false;
12
	private $tri_demande = false;
12
	
13
	
13
	private $criteres_acceptes = array(
14
	private $criteres_acceptes = array(
14
		"recherche",
15
		"recherche",
15
		"dpt",
16
		"dpt",
16
		"taxon",
17
		"taxon",
17
		"genre",
18
		"genre",
18
		"mot_clef",
19
		"mot_clef",
19
		"date",
20
		"date",
20
		"commune",
21
		"commune",
21
		"famille",
22
		"famille",
22
		"tag",
23
		"tag",
23
		"auteur"
24
		"auteur"
24
	);
25
	);
25
	
26
	
26
	private $criteres = array();
27
	private $criteres = array();
27
	
28
	
28
	/**
29
	/**
29
	* 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
30
	* http://localhost/jrest/ExempleService/
31
	* http://localhost/jrest/ExempleService/
31
	*
32
	*
32
	* Sert normalement à renvoyer la description des possibilités du service
33
	* Sert normalement à renvoyer la description des possibilités du service
33
	*
34
	*
34
	*/
35
	*/
35
	public function getRessource() {
36
	public function getRessource() {
36
		return $this->getElement(array());
37
		return $this->getElement(array());
37
	}
38
	}
38
	
39
	
39
	/**
40
	/**
40
	* 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
41
	* http://localhost/jrest/ExempleService/uid[0]/$uid[1]/ etc...
42
	* http://localhost/jrest/ExempleService/uid[0]/$uid[1]/ etc...
42
	*
43
	*
43
	* 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
44
	* 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
45
	* 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
46
	* dans le tableau $_GET
47
	* dans le tableau $_GET
47
	* 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
48
	* http://localhost/jrest/ExempleService/2501?format=HTML
49
	* http://localhost/jrest/ExempleService/2501?format=HTML
49
	*
50
	*
50
	* @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
51
	*
52
	*
52
	*/
53
	*/
53
	public function getElement($uid)
54
	public function getElement($uid)
54
	{	
55
	{	
55
		$this->collecterCriteresRecherche();
56
		$this->collecterCriteresRecherche();
56
		
57
		
57
		switch ($this->format) {
58
		switch ($this->format) {
58
				
59
				
59
			case 'html':
60
			case 'html':
60
					
61
					
61
			case 'json':
62
			case 'json':
62
				$images = $this->obtenirImagesAvecObservations();
63
				$images = $this->obtenirImagesAvecObservations();
63
				
64
				
64
				if(!empty($this->criteres)) {
65
				if(!empty($this->criteres)) {
65
					$images_filtrees = array();
66
					$images_filtrees = array();
66
					foreach($images as $ligne_image) {
67
					foreach($images as $ligne_image) {
67
						if($this->ligneCorrespondAuxCriteres($ligne_image)) {
68
						if($this->ligneCorrespondAuxCriteres($ligne_image)) {
68
							$images_filtrees[] = $ligne_image;
69
							$images_filtrees[] = $ligne_image;
69
						}
70
						}
70
					}
71
					}
71
				} else  {
72
				} else  {
72
					$images_filtrees = $images;
73
					$images_filtrees = $images;
73
				}
74
				}
74
				
75
				
75
				$total = count($images_filtrees);
76
				$total = count($images_filtrees);
76
				if($this->tri_demande) {
77
				if($this->tri_demande) {
77
					usort($images_filtrees, array($this,'comparerObservations'));
78
					usort($images_filtrees, array($this,'comparerObservations'));
78
				}
79
				}
79
				$tranche = array_slice($images_filtrees,$this->debut,$this->limite);
80
				$tranche = array_slice($images_filtrees,$this->debut,$this->limite);
80
				
81
				
81
				$retour = array('total' => $total,
82
				$retour = array('total' => $total,
82
								'contenu' => $tranche
83
								'contenu' => $tranche
83
								);	
84
								);	
84
				
85
				
85
				$retour = json_encode($retour);
86
				$retour = json_encode($retour);
86
				$mime = 'application/json';
87
				$mime = 'application/json';
87
				break;
88
				break;
88
					
89
					
89
			case 'xml':
90
			case 'xml':
90
				break;
91
				break;
91
		}
92
		}
92
	
93
	
93
		$this->envoyer($retour,$mime);
94
		$this->envoyer($retour,$mime);
94
	}
95
	}
95
	
96
	
96
	private function ajouterVoteProtocoleAleatoire() {
97
	private function ajouterVoteProtocoleAleatoire() {
97
		$vote = array('id_protocole' => round(rand(1,2)),
98
		$vote = array('id_protocole' => round(rand(1,2)),
98
						'vote' => round(rand(0,5)),
99
						'vote' => round(rand(0,5)),
99
						'contributeur' => $this->getAuteurPif(),
100
						'contributeur' => $this->getAuteurPif(),
100
						'date' => date('Y-m-d')
101
						'date' => date('Y-m-d')
101
						);
102
						);
102
		return $vote;		
103
		return $vote;		
103
	}
104
	}
104
	
105
	
105
	private function collecterCriteresRecherche() {
106
	private function collecterCriteresRecherche() {
106
				
107
				
107
		$this->debut = isset($_GET['debut']) ? $_GET['debut'] : $this->debut;
108
		$this->debut = isset($_GET['debut']) ? $_GET['debut'] : $this->debut;
108
		$this->limite = isset($_GET['limite']) ? $_GET['limite'] : $this->limite;
109
		$this->limite = isset($_GET['limite']) ? $_GET['limite'] : $this->limite;
109
		
110
		
110
		$this->champ_tri = isset($_GET['tri']) ? $_GET['tri'] : $this->champ_tri;
111
		$this->champ_tri = isset($_GET['tri']) ? $_GET['tri'] : $this->champ_tri;
111
		$this->ordre = isset($_GET['ordre']) ? $_GET['ordre'] : $this->ordre;
112
		$this->ordre = isset($_GET['ordre']) ? $_GET['ordre'] : $this->ordre;
112
		
113
		
113
		$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;
114
		
116
		
115
		if(isset($_GET['format'])) {
117
		if(isset($_GET['format'])) {
116
			$this->format = strtolower($_GET['format']);
118
			$this->format = strtolower($_GET['format']);
117
		}
119
		}
118
		
120
		
119
		foreach($_GET as $cle => $valeur) {
121
		foreach($_GET as $cle => $valeur) {
120
			if(in_array(strtolower(trim($cle)), $this->criteres_acceptes)) {
122
			if(in_array(strtolower(trim($cle)), $this->criteres_acceptes)) {
121
				$this->criteres[$cle] = $valeur;
123
				$this->criteres[$cle] = $valeur;
122
			}
124
			}
123
		}
125
		}
124
	}
126
	}
125
	
127
	
126
	private function ligneCorrespondAuxCriteres($ligne_image) {
128
	private function ligneCorrespondAuxCriteres($ligne_image) {
127
		
129
		
128
		$correspond = true;
130
		$correspond = true;
129
		
131
		
130
		foreach($this->criteres as $critere => $valeur) {
132
		foreach($this->criteres as $critere => $valeur) {
131
			
133
			
132
			$valeur = trim($valeur);
134
			$valeur = trim($valeur);
133
			
135
			
134
			switch($critere) {
136
			switch($critere) {
135
				case "recherche":
137
				case "recherche":
136
					$correspond = (substr($ligne_image->observation->ce_zone_geo, 0, 2) == $valeur) |
138
					$correspond = (substr($ligne_image->observation->ce_zone_geo, 0, 2) == $valeur) |
137
					$ligne_image->observation->ce_zone_geo == $valeur |
139
					$ligne_image->observation->ce_zone_geo == $valeur |
138
					stristr($ligne_image->observation->nom_sel, $valeur) != '' |
140
					stristr($ligne_image->observation->nom_sel, $valeur) != '' |
139
					stristr($ligne_image->observation->nom_ret, $valeur) != '' |
141
					stristr($ligne_image->observation->nom_ret, $valeur) != '' |
140
					stristr($ligne_image->observation->nom_sel, $valeur) != '' |
142
					stristr($ligne_image->observation->nom_sel, $valeur) != '' |
141
					stristr($ligne_image->observation->nom_ret, $valeur) != '' |
143
					stristr($ligne_image->observation->nom_ret, $valeur) != '' |
142
					stristr($ligne_image->observation->mots_cles_texte, $valeur) != '' |
144
					stristr($ligne_image->observation->mots_cles_texte, $valeur) != '' |
143
					stristr($ligne_image->observation->date_observation, $valeur) != '' |
145
					stristr($ligne_image->observation->date_observation, $valeur) != '' |
144
					stristr($ligne_image->date_prise_de_vue, $valeur) != '' |
146
					stristr($ligne_image->date_prise_de_vue, $valeur) != '' |
145
					stristr($ligne_image->observation->zone_geo, $valeur) != '' |
147
					stristr($ligne_image->observation->zone_geo, $valeur) != '' |
146
					stristr($ligne_image->observation->famille, $valeur) != '' |
148
					stristr($ligne_image->observation->famille, $valeur) != '' |
147
					stristr($ligne_image->mots_cles_texte, $valeur) != '' |
149
					stristr($ligne_image->mots_cles_texte, $valeur) != '' |
148
					stristr($ligne_image->prenom_utilisateur, $valeur) != '' |
150
					stristr($ligne_image->prenom_utilisateur, $valeur) != '' |
149
					stristr($ligne_image->nom_utilisateur, $valeur) != '' |
151
					stristr($ligne_image->nom_utilisateur, $valeur) != '' |
150
					stristr($ligne_image->courriel_utilisateur, $valeur) != '' 	;		
152
					stristr($ligne_image->courriel_utilisateur, $valeur) != '' 	;		
151
				break;
153
				break;
152
				case "dpt":
154
				case "dpt":
153
					$correspond = (substr($ligne_image->observation->ce_zone_geo, 0, 2) == $valeur) |
155
					$correspond = (substr($ligne_image->observation->ce_zone_geo, 0, 2) == $valeur) |
154
					$ligne_image->observation->ce_zone_geo == $valeur;
156
					$ligne_image->observation->ce_zone_geo == $valeur;
155
				break;
157
				break;
156
				case "taxon":
158
				case "taxon":
157
					$correspond = stristr($ligne_image->observation->nom_sel, $valeur) != '' |
159
					$correspond = stristr($ligne_image->observation->nom_sel, $valeur) != '' |
158
								stristr($ligne_image->observation->nom_ret, $valeur) != '';
160
								stristr($ligne_image->observation->nom_ret, $valeur) != '';
159
				break;
161
				break;
160
				case "genre":
162
				case "genre":
161
					$correspond = stristr($ligne_image->observation->nom_sel, $valeur) != '' |
163
					$correspond = stristr($ligne_image->observation->nom_sel, $valeur) != '' |
162
								stristr($ligne_image->observation->nom_ret, $valeur) != '';
164
								stristr($ligne_image->observation->nom_ret, $valeur) != '';
163
				break;
165
				break;
164
				case "mot_clef":
166
				case "mot_clef":
165
					$correspond = stristr($ligne_image->observation->mot_cles_texte, $valeur);
167
					$correspond = stristr($ligne_image->observation->mot_cles_texte, $valeur);
166
				break;
168
				break;
167
				case "date":
169
				case "date":
168
					$correspond = stristr($ligne_image->observation->date_observation, $valeur) != '' |
170
					$correspond = stristr($ligne_image->observation->date_observation, $valeur) != '' |
169
									stristr($ligne_image->date_prise_de_vue, $valeur) != '';
171
									stristr($ligne_image->date_prise_de_vue, $valeur) != '';
170
				break;
172
				break;
171
				case "commune":
173
				case "commune":
172
					$correspond = stristr($ligne_image->observation->zone_geo, $valeur);
174
					$correspond = stristr($ligne_image->observation->zone_geo, $valeur);
173
				break;
175
				break;
174
				case "famille":
176
				case "famille":
175
					$correspond = stristr($ligne_image->observation->famille, $valeur);
177
					$correspond = stristr($ligne_image->observation->famille, $valeur);
176
				break;
178
				break;
177
				case "tag":
179
				case "tag":
178
					$correspond = stristr($ligne_image->mots_cles_texte, $valeur);
180
					$correspond = stristr($ligne_image->mots_cles_texte, $valeur);
179
				break;
181
				break;
180
				case "auteur":
182
				case "auteur":
181
					$correspond = stristr($ligne_image->prenom_utilisateur, $valeur) != '' | 
183
					$correspond = stristr($ligne_image->prenom_utilisateur, $valeur) != '' | 
182
									stristr($ligne_image->nom_utilisateur, $valeur) != '' | 
184
									stristr($ligne_image->nom_utilisateur, $valeur) != '' | 
183
									stristr($ligne_image->courriel_utilisateur, $valeur) != '' ;
185
									stristr($ligne_image->courriel_utilisateur, $valeur) != '' ;
184
				break;
186
				break;
185
			}
187
			}
186
			
188
			
187
			if(!$correspond) break;
189
			if(!$correspond) break;
188
		}
190
		}
189
		
191
		
190
		return $correspond;
192
		return $correspond;
191
	}
193
	}
192
	
194
	
193
	private function comparerObservations($image_a, $image_b) {
195
	private function comparerObservations($image_a, $image_b) {
-
 
196
		
-
 
197
		$valeur_tri = null;
194
		
198
		
-
 
199
		switch($this->champ_tri) {
-
 
200
			case 'date_observation':
-
 
201
				$valeur_tri = $this->comparerDate($image_a, $image_b);
-
 
202
			break;
-
 
203
			case 'nb_votes':
-
 
204
				$valeur_tri = $this->comparerNbVote($image_a, $image_b);
-
 
205
			break;
-
 
206
		}
-
 
207
		
-
 
208
		return $valeur_tri;
-
 
209
	}
-
 
210
	
-
 
211
	private function comparerDate($image_a, $image_b) {
-
 
212
		
195
		$champ_tri = $this->champ_tri;
213
		$champ_tri = 'date_observation';
196
		
214
		
197
		$champ_a_comparer_a = isset($image_a->observation->$champ_tri) ? $image_a->observation->$champ_tri : 0;
215
		$champ_a_comparer_a = isset($image_a->observation->$champ_tri) ? $image_a->observation->$champ_tri : 0;
198
		$champ_a_comparer_b = isset($image_b->observation->$champ_tri) ? $image_b->observation->$champ_tri : 0;
216
		$champ_a_comparer_b = isset($image_b->observation->$champ_tri) ? $image_b->observation->$champ_tri : 0;
199
		
217
		
200
		if($this->ordre == 'asc') {
218
		if($this->ordre == 'asc') {
201
			return $champ_a_comparer_a > $champ_a_comparer_b;
219
			return $champ_a_comparer_a > $champ_a_comparer_b;
202
		} else {
220
		} else {
203
			return $champ_a_comparer_a < $champ_a_comparer_b;
221
			return $champ_a_comparer_a < $champ_a_comparer_b;
204
		}
222
		}
-
 
223
	}
-
 
224
	
-
 
225
	private function comparerNbVote($image_a, $image_b) {
-
 
226
		$id_protocole = $this->protocole_tri;
-
 
227
		$votes_a = 0;
-
 
228
		$votes_b = 0;
-
 
229
		
-
 
230
		if(isset($image_a->votes)) {
-
 
231
			foreach($image_a->votes as $vote_a) {
-
 
232
				$votes_a += $vote_a->id_protocole == $id_protocole ? 1 : 0;
-
 
233
			}
-
 
234
		}
-
 
235
		
-
 
236
		if(isset($image_b->votes)) {
-
 
237
			foreach($image_b->votes as $vote_b) {
-
 
238
				$votes_b += $vote_b->id_protocole == $id_protocole ? 1 : 0;
-
 
239
			}
-
 
240
		}
-
 
241
		
-
 
242
		if ($votes_a == $votes_b) {
-
 
243
			return 0;
-
 
244
		}
-
 
245
		
-
 
246
		if($this->ordre == 'asc') {
-
 
247
			return ($votes_a < $votes_b) ? -1 : 1;
-
 
248
		} else {
-
 
249
			return ($votes_a > $votes_b) ? -1 : 1;
-
 
250
		}
205
	}
251
	}
206
	
252
	
207
	private function obtenirImagesAvecObservations() {
253
	private function obtenirImagesAvecObservations() {
208
		return json_decode(file_get_contents(realpath(dirname(__FILE__)).'/imagesmock.json'));
254
		return json_decode(file_get_contents(realpath(dirname(__FILE__)).'/imagesmock.json'));
209
	}
255
	}
210
}
256
}
211
?>
257
?>