Subversion Repositories eFlore/Applications.del

Rev

Rev 499 | 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
 
486 aurelien 105
	private function collecterCriteresRecherche() {
106
 
107
		$this->debut = isset($_GET['debut']) ? $_GET['debut'] : $this->debut;
108
		$this->limite = isset($_GET['limite']) ? $_GET['limite'] : $this->limite;
109
 
110
		$this->champ_tri = isset($_GET['tri']) ? $_GET['tri'] : $this->champ_tri;
111
		$this->ordre = isset($_GET['ordre']) ? $_GET['ordre'] : $this->ordre;
112
 
113
		$this->tri_demande = isset($_GET['tri']) ? true : false;
114
 
115
		if(isset($_GET['format'])) {
116
			$this->format = strtolower($_GET['format']);
117
		}
118
 
119
		foreach($_GET as $cle => $valeur) {
120
			if(in_array(strtolower(trim($cle)), $this->criteres_acceptes)) {
121
				$this->criteres[$cle] = $valeur;
122
			}
123
		}
124
	}
125
 
126
	private function ligneCorrespondAuxCriteres($ligne_image) {
127
 
128
		$correspond = true;
129
 
130
		foreach($this->criteres as $critere => $valeur) {
499 aurelien 131
 
132
			$valeur = trim($valeur);
133
 
486 aurelien 134
			switch($critere) {
135
				case "recherche":
136
					$correspond = (substr($ligne_image->observation->ce_zone_geo, 0, 2) == $valeur) |
499 aurelien 137
					$ligne_image->observation->ce_zone_geo == $valeur |
486 aurelien 138
					stristr($ligne_image->observation->nom_sel, $valeur) != '' |
139
					stristr($ligne_image->observation->nom_ret, $valeur) != '' |
140
					stristr($ligne_image->observation->nom_sel, $valeur) != '' |
141
					stristr($ligne_image->observation->nom_ret, $valeur) != '' |
142
					stristr($ligne_image->observation->mots_cles_texte, $valeur) != '' |
143
					stristr($ligne_image->observation->date_observation, $valeur) != '' |
144
					stristr($ligne_image->date_prise_de_vue, $valeur) != '' |
145
					stristr($ligne_image->observation->zone_geo, $valeur) != '' |
146
					stristr($ligne_image->observation->famille, $valeur) != '' |
147
					stristr($ligne_image->mots_cles_texte, $valeur) != '' |
148
					stristr($ligne_image->prenom_utilisateur, $valeur) != '' |
499 aurelien 149
					stristr($ligne_image->nom_utilisateur, $valeur) != '' |
150
					stristr($ligne_image->courriel_utilisateur, $valeur) != '' 	;
486 aurelien 151
				break;
152
				case "dpt":
499 aurelien 153
					$correspond = (substr($ligne_image->observation->ce_zone_geo, 0, 2) == $valeur) |
154
					$ligne_image->observation->ce_zone_geo == $valeur;
486 aurelien 155
				break;
156
				case "taxon":
157
					$correspond = stristr($ligne_image->observation->nom_sel, $valeur) != '' |
158
								stristr($ligne_image->observation->nom_ret, $valeur) != '';
159
				break;
160
				case "genre":
161
					$correspond = stristr($ligne_image->observation->nom_sel, $valeur) != '' |
162
								stristr($ligne_image->observation->nom_ret, $valeur) != '';
163
				break;
164
				case "mot_clef":
165
					$correspond = stristr($ligne_image->observation->mot_cles_texte, $valeur);
166
				break;
167
				case "date":
168
					$correspond = stristr($ligne_image->observation->date_observation, $valeur) != '' |
169
									stristr($ligne_image->date_prise_de_vue, $valeur) != '';
170
				break;
171
				case "commune":
172
					$correspond = stristr($ligne_image->observation->zone_geo, $valeur);
173
				break;
174
				case "famille":
175
					$correspond = stristr($ligne_image->observation->famille, $valeur);
176
				break;
177
				case "tag":
178
					$correspond = stristr($ligne_image->mots_cles_texte, $valeur);
179
				break;
180
				case "auteur":
181
					$correspond = stristr($ligne_image->prenom_utilisateur, $valeur) != '' |
499 aurelien 182
									stristr($ligne_image->nom_utilisateur, $valeur) != '' |
183
									stristr($ligne_image->courriel_utilisateur, $valeur) != '' ;
486 aurelien 184
				break;
185
			}
186
 
187
			if(!$correspond) break;
188
		}
189
 
190
		return $correspond;
191
	}
192
 
468 aurelien 193
	private function comparerObservations($image_a, $image_b) {
194
 
195
		$champ_tri = $this->champ_tri;
196
 
197
		$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;
199
 
487 benjamin 200
		if($this->ordre == 'asc') {
468 aurelien 201
			return $champ_a_comparer_a > $champ_a_comparer_b;
202
		} else {
203
			return $champ_a_comparer_a < $champ_a_comparer_b;
204
		}
205
	}
206
 
384 aurelien 207
	private function obtenirImagesAvecObservations() {
208
		return json_decode(file_get_contents(realpath(dirname(__FILE__)).'/imagesmock.json'));
209
	}
210
}
211
?>