341 |
aurelien |
1 |
<?php
|
|
|
2 |
class Observations extends Del {
|
|
|
3 |
|
|
|
4 |
private $debut = 0;
|
|
|
5 |
private $limite = 50;
|
|
|
6 |
|
468 |
aurelien |
7 |
private $champ_tri = 'date_observation';
|
|
|
8 |
private $ordre = 'desc';
|
|
|
9 |
private $tri_demande = false;
|
|
|
10 |
|
341 |
aurelien |
11 |
/**
|
|
|
12 |
* Méthode appelée avec une requête de type GET avec une url de la forme
|
|
|
13 |
* http://localhost/jrest/ExempleService/
|
|
|
14 |
*
|
|
|
15 |
* Sert normalement à renvoyer la description des possibilités du service
|
|
|
16 |
*
|
|
|
17 |
*/
|
|
|
18 |
public function getRessource() {
|
|
|
19 |
return $this->getElement(array());
|
|
|
20 |
}
|
|
|
21 |
|
|
|
22 |
/**
|
|
|
23 |
* 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...
|
|
|
25 |
*
|
|
|
26 |
* 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
|
|
|
28 |
* Le filtrage, le format de retour, les paramètres optionnels ... sont normalement indiqués
|
|
|
29 |
* dans le tableau $_GET
|
|
|
30 |
* Pour obtenir l'élément 2501 dans le format HTML cela pourrait donner
|
|
|
31 |
* http://localhost/jrest/ExempleService/2501?format=HTML
|
|
|
32 |
*
|
|
|
33 |
* @param $uid un tableau contenant les élements passés dans l'url après le nom du service
|
|
|
34 |
*
|
|
|
35 |
*/
|
|
|
36 |
public function getElement($uid)
|
|
|
37 |
{
|
|
|
38 |
$format = 'html';
|
|
|
39 |
|
|
|
40 |
$this->debut = isset($_GET['debut']) ? $_GET['debut'] : $this->debut;
|
|
|
41 |
$this->limite = isset($_GET['limite']) ? $_GET['limite'] : $this->limite;
|
468 |
aurelien |
42 |
|
|
|
43 |
$this->champ_tri = isset($_GET['tri']) ? $_GET['tri'] : $this->champ_tri;
|
|
|
44 |
$this->ordre = isset($_GET['ordre']) ? $_GET['ordre'] : $this->ordre;
|
|
|
45 |
|
|
|
46 |
$this->tri_demande = isset($_GET['tri']) ? true : false;
|
341 |
aurelien |
47 |
|
|
|
48 |
if(isset($_GET['format'])) {
|
|
|
49 |
$format = strtolower($_GET['format']);
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
switch ($format) {
|
|
|
53 |
|
|
|
54 |
case 'html':
|
|
|
55 |
|
|
|
56 |
case 'json':
|
|
|
57 |
$obs = $this->obtenirObservationsAvecImages();
|
|
|
58 |
$total = count($obs);
|
468 |
aurelien |
59 |
if($this->tri_demande) {
|
|
|
60 |
usort($obs, array($this,'comparerObservations'));
|
|
|
61 |
}
|
341 |
aurelien |
62 |
$tranche = array_slice($obs,$this->debut,$this->limite);
|
|
|
63 |
|
|
|
64 |
$retour = array('total' => count($obs),
|
|
|
65 |
'contenu' => array_slice($obs,$this->debut,$this->limite)
|
|
|
66 |
);
|
|
|
67 |
$retour = json_encode($retour);
|
|
|
68 |
$mime = 'application/json';
|
|
|
69 |
break;
|
|
|
70 |
|
|
|
71 |
case 'xml':
|
|
|
72 |
break;
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
$this->envoyer($retour,$mime);
|
|
|
76 |
}
|
|
|
77 |
|
468 |
aurelien |
78 |
private function comparerObservations($observation_a, $observation_b) {
|
|
|
79 |
|
|
|
80 |
$champ_tri = $this->champ_tri;
|
|
|
81 |
|
|
|
82 |
$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;
|
|
|
84 |
|
|
|
85 |
if($this->ordre == 'desc') {
|
|
|
86 |
return $observation_a > $observation_b;
|
|
|
87 |
} else {
|
|
|
88 |
return $observation_a < $observation_b;
|
|
|
89 |
}
|
|
|
90 |
}
|
|
|
91 |
|
341 |
aurelien |
92 |
private function obtenirObservationsAvecImages() {
|
|
|
93 |
return json_decode(file_get_contents(realpath(dirname(__FILE__)).'/obsmock.json'));
|
|
|
94 |
}
|
|
|
95 |
}
|
|
|
96 |
?>
|