| 858 | gduche | 1 | <?php
 | 
        
           | 1795 | jpm | 2 | // declare(encoding='UTF-8');
 | 
        
           | 858 | gduche | 3 | /**
 | 
        
           | 1795 | jpm | 4 |  * Classe principale de chargement des sous-services concernant les commentaires.
 | 
        
           | 1793 | jpm | 5 |  *
 | 
        
           |  |  | 6 |  * URLs possibles :
 | 
        
           |  |  | 7 |  *
 | 
        
           |  |  | 8 |  * GET :
 | 
        
           |  |  | 9 |  * http://localhost/del/services/0.1/commentaires => liste tous les commentaires
 | 
        
           |  |  | 10 |  * http://localhost/del/services/0.1/commentaires/#id => retourne le contenu d'un commentaire d'id #id
 | 
        
           |  |  | 11 |  *
 | 
        
           |  |  | 12 |  * PUT :
 | 
        
           |  |  | 13 |  * http://localhost/del/services/0.1/commentaires => Ajoute un nouveau commentaire
 | 
        
           |  |  | 14 |  *
 | 
        
           |  |  | 15 |  * DELETE :
 | 
        
           |  |  | 16 |  * http://localhost/del/services/0.1/commentaires/#id => supprime le commentaire d'id #id
 | 
        
           |  |  | 17 |  *
 | 
        
           | 1795 | jpm | 18 |  * @category   DEL
 | 
        
           |  |  | 19 |  * @package    Services
 | 
        
           | 1793 | jpm | 20 |  * @subpackage Commentaires
 | 
        
           | 1795 | jpm | 21 |  * @version    0.1
 | 
        
           |  |  | 22 |  * @author     Mathias CHOUET <mathias@tela-botanica.org>
 | 
        
           |  |  | 23 |  * @author     Jean-Pascal MILCENT <jpm@tela-botanica.org>
 | 
        
           |  |  | 24 |  * @author     Aurelien PERONNET <aurelien@tela-botanica.org>
 | 
        
           |  |  | 25 |  * @license    GPL v3 <http://www.gnu.org/licenses/gpl.txt>
 | 
        
           |  |  | 26 |  * @license    CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
 | 
        
           |  |  | 27 |  * @copyright  1999-2014 Tela Botanica <accueil@tela-botanica.org>
 | 
        
           | 1793 | jpm | 28 |  */
 | 
        
           | 858 | gduche | 29 | class Commentaires extends RestService {
 | 
        
           |  |  | 30 |   | 
        
           |  |  | 31 | 	private $parametres = array();
 | 
        
           |  |  | 32 | 	private $ressources = array();
 | 
        
           |  |  | 33 | 	private $methode = null;
 | 
        
           | 1793 | jpm | 34 | 	private $serviceNom = 'commentaires';
 | 
        
           |  |  | 35 | 	private $sousServiceNom = null;
 | 
        
           | 858 | gduche | 36 | 	private $cheminCourant = null;
 | 
        
           | 1795 | jpm | 37 | 	private $erreur = null;
 | 
        
           | 858 | gduche | 38 |   | 
        
           |  |  | 39 | 	private $conteneur;
 | 
        
           | 1793 | jpm | 40 |   | 
        
           | 858 | gduche | 41 | 	/** Indique si oui (true) ou non (false), on veut utiliser les paramètres bruts. */
 | 
        
           |  |  | 42 | 	protected $utilisationParametresBruts = true;
 | 
        
           |  |  | 43 |   | 
        
           |  |  | 44 | 	public function __construct() {
 | 
        
           |  |  | 45 | 		$this->cheminCourant = dirname(__FILE__).DS;
 | 
        
           |  |  | 46 | 	}
 | 
        
           |  |  | 47 |   | 
        
           |  |  | 48 | 	public function consulter($ressources, $parametres) {
 | 
        
           |  |  | 49 | 		$this->methode = 'consulter';
 | 
        
           | 1793 | jpm | 50 | 		$this->initialiserRessourcesEtParametres($ressources, $parametres);
 | 
        
           |  |  | 51 | 		return $this->executerService();
 | 
        
           | 858 | gduche | 52 | 	}
 | 
        
           | 1793 | jpm | 53 |   | 
        
           |  |  | 54 | 	public function ajouter($ressources, $requeteDonnees) {
 | 
        
           | 858 | gduche | 55 | 		$this->methode = 'ajouter';
 | 
        
           | 1793 | jpm | 56 | 		$this->initialiserRessourcesEtParametres($ressources, $requeteDonnees);
 | 
        
           |  |  | 57 | 		return $this->executerService();
 | 
        
           |  |  | 58 | 	}
 | 
        
           |  |  | 59 |   | 
        
           |  |  | 60 | 	public function supprimer($ressources) {
 | 
        
           |  |  | 61 | 		$this->methode = 'supprimer';
 | 
        
           |  |  | 62 | 		$this->initialiserRessourcesEtParametres($ressources);
 | 
        
           |  |  | 63 | 		return $this->executerService();
 | 
        
           |  |  | 64 | 	}
 | 
        
           |  |  | 65 |   | 
        
           |  |  | 66 | 	private function initialiserRessourcesEtParametres($ressources, $parametres = array()) {
 | 
        
           |  |  | 67 | 		$this->ressources = $ressources;
 | 
        
           |  |  | 68 | 		$this->parametres = $parametres;
 | 
        
           |  |  | 69 | 	}
 | 
        
           |  |  | 70 |   | 
        
           |  |  | 71 | 	private function executerService() {
 | 
        
           | 858 | gduche | 72 | 		$reponseHttp = new ReponseHttp();
 | 
        
           |  |  | 73 | 		try {
 | 
        
           |  |  | 74 | 			$this->conteneur = new Conteneur($this->parametres);
 | 
        
           |  |  | 75 | 			$resultat = $this->traiterRessources();
 | 
        
           |  |  | 76 | 			$reponseHttp->setResultatService($resultat);
 | 
        
           |  |  | 77 | 		} catch (Exception $e) {
 | 
        
           |  |  | 78 | 			$reponseHttp->ajouterErreur($e);
 | 
        
           |  |  | 79 | 		}
 | 
        
           | 1793 | jpm | 80 | 		$reponseHttp->emettreLesEntetes();
 | 
        
           | 858 | gduche | 81 | 		$corps = $reponseHttp->getCorps();
 | 
        
           |  |  | 82 | 		return $corps;
 | 
        
           |  |  | 83 | 	}
 | 
        
           |  |  | 84 |   | 
        
           |  |  | 85 | 	private function traiterRessources() {
 | 
        
           | 1793 | jpm | 86 | 		$this->analyserRessources();
 | 
        
           | 858 | gduche | 87 | 		$retour = $this->initialiserService();
 | 
        
           |  |  | 88 | 		return $retour;
 | 
        
           |  |  | 89 | 	}
 | 
        
           |  |  | 90 |   | 
        
           | 1793 | jpm | 91 | 	private function analyserRessources() {
 | 
        
           |  |  | 92 | 		if ($this->methode == 'consulter') {
 | 
        
           |  |  | 93 | 			if (!isset($this->ressources) || empty($this->ressources)) {
 | 
        
           |  |  | 94 | 				$this->sousServiceNom = 'liste-commentaires';
 | 
        
           |  |  | 95 | 			} else if (isset($this->ressources[0]) && count($this->ressources) == 1 && is_numeric($this->ressources[0])) {
 | 
        
           | 1839 | jpm | 96 | 				$this->sousServiceNom = 'commentaire-details';
 | 
        
           | 1793 | jpm | 97 | 			}
 | 
        
           |  |  | 98 | 		} else if ($this->methode == 'ajouter') {
 | 
        
           |  |  | 99 | 			$this->sousServiceNom = 'ajouter-commentaire';
 | 
        
           |  |  | 100 | 		} else if ($this->methode == 'supprimer') {
 | 
        
           |  |  | 101 | 			if (isset($this->ressources[0]) && count($this->ressources) == 1 && is_numeric($this->ressources[0])) {
 | 
        
           |  |  | 102 | 				$this->sousServiceNom = 'supprimer-commentaire';
 | 
        
           | 1795 | jpm | 103 | 			} else {
 | 
        
           |  |  | 104 | 				$this->erreur = "L'identifiant du commentaire est obligatoire et doit être un entier.";
 | 
        
           | 1793 | jpm | 105 | 			}
 | 
        
           |  |  | 106 | 		}
 | 
        
           |  |  | 107 |   | 
        
           |  |  | 108 | 		if ($this->sousServiceNom == null) {
 | 
        
           |  |  | 109 | 			$this->lancerMessageErreurRessource();
 | 
        
           |  |  | 110 | 		}
 | 
        
           | 858 | gduche | 111 | 	}
 | 
        
           |  |  | 112 |   | 
        
           | 1793 | jpm | 113 | 	private function lancerMessageErreurRessource() {
 | 
        
           |  |  | 114 | 		$ressource = $this->sousServiceNom.'/'.implode('/', $this->ressources);
 | 
        
           | 1795 | jpm | 115 | 		if ($this->erreur != null) {
 | 
        
           |  |  | 116 | 			$message = $this->erreur;
 | 
        
           |  |  | 117 | 		} else {
 | 
        
           |  |  | 118 | 			$message = "La ressource demandée '$ressource' ".
 | 
        
           |  |  | 119 | 				"n'est pas disponible pour le service ".$this->serviceNom." !\n".
 | 
        
           |  |  | 120 | 				"Les URLs disponibles sont : \n".
 | 
        
           |  |  | 121 | 				" - en GET : commentaires, commentaires/#id \n".
 | 
        
           |  |  | 122 | 				" - en PUT : commentaires".
 | 
        
           |  |  | 123 | 				" - en DELETE : commentaires/#id\n".
 | 
        
           |  |  | 124 | 				"#id représente un nombre entier identifiant un commentaire.";
 | 
        
           |  |  | 125 | 		}
 | 
        
           | 1793 | jpm | 126 | 		$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
 | 
        
           |  |  | 127 | 		throw new Exception($message, $code);
 | 
        
           |  |  | 128 | 	}
 | 
        
           |  |  | 129 |   | 
        
           | 858 | gduche | 130 | 	private function initialiserService() {
 | 
        
           | 1793 | jpm | 131 | 		$classe = $this->obtenirNomClasseService($this->sousServiceNom);
 | 
        
           |  |  | 132 | 		//echo $this->sousServiceNom.':'.$classe."\n";
 | 
        
           |  |  | 133 | 		//echo 'Ressources :'.print_r($this->ressources, true);
 | 
        
           |  |  | 134 | 		//echo 'Parametres :'.print_r($this->parametres, true);
 | 
        
           | 858 | gduche | 135 | 		$chemins = array();
 | 
        
           | 1793 | jpm | 136 | 		$chemins[] = $this->cheminCourant.$this->serviceNom.DS.$classe.'.php';
 | 
        
           | 858 | gduche | 137 | 		$chemins[] = $this->cheminCourant.'commun'.DS.$classe.'.php';
 | 
        
           |  |  | 138 | 		$retour = '';
 | 
        
           |  |  | 139 | 		$service = null;
 | 
        
           |  |  | 140 | 		foreach ($chemins as $chemin) {
 | 
        
           |  |  | 141 | 			if (file_exists($chemin)) {
 | 
        
           |  |  | 142 | 				require_once $chemin;
 | 
        
           |  |  | 143 | 				$service = new $classe($this->conteneur);
 | 
        
           |  |  | 144 | 				if ($this->methode == 'consulter') {
 | 
        
           | 1795 | jpm | 145 | 					$retour = $service->consulter($this->ressources);
 | 
        
           | 858 | gduche | 146 | 				} elseif ($this->methode == 'ajouter') {
 | 
        
           | 1795 | jpm | 147 | 					$retour = $service->ajouter($this->parametres);
 | 
        
           | 864 | gduche | 148 | 				} elseif ($this->methode == 'supprimer') {
 | 
        
           | 1793 | jpm | 149 | 					$retour = $service->supprimer($this->ressources);
 | 
        
           | 858 | gduche | 150 | 				}
 | 
        
           |  |  | 151 | 			}
 | 
        
           |  |  | 152 | 		}
 | 
        
           | 1793 | jpm | 153 |   | 
        
           | 858 | gduche | 154 | 		if (is_null($service)) {
 | 
        
           | 1793 | jpm | 155 | 			$ressource = $this->sousServiceNom.'/'.implode('/', $this->ressources);
 | 
        
           |  |  | 156 | 			$message = "Le classe '$classe' correspondant à la ressource '$ressource' ".
 | 
        
           |  |  | 157 | 				"n'existe pas dans le service '{$this->serviceNom}' !";
 | 
        
           | 858 | gduche | 158 | 			$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
 | 
        
           |  |  | 159 | 			throw new Exception($message, $code);
 | 
        
           |  |  | 160 | 		}
 | 
        
           |  |  | 161 | 		return $retour;
 | 
        
           |  |  | 162 | 	}
 | 
        
           |  |  | 163 |   | 
        
           |  |  | 164 | 	private function obtenirNomClasseService($mot) {
 | 
        
           |  |  | 165 | 		$classeNom = str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $mot))));
 | 
        
           |  |  | 166 | 		return $classeNom;
 | 
        
           |  |  | 167 | 	}
 | 
        
           | 1793 | jpm | 168 | }
 |