1 |
<?php |
2 |
|
3 |
/* |
4 |
* Processes requests received via services which concern consultations (by |
5 |
* ERP or other services). |
6 |
* |
7 |
* @author Mirna Limic <[email protected]> |
8 |
* Date: 18/10/2012 |
9 |
* |
10 |
* Follow-up: |
11 |
* Bugs: |
12 |
*/ |
13 |
|
14 |
|
15 |
require_once ("./metier/metiermanager.php"); |
16 |
require_once("../obj/consultation.class.php"); |
17 |
|
18 |
class ConsultationManager extends MetierManager { |
19 |
|
20 |
|
21 |
/* |
22 |
* Constructor. |
23 |
* |
24 |
* Calls it's parent constructor to establish a DB connection |
25 |
* and initializes the instance variables |
26 |
*/ |
27 |
public function __construct() { |
28 |
parent::__construct(); |
29 |
/*$this->date_limite = null; |
30 |
$this->date_retour = null; |
31 |
$this->date_envoi = null; |
32 |
$this->dossier_id = null;*/ |
33 |
$this->decisions = null; |
34 |
$this->metier_instance = null; |
35 |
$this->filename_prefix = null; |
36 |
$this->filename = null; |
37 |
} |
38 |
|
39 |
|
40 |
/* |
41 |
* Destructor |
42 |
* |
43 |
* Call's its parent's destructor. |
44 |
*/ |
45 |
public function __destruct() { |
46 |
parent::__destruct(); |
47 |
} |
48 |
|
49 |
|
50 |
/* |
51 |
* Verifies that a date falls inside of a date interval |
52 |
* @param string $date_str The string that should fall |
53 |
* within the interval |
54 |
* @param string $date_start_str The begining of the interval |
55 |
* @param string $date_end_str The end of the interval |
56 |
* @return book true if $date_str is found inside of the |
57 |
* interval, false otherwise |
58 |
*/ |
59 |
private function dateInsideInterval($date_str, $date_start_str = null, |
60 |
$date_end_str = null) { |
61 |
$dates_str = array($date_start_str, $date_str, $date_end_str); |
62 |
if (count($dates_str) == 1) { |
63 |
return true; |
64 |
} |
65 |
$dates = array(); |
66 |
$prev_date = -1; |
67 |
for ($i = 0; $i < 3; $i++) { |
68 |
if ($dates_str[$i] == null) { |
69 |
$dates[] = null; |
70 |
continue; |
71 |
} |
72 |
$d = explode('/', $dates_str[$i]); |
73 |
$date = strtotime($d[2].'-'.$d[1].'-'.$d[0]); |
74 |
if ($i > 0 && $date < $prev_date) { |
75 |
return false; |
76 |
} |
77 |
$prev_date = $date; |
78 |
} |
79 |
return true; |
80 |
} |
81 |
|
82 |
|
83 |
/* |
84 |
* Verifies that the date_retour received in the incomming data |
85 |
* is in the (date_envoi, date_limite) interval. Also, check that |
86 |
* there is no date_retour set for the consultation, as well as the |
87 |
* validity of the date itself |
88 |
* @param string $date_str The date_retour recived in the request |
89 |
* @param string $id The discussion id |
90 |
* @return string OK on success, BAD_DATA if the data sent is invalid, |
91 |
* KO otherwise |
92 |
*/ |
93 |
private function checkDates($date_str, $id) { |
94 |
// check that the consultation exists in DB |
95 |
if (empty($this->metier_instance->valF['date_limite'])) { |
96 |
$this->setMessage("La consultation ".$id." n'était pas trouvé"); |
97 |
return $this->BAD_DATA; |
98 |
} |
99 |
|
100 |
// check that the date_retour does not already exist |
101 |
if (!empty($this->metier_instance->valF['date_retour'])) { |
102 |
$this->setMessage("Le date de retour de la consultation ".$id. |
103 |
" existe déjà"); |
104 |
return $this->BAD_DATA; |
105 |
} |
106 |
|
107 |
// check that the date is valid |
108 |
$date = explode("/", $date_str); |
109 |
if (!checkdate($date[1], $date[0], $date[2])) { |
110 |
$this->setMessage("Le date de retour fourni pour la consultation ". |
111 |
$id." n'est pas bon (en bon format)"); |
112 |
return $this->BAD_DATA; |
113 |
} |
114 |
|
115 |
if (!$this->dateInsideInterval($date_str, |
116 |
$this->metier_instance->valF['date_envoi'], |
117 |
$this->metier_instance->valF['date_limite'])) { |
118 |
$this->setMessage("Le date de retour fourni pour la consultation ". |
119 |
$id." n'est pas entre le date d'envoi ". |
120 |
"et le date limite de la demande de consultation"); |
121 |
return $this->BAD_DATA; |
122 |
} |
123 |
return $this->OK; |
124 |
} |
125 |
|
126 |
|
127 |
/* |
128 |
* Verifies that the avis received in the incomming data corresponds |
129 |
* to one of the avis found in DB. |
130 |
* @param string $avis The avis recived in the request |
131 |
* @param string $id The discussion id |
132 |
* @return string OK on success, KO otherwise |
133 |
*/ |
134 |
private function avisValid($avis, $id) { |
135 |
// get all of the decisions possible from table avis_decision |
136 |
$sql = "SELECT libelle, avis_consultation FROM avis_consultation"; |
137 |
$res = $this->db->query($sql); |
138 |
if ($this->checkDBError($res, 'Erreur lors de recuperation des avis '. |
139 |
'possibles pendant traitement de la consultation '.$id)) { |
140 |
return $this->KO; |
141 |
} |
142 |
|
143 |
$this->decisions = array(); |
144 |
while ($row =& $res->fetchRow(DB_FETCHMODE_ORDERED)) { |
145 |
$this->decisions[$row[0]] = $row[1]; |
146 |
} |
147 |
$res->free(); |
148 |
|
149 |
// check that the decision received corresponds to a decision in DB |
150 |
if (!in_array($avis, array_keys($this->decisions))) { |
151 |
$this->setMessage( |
152 |
"L'avis de consultation reçu n'est pas feasible"); |
153 |
return $this->KO; |
154 |
} |
155 |
return $this->OK; |
156 |
} |
157 |
|
158 |
|
159 |
/* |
160 |
* Verifies that all the data concerning file to be stored (if any) |
161 |
* are present in full or not present at all |
162 |
* @param mixed $data The data recived in the request |
163 |
* @param string $id The discussion id |
164 |
* @return string OK on success, KO otherwise |
165 |
*/ |
166 |
private function fichierDataValid(&$data, $id) { |
167 |
// do the check |
168 |
if (!isset($data['nom_fichier']) && isset($data['fichier_base64']) |
169 |
|| isset($data['nom_fichier']) && !isset($data['fichier_base64'])) { |
170 |
$this->setMessage( |
171 |
"Les données de fichier d'avis de la consultation ".$id. |
172 |
" ne sont pas complètes "); |
173 |
return $this->KO; |
174 |
} |
175 |
return $this->OK; |
176 |
} |
177 |
|
178 |
|
179 |
/* |
180 |
* Store the data recived in the request into a file on the |
181 |
* local filesystem. |
182 |
* @todo This function will need to be changed for the save to |
183 |
* be on GED |
184 |
* @param mixed $data The data received with the request |
185 |
* @param string $id The consultation ID |
186 |
* @return string OK on success, KO otherwise |
187 |
*/ |
188 |
private function storeDecisionFile(&$data, $id) { |
189 |
if (isset($data['fichier_base64'])) { |
190 |
$dir = dirname(__FILE__).'/../../trs/'.$this->dossier_id; |
191 |
|
192 |
// if a file already exists by that name and it |
193 |
// is not a directory, back out |
194 |
if (file_exists($dir) && !is_dir($dir)) { |
195 |
$this->setMessage("Fichier d'avis ne peut pas être ". |
196 |
"enregistré pour la consultation ".$id); |
197 |
return $this->KO; |
198 |
} |
199 |
// if a dirextory by that name exists, make sure it does |
200 |
// not already contain an avis de consultation |
201 |
if (file_exists($dir) && is_dir($dir)) { |
202 |
$dir_contents = trim(shell_exec('ls '.$dir)); |
203 |
if (strpos($dir_contents, ' ') != false) { |
204 |
$dir_contents = explode(' ', $dir_contents); |
205 |
} else { |
206 |
$dir_contents = array($dir_contents); |
207 |
} |
208 |
foreach ($dir_contents as $filename) { |
209 |
if (strspn($filename, $this->filename_prefix, 0) > 0) { |
210 |
$this->setMessage("Un fichier d'avis existe déjà ". |
211 |
"pour la consultation ".$id); |
212 |
return $this->KO; |
213 |
} |
214 |
} |
215 |
} |
216 |
// if no file by that name exists, create the directory |
217 |
if (!file_exists($dir)) { |
218 |
if (!mkdir($dir, 0775)) { |
219 |
$this->setMessage("Erreur dans la création de répertoire ". |
220 |
"pour la sauvegarde de l'avis de la consultation ".$id); |
221 |
return $this->KO; |
222 |
} |
223 |
} |
224 |
|
225 |
|
226 |
// store the file contents into the file named: |
227 |
// consultation_<ID>_<file_name_received> |
228 |
$file_len = strlen($data['fichier_base64']); |
229 |
$filename = $dir.$this->metier_instance->valF['dossier'].'/'. |
230 |
$this->filename_prefix.$data['nom_fichier']; |
231 |
$this->filename = $filename; |
232 |
$file = fopen($filename, 'w'); |
233 |
if (!$file) { |
234 |
$this->setMessage("Echec en création de pointeur de fichier ". |
235 |
"pour la sauvegarde de l'avis de la consultation ".$id); |
236 |
return $this->KO; |
237 |
} |
238 |
// check that the number of bytes written is equal to the length |
239 |
// of the data received |
240 |
$num_written = fwrite($file, $data['fichier_base64'], $file_len); |
241 |
if (!$num_written || $num_written != $file_len) { |
242 |
$this->setMessage("Sauvegarde de fichier d'avis pour la ". |
243 |
"consultation ".$id." a échouée"); |
244 |
// remove the file |
245 |
// the return value from shell can't be used for checking since |
246 |
// one can not know if the NULL returned is because there was no |
247 |
// output or because there was an error |
248 |
$ret = shell_exec("rm -f $filename 2>&1"); |
249 |
//if ($ret == NULL) { // an error occured while deleting the file |
250 |
// $msg = $this->getMessage(); |
251 |
// $this->setMessage($msg . ' Problème pendent la suppression '. |
252 |
// 'des données corrompues'); |
253 |
//} |
254 |
return $this->KO; |
255 |
} |
256 |
fclose($file); |
257 |
} |
258 |
return $this->OK; |
259 |
} |
260 |
|
261 |
/* |
262 |
* Called when one of the external services sends its recommendation |
263 |
* as a responde to a demand issued by openfoncier. |
264 |
* @param mixed $data The array containing building number. |
265 |
* @param string $id The ID of the dossier. |
266 |
* @return bool 'OK' |
267 |
*/ |
268 |
public function consultationDecision($data, $id) { |
269 |
|
270 |
// get all of the dates for the consultation from DB |
271 |
// and check the date received is ok with respect to the |
272 |
// time interval set by (date_envoi, date_limite) |
273 |
// equally extract the dossier ID while at it |
274 |
$this->metier_instance = new consultation($id, $this->db, 0); |
275 |
|
276 |
// check that the consultation was found |
277 |
if (isset($this->metier_instance->errors['db_debuginfo']) |
278 |
&& !empty($consultation->errors['db_debuginfo'])) { |
279 |
$this->setMessage("Erreur lors de recuperation de la ". |
280 |
"consultation $id"); |
281 |
return $this->KO; |
282 |
} |
283 |
|
284 |
// check that the date sent is applicable |
285 |
$res = $this->checkDates($data['date_retour'], $id); |
286 |
if ($res != $this->OK) { |
287 |
return $res; |
288 |
} |
289 |
|
290 |
// set the prefix for the filename to use in case file data |
291 |
// was received |
292 |
$this->filename_prefix = "consultation_".$id."_"; |
293 |
// check that data regarding fichier is complete |
294 |
$res = $this->fichierDataValid($data, $id); |
295 |
if ($res != $this->OK) { |
296 |
return $res; |
297 |
} |
298 |
|
299 |
// check that the decision sent is one of the decisions found in DB |
300 |
// and store the possible decisions in $this->decisions |
301 |
$res = $this->avisValid($data['avis'], $id); |
302 |
if ($res != $this->OK) { |
303 |
return $res; |
304 |
} |
305 |
// first store the file if needed |
306 |
$res = $this->storeDecisionFile($data, $id); |
307 |
if ($res != $this->OK) { |
308 |
return $res; |
309 |
} |
310 |
$this->metier_instance->valF['date_retour'] = $data['date_retour']; |
311 |
$this->metier_instance->valF['avis_consultation'] = $this->decisions[$data['avis']]; |
312 |
$this->metier_instance->valF['lu'] = FALSE; |
313 |
if (isset($data['nom_fichier'])) { |
314 |
$this->metier_instance->valF['fichier'] = $this->filename_prefix . $data['nom_fichier']; |
315 |
} |
316 |
if (isset($data['motivation'])) { |
317 |
$this->metier_instance->valF['motivation'] = $data['motivation']; |
318 |
} |
319 |
|
320 |
$ret = parent::modifier($this->metier_instance->valF, |
321 |
"L'avis de la consultation $id a été pris en compte", |
322 |
"Erreur pendant le traitemande de la demande pour la consultation $id"); |
323 |
if ($ret != $this->OK) { |
324 |
// delete the file on disk |
325 |
if (isset($data['nom_fichier'])) { |
326 |
shell_exec("rm -f $this->filename"); |
327 |
} |
328 |
} |
329 |
return $ret; |
330 |
} |
331 |
|
332 |
|
333 |
} |
334 |
|
335 |
?> |