New file |
0,0 → 1,247 |
<?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; |
|
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()); |
} |
|
/** |
* 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; |
} |
} |
} |
|
private function ligneCorrespondAuxCriteres($ligne_observation) { |
|
$correspond = true; |
|
foreach($this->criteres as $critere => $valeur) { |
|
$valeur = trim($valeur); |
|
switch($critere) { |
case "recherche": |
$correspond = $this->correspondAChampDepartement($ligne_observation, $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->milieu, $valeur) != '' | |
stristr($ligne_observation->famille, $valeur) != '' | |
$this->correspondAChampNomOuPrenom($ligne_observation, $valeur); |
|
foreach ($ligne_observation->images as $image){ |
$correspond =$correspond|stristr($image->date_prise_de_vue, $valeur) != '' | |
stristr($image->mots_cles_texte, $valeur) != ''; |
} |
break; |
case "dpt": |
$correspond = $this->correspondAChampDepartement($ligne_observation, $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 = $this->correspondAChampNomOuPrenom($ligne_observation, $valeur); |
break; |
} |
|
if(!$correspond) break; |
} |
|
return $correspond; |
} |
|
private function correspondAChampDepartement($ligne_observation, $valeur) { |
$correspond = false; |
if(!is_numeric($valeur)) { |
$valeur = $this->obtenirCodeDepartementPourNom($valeur); |
} |
|
if($valeur) { |
$correspond = (substr($ligne_observation->ce_zone_geo, 0, 2) == $valeur) | |
$ligne_observation->ce_zone_geo == $valeur; |
} |
|
return $correspond; |
} |
|
private function correspondAChampNomOuPrenom($ligne_observation, $valeur) { |
|
while(substr_count($valeur, ' ') > 0) { |
$valeur = str_replace(' ',' ', $valeur); |
} |
|
$chaine_nom_prenom = strtolower($ligne_observation->nom_utilisateur).' '.strtolower($ligne_observation->prenom_utilisateur); |
$chaine_nom_prenom = trim($chaine_nom_prenom); |
$chaine_prenom_nom = strtolower($ligne_observation->prenom_utilisateur).' '.strtolower($ligne_observation->nom_utilisateur); |
$chaine_prenom_nom = trim($chaine_prenom_nom); |
$valeur = strtolower($valeur); |
|
$correspond = stristr($ligne_observation->prenom_utilisateur, $valeur) != '' | |
stristr($ligne_observation->nom_utilisateur, $valeur) != '' | |
stristr($chaine_nom_prenom, $valeur) != '' | |
stristr($chaine_prenom_nom, $valeur) != ''; |
|
return $correspond; |
} |
|
private function obtenirCodeDepartementPourNom($nom) { |
$nom = $this->formaterChaineMinusculeSansAccents($nom); |
$tableau_communes = (array)$this->getTableauCorrespondanceDepartement(); |
$code = isset($tableau_communes[$nom]) ? $tableau_communes[$nom] : false; |
|
return $code; |
} |
|
private function getTableauCorrespondanceDepartement() { |
return json_decode(file_get_contents(realpath(dirname(__FILE__)).'/mocks/departementsmock.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__)).'/mocks/obsmock.json')); |
} |
} |
?> |