31 |
aurelien |
1 |
<?php
|
|
|
2 |
// declare(encoding='UTF-8');
|
|
|
3 |
/**
|
|
|
4 |
* Classe modèle spécifique à l'application, donc d'accés au données, elle ne devrait pas être appelée de l'extérieur.
|
|
|
5 |
* Elle est abstraite donc doit obligatoirement être étendue.
|
|
|
6 |
*
|
|
|
7 |
* @category Php5
|
|
|
8 |
* @package ODS_saisie
|
|
|
9 |
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
|
|
|
10 |
* @copyright 2010 Tela-Botanica
|
|
|
11 |
* @license http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL
|
|
|
12 |
* @license http://www.gnu.org/licenses/gpl.html Licence GNU-GPL
|
|
|
13 |
* @version SVN: $Id$
|
|
|
14 |
*/
|
|
|
15 |
abstract class Dao {
|
|
|
16 |
const ORDRE_ASCENDANT = 'ASC';
|
|
|
17 |
const ORDRE_DESCENDANT = 'DESC';
|
|
|
18 |
const HTTP_REQUETE_SEPARATEUR = '&';
|
|
|
19 |
protected $distinction = '0';
|
|
|
20 |
protected $limite_debut = null;
|
|
|
21 |
protected $limite_nbre = null;
|
|
|
22 |
protected $ordre = null;
|
|
|
23 |
protected $url_jrest = null;
|
|
|
24 |
|
|
|
25 |
public function __construct() {
|
|
|
26 |
$this->url_jrest = Config::get('url_jrest');
|
|
|
27 |
}
|
|
|
28 |
|
|
|
29 |
//+----------------------------------------------------------------------------------------------------------------+
|
|
|
30 |
// ACCESSEURS
|
|
|
31 |
|
|
|
32 |
public function setDistinction($distinct) {
|
|
|
33 |
$this->distinction = $distinct;
|
|
|
34 |
}
|
|
|
35 |
public function getDistinction() {
|
|
|
36 |
return $this->distinction;
|
|
|
37 |
}
|
|
|
38 |
public function viderDistinction() {
|
|
|
39 |
$this->distinction = null;
|
|
|
40 |
}
|
|
|
41 |
|
|
|
42 |
public function avoirLimitation() {
|
|
|
43 |
$limitation = false;
|
|
|
44 |
if (!is_null($this->limite_debut) && !is_null($this->limite_nbre)) {
|
|
|
45 |
$limitation = true;
|
|
|
46 |
}
|
|
|
47 |
return $limitation;
|
|
|
48 |
}
|
|
|
49 |
public function setLimitation($limite_debut, $limite_nbre) {
|
|
|
50 |
$this->limite_debut = $limite_debut;
|
|
|
51 |
$this->limite_nbre = $limite_nbre;
|
|
|
52 |
}
|
|
|
53 |
public function getLimiteDebut() {
|
|
|
54 |
return $this->limite_debut;
|
|
|
55 |
}
|
|
|
56 |
public function getLimiteNbre() {
|
|
|
57 |
return $this->limite_nbre;
|
|
|
58 |
}
|
|
|
59 |
public function viderLimite() {
|
|
|
60 |
$this->limite_debut = null;
|
|
|
61 |
$this->limite_nbre = null;
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
public function addOrdre($champ, $trie = self::ORDRE_ASCENDANT) {
|
|
|
65 |
if (!isset($this->ordre[$champ])) {
|
|
|
66 |
if (self::ORDRE_ASCENDANT == $trie || self::ORDRE_DESCENDANT == $trie) {
|
|
|
67 |
$this->ordre[$champ] = $trie;
|
|
|
68 |
} else {
|
|
|
69 |
$e = "La valeur pour le tri doit être : {self::ORDRE_ASCENDANT} ou {self::ORDRE_DESCENDANT}.";
|
|
|
70 |
trigger_error($e, E_USER_WARNING);
|
|
|
71 |
}
|
|
|
72 |
} else {
|
|
|
73 |
$e = "Le champ $champ existe déjà dans le tableau des ordres.";
|
|
|
74 |
trigger_error($e, E_USER_WARNING);
|
|
|
75 |
}
|
|
|
76 |
}
|
|
|
77 |
public function getOrdre() {
|
|
|
78 |
$champs = array();
|
|
|
79 |
foreach ($this->ordre as $champ => $trie) {
|
|
|
80 |
$champs[] = "$champ $trie";
|
|
|
81 |
}
|
|
|
82 |
return implode(', ', $champs);
|
|
|
83 |
}
|
|
|
84 |
public function viderOrdre() {
|
|
|
85 |
$this->ordre = null;
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
//+----------------------------------------------------------------------------------------------------------------+
|
|
|
89 |
// MÉTHODES
|
|
|
90 |
|
|
|
91 |
protected function envoyerRequeteConsultation($url) {
|
|
|
92 |
$url = $this->traiterUrlParametres($url);
|
|
|
93 |
$retour = $this->envoyerRequete($url, 'GET');
|
|
|
94 |
return $retour;
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
protected function envoyerRequeteAjout($url, Array $donnees) {
|
|
|
98 |
$retour = $this->envoyerRequete($url, 'PUT', $donnees);
|
|
|
99 |
return $retour;
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
protected function envoyerRequeteModif($url, Array $donnees) {
|
|
|
103 |
$retour = $this->envoyerRequete($url, 'POST', $donnees);
|
|
|
104 |
return $retour;
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
protected function envoyerRequeteSuppression($url) {
|
|
|
108 |
$retour = $this->envoyerRequete($url, 'DELETE');
|
|
|
109 |
return $retour;
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
private function envoyerRequete($url, $mode, Array $donnees = array()) {
|
|
|
113 |
$contenu = false;
|
|
|
114 |
if ($mode != 'GET' && $mode != 'PUT' && $mode != 'POST' && $mode != 'DELETE') {
|
|
|
115 |
$e = "Le mode de requête '$mode' n'est pas accepté!";
|
|
|
116 |
trigger_error($e, E_USER_WARNING);
|
|
|
117 |
} else {
|
|
|
118 |
$contexte = stream_context_create(array(
|
|
|
119 |
'http' => array(
|
|
|
120 |
'method' => $mode,
|
|
|
121 |
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
|
|
|
122 |
'content' => http_build_query($donnees, null, self::HTTP_REQUETE_SEPARATEUR))));
|
|
|
123 |
|
|
|
124 |
$flux = @fopen($url, 'r', false, $contexte);
|
|
|
125 |
|
|
|
126 |
if (!$flux) {
|
|
|
127 |
$this->traiterEntete($http_response_header, $url);
|
|
|
128 |
$e = "L'ouverture de l'url '$url' par la méthode HTTP '$mode' a échoué!";
|
|
|
129 |
trigger_error($e, E_USER_WARNING);
|
|
|
130 |
} else {
|
|
|
131 |
// Informations sur les en-têtes et métadonnées du flux
|
|
|
132 |
$entetes = stream_get_meta_data($flux);
|
|
|
133 |
$this->traiterEntete($entetes, $url);
|
|
|
134 |
|
|
|
135 |
// Contenu actuel de $url
|
|
|
136 |
$contenu = stream_get_contents($flux);
|
|
|
137 |
|
|
|
138 |
fclose($flux);
|
|
|
139 |
}
|
|
|
140 |
}
|
|
|
141 |
$this->reinitialiser();
|
|
|
142 |
return $contenu;
|
|
|
143 |
}
|
|
|
144 |
|
|
|
145 |
private function traiterUrlParametres($url) {
|
|
|
146 |
$parametres = array();
|
|
|
147 |
if (! is_null($this->getLimiteDebut())) {
|
|
|
148 |
$parametres[] = 'start='.$this->getLimiteDebut();
|
|
|
149 |
}
|
|
|
150 |
if (! is_null($this->getLimiteNbre())) {
|
|
|
151 |
$parametres[] = 'limit='.$this->getLimiteNbre();
|
|
|
152 |
}
|
|
|
153 |
if (! is_null($this->ordre)) {
|
|
|
154 |
$parametres[] = 'orderby='.urlencode($this->getOrdre());
|
|
|
155 |
}
|
|
|
156 |
if ($this->getDistinction() != 0) {
|
|
|
157 |
$parametres[] = 'distinct='.$this->getDistinction();
|
|
|
158 |
}
|
|
|
159 |
if (count($parametres) > 0) {
|
|
|
160 |
$url_parametres = implode('&', $parametres);
|
|
|
161 |
$url = $url.'?'.$url_parametres;
|
|
|
162 |
}
|
|
|
163 |
return $url;
|
|
|
164 |
}
|
|
|
165 |
|
|
|
166 |
private function traiterEntete($entetes, $uri) {
|
|
|
167 |
$infos = $this->analyserEntete($entetes, $uri);
|
|
|
168 |
$this->traiterEnteteDebug($infos);
|
|
|
169 |
$this->traiterEnteteMessage($infos);
|
|
|
170 |
}
|
|
|
171 |
|
|
|
172 |
private function analyserEntete($entetes, $uri) {
|
|
|
173 |
$infos = array('date' => null, 'uri' => $uri, 'debugs' => null, 'messages' => null);
|
|
|
174 |
|
|
|
175 |
if (isset($entetes['wrapper_data'])) {
|
|
|
176 |
$entetes = $entetes['wrapper_data'];
|
|
|
177 |
}
|
|
|
178 |
foreach ($entetes as $entete) {
|
|
|
179 |
if (preg_match('/^X-DebugJrest-Data: (.+)$/', $entete, $match)) {
|
|
|
180 |
$infos['debugs'] = json_decode($match[1]);
|
|
|
181 |
}
|
|
|
182 |
if (preg_match('/^X-MessageJrest-Data: (.+)$/', $entete, $match)) {
|
|
|
183 |
$infos['messages'] = json_decode($match[1]);
|
|
|
184 |
}
|
|
|
185 |
if (preg_match('/^Date: .+ ([012][0-9]:[012345][0-9]:[012345][0-9]) .*$/', $entete, $match)) {
|
|
|
186 |
$infos['date'] = $match[1];
|
|
|
187 |
}
|
|
|
188 |
}
|
|
|
189 |
return $infos;
|
|
|
190 |
}
|
|
|
191 |
|
|
|
192 |
private function traiterEnteteDebug($entetes) {
|
|
|
193 |
if (isset($entetes['debugs'])) {
|
|
|
194 |
$date = $entetes['date'];
|
|
|
195 |
$uri = $entetes['uri'];
|
|
|
196 |
$debugs = $entetes['debugs'];
|
|
|
197 |
foreach ($debugs as $debug) {
|
|
|
198 |
Debug::printr("DEBUG : $date - $uri :\n$debug");
|
|
|
199 |
}
|
|
|
200 |
}
|
|
|
201 |
}
|
|
|
202 |
|
|
|
203 |
private function traiterEnteteMessage($entetes) {
|
|
|
204 |
if (isset($entetes['messages'])) {
|
|
|
205 |
$date = $entetes['date'];
|
|
|
206 |
$uri = $entetes['uri'];
|
|
|
207 |
$messages = $entetes['messages'];
|
|
|
208 |
foreach ($messages as $message) {
|
|
|
209 |
Debug::printr("MESSAGE : $date - $uri :\n$message");
|
|
|
210 |
}
|
|
|
211 |
}
|
|
|
212 |
}
|
|
|
213 |
|
|
|
214 |
private function reinitialiser() {
|
|
|
215 |
$this->viderDistinction();
|
|
|
216 |
$this->viderLimite();
|
|
|
217 |
$this->viderOrdre();
|
|
|
218 |
}
|
91 |
aurelien |
219 |
}
|
|
|
220 |
?>
|