Subversion Repositories eFlore/Applications.del

Compare Revisions

Ignore whitespace Rev 754 → Rev 755

/trunk/services/modules/0.1/Observations.php
New file
0,0 → 1,179
<?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 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 $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) {
$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;
}
 
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;
}
private function avoirRessourceIdentifiant() {
$presenceId = false;
if (is_numeric($this->ressources[0])) {
$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;
}
/*------------------------------------------------------------------------------------------------------------------
CONFIGURATION DU PROJET
------------------------------------------------------------------------------------------------------------------*/
private function initialiserProjet() {
$this->chargerNomDuProjet();
$this->chargerConfigProjet();
 
}
 
private function chargerNomDuProjet() {
$this->projetNom = 'observations';
}
 
private function chargerConfigProjet() {
$projet = $this->projetNom;
$chemin = Config::get('chemin_configurations')."config_$projet.ini";
Config::charger($chemin);
}
 
/*------------------------------------------------------------------------------------------------------------------
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()) {
if (sizeof($this->ressources) == 1) {
$presenceRessourceService = true;
$this->serviceNom = 'observation';
} else {
if (isset($this->ressources[1])) {
$presenceRessourceService = $this->avoirRessourceSousService();
}
}
}
} else {
$presenceRessourceService = true;
$this->serviceNom = 'liste-observations';
}
return $presenceRessourceService;
}
private function avoirRessourceSousService() {
$presenceRessourceService = false;
$servicesDispo = Outils::recupererTableauConfig('servicesDispo');
$service = $this->ressources[1];
if (in_array($service, $servicesDispo)) {
$presenceRessourceService = true;
$this->serviceNom = 'votes-image';
} 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);
}
return $presenceRessourceService;
}
 
private function initialiserService() {
//$this->chargerNomDuService();
$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);
$retour = $service->consulter($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;
}
 
 
}
?>