Subversion Repositories eFlore/Applications.del

Compare Revisions

Ignore whitespace Rev 468 → Rev 487

/trunk/jrest/services/Observations.php
1,96 → 1,204
<?php
class Observations extends Del {
 
private $format = 'json';
 
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 = 'html';
$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']);
 
private $criteres_acceptes = array(
"recherche",
"dpt",
"taxon",
"genre",
"mot_clef",
"date",
"commune",
"famille",
"tag",
"auteur"
);
 
/**
* 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());
}
switch ($format) {
case 'html':
case 'json':
$obs = $this->obtenirObservationsAvecImages();
$total = count($obs);
if($this->tri_demande) {
usort($obs, array($this,'comparerObservations'));
 
/**
* 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)
{
$this->collecterCriteresRecherche();
 
switch ($this->format) {
 
case 'html':
 
case 'json':
 
$obs = $this->obtenirObservationsAvecImages();
$total = count($obs);
if(!empty($this->criteres)) {
$obs_filtrees = array();
foreach($obs as $ligne_observation) {
if($this->ligneCorrespondAuxCriteres($ligne_observation)) {
$obs_filtrees[] = $ligne_observation;
}
}
} else {
$obs_filtrees = $obs;
}
 
$total = count($obs_filtrees);
if($this->tri_demande) {
usort($obs_filtrees, array($this,'comparerObservations'));
}
$tranche = array_slice($obs_filtrees,$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 collecterCriteresRecherche() {
 
$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'])) {
$this->format = strtolower($_GET['format']);
}
 
foreach($_GET as $cle => $valeur) {
if(in_array(strtolower(trim($cle)), $this->criteres_acceptes)) {
$this->criteres[$cle] = $valeur;
}
$tranche = array_slice($obs,$this->debut,$this->limite);
$retour = array('total' => count($obs),
'contenu' => array_slice($obs,$this->debut,$this->limite)
);
$retour = json_encode($retour);
$mime = 'application/json';
break;
case 'xml':
break;
}
}
$this->envoyer($retour,$mime);
}
private function comparerObservations($observation_a, $observation_b) {
$champ_tri = $this->champ_tri;
$observation_a = isset($observation_a->$champ_tri) ? $observation_a->$champ_tri : -1;
$observation_b = isset($observation_b->$champ_tri) ? $observation_b->$champ_tri : -1;
if($this->ordre == 'desc') {
return $observation_a > $observation_b;
} else {
return $observation_a < $observation_b;
 
private function ligneCorrespondAuxCriteres($ligne_observation) {
 
$correspond = true;
 
foreach($this->criteres as $critere => $valeur) {
switch($critere) {
case "recherche":
$correspond = (substr($ligne_observation->ce_zone_geo, 0, 2) == $valeur) |
stristr($ligne_observation->nom_sel, $valeur) != '' |
stristr($ligne_observation->nom_ret, $valeur) != '' |
stristr($ligne_observation->nom_sel, $valeur) != '' |
stristr($ligne_observation->nom_ret, $valeur) != '' |
stristr($ligne_observation->mots_cles_texte, $valeur) != '' |
stristr($ligne_observation->date_observation, $valeur) != '' |
stristr($ligne_observation->zone_geo, $valeur) != '' |
stristr($ligne_observation->famille, $valeur) != '';
foreach ($ligne_observation->images as $image){
$correspond =$correspond|stristr($image->date_prise_de_vue, $valeur) != '' |
stristr($image->mots_cles_texte, $valeur) != '' |
stristr($image->prenom_utilisateur, $valeur) != '' |
stristr($image->nom_utilisateur, $valeur) != '';
}
break;
case "dpt":
$correspond = (substr($ligne_observation->ce_zone_geo, 0, 2) == $valeur);
break;
case "taxon":
$correspond = stristr($ligne_observation->nom_sel, $valeur) != '' |
stristr($ligne_observation->nom_ret, $valeur) != '';
break;
case "genre":
$correspond = stristr($ligne_observation->nom_sel, $valeur) != '' |
stristr($ligne_observation->nom_ret, $valeur) != '';
break;
case "mot_clef":
$correspond = stristr($ligne_observation->mot_cles_texte, $valeur);
break;
case "date":
$correspond = stristr($ligne_observation->date_observation, $valeur) != '';
foreach ($ligne_observation->images as $image){
$correspond = $correspond|stristr($image->date_prise_de_vue, $valeur) != '';
}
 
break;
case "commune":
$correspond = stristr($ligne_observation->zone_geo, $valeur);
break;
case "famille":
$correspond = stristr($ligne_observation->famille, $valeur);
break;
case "tag":
$correspond = stristr($ligne_observation->images->mots_cles_texte, $valeur);
break;
case "auteur":
$correspond = stristr($ligne_observation->images->prenom_utilisateur, $valeur) != '' ;
foreach ($ligne_observation->images as $image){
$correspond = $correspond|stristr($image->nom_utilisateur, $valeur) != '';
}
 
break;
}
 
if(!$correspond) break;
}
 
return $correspond;
}
}
private function obtenirObservationsAvecImages() {
return json_decode(file_get_contents(realpath(dirname(__FILE__)).'/obsmock.json'));
}
 
private function comparerObservations($observation_a, $observation_b) {
 
$champ_tri = $this->champ_tri;
 
$observation_a = isset($observation_a->$champ_tri) ? $observation_a->$champ_tri : -1;
$observation_b = isset($observation_b->$champ_tri) ? $observation_b->$champ_tri : -1;
 
if($this->ordre == 'asc') {
return $observation_a > $observation_b;
} else {
return $observation_a < $observation_b;
}
}
 
private function obtenirObservationsAvecImages() {
return json_decode(file_get_contents(realpath(dirname(__FILE__)).'/obsmock.json'));
}
}
?>