1,33 → 1,53 |
<?php |
/** |
* Description : |
* Classe principale de chargement des services d'eFlore. |
* 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 |
* |
* |
* Encodage en entrée : utf8 |
* Encodage en sortie : utf8 |
* @package eflore-projets |
* |
* @category DEL |
* @package Observations |
* @version 0.1 |
* @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) |
* @copyright 1999-2014 Tela Botanica (accueil@tela-botanica.org) |
*/ |
class Observations extends RestService { |
|
|
/* |
* url possibles : |
* http://localhost/del/services/0.1/observations/ => toutes les observations (infos obs, infos images, infos propositions, infos nb commentaires) |
* http://localhost/del/services/0.1/observations/#id => une observation donnée et ses images, SANS LES propositions & nombre de commentaire |
* */ |
|
private $parametres = array(); |
private $ressources = array(); |
private $methode = null; |
private $projetNom = array(); |
private $serviceNom = array(); |
private $serviceNom = 'observations'; |
private $sousServiceNom = null; |
private $cheminCourant = null; |
|
private $conteneur; |
41,157 → 61,155 |
|
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); |
return $this->executerService(); |
} |
$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(); |
return $this->executerService(); |
} |
} |
|
public function modifier($ressources, $requeteDonnees) { |
$this->methode = 'modifier'; |
$resultat = ''; |
$this->initialiserRessourcesEtParametres($ressources, $requeteDonnees); |
return $this->executerService(); |
} |
|
private function executerService() { |
$reponseHttp = new ReponseHttp(); |
try { |
$this->initialiserRessourcesEtParametres($ressources, $requeteDonnees); |
$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) { |
private function initialiserRessourcesEtParametres($ressources, $parametres = array()) { |
$this->ressources = $ressources; |
$this->parametres = $parametres; |
} |
|
private function traiterRessources() { |
$retour = ''; |
$this->initialiserProjet(); |
if ($this->avoirRessourceService()) { |
$this->chargerConfigService(); |
$this->analyserRessources(); |
$retour = $this->initialiserService(); |
} |
return $retour; |
} |
|
private function avoirRessourceIdentifiant($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); |
private function chargerConfigService() { |
$chemin = Config::get('chemin_configurations')."config_{$this->serviceNom}.ini"; |
Config::charger($chemin); |
} |
return $presenceId; |
} |
|
/*------------------------------------------------------------------------------------------------------------------ |
CONFIGURATION DU PROJET |
CONFIGURATION DU SERVICE |
------------------------------------------------------------------------------------------------------------------*/ |
private function initialiserProjet() { |
$this->chargerNomDuProjet(); |
$this->chargerConfigProjet(); |
|
private function analyserRessources() { |
if ($this->methode == 'consulter') { |
$this->analyserRessoucesConsultation(); |
} else if ($this->methode == 'modifier' || $this->methode == 'ajouter') { |
$this->analyserRessoucesModification(); |
} |
} |
|
private function chargerNomDuProjet() { |
$this->projetNom = 'observations'; |
private function analyserRessoucesConsultation() { |
if (count($this->ressources) == 0) { |
if ($this->verifierParametreValeur('retour.format', 'widget')) { |
// http://localhost/service:del:0.1/observations?retour.format=widget |
$this->sousServiceNom = 'liste-observations-widget'; |
} else { |
// 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'; |
} |
} 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'; |
} |
} |
|
private function chargerConfigProjet() { |
$projet = $this->projetNom; |
$chemin = Config::get('chemin_configurations')."config_$projet.ini"; |
Config::charger($chemin); |
if ($this->sousServiceNom == null) { |
$this->lancerMessageErreurRessource(); |
} |
} |
|
/*------------------------------------------------------------------------------------------------------------------ |
CONFIGURATION DU SERVICE |
------------------------------------------------------------------------------------------------------------------*/ |
private function avoirRessourceService() { |
/* |
* url possibles : |
* http://localhost/del/services/0.1/observations/ => toutes les observations (infos obs, infos images, infos propositions, infos nb commentaires) |
* http://localhost/del/services/0.1/observations/#id => une observation donnée et ses images, SANS LES propositions & nombre de commentaire |
* */ |
$presenceRessourceService = false; |
if (isset($this->ressources[0])) { |
if ($this->avoirRessourceIdentifiant(0)) { |
if (sizeof($this->ressources) == 1) { |
$presenceRessourceService = true; |
$this->serviceNom = 'observation'; |
} else { |
if (isset($this->ressources[1])) { |
$presenceRessourceService = $this->avoirRessourceSousService(); |
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'; |
} |
} 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'; |
} |
} |
} else { |
if (isset($this->parametres['retour.format']) && $this->parametres['retour.format'] == 'widget') { |
$this->methode = 'consulter'; |
$presenceRessourceService = true; |
$this->serviceNom = 'liste-observations-widget'; |
} else { |
$presenceRessourceService = true; |
$this->serviceNom = 'liste-observations'; |
|
if ($this->sousServiceNom == null) { |
$this->lancerMessageErreurRessource(); |
} |
} |
return $presenceRessourceService; |
|
private function etreRessourceIdentifiant($num) { |
$presenceId = false; |
if (isset($this->ressources[$num]) && is_numeric($this->ressources[$num])) { |
$presenceId = true; |
} |
return $presenceId; |
} |
|
private function avoirRessourceSousService() { |
$presenceRessourceService = false; |
$servicesDispo = Outils::recupererTableauConfig('servicesDispo'); |
if ($this->avoirRessourceIdentifiant(1)) { |
$service = $this->ressources[2]; |
if (in_array($service, $servicesDispo)) { |
$presenceRessourceService = true; |
$this->serviceNom = 'vote-observation'; |
} else { |
$message = "Le service demandé '$service' n'est pas disponible pour le projet {$this->projetNom} !\n". |
"Les services disponibles sont : ".implode(', ', $servicesDispo); |
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE; |
throw new Exception($message, $code); |
private function verifierRessourceValeur($num, $valeur) { |
$ok = false; |
if (isset($this->ressources[$num]) && $this->ressources[$num] == $valeur) { |
$ok = true; |
} |
return $ok; |
} |
return $presenceRessourceService; |
|
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() { |
//$this->chargerNomDuService(); |
|
$classe = $this->obtenirNomClasseService($this->serviceNom); |
$classe = $this->obtenirNomClasseService($this->sousServiceNom); |
$chemins = array(); |
$chemins[] = $this->cheminCourant.$this->projetNom.DS.$classe.'.php'; |
$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->projetNom.'.ini'); |
$this->conteneur->chargerConfiguration('config_'.$this->serviceNom.'.ini'); |
require_once $chemin; |
$service = new $classe($this->conteneur); |
if ($this->methode == 'consulter') { |
205,7 → 223,9 |
} |
|
if (is_null($service)) { |
$message = "Le service demandé '{$this->serviceNom}' n'existe pas dans le projet {$this->projetNom} !"; |
$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); |
} |
216,7 → 236,4 |
$classeNom = str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $mot)))); |
return $classeNom; |
} |
|
|
} |
?> |
} |