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