Rev 505 | Blame | Last modification | View Log | RSS feed
<?phpclass Images extends Del {private $format = 'json';private $debut = 0;private $limite = 50;private $champ_tri = 'date_observation';private $protocole_tri = 0;private $ordre = 'desc';private $tri_demande = false;private $criteres_acceptes = array("recherche","dpt","taxon","genre","mot_clef","date","commune","famille","tag","auteur");private $criteres = array();/*** 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':$images = $this->obtenirImagesAvecObservations();if(!empty($this->criteres)) {$images_filtrees = array();foreach($images as $ligne_image) {if($this->ligneCorrespondAuxCriteres($ligne_image)) {$images_filtrees[] = $ligne_image;}}} else {$images_filtrees = $images;}$total = count($images_filtrees);if($this->tri_demande) {usort($images_filtrees, array($this,'comparerObservations'));}$tranche = array_slice($images_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 ajouterVoteProtocoleAleatoire() {$vote = array('id_protocole' => round(rand(1,2)),'vote' => round(rand(0,5)),'contributeur' => $this->getAuteurPif(),'date' => date('Y-m-d'));return $vote;}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;$this->protocole_tri = isset($_GET['id_protocole']) ? $_GET['id_protocole'] : $this->protocole_tri;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_image) {$correspond = true;foreach($this->criteres as $critere => $valeur) {$valeur = trim($valeur);switch($critere) {case "recherche":$correspond = (substr($ligne_image->observation->ce_zone_geo, 0, 2) == $valeur) |$ligne_image->observation->ce_zone_geo == $valeur |stristr($ligne_image->observation->nom_sel, $valeur) != '' |stristr($ligne_image->observation->nom_ret, $valeur) != '' |stristr($ligne_image->observation->nom_sel, $valeur) != '' |stristr($ligne_image->observation->nom_ret, $valeur) != '' |stristr($ligne_image->observation->mots_cles_texte, $valeur) != '' |stristr($ligne_image->observation->date_observation, $valeur) != '' |stristr($ligne_image->date_prise_de_vue, $valeur) != '' |stristr($ligne_image->observation->zone_geo, $valeur) != '' |stristr($ligne_image->observation->famille, $valeur) != '' |stristr($ligne_image->mots_cles_texte, $valeur) != '' |stristr($ligne_image->prenom_utilisateur, $valeur) != '' |stristr($ligne_image->nom_utilisateur, $valeur) != '' |stristr($ligne_image->courriel_utilisateur, $valeur) != '' ;break;case "dpt":$correspond = (substr($ligne_image->observation->ce_zone_geo, 0, 2) == $valeur) |$ligne_image->observation->ce_zone_geo == $valeur;break;case "taxon":$correspond = stristr($ligne_image->observation->nom_sel, $valeur) != '' |stristr($ligne_image->observation->nom_ret, $valeur) != '';break;case "genre":$correspond = stristr($ligne_image->observation->nom_sel, $valeur) != '' |stristr($ligne_image->observation->nom_ret, $valeur) != '';break;case "mot_clef":$correspond = stristr($ligne_image->observation->mot_cles_texte, $valeur);break;case "date":$correspond = stristr($ligne_image->observation->date_observation, $valeur) != '' |stristr($ligne_image->date_prise_de_vue, $valeur) != '';break;case "commune":$correspond = stristr($ligne_image->observation->zone_geo, $valeur);break;case "famille":$correspond = stristr($ligne_image->observation->famille, $valeur);break;case "tag":$correspond = stristr($ligne_image->mots_cles_texte, $valeur);break;case "auteur":$correspond = stristr($ligne_image->prenom_utilisateur, $valeur) != '' |stristr($ligne_image->nom_utilisateur, $valeur) != '' |stristr($ligne_image->courriel_utilisateur, $valeur) != '' ;break;}if(!$correspond) break;}return $correspond;}private function comparerObservations($image_a, $image_b) {$valeur_tri = null;switch($this->champ_tri) {case 'date_observation':$valeur_tri = $this->comparerDate($image_a, $image_b);break;case 'nb_votes':$valeur_tri = $this->comparerNbVote($image_a, $image_b);break;}return $valeur_tri;}private function comparerDate($image_a, $image_b) {$champ_tri = 'date_observation';$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 == 'asc') {return $champ_a_comparer_a > $champ_a_comparer_b;} else {return $champ_a_comparer_a < $champ_a_comparer_b;}}private function comparerNbVote($image_a, $image_b) {$id_protocole = $this->protocole_tri;$votes_a = 0;$votes_b = 0;if(isset($image_a->votes)) {foreach($image_a->votes as $vote_a) {$votes_a += $vote_a->id_protocole == $id_protocole ? 1 : 0;}}if(isset($image_b->votes)) {foreach($image_b->votes as $vote_b) {$votes_b += $vote_b->id_protocole == $id_protocole ? 1 : 0;}}if ($votes_a == $votes_b) {return 0;}if($this->ordre == 'asc') {return ($votes_a < $votes_b) ? -1 : 1;} else {return ($votes_a > $votes_b) ? -1 : 1;}}private function obtenirImagesAvecObservations() {return json_decode(file_get_contents(realpath(dirname(__FILE__)).'/imagesmock.json'));}}?>