Subversion Repositories eFlore/Applications.del

Rev

Rev 468 | 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) {
121
				switch($critere) {
122
					case "recherche":
123
						$correspond = (substr($ligne_observation->ce_zone_geo, 0, 2) == $valeur) |
124
						stristr($ligne_observation->nom_sel, $valeur) != '' |
125
						stristr($ligne_observation->nom_ret, $valeur) != '' |
126
						stristr($ligne_observation->nom_sel, $valeur) != '' |
127
						stristr($ligne_observation->nom_ret, $valeur) != '' |
128
						stristr($ligne_observation->mots_cles_texte, $valeur) != '' |
129
						stristr($ligne_observation->date_observation, $valeur) != '' |
130
						stristr($ligne_observation->zone_geo, $valeur) != '' |
131
						stristr($ligne_observation->famille, $valeur) != '';
132
 
133
 
134
						foreach ($ligne_observation->images as $image){
135
							$correspond =$correspond|stristr($image->date_prise_de_vue, $valeur) != '' |
136
							stristr($image->mots_cles_texte, $valeur) != '' |
137
							stristr($image->prenom_utilisateur, $valeur) != '' |
138
							stristr($image->nom_utilisateur, $valeur) != '';
139
						}
140
						break;
141
					case "dpt":
142
						$correspond = (substr($ligne_observation->ce_zone_geo, 0, 2) == $valeur);
143
						break;
144
					case "taxon":
145
						$correspond = stristr($ligne_observation->nom_sel, $valeur) != '' |
146
						stristr($ligne_observation->nom_ret, $valeur) != '';
147
						break;
148
					case "genre":
149
						$correspond = stristr($ligne_observation->nom_sel, $valeur) != '' |
150
						stristr($ligne_observation->nom_ret, $valeur) != '';
151
						break;
152
					case "mot_clef":
153
						$correspond = stristr($ligne_observation->mot_cles_texte, $valeur);
154
						break;
155
					case "date":
156
						$correspond = stristr($ligne_observation->date_observation, $valeur) != '';
157
						foreach ($ligne_observation->images as $image){
158
							$correspond = $correspond|stristr($image->date_prise_de_vue, $valeur) != '';
159
						}
160
 
161
						break;
162
					case "commune":
163
						$correspond = stristr($ligne_observation->zone_geo, $valeur);
164
						break;
165
					case "famille":
166
						$correspond = stristr($ligne_observation->famille, $valeur);
167
						break;
168
					case "tag":
169
						$correspond = stristr($ligne_observation->images->mots_cles_texte, $valeur);
170
						break;
171
					case "auteur":
172
						$correspond = stristr($ligne_observation->images->prenom_utilisateur, $valeur) != '' ;
173
						foreach ($ligne_observation->images as $image){
174
							$correspond = $correspond|stristr($image->nom_utilisateur, $valeur) != '';
175
						}
176
 
177
						break;
178
				}
179
 
180
				if(!$correspond) break;
181
			}
182
 
183
			return $correspond;
468 aurelien 184
		}
487 benjamin 185
 
186
		private function comparerObservations($observation_a, $observation_b) {
187
 
188
			$champ_tri = $this->champ_tri;
189
 
190
			$observation_a = isset($observation_a->$champ_tri) ? $observation_a->$champ_tri : -1;
191
			$observation_b = isset($observation_b->$champ_tri) ? $observation_b->$champ_tri : -1;
192
 
193
			if($this->ordre == 'asc') {
194
				return $observation_a > $observation_b;
195
			} else {
196
				return $observation_a < $observation_b;
197
			}
198
		}
199
 
200
		private function obtenirObservationsAvecImages() {
201
			return json_decode(file_get_contents(realpath(dirname(__FILE__)).'/obsmock.json'));
202
		}
341 aurelien 203
}
204
?>