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