Rev 384 | Blame | Last modification | View Log | RSS feed
<?php
class Images extends Del {
private $debut = 0;
private $limite = 50;
private $champ_tri = 'date_observation';
private $ordre = 'desc';
private $tri_demande = false;
/**
* Méthode appelée avec une requête de type GET avec une url de la forme
* http://localhost/jrest/ExempleService/
*
* Sert normalement à renvoyer la description des possibilités du service
*
*/
public function getRessource() {
return $this->getElement(array());
}
/**
* Méthode appelée avec une requête de type GET avec une url de la forme
* http://localhost/jrest/ExempleService/uid[0]/$uid[1]/ etc...
*
* Sert normalement à ramener un élément précis indiqué par un identifiant
* qui se situe dans l'url après le nom du service
* Le filtrage, le format de retour, les paramètres optionnels ... sont normalement indiqués
* dans le tableau $_GET
* Pour obtenir l'élément 2501 dans le format HTML cela pourrait donner
* http://localhost/jrest/ExempleService/2501?format=HTML
*
* @param $uid un tableau contenant les élements passés dans l'url après le nom du service
*
*/
public function getElement($uid)
{
$format = 'json';
$this->debut = isset($_GET['debut']) ? $_GET['debut'] : $this->debut;
$this->limite = isset($_GET['limite']) ? $_GET['limite'] : $this->limite;
$this->champ_tri = isset($_GET['tri']) ? $_GET['tri'] : $this->champ_tri;
$this->ordre = isset($_GET['ordre']) ? $_GET['ordre'] : $this->ordre;
$this->tri_demande = isset($_GET['tri']) ? true : false;
if(isset($_GET['format'])) {
$format = strtolower($_GET['format']);
}
switch ($format) {
case 'html':
case 'json':
$images = $this->obtenirImagesAvecObservations();
$total = count($images);
if($this->tri_demande) {
usort($images, array($this,'comparerObservations'));
}
$tranche = array_slice($images,$this->debut,$this->limite);
$retour = array('total' => $total,
'contenu' => $tranche
);
$retour = json_encode($retour);
$mime = 'application/json';
break;
case 'xml':
break;
}
$this->envoyer($retour,$mime);
}
private function comparerObservations($image_a, $image_b) {
$champ_tri = $this->champ_tri;
$champ_a_comparer_a = isset($image_a->observation->$champ_tri) ? $image_a->observation->$champ_tri : 0;
$champ_a_comparer_b = isset($image_b->observation->$champ_tri) ? $image_b->observation->$champ_tri : 0;
if($this->ordre == 'desc') {
return $champ_a_comparer_a > $champ_a_comparer_b;
} else {
return $champ_a_comparer_a < $champ_a_comparer_b;
}
}
private function obtenirImagesAvecObservations() {
return json_decode(file_get_contents(realpath(dirname(__FILE__)).'/imagesmock.json'));
}
}
?>