Subversion Repositories eFlore/Applications.del

Rev

Rev 468 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 468 Rev 487
Line 1... Line 1...
1
<?php
1
<?php
2
class Observations extends Del {
2
class Observations extends Del {
3
	
3
 
-
 
4
	private $format = 'json';
-
 
5
 
4
	private $debut = 0;
6
	private $debut = 0;
5
	private $limite = 50;
7
	private $limite = 50;
6
	
8
 
7
	private $champ_tri = 'date_observation';
9
	private $champ_tri = 'date_observation';
8
	private $ordre = 'desc';
10
	private $ordre = 'desc';
9
	private $tri_demande = false;
11
	private $tri_demande = false;
10
	
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
 
11
	/**
26
		/**
12
	* Méthode appelée avec une requête de type GET avec une url de la forme
27
		 * Méthode appelée avec une requête de type GET avec une url de la forme
13
	* http://localhost/jrest/ExempleService/
28
		 * http://localhost/jrest/ExempleService/
14
	*
29
		 *
15
	* Sert normalement à renvoyer la description des possibilités du service
30
		 * Sert normalement à renvoyer la description des possibilités du service
16
	*
31
		 *
17
	*/
32
		 */
18
	public function getRessource() {
33
		public function getRessource() {
19
		return $this->getElement(array());
34
			return $this->getElement(array());
20
	}
35
		}
21
	
36
 
22
	/**
37
		/**
23
	* Méthode appelée avec une requête de type GET avec une url de la forme
38
		 * Méthode appelée avec une requête de type GET avec une url de la forme
24
	* http://localhost/jrest/ExempleService/uid[0]/$uid[1]/ etc...
39
		 * http://localhost/jrest/ExempleService/uid[0]/$uid[1]/ etc...
25
	*
40
		 *
26
	* Sert normalement à ramener un élément précis indiqué par un identifiant
41
		 * Sert normalement à ramener un élément précis indiqué par un identifiant
27
	* qui se situe dans l'url après le nom du service
42
		 * qui se situe dans l'url après le nom du service
28
	* Le filtrage, le format de retour, les paramètres optionnels ... sont normalement indiqués
43
		 * Le filtrage, le format de retour, les paramètres optionnels ... sont normalement indiqués
29
	* dans le tableau $_GET
44
		 * dans le tableau $_GET
30
	* Pour obtenir l'élément 2501 dans le format HTML cela pourrait donner
45
		 * Pour obtenir l'élément 2501 dans le format HTML cela pourrait donner
31
	* http://localhost/jrest/ExempleService/2501?format=HTML
46
		 * http://localhost/jrest/ExempleService/2501?format=HTML
32
	*
47
		 *
33
	* @param $uid un tableau contenant les élements passés dans l'url après le nom du service
48
		 * @param $uid un tableau contenant les élements passés dans l'url après le nom du service
34
	*
49
		 *
35
	*/
50
		 */
36
	public function getElement($uid)
51
		public function getElement($uid)
37
	{
52
		{
-
 
53
			$this->collecterCriteresRecherche();
-
 
54
 
-
 
55
			switch ($this->format) {
-
 
56
 
38
		$format = 'html';
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
			}
39
		
91
 
-
 
92
			$this->envoyer($retour,$mime);
-
 
93
		}
-
 
94
 
-
 
95
		private function collecterCriteresRecherche() {
-
 
96
 
40
		$this->debut = isset($_GET['debut']) ? $_GET['debut'] : $this->debut;
97
			$this->debut = isset($_GET['debut']) ? $_GET['debut'] : $this->debut;
41
		$this->limite = isset($_GET['limite']) ? $_GET['limite'] : $this->limite;
98
			$this->limite = isset($_GET['limite']) ? $_GET['limite'] : $this->limite;
42
		
99
 
43
		$this->champ_tri = isset($_GET['tri']) ? $_GET['tri'] : $this->champ_tri;
100
			$this->champ_tri = isset($_GET['tri']) ? $_GET['tri'] : $this->champ_tri;
44
		$this->ordre = isset($_GET['ordre']) ? $_GET['ordre'] : $this->ordre;
101
			$this->ordre = isset($_GET['ordre']) ? $_GET['ordre'] : $this->ordre;
45
		
102
 
46
		$this->tri_demande = isset($_GET['tri']) ? true : false;
103
			$this->tri_demande = isset($_GET['tri']) ? true : false;
47
	
104
 
48
		if(isset($_GET['format'])) {
105
			if(isset($_GET['format'])) {
49
			$format = strtolower($_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;
-
 
112
				}
-
 
113
			}
50
		}
114
		}
-
 
115
 
-
 
116
		private function ligneCorrespondAuxCriteres($ligne_observation) {
-
 
117
 
-
 
118
			$correspond = true;
51
	
119
 
-
 
120
			foreach($this->criteres as $critere => $valeur) {
52
		switch ($format) {
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
							
53
				
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;
54
			case 'html':
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
						}
55
					
160
 
-
 
161
						break;
56
			case 'json':
162
					case "commune":
57
				$obs = $this->obtenirObservationsAvecImages();
163
						$correspond = stristr($ligne_observation->zone_geo, $valeur);
-
 
164
						break;
58
				$total = count($obs);
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;
59
				if($this->tri_demande) {
171
					case "auteur":
-
 
172
						$correspond = stristr($ligne_observation->images->prenom_utilisateur, $valeur) != '' ;
60
					usort($obs, array($this,'comparerObservations'));
173
						foreach ($ligne_observation->images as $image){
-
 
174
							$correspond = $correspond|stristr($image->nom_utilisateur, $valeur) != '';
-
 
175
						}
-
 
176
 
-
 
177
						break;
61
				}
178
				}
62
				$tranche = array_slice($obs,$this->debut,$this->limite);
-
 
63
				
179
 
64
				$retour = array('total' => count($obs),
-
 
65
								'contenu' => array_slice($obs,$this->debut,$this->limite)
-
 
66
								);
-
 
67
				$retour = json_encode($retour);
180
				if(!$correspond) break;
68
				$mime = 'application/json';
-
 
69
				break;
-
 
70
					
-
 
71
			case 'xml':
-
 
72
				break;
-
 
73
		}
181
			}
74
	
182
 
75
		$this->envoyer($retour,$mime);
183
			return $correspond;
76
	}
184
		}
77
	
185
 
78
	private function comparerObservations($observation_a, $observation_b) {
186
		private function comparerObservations($observation_a, $observation_b) {
79
	
187
 
80
		$champ_tri = $this->champ_tri;
188
			$champ_tri = $this->champ_tri;
81
	
189
 
82
		$observation_a = isset($observation_a->$champ_tri) ? $observation_a->$champ_tri : -1;
190
			$observation_a = isset($observation_a->$champ_tri) ? $observation_a->$champ_tri : -1;
83
		$observation_b = isset($observation_b->$champ_tri) ? $observation_b->$champ_tri : -1;
191
			$observation_b = isset($observation_b->$champ_tri) ? $observation_b->$champ_tri : -1;
84
	
192
 
85
		if($this->ordre == 'desc') {
193
			if($this->ordre == 'asc') {
86
			return $observation_a > $observation_b;
194
				return $observation_a > $observation_b;
87
		} else {
195
			} else {
88
			return $observation_a < $observation_b;
196
				return $observation_a < $observation_b;
89
		}
197
			}
90
	}
198
		}
91
	
199
 
92
	private function obtenirObservationsAvecImages() {
200
		private function obtenirObservationsAvecImages() {
93
		return json_decode(file_get_contents(realpath(dirname(__FILE__)).'/obsmock.json'));
201
			return json_decode(file_get_contents(realpath(dirname(__FILE__)).'/obsmock.json'));
94
	}
202
		}
95
}
203
}
96
?>
204
?>
97
205