Subversion Repositories eFlore/Applications.del

Compare Revisions

Ignore whitespace Rev 2214 → Rev 2215

/trunk/services/modules/0.1/Tepik.php
New file
0,0 → 1,227
<?php
// declare(encoding='UTF-8');
/**
* Classe principale de chargement des services Observations.
*
* URLs possibles :
* GET :
* http://localhost/service:del:0.1/observations
* toutes les observations (infos obs, infos images, infos propositions, infos nb commentaires)
*
* http://localhost/service:del:0.1/observations?retour.format=widget
* toutes les infos des observations pour le Widget DEL
*
* http://localhost/service:del:0.1/observations/#idObs
* une observation donnée et ses images, SANS LES propositions & nombre de commentaire*
*
* http://localhost/service:del:0.1/observations/#idObs/#idVote/vote
* toutes les infos sur les votes d'une proposition
*
* PUT :
* http://localhost/service:del:0.1/observations/#idObs/#idCommentaire/vote
* ajoute un vote (+ ou -) pour une obs et une proposition donnée
*
* POST :
* http://localhost/service:del:0.1/observations/#idObs
* utilisé seulement par les admins pour modifier une obs depuis DEL (dépublication des obs)
*
* http://localhost/service:del:0.1/observations/#idObs/#idCommentaire/vote
* modifie un vote (+ ou -) pour une obs et une proposition donnée
*
* @category DEL
* @package Services
* @subpackage Observations
* @version 0.1
* @author Mathias CHOUET <mathias@tela-botanica.org>
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
* @author Aurelien PERONNET <aurelien@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>
* @copyright 1999-2014 Tela Botanica <accueil@tela-botanica.org>
*/
 
class Observations extends RestService {
 
private $parametres = array();
private $ressources = array();
private $methode = null;
private $serviceNom = 'observations';
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 ajouter($ressources, $requeteDonnees) {
$this->methode = 'ajouter';
$this->initialiserRessourcesEtParametres($ressources, $requeteDonnees);
return $this->executerService();
}
 
public function modifier($ressources, $requeteDonnees) {
$this->methode = 'modifier';
$this->initialiserRessourcesEtParametres($ressources, $requeteDonnees);
return $this->executerService();
}
 
private function executerService() {
$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 initialiserRessourcesEtParametres($ressources, $parametres = array()) {
$this->ressources = $ressources;
$this->parametres = $parametres;
}
 
private function traiterRessources() {
$this->analyserRessources();
$retour = $this->initialiserService();
return $retour;
}
 
private function analyserRessources() {
if ($this->methode == 'consulter') {
$this->analyserRessoucesConsultation();
} else if ($this->methode == 'modifier' || $this->methode == 'ajouter') {
$this->analyserRessoucesModification();
}
}
 
private function analyserRessoucesConsultation() {
if (count($this->ressources) == 0) {
// http://localhost/service:del:0.1/observations
$this->sousServiceNom = 'liste-observations';
} else if (count($this->ressources) == 1) {
if ($this->etreRessourceIdentifiant(0)) {
// http://localhost/service:del:0.1/observations/#idObs
$this->sousServiceNom = 'observation-details';
}
} else if (count($this->ressources) == 3) {
if ($this->etreRessourceIdentifiant(0) && $this->etreRessourceIdentifiant(1) && $this->verifierRessourceValeur(2, 'vote')) {
// http://localhost/service:del:0.1/observations/#idObs/#idProposition/vote/
$this->sousServiceNom = 'vote-observation';
}
}
 
if ($this->sousServiceNom == null) {
$this->lancerMessageErreurRessource();
}
}
 
private function analyserRessoucesModification() {
if (count($this->ressources) == 1) {
if ($this->methode == 'modifier' && $this->etreRessourceIdentifiant(0)) {
// http://localhost/service:del:0.1/observations/#idObs
$this->sousServiceNom = 'observation-details';
}
} else if (count($this->ressources) == 3) {
if ($this->etreRessourceIdentifiant(0) && $this->etreRessourceIdentifiant(1) && $this->verifierRessourceValeur(2, 'vote')) {
// http://localhost/service:del:0.1/observations/#idObs/#idProposition/vote/
$this->sousServiceNom = 'vote-observation';
}
}
 
if ($this->sousServiceNom == null) {
$this->lancerMessageErreurRessource();
}
}
 
private function etreRessourceIdentifiant($num) {
$presenceId = false;
if (isset($this->ressources[$num]) && is_numeric($this->ressources[$num])) {
$presenceId = true;
}
return $presenceId;
}
 
private function verifierRessourceValeur($num, $valeur) {
$ok = false;
if (isset($this->ressources[$num]) && $this->ressources[$num] == $valeur) {
$ok = true;
}
return $ok;
}
 
private function verifierParametreValeur($cle, $valeur) {
$ok = false;
if (isset($this->parametres[$cle]) && $this->ressources[$cle] == $valeur) {
$ok = true;
}
return $ok;
}
 
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 : observations, observations/#idObs/#idProposition/vote \n".
" - en POST : observations/#id, observations/#idObs/#idProposition/vote \n".
" - en PUT : observations/#idObs/#idProposition/vote \n";
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
throw new Exception($message, $code);
}
 
private function initialiserService() {
$classe = $this->obtenirNomClasseService($this->sousServiceNom);
$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)) {
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);
} else {
$message = "Le sous-service '{$this->sousServiceNom}' du service '{$this->serviceNom}' ".
"ne possède pas de méthode '{$this->methode}' !";
$code = RestServeur::HTTP_NON_IMPLEMENTE;
throw new Exception($message, $code);
}
}
}
 
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;
}
}