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