Subversion Repositories eFlore/Applications.del

Rev

Rev 487 | 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';
10
	private $ordre = 'desc';
11
	private $tri_demande = false;
12
 
486 aurelien 13
	private $criteres_acceptes = array(
14
		"recherche",
15
		"dpt",
16
		"taxon",
17
		"genre",
18
		"mot_clef",
19
		"date",
20
		"commune",
21
		"famille",
22
		"tag",
23
		"auteur"
24
	);
25
 
26
	private $criteres = array();
27
 
384 aurelien 28
	/**
29
	* Méthode appelée avec une requête de type GET avec une url de la forme
30
	* http://localhost/jrest/ExempleService/
31
	*
32
	* Sert normalement à renvoyer la description des possibilités du service
33
	*
34
	*/
35
	public function getRessource() {
36
		return $this->getElement(array());
37
	}
38
 
39
	/**
40
	* 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
	*
43
	* 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
	* Le filtrage, le format de retour, les paramètres optionnels ... sont normalement indiqués
46
	* dans le tableau $_GET
47
	* Pour obtenir l'élément 2501 dans le format HTML cela pourrait donner
48
	* http://localhost/jrest/ExempleService/2501?format=HTML
49
	*
50
	* @param $uid un tableau contenant les élements passés dans l'url après le nom du service
51
	*
52
	*/
53
	public function getElement($uid)
486 aurelien 54
	{
55
		$this->collecterCriteresRecherche();
384 aurelien 56
 
486 aurelien 57
		switch ($this->format) {
384 aurelien 58
 
59
			case 'html':
60
 
61
			case 'json':
62
				$images = $this->obtenirImagesAvecObservations();
63
 
486 aurelien 64
				if(!empty($this->criteres)) {
65
					$images_filtrees = array();
66
					foreach($images as $ligne_image) {
67
						if($this->ligneCorrespondAuxCriteres($ligne_image)) {
68
							$images_filtrees[] = $ligne_image;
69
						}
70
					}
71
				} else  {
72
					$images_filtrees = $images;
73
				}
74
 
75
				$total = count($images_filtrees);
468 aurelien 76
				if($this->tri_demande) {
486 aurelien 77
					usort($images_filtrees, array($this,'comparerObservations'));
468 aurelien 78
				}
486 aurelien 79
				$tranche = array_slice($images_filtrees,$this->debut,$this->limite);
384 aurelien 80
 
468 aurelien 81
				$retour = array('total' => $total,
384 aurelien 82
								'contenu' => $tranche
499 aurelien 83
								);
384 aurelien 84
 
85
				$retour = json_encode($retour);
86
				$mime = 'application/json';
87
				break;
88
 
89
			case 'xml':
90
				break;
91
		}
92
 
93
		$this->envoyer($retour,$mime);
94
	}
95
 
499 aurelien 96
	private function ajouterVoteProtocoleAleatoire() {
97
		$vote = array('id_protocole' => round(rand(1,2)),
98
						'vote' => round(rand(0,5)),
99
						'contributeur' => $this->getAuteurPif(),
100
						'date' => date('Y-m-d')
101
						);
102
		return $vote;
103
	}
104
 
105
	private function getAuteurPif() {
106
		$auteurs = array('Aurélien', 'Benjamin', 'Elise', 'Grégoire', 'Marie');
107
		return $auteurs[round(rand(0,4))];
108
	}
109
 
486 aurelien 110
	private function collecterCriteresRecherche() {
111
 
112
		$this->debut = isset($_GET['debut']) ? $_GET['debut'] : $this->debut;
113
		$this->limite = isset($_GET['limite']) ? $_GET['limite'] : $this->limite;
114
 
115
		$this->champ_tri = isset($_GET['tri']) ? $_GET['tri'] : $this->champ_tri;
116
		$this->ordre = isset($_GET['ordre']) ? $_GET['ordre'] : $this->ordre;
117
 
118
		$this->tri_demande = isset($_GET['tri']) ? true : false;
119
 
120
		if(isset($_GET['format'])) {
121
			$this->format = strtolower($_GET['format']);
122
		}
123
 
124
		foreach($_GET as $cle => $valeur) {
125
			if(in_array(strtolower(trim($cle)), $this->criteres_acceptes)) {
126
				$this->criteres[$cle] = $valeur;
127
			}
128
		}
129
	}
130
 
131
	private function ligneCorrespondAuxCriteres($ligne_image) {
132
 
133
		$correspond = true;
134
 
135
		foreach($this->criteres as $critere => $valeur) {
499 aurelien 136
 
137
			$valeur = trim($valeur);
138
 
486 aurelien 139
			switch($critere) {
140
				case "recherche":
141
					$correspond = (substr($ligne_image->observation->ce_zone_geo, 0, 2) == $valeur) |
499 aurelien 142
					$ligne_image->observation->ce_zone_geo == $valeur |
486 aurelien 143
					stristr($ligne_image->observation->nom_sel, $valeur) != '' |
144
					stristr($ligne_image->observation->nom_ret, $valeur) != '' |
145
					stristr($ligne_image->observation->nom_sel, $valeur) != '' |
146
					stristr($ligne_image->observation->nom_ret, $valeur) != '' |
147
					stristr($ligne_image->observation->mots_cles_texte, $valeur) != '' |
148
					stristr($ligne_image->observation->date_observation, $valeur) != '' |
149
					stristr($ligne_image->date_prise_de_vue, $valeur) != '' |
150
					stristr($ligne_image->observation->zone_geo, $valeur) != '' |
151
					stristr($ligne_image->observation->famille, $valeur) != '' |
152
					stristr($ligne_image->mots_cles_texte, $valeur) != '' |
153
					stristr($ligne_image->prenom_utilisateur, $valeur) != '' |
499 aurelien 154
					stristr($ligne_image->nom_utilisateur, $valeur) != '' |
155
					stristr($ligne_image->courriel_utilisateur, $valeur) != '' 	;
486 aurelien 156
				break;
157
				case "dpt":
499 aurelien 158
					$correspond = (substr($ligne_image->observation->ce_zone_geo, 0, 2) == $valeur) |
159
					$ligne_image->observation->ce_zone_geo == $valeur;
486 aurelien 160
				break;
161
				case "taxon":
162
					$correspond = stristr($ligne_image->observation->nom_sel, $valeur) != '' |
163
								stristr($ligne_image->observation->nom_ret, $valeur) != '';
164
				break;
165
				case "genre":
166
					$correspond = stristr($ligne_image->observation->nom_sel, $valeur) != '' |
167
								stristr($ligne_image->observation->nom_ret, $valeur) != '';
168
				break;
169
				case "mot_clef":
170
					$correspond = stristr($ligne_image->observation->mot_cles_texte, $valeur);
171
				break;
172
				case "date":
173
					$correspond = stristr($ligne_image->observation->date_observation, $valeur) != '' |
174
									stristr($ligne_image->date_prise_de_vue, $valeur) != '';
175
				break;
176
				case "commune":
177
					$correspond = stristr($ligne_image->observation->zone_geo, $valeur);
178
				break;
179
				case "famille":
180
					$correspond = stristr($ligne_image->observation->famille, $valeur);
181
				break;
182
				case "tag":
183
					$correspond = stristr($ligne_image->mots_cles_texte, $valeur);
184
				break;
185
				case "auteur":
186
					$correspond = stristr($ligne_image->prenom_utilisateur, $valeur) != '' |
499 aurelien 187
									stristr($ligne_image->nom_utilisateur, $valeur) != '' |
188
									stristr($ligne_image->courriel_utilisateur, $valeur) != '' ;
486 aurelien 189
				break;
190
			}
191
 
192
			if(!$correspond) break;
193
		}
194
 
195
		return $correspond;
196
	}
197
 
468 aurelien 198
	private function comparerObservations($image_a, $image_b) {
199
 
200
		$champ_tri = $this->champ_tri;
201
 
202
		$champ_a_comparer_a = isset($image_a->observation->$champ_tri) ? $image_a->observation->$champ_tri : 0;
203
		$champ_a_comparer_b = isset($image_b->observation->$champ_tri) ? $image_b->observation->$champ_tri : 0;
204
 
487 benjamin 205
		if($this->ordre == 'asc') {
468 aurelien 206
			return $champ_a_comparer_a > $champ_a_comparer_b;
207
		} else {
208
			return $champ_a_comparer_a < $champ_a_comparer_b;
209
		}
210
	}
211
 
384 aurelien 212
	private function obtenirImagesAvecObservations() {
213
		return json_decode(file_get_contents(realpath(dirname(__FILE__)).'/imagesmock.json'));
214
	}
215
}
216
?>