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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 716 - (hide annotations)
Fri Nov 16 11:24:23 2012 UTC (12 years, 2 months ago) by mlimic
File size: 8309 byte(s)
Bug: Existance of file inside of JSON sent data was obligatory.
Fix: File data procesed only if sent with the request

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 mlimic 573 require_once("../obj/consultation.class.php");
17 mlimic 515
18     class ConsultationManager extends MetierManager {
19    
20 mlimic 559
21 mlimic 522 /*
22     * Constructor.
23     *
24 mlimic 564 * Calls it's parent constructor to establish a DB connection
25     * and initializes the instance variables
26 mlimic 522 */
27 mlimic 526 public function __construct() {
28 mlimic 515 parent::__construct();
29 mlimic 573 /*$this->date_limite = null;
30 mlimic 559 $this->date_retour = null;
31     $this->date_envoi = null;
32 mlimic 573 $this->dossier_id = null;*/
33 mlimic 559 $this->decisions = null;
34 mlimic 573 $this->metier_instance = null;
35     $this->filename_prefix = null;
36     $this->filename = null;
37 mlimic 515 }
38    
39 mlimic 523
40 mlimic 522 /*
41     * Destructor
42     *
43     * Call's its parent's destructor.
44     */
45 mlimic 526 public function __destruct() {
46 mlimic 515 parent::__destruct();
47     }
48 mlimic 522
49 mlimic 564
50     /*
51     * Verifies that the date_retour received in the incomming data
52     * is in the (date_envoi, date_limite) interval. Also, check that
53     * there is no date_retour set for the consultation, as well as the
54     * validity of the date itself
55     * @param string $date_str The date_retour recived in the request
56     * @param string $id The discussion id
57     * @return string OK on success, BAD_DATA if the data sent is invalid,
58     * KO otherwise
59     */
60     private function checkDates($date_str, $id) {
61 mlimic 559 // check that the consultation exists in DB
62 mlimic 573 if (empty($this->metier_instance->valF['date_limite'])) {
63 mlimic 564 $this->setMessage("La consultation ".$id." n'était pas trouvé");
64 mlimic 559 return $this->BAD_DATA;
65     }
66    
67     // check that the date_retour does not already exist
68 mlimic 573 if (!empty($this->metier_instance->valF['date_retour'])) {
69 mlimic 564 $this->setMessage("Le date de retour de la consultation ".$id.
70     " existe déjà");
71 mlimic 559 return $this->BAD_DATA;
72     }
73    
74     // check that the date is valid
75     $date = explode("/", $date_str);
76     if (!checkdate($date[1], $date[0], $date[2])) {
77 mlimic 564 $this->setMessage("Le date de retour fourni pour la consultation ".
78     $id." n'est pas bon (en bon format)");
79 mlimic 559 return $this->BAD_DATA;
80     }
81    
82 mlimic 573 if (!$this->dateInsideInterval($date_str,
83     $this->metier_instance->valF['date_envoi'],
84     $this->metier_instance->valF['date_limite'])) {
85 mlimic 564 $this->setMessage("Le date de retour fourni pour la consultation ".
86     $id." n'est pas entre le date d'envoi ".
87 mlimic 559 "et le date limite de la demande de consultation");
88     return $this->BAD_DATA;
89     }
90     return $this->OK;
91     }
92    
93    
94 mlimic 564 /*
95     * Verifies that the avis received in the incomming data corresponds
96     * to one of the avis found in DB.
97     * @param string $avis The avis recived in the request
98     * @param string $id The discussion id
99     * @return string OK on success, KO otherwise
100     */
101     private function avisValid($avis, $id) {
102 mlimic 559 // get all of the decisions possible from table avis_decision
103     $sql = "SELECT libelle, avis_consultation FROM avis_consultation";
104     $res = $this->db->query($sql);
105     if ($this->checkDBError($res, 'Erreur lors de recuperation des avis '.
106 mlimic 564 'possibles pendant traitement de la consultation '.$id)) {
107 mlimic 559 return $this->KO;
108     }
109    
110     $this->decisions = array();
111     while ($row =& $res->fetchRow(DB_FETCHMODE_ORDERED)) {
112     $this->decisions[$row[0]] = $row[1];
113     }
114     $res->free();
115    
116     // check that the decision received corresponds to a decision in DB
117     if (!in_array($avis, array_keys($this->decisions))) {
118 mlimic 564 $this->setMessage(
119 mlimic 573 "L'avis de consultation reçu n'est pas feasible");
120 mlimic 564 return $this->KO;
121 mlimic 559 }
122     return $this->OK;
123     }
124    
125    
126 mlimic 564 /*
127     * Verifies that all the data concerning file to be stored (if any)
128     * are present in full or not present at all
129     * @param mixed $data The data recived in the request
130     * @param string $id The discussion id
131     * @return string OK on success, KO otherwise
132     */
133     private function fichierDataValid(&$data, $id) {
134 mlimic 559 // do the check
135     if (!isset($data['nom_fichier']) && isset($data['fichier_base64'])
136     || isset($data['nom_fichier']) && !isset($data['fichier_base64'])) {
137     $this->setMessage(
138 mlimic 564 "Les données de fichier d'avis de la consultation ".$id.
139     " ne sont pas complètes ");
140     return $this->KO;
141 mlimic 559 }
142     return $this->OK;
143     }
144    
145 mlimic 564 /*
146 mlimic 522 * Called when one of the external services sends its recommendation
147     * as a responde to a demand issued by openfoncier.
148     * @param mixed $data The array containing building number.
149     * @param string $id The ID of the dossier.
150     * @return bool 'OK'
151     */
152 mlimic 559 public function consultationDecision($data, $id) {
153 mlimic 573
154 mlimic 559 // get all of the dates for the consultation from DB
155     // and check the date received is ok with respect to the
156     // time interval set by (date_envoi, date_limite)
157     // equally extract the dossier ID while at it
158 mlimic 573 $this->metier_instance = new consultation($id, $this->db, 0);
159    
160     // check that the consultation was found
161     if (isset($this->metier_instance->errors['db_debuginfo'])
162     && !empty($consultation->errors['db_debuginfo'])) {
163     $this->setMessage("Erreur lors de recuperation de la ".
164     "consultation $id");
165     return $this->KO;
166 mlimic 515 }
167 mlimic 559
168 mlimic 564 // check that the date sent is applicable
169     $res = $this->checkDates($data['date_retour'], $id);
170 mlimic 559 if ($res != $this->OK) {
171     return $res;
172     }
173 mlimic 573
174     // set the prefix for the filename to use in case file data
175     // was received
176     $this->filename_prefix = "consultation_".$id."_";
177 mlimic 559 // check that data regarding fichier is complete
178 mlimic 564 $res = $this->fichierDataValid($data, $id);
179 mlimic 559 if ($res != $this->OK) {
180     return $res;
181     }
182    
183     // check that the decision sent is one of the decisions found in DB
184     // and store the possible decisions in $this->decisions
185 mlimic 564 $res = $this->avisValid($data['avis'], $id);
186 mlimic 559 if ($res != $this->OK) {
187     return $res;
188 nhaye 645 }
189 mlimic 716 if(isset($data['fichier_base64'])) {
190     if($file_content = base64_decode ($data['fichier_base64'] , TRUE )) {
191     // first store the file if needed
192     $res = $this->f->storeDecisionFile($file_content,
193 nhaye 645 $data['nom_fichier'],
194     $this->metier_instance->valF['dossier'],
195     $this->filename_prefix);
196 mlimic 716 if ($res !== true) {
197     return $res;
198     }
199     } else {
200     return _("Le contenu du fichier n'est pas valide.");
201 nhaye 702 }
202 mlimic 559 }
203 nhaye 702
204 mlimic 573 $this->metier_instance->valF['date_retour'] = $data['date_retour'];
205     $this->metier_instance->valF['avis_consultation'] = $this->decisions[$data['avis']];
206     $this->metier_instance->valF['lu'] = FALSE;
207 mlimic 559 if (isset($data['nom_fichier'])) {
208 mlimic 573 $this->metier_instance->valF['fichier'] = $this->filename_prefix . $data['nom_fichier'];
209 mlimic 559 }
210 mlimic 573 if (isset($data['motivation'])) {
211     $this->metier_instance->valF['motivation'] = $data['motivation'];
212 mlimic 559 }
213    
214 mlimic 573 $ret = parent::modifier($this->metier_instance->valF,
215     "L'avis de la consultation $id a été pris en compte",
216     "Erreur pendant le traitemande de la demande pour la consultation $id");
217     if ($ret != $this->OK) {
218     // delete the file on disk
219     if (isset($data['nom_fichier'])) {
220     shell_exec("rm -f $this->filename");
221     }
222     }
223     return $ret;
224 mlimic 515 }
225    
226 mlimic 559
227 mlimic 515 }
228    
229     ?>

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26