/[openfoncier]/trunk/services/metier/consultationmanager.php
ViewVC logotype

Annotation of /trunk/services/metier/consultationmanager.php

Parent Directory Parent Directory | Revision Log Revision Log


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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26