Subversion Repositories eFlore/Applications.del

Rev

Rev 1700 | Blame | Compare with Previous | Last modification | View Log | RSS feed

<?php
/**
 * Classe principale de chargement des services concernant les déterminations dans DEL.
 *
 * Encodage en entrée : utf8
 * Encodage en sortie : utf8
 * @package eflore-projets
 * @author Jennifer DHÉ <jennifer.dhe@tela-botanica.org>
 * @author Delphine CAUQUIL <delphine@tela-botanica.org>
 * @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
 * @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
 * @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
 * @version 0.1
 * @copyright 1999-2011 Tela Botanica (accueil@tela-botanica.org)
 */
class Determinations extends RestService {

        private $parametres = array();
        private $ressources = array();
        private $methode = null;
        private $serviceNom = 'determinations';
        private $sousServiceNom = null;
        private $cheminCourant = null;

        private $conteneur;

        /** Indique si oui (true) ou non (false), on veut utiliser les paramètres bruts. */
        protected $utilisationParametresBruts = true;

        public function __construct() {
                $this->cheminCourant = dirname(__FILE__).DS;
        }

        public function consulter($ressources, $parametres) {
                $this->methode = 'consulter';
                $this->initialiserRessourcesEtParametres($ressources, $parametres);
                return $this->executerService();
        }

        public function modifier($ressources, $requeteDonnees) {
                $this->methode = 'modifier';
                $this->initialiserRessourcesEtParametres($ressources, $requeteDonnees);
                return $this->executerService();
        }

        private function initialiserRessourcesEtParametres($ressources, $parametres = array()) {
                $this->ressources = $ressources;
                $this->parametres = $parametres;
        }

        private function executerService() {
                $resultat = '';
                $reponseHttp = new ReponseHttp();
                try {
                        $this->conteneur = new Conteneur($this->parametres);
                        $resultat = $this->traiterRessources();
                        $reponseHttp->setResultatService($resultat);
                } catch (Exception $e) {
                        $reponseHttp->ajouterErreur($e);
                }
                $reponseHttp->emettreLesEntetes();
                $corps = $reponseHttp->getCorps();
                return $corps;
        }

        private function traiterRessources() {
                $this->chargerConfigService();
                $this->analyserRessources();
                $retour = $this->initialiserService();
                return $retour;
        }

        private function chargerConfigService() {
                $chemin = Config::get('chemin_configurations')."config_{$this->serviceNom}.ini";
                Config::charger($chemin);
        }

        /**
         * URLs possibles :
         *
         * GET :
         * http://localhost/del/services/0.1/determinations/images-determinations-probables =>
         *
         *
         * PUT :
         *
         *
         * POST :
         * http://localhost/del/services/0.1/determinations/valider-determination/#idProposition => Permet d'accepter une proposition donnée
         *
         * DELETE :
         *
         */
        private function analyserRessources() {
                if ($this->methode == 'consulter') {
                        if ($this->ressources[0] == 'images-determinations-probables') {
                                $this->sousServiceNom = 'liste-images-determinations-probables';
                        }
                } else if ($this->methode == 'modifier') {
                        if ($this->ressources[0] == 'valider-determination') {
                                $this->sousServiceNom = 'valider-determination';
                        }
                }

                if ($this->sousServiceNom == null) {
                        $this->lancerMessageErreurRessource();
                }
        }

        private function lancerMessageErreurRessource() {
                $ressource = $this->sousServiceNom.'/'.implode('/', $this->ressources);
                $message = "La ressource demandée '$ressource' ".
                        "n'est pas disponible pour le service ".$this->serviceNom." !\n".
                        "Les URLs disponibles sont : \n".
                        " - en GET : determinations/images-determinations-probables \n".
                        " - en POST : determinations/valider-determination";
                $code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
                throw new Exception($message, $code);
        }

        private function initialiserService() {
                $classe = $this->obtenirNomClasseService($this->sousServiceNom);
                //echo $this->sousServiceNom.':'.$classe."\n";
                //echo 'Ressources :'.print_r($this->ressources, true);
                //echo 'Parametres :'.print_r($this->parametres, true);
                $chemins = array();
                $chemins[] = $this->cheminCourant.$this->serviceNom.DS.$classe.'.php';
                $chemins[] = $this->cheminCourant.'commun'.DS.$classe.'.php';
                $retour = '';
                $service = null;
                foreach ($chemins as $chemin) {
                        if (file_exists($chemin)) {
                                $this->conteneur->chargerConfiguration('config_'.$this->serviceNom.'.ini');
                                require_once $chemin;
                                $service = new $classe($this->conteneur);
                                if ($this->methode == 'consulter') {
                                        $retour = $service->consulter($this->ressources, $this->parametres);
                                } elseif ($this->methode == 'ajouter') {
                                        $retour = $service->ajouter($this->ressources, $this->parametres);
                                } elseif ($this->methode == 'modifier') {
                                        $retour = $service->modifier($this->ressources, $this->parametres);
                                } elseif ($this->methode == 'supprimer') {
                                        $retour = $service->supprimer($this->ressources);
                                }
                        }
                }

                if (is_null($service)) {
                        $ressource = $this->sousServiceNom.'/'.implode('/', $this->ressources);
                        $message = "Le classe '$classe' correspondant à la ressource '$ressource' ".
                                "n'existe pas dans le service '{$this->serviceNom}' !";
                        $code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
                        throw new Exception($message, $code);
                }
                return $retour;
        }

        private function obtenirNomClasseService($mot) {
                $classeNom = str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $mot))));
                return $classeNom;
        }
}