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 |
/* |
/** |
|
* Constructor. |
|
27 |
* |
* |
28 |
* Calls it's parent constructor to establish a DB connection. |
*/ |
29 |
*/ |
var $metier_instance = null; |
30 |
public function __construct() { |
var $filename_prefix = null; |
31 |
parent::__construct(); |
var $filename = null; |
32 |
$this->date_limite = null; |
|
33 |
$this->date_retour = null; |
/** |
34 |
$this->date_envoi = null; |
* Cette méthode permet de modifier une consultation existante pour lui |
35 |
$this->dossier_id = null; |
* ajouter les informations de retour d'avis. |
36 |
$this->decisions = null; |
* |
37 |
} |
* @param mixed $request_data Les données JSON reçues |
38 |
|
* @param string $id L'identifiant de la ressource |
39 |
|
*/ |
40 |
/* |
public function consultationDecision($data, $id) { |
|
* Destructor |
|
|
* |
|
|
* Call's its parent's destructor. |
|
|
*/ |
|
|
public function __destruct() { |
|
|
parent::__destruct(); |
|
|
} |
|
41 |
|
|
42 |
|
// Si l'identifiant envoyé n'est pas un numérique alors on ajoute un |
43 |
private function dateValid($date_str, $date_start_str = null, |
// message d'informations et on retourne un résultat d'erreur |
44 |
$date_end_str = null) { |
if (!is_numeric($id)) { |
45 |
$dates_str = array($date_start_str, $date_end_str); |
$this->setMessage("L'identifiant '".$id."' fourni est incorrect."); |
46 |
$dates = array(); |
return $this->BAD_DATA; |
|
for ($i = 0; $i < 2; $i++) { |
|
|
if ($dates_str[$i] == null) { |
|
|
$dates[] = null; |
|
|
continue; |
|
|
} |
|
|
$d = explode('-', $dates_str[$i]); |
|
|
$dates[] = strtotime($d[2].'-'.$d[1].'-'.$d[0]); |
|
|
} |
|
|
$d = explode('/', $date_str); |
|
|
$date = strtotime(implode('-', $d)); |
|
|
|
|
|
print ' start:'.strval($dates[0]); |
|
|
print ' end:'.strval($dates[1]); |
|
|
print ' sent:'.strval($date); |
|
|
if ($dates[0] && $date < $dates[0]) { |
|
|
return false; |
|
|
} |
|
|
if ($dates[1] && $date > $dates[1]) { |
|
|
return false; |
|
47 |
} |
} |
48 |
return true; |
|
49 |
} |
// On instancie la consultation sur laquelle porte la requête |
50 |
|
$this->metier_instance = new consultation($id, $this->db, 0); |
51 |
|
|
52 |
private function getDataFromDB($id) { |
// On vérifie si l'instanciation a produit une erreur de base de données |
53 |
$sql = 'SELECT date_envoi, date_retour, date_limite, dossier FROM '. |
// alors on ajoute un message d'informations et on retourne un résultat |
54 |
'consultation WHERE consultation = '.$id; |
// d'erreur |
55 |
$res = $this->db->query($sql); |
if (isset($this->metier_instance->errors['db_debuginfo']) |
56 |
if ($this->checkDBError($res, 'Erreur lors de recuperation des dates '. |
&& !empty($this->metier_instance->errors['db_debuginfo'])) { |
57 |
'de la consultation :'.$id)) { |
$this->setMessage("Erreur lors de la récupération de la". |
58 |
|
" consultation '".$id."'."); |
59 |
return $this->KO; |
return $this->KO; |
60 |
} |
} |
61 |
while ($row =& $res->fetchRow(DB_FETCHMODE_ORDERED)) { |
|
62 |
$this->date_envoi = $row[0];; |
// Si l'identifiant de la consultation instanciée est différent de |
63 |
$this->date_retour = $row[1]; |
// l'identifiant envoyé alors on ajoute un message d'informations et |
64 |
$this->date_limite = $row[2]; |
// on retourne un résultat d'erreur |
65 |
$this->dossier_id = $row[3]; |
if ($id != $this->getMetierInstanceValForPrimaryKey()) { |
66 |
} |
$this->setMessage("Aucune consultation '".$id."'."); |
|
|
|
|
print ' date_envoi:'.$this->date_envoi; |
|
|
print ' date_limite:'.$this->date_limite; |
|
|
$res->free(); |
|
|
return $this->OK; |
|
|
} |
|
|
|
|
|
private function checkDates($date_str) { |
|
|
// check that the consultation exists in DB |
|
|
if (!$this->date_limite) { |
|
|
$this->setMessage("La consultation n'était pas trouvé"); |
|
67 |
return $this->BAD_DATA; |
return $this->BAD_DATA; |
68 |
} |
} |
69 |
|
|
70 |
// check that the date_retour does not already exist |
// Si la consultation possède déjà une date de retour ou un avis ou une |
71 |
if (!empty($this->date_retour)) { |
// motivation alors on ajoute un message d'informations et on retourne |
72 |
$this->setMessage('Le date de retour déjà existe pour la '. |
// un résultat d'erreur |
73 |
'consultation'); |
$date_retour = $this->getMetierInstanceValForField("date_retour"); |
74 |
|
$avis = $this->getMetierInstanceValForField("avis"); |
75 |
|
$motivation = $this->getMetierInstanceValForField("motivation"); |
76 |
|
if (!empty($date_retour) || !empty($avis) || !empty($motivation)) { |
77 |
|
$this->setMessage("Un retour d'avis a déjà été rendu pour la". |
78 |
|
" consultation '".$id."'."); |
79 |
return $this->BAD_DATA; |
return $this->BAD_DATA; |
80 |
} |
} |
81 |
|
|
82 |
// check that the date is valid |
// Le format de la date de retour valide est 'JJ/MM/AAAA' |
83 |
$date = explode("/", $date_str); |
// Si la donnée fournie n'est pas valide alors on ajoute |
84 |
print ' date:'; |
// un message d'informations et on retourne un résultat d'erreur |
85 |
print_r($date); |
$date_retour = explode("/", $data['date_retour']); |
86 |
if (!checkdate($date[1], $date[0], $date[2])) { |
if (count($date_retour)!= 3 |
87 |
$this->setMessage("Le date fourni n'est pas bon (en bon format)"); |
|| !checkdate($date_retour[1], $date_retour[0], $date_retour[2])) { |
88 |
|
$this->setMessage("Le format de la date de retour d'avis fournie". |
89 |
|
" pour la consultation '".$id."' n'est pas". |
90 |
|
" correct."); |
91 |
return $this->BAD_DATA; |
return $this->BAD_DATA; |
92 |
} |
} |
93 |
|
|
94 |
if (!$this->dateValid($date_str, $this->date_envoi, $this->date_limite)) { |
// Si la date de retour de l'avis ne se situe pas entre la date d'envoi |
95 |
$this->setMessage("Le date fourni n'est pas entre le date d'envoi ". |
// de la consultation et la date limite de retour d'avis alors on ajoute |
96 |
"et le date limite de la demande de consultation"); |
// un message d'informations et on retourne un résultat d'erreur |
97 |
|
$date_retour = $data['date_retour']; |
98 |
|
$date_envoi = $this->getMetierInstanceValForField("date_envoi"); |
99 |
|
$date_limite = $this->getMetierInstanceValForField("date_limite"); |
100 |
|
if (!$this->dateInsideInterval($date_retour, |
101 |
|
$date_envoi, $date_limite)) { |
102 |
|
$this->setMessage("La date de retour d'avis fournie pour la". |
103 |
|
" consultation '".$id."' ne se trouve pas entre". |
104 |
|
" la date d'envoi et la date limite."); |
105 |
return $this->BAD_DATA; |
return $this->BAD_DATA; |
106 |
} |
} |
|
return $this->OK; |
|
|
} |
|
107 |
|
|
108 |
|
// => DATE DE RETOUR |
109 |
|
$this->metier_instance->valF['date_retour'] = $data['date_retour']; |
110 |
|
|
111 |
private function avisValid($avis) { |
// Si l'avis fourni ne correspond pas à la liste d'avis valides alors |
112 |
// get all of the decisions possible from table avis_decision |
// on ajoute un message d'informations et on retourne un résultat |
113 |
$sql = "SELECT libelle, avis_consultation FROM avis_consultation"; |
// d'erreur |
114 |
|
$avis = $data["avis"]; |
115 |
|
$avis_valid = array("Favorable", |
116 |
|
"Favorable avec réserve", "Défavorable", ); |
117 |
|
if (!in_array($avis, $avis_valid)) { |
118 |
|
$this->setMessage("L'avis du retour d'avis fourni pour la". |
119 |
|
" consultation '".$id."' n'est pas correct."); |
120 |
|
return $this->BAD_DATA; |
121 |
|
} |
122 |
|
|
123 |
|
// Récupération de la référence vers un avis_consultation existant |
124 |
|
// On liste les avis possibles et on récupère l'identifiant |
125 |
|
// correspondant au libellé transmis en paramètre |
126 |
|
$sql = " SELECT avis_consultation, libelle "; |
127 |
|
$sql .= " FROM ".DB_PREFIXE."avis_consultation "; |
128 |
|
$sql .= " WHERE ((avis_consultation.om_validite_debut IS NULL "; |
129 |
|
$sql .= " AND (avis_consultation.om_validite_fin IS NULL "; |
130 |
|
$sql .= " OR avis_consultation.om_validite_fin > CURRENT_DATE)) "; |
131 |
|
$sql .= " OR (avis_consultation.om_validite_debut <= CURRENT_DATE "; |
132 |
|
$sql .= " AND (avis_consultation.om_validite_fin IS NULL "; |
133 |
|
$sql .= " OR avis_consultation.om_validite_fin > CURRENT_DATE))) "; |
134 |
$res = $this->db->query($sql); |
$res = $this->db->query($sql); |
135 |
if ($this->checkDBError($res, 'Erreur lors de recuperation des avis '. |
// Logger |
136 |
'possibles pendant traitement de la consultation :'.$id)) { |
$this->addToLog("consultationDecision(): db->query(\"".$sql."\")", |
137 |
|
VERBOSE_MODE); |
138 |
|
|
139 |
|
// Si une erreur de base de données se produit sur cette requête |
140 |
|
// alors on retourne un résultat d'erreur |
141 |
|
if ($this->f->isDatabaseError($res, true)) { |
142 |
|
$this->setMessage("Erreur lors de la récupération des avis pour la". |
143 |
|
" consultation '".$id."'."); |
144 |
return $this->KO; |
return $this->KO; |
145 |
} |
} |
146 |
|
|
147 |
$this->decisions = array(); |
// Récupération de la référence vers un avis_consultation existant |
148 |
while ($row =& $res->fetchRow(DB_FETCHMODE_ORDERED)) { |
// On liste les avis possibles et on récupère l'identifiant |
149 |
$this->decisions[$row[0]] = $row[1]; |
// correspondant au libellé transmis en paramètre |
150 |
|
$avis = $data["avis"]; |
151 |
|
$avis_id = NULL; |
152 |
|
while ($row =& $res->fetchRow(DB_FETCHMODE_ASSOC)) { |
153 |
|
if ($avis == $row["libelle"]) { |
154 |
|
$avis_id = $row["avis_consultation"]; |
155 |
|
break; |
156 |
|
} |
157 |
} |
} |
158 |
$res->free(); |
$res->free(); |
|
|
|
|
// check that the decision received corresponds to a decision in DB |
|
|
if (!in_array($avis, array_keys($this->decisions))) { |
|
|
$this->setMessage(__FILE__."::".__FUNCTION__. |
|
|
": L'avis reçu n'est pas trouvé dans la BD."); |
|
|
$this->KO; |
|
|
} |
|
|
print ' decisions:'; |
|
|
print_r($this->decisions); |
|
|
return $this->OK; |
|
|
} |
|
159 |
|
|
160 |
|
// Si la décision n'existe pas dans la base de données alors on ajoute |
161 |
private function fichierDataValid(&$data) { |
// un message d'informations et on retourne un résultat d'erreur |
162 |
// do the check |
if (is_null($avis_id)) { |
163 |
if (!isset($data['nom_fichier']) && isset($data['fichier_base64']) |
$this->setMessage("L'avis n'existe pas."); |
164 |
|| isset($data['nom_fichier']) && !isset($data['fichier_base64'])) { |
return $this->KO; |
|
print ' problem_fichier_data '; |
|
|
$this->setMessage( |
|
|
"Les données concernant le fichier ne sont pas complètes"); |
|
|
return $this->KO; |
|
165 |
} |
} |
|
print ' ok_fichier_data '; |
|
|
return $this->OK; |
|
|
} |
|
166 |
|
|
167 |
|
// => AVIS |
168 |
|
$this->metier_instance->valF['avis_consultation'] = $avis_id; |
169 |
|
|
170 |
|
// Si un nom de fichier (nom_fichier) est fourni mais pas un contenu de |
171 |
|
// fichier (fichier_base64) alors on ajoute un message d'informations |
172 |
|
// et on retourne un résultat d'erreur |
173 |
|
if (isset($data['nom_fichier']) && !isset($data['fichier_base64']) |
174 |
|
|| isset($data['fichier_base64']) && !isset($data['nom_fichier'])) { |
175 |
|
$this->setMessage("Les informations du fichier de retour d'avis". |
176 |
|
" fournies pour la consultation '".$id."' ne". |
177 |
|
" sont pas correctes."); |
178 |
|
return $this->BAD_DATA; |
179 |
|
} |
180 |
|
|
181 |
private function storeDecisionFile(&$data) { |
// |
182 |
|
$this->filename_prefix = "consultation_".$id."_"; |
183 |
|
|
184 |
|
// |
185 |
if (isset($data['fichier_base64'])) { |
if (isset($data['fichier_base64'])) { |
186 |
$dir = dirname(__FILE__).'/../trs/'.$dossier_id; |
// |
187 |
if (!file_exists($dir) && !is_dir($dir)) { |
if ($file_content = base64_decode($data['fichier_base64'], true)) { |
188 |
mkdir($dir, 755); |
// |
189 |
} |
$r = $this->f->storeDecisionFile($file_content, |
190 |
$file_len = strlen($data['fichier_base64']); |
$data['nom_fichier'], |
191 |
$filename = $dir.'/consultation_'.$id.'_'.$data['nom_fichier']; |
$this->getMetierInstanceValForField("dossier"), |
192 |
$file = fopen($filename, 'w'); |
$this->filename_prefix); |
193 |
if (!$file) { |
// XXX Vérifier le retour d'erreur de la méthode |
194 |
$this->setMessage(__FILE__."::".__FUNCTION__. |
// StoreDecisionFile |
195 |
": Echec en création de pointeur de fichier"); |
if ($r !== true) { |
196 |
$this->KO; |
return $r; |
197 |
} |
} |
198 |
$num_written = fwrite($file, $data[''], $file_len); |
} else { |
199 |
if (!$num_writeen || $num_written != $file_len) { |
$this->setMessage("Le contenu du fichier n'est pas valide."); |
200 |
$this->setMessage(__FILE__."::".__FUNCTION__. |
return $this->BAD_DATA; |
|
": Sauvegarde de fichier échouée"); |
|
|
$this->KO; |
|
201 |
} |
} |
|
fclose($file); |
|
|
} |
|
|
return $this->OK; |
|
|
} |
|
|
|
|
|
/* |
|
|
* Called when one of the external services sends its recommendation |
|
|
* as a responde to a demand issued by openfoncier. |
|
|
* @param mixed $data The array containing building number. |
|
|
* @param string $id The ID of the dossier. |
|
|
* @return bool 'OK' |
|
|
*/ |
|
|
public function consultationDecision($data, $id) { |
|
|
|
|
|
// get all of the dates for the consultation from DB |
|
|
// and check the date received is ok with respect to the |
|
|
// time interval set by (date_envoi, date_limite) |
|
|
// equally extract the dossier ID while at it |
|
|
$res = $this->getDataFromDB($id); |
|
|
if ($res != $this->OK) { |
|
|
return $res; |
|
|
} |
|
|
|
|
|
$res = $this->checkDates($data['date_retour']); |
|
|
if ($res != $this->OK) { |
|
|
return $res; |
|
|
} |
|
|
|
|
|
// check that data regarding fichier is complete |
|
|
$res = $this->fichierDataValid($data); |
|
|
if ($res != $this->OK) { |
|
|
return $res; |
|
|
} |
|
|
|
|
|
|
|
|
// check that the decision sent is one of the decisions found in DB |
|
|
// and store the possible decisions in $this->decisions |
|
|
$res = $this->avisValid($data['avis']); |
|
|
if ($res != $this->OK) { |
|
|
return $res; |
|
|
} |
|
|
|
|
|
// first store the file if needed |
|
|
$res = $this->storeDecisionFile($data); |
|
|
if ($res != $this->OK) { |
|
|
return $res; |
|
|
} |
|
|
|
|
|
// update the DB |
|
|
$date = explode("/", $data['date_retour']); |
|
|
$sql = "UPDATE consultation SET ". |
|
|
"date_retour = DATE '". |
|
|
$date[2]."-".$date[1]."-".$date[0]."', "; |
|
|
if (isset($data['motivation'])) { |
|
|
$sql .= "motivation = " . $data['motivation'] . ', '; |
|
202 |
} |
} |
203 |
|
|
204 |
|
// => FICHIER |
205 |
if (isset($data['nom_fichier'])) { |
if (isset($data['nom_fichier'])) { |
206 |
$sql .= "fichier = ".$data['nom_fichier'] . ', '; |
$this->metier_instance->valF['fichier'] = $this->filename_prefix.$data['nom_fichier']; |
207 |
} |
} |
|
$sql .= "avis_consultation = ".$this->decisions[$data['avis']]. |
|
|
", lu = false WHERE consultation = ".$id; |
|
|
|
|
|
|
|
|
print ' $sql:'.$sql.' '; |
|
208 |
|
|
209 |
$res = $this->db->query($sql); |
// => MOTIVATION |
210 |
if ($this->checkDBError($res, 'Erreur lors de mis à jour '. |
if (isset($data['motivation'])) { |
211 |
'de la consultation '.$id.' dans le DB')) { |
$this->metier_instance->valF['motivation'] = $data['motivation']; |
|
return $this->KO; |
|
212 |
} |
} |
213 |
|
|
214 |
$this->setMessage("L'avis été pris en compte"); |
// => LU |
215 |
return $this->OK; |
$this->metier_instance->valF['lu'] = false; |
216 |
|
|
217 |
|
// Exécution du traitement |
218 |
|
$ret = parent::modifier($this->metier_instance->valF, |
219 |
|
"L'avis de la consultation $id a été pris en compte", |
220 |
|
"Erreur pendant le traitemande de la demande pour la consultation $id"); |
221 |
|
|
222 |
|
// XXX vérifier ce retour car filename n'est jamais rempli |
223 |
|
if ($ret != $this->OK) { |
224 |
|
// delete the file on disk |
225 |
|
if (isset($data['nom_fichier'])) { |
226 |
|
shell_exec("rm -f $this->filename"); |
227 |
|
} |
228 |
|
} |
229 |
|
return $ret; |
230 |
|
|
231 |
} |
} |
232 |
|
|
|
|
|
233 |
} |
} |
234 |
|
|
235 |
?> |
?> |