Subversion Repositories eFlore/Applications.del

Rev

Rev 1144 | Rev 1700 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

<?php
/**
* Description :
* Classe principale de chargement des services d'eFlore.
*
* 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 {

        
        /*
         * url possibles : 
         * http://localhost/del/services/0.1/images/ => toutes les images, appel à la classe ListeImages
         * http://localhost/del/services/0.1/images/#id => une image donnée => non utilisé sur DEL pour l'instant
         * http://localhost/del/services/0.1/images/#id/votes/ => tous les votes d'une image classés par protocole
         * http://localhost/del/services/0.1/images/#id/votes/protocole/#id => tous les votes d'une image et d'un protocole donné
         * http://localhost/del/services/0.1/images/#id/votes/#id => un vote donné pour une image donnée.
         * */
        
        private $parametres = array();
        private $ressources = array();
        private $methode = null;
        private $projetNom = array();
        private $serviceNom = array();
        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';
                $resultat = '';
                $reponseHttp = new ReponseHttp();
                try {
                        $this->initialiserRessourcesEtParametres($ressources, $parametres);
                        $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;
        }
        
        public function ajouter($ressources, $requeteDonnees) {
                $this->methode = 'ajouter';
                $resultat = '';
                $reponseHttp = new ReponseHttp();
                try {
                        $this->initialiserRessourcesEtParametres($ressources, $requeteDonnees);
                        $this->conteneur = new Conteneur($this->parametres);
                        $resultat = $this->traiterRessources();
                } catch (Exception $e) {
                        $reponseHttp->ajouterErreur($e);
                        $reponseHttp->emettreLesEntetes();
                        echo $reponseHttp->getCorps();
                }
        }
        
        public function modifier($ressources, $requeteDonnees) {
                $this->methode = 'modifier';
                $resultat = '';
                $reponseHttp = new ReponseHttp();
                try {
                        $this->initialiserRessourcesEtParametres($ressources, $requeteDonnees);
                        $this->conteneur = new Conteneur($this->parametres);
                        $resultat = $this->traiterRessources();
                } catch (Exception $e) {
                        $reponseHttp->ajouterErreur($e);
                        $reponseHttp->emettreLesEntetes();
                        echo $reponseHttp->getCorps();
                }
        }

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

        private function traiterRessources() {
                $retour = '';
                $this->initialiserProjet();
                if ($this->avoirRessourceService()) {
                        $retour = $this->initialiserService();
                }
                return $retour;
        }
        

        /*------------------------------------------------------------------------------------------------------------------
                                                                                CONFIGURATION DU PROJET
        ------------------------------------------------------------------------------------------------------------------*/
        private function initialiserProjet() {
                $this->chargerNomDuProjet();
                $this->chargerConfigProjet();

        }

        private function chargerNomDuProjet() {
                $this->projetNom = 'determinations';
        }

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

        /*------------------------------------------------------------------------------------------------------------------
                                                                CONFIGURATION DU SERVICE
        ------------------------------------------------------------------------------------------------------------------*/
        private function avoirRessourceService() {
                $presenceRessourceService = false;
                if (isset($this->ressources[0])) {
                        if($this->ressources[0] == 'images-determinations-probables') {
                                $presenceRessourceService = true;
                                $this->serviceNom = 'liste-images-determinations-probables';
                        } else if($this->ressources[0] == 'valider-determination') {
                                $presenceRessourceService = true;
                                $this->serviceNom = 'valider-determination';
                        } else {
                                $this->editerMessageErreurRessource();
                        }
                }
                return $presenceRessourceService;
        }
        
        private function editerMessageErreurRessource() {
                $message = "Le service demandé '".$this->projetNom.'/'.implode('/', $this->ressources).
                                        "' n'est pas disponible pour le projet ".$this->projetNom." !\n".
                                        "Les services disponibles sont : images, images/#id/votes, images/#id/vote/#id_vote";
                $code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
                throw new Exception($message, $code);
        }
        
        private function etreRessourceIdentifiant($num) {
                $presenceId = false;
                if (is_numeric($this->ressources[$num])) {
                        $presenceId = true;
                } else {
                        $message = "Le service demandé '$service' nécessite d'avoir un identifiant d'image valide";
                        $code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
                        throw new Exception($message, $code);
                }
                return $presenceId;
        }

        private function initialiserService() {
                
                $classe = $this->obtenirNomClasseService($this->serviceNom);
                $chemins = array();
                $chemins[] = $this->cheminCourant.$this->projetNom.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->projetNom.'.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);
                                }
                        }
                }
                
                if (is_null($service)) {
                        $message = "Le service demandé '{$this->serviceNom}' n'existe pas dans le projet {$this->projetNom} !";
                        $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;
        }
}
?>