1 |
mlimic |
515 |
<?php |
2 |
|
|
|
3 |
mlimic |
522 |
/* |
4 |
|
|
* The base class for all of the xxxManagers used for processing the |
5 |
|
|
* received requests. |
6 |
|
|
* |
7 |
|
|
* @author: Mirna Limic <[email protected]> |
8 |
|
|
* |
9 |
|
|
* Date: 15/10/2012 |
10 |
|
|
* Follow-up: |
11 |
|
|
* Bugs: Unknown |
12 |
|
|
* |
13 |
|
|
*/ |
14 |
mlimic |
515 |
|
15 |
mlimic |
522 |
|
16 |
|
|
|
17 |
|
|
require_once ("../obj/utils.class.php"); |
18 |
mlimic |
573 |
//require_once ("../obj/om_utilisateur.class.php"); |
19 |
mlimic |
522 |
|
20 |
mlimic |
515 |
class MetierManager { |
21 |
|
|
|
22 |
mlimic |
522 |
|
23 |
|
|
/**#@+ |
24 |
|
|
* @access protected |
25 |
|
|
* @var string |
26 |
|
|
*/ |
27 |
mlimic |
515 |
var $KO = 'KO'; |
28 |
|
|
var $OK = 'OK'; |
29 |
mlimic |
540 |
var $NA = 'NA'; // not apllicable |
30 |
mlimic |
559 |
var $BAD_DATA = 'BAD_DATA'; |
31 |
mlimic |
515 |
var $DEBUG = 0; |
32 |
|
|
var $GENERIC_ERROR = "Error during request processing"; |
33 |
|
|
|
34 |
mlimic |
522 |
/* |
35 |
|
|
* Constructeur. |
36 |
|
|
* |
37 |
|
|
* Sets 'collectivite', and 'login' in the $_SESSION. |
38 |
|
|
* @todo The value stored in the $_SESSION['collectivite'] needs to |
39 |
|
|
* be obtained rather than hardcoded. |
40 |
|
|
* @todo The value stored in the $_SESSION['login'] could be obtained |
41 |
|
|
* rather than being hardcoded to "somelogin". |
42 |
|
|
* @uses utils Uses the obj/utils.class.php to establish a DB connection. |
43 |
|
|
* @uses application::connectDatabase() In this function the execution |
44 |
|
|
* could terminate if the connection to DB cannot be established. The |
45 |
|
|
* important thing to prevent this is to set the $_SESSION['login'] to |
46 |
|
|
* value that is not empty and that is bogus. |
47 |
|
|
*/ |
48 |
mlimic |
526 |
protected function __construct() { |
49 |
mlimic |
515 |
session_start(); |
50 |
|
|
// Setter les variables de $_SESSION pour la connexion avec BD |
51 |
|
|
$_SESSION['collectivite'] = 1; // Arles |
52 |
mlimic |
522 |
// login has to be set to some value, i.e. it must not be empty, |
53 |
|
|
// and $_SESSION['justlogin'] MUST NOT be set, otherwise |
54 |
|
|
// isAuthenticated() inside of om_application will fail, and the |
55 |
|
|
// the script will stop executing |
56 |
mlimic |
577 |
$_SESSION['login'] = "admin"; |
57 |
mlimic |
515 |
// Genere une connexion a la base de donnees |
58 |
|
|
$this->f = new utils("nohtml"); |
59 |
mlimic |
522 |
// set the type of the DB to use |
60 |
mlimic |
515 |
$this->f->db->phptype = 'pgsql'; |
61 |
mlimic |
522 |
// save the reference to DB |
62 |
mlimic |
540 |
$this->db = $this->f->db; |
63 |
|
|
$this->msg = ''; |
64 |
mlimic |
515 |
}// fin constructeur |
65 |
|
|
|
66 |
mlimic |
522 |
|
67 |
|
|
/* |
68 |
|
|
* Destructor |
69 |
|
|
* |
70 |
|
|
* Unsets the instance of the utils class. |
71 |
|
|
* Unsets in the $_SESSION the key=>value pairs that were set in |
72 |
|
|
* the constructor. |
73 |
|
|
*/ |
74 |
mlimic |
526 |
protected function __destruct() { |
75 |
mlimic |
515 |
unset($this->f); |
76 |
|
|
unset($_SESSION['login']); |
77 |
|
|
unset($_SESSION['collectivite']); |
78 |
|
|
} |
79 |
|
|
|
80 |
mlimic |
522 |
|
81 |
|
|
/* |
82 |
|
|
* Checks that the data received in the request is valid. |
83 |
|
|
* |
84 |
|
|
* To be overridden in the derived classes. |
85 |
|
|
* @return bool Always return true. |
86 |
|
|
*/ |
87 |
|
|
protected function requestValid() { |
88 |
mlimic |
515 |
// A etre implemente dans les classes derivees |
89 |
|
|
return true; |
90 |
|
|
} |
91 |
|
|
|
92 |
mlimic |
522 |
|
93 |
|
|
/* |
94 |
|
|
* Filters out the HTML tags from a string. |
95 |
|
|
* |
96 |
|
|
* @param string $var The string from which to filter out the HTML tags. |
97 |
|
|
* @return string Returns $var without the HTML tags. |
98 |
|
|
*/ |
99 |
|
|
private function filterOutHtmlTags($var) { |
100 |
mlimic |
515 |
$pattern = '/<[\/]*[\sa-zA-Z0-9="]*[\/]*>/'; |
101 |
|
|
$replacement = ''; |
102 |
|
|
return preg_replace($pattern, $replacement, $var); |
103 |
|
|
} |
104 |
|
|
|
105 |
mlimic |
522 |
|
106 |
|
|
/* |
107 |
|
|
* Executes the insertion of a record into DB by calling the |
108 |
|
|
* dbform::ajouter() method. |
109 |
|
|
* |
110 |
|
|
* Precondition: $this->metier_instance is not null. |
111 |
|
|
* |
112 |
|
|
* @uses dbform::ajouter() The method that does insertion of data |
113 |
|
|
* into a DB |
114 |
|
|
* @return string Returns 'OK' on success, error message set by the |
115 |
|
|
* application (if it was set), a generic error message otherwise. |
116 |
|
|
*/ |
117 |
|
|
protected function ajouter(&$data) { |
118 |
mlimic |
515 |
// essai d'ajout des donnees dans la base de donnees |
119 |
|
|
$this->metier_instance->ajouter($data, $this->db, $this->DEBUG); |
120 |
|
|
if ($this->metier_instance->correct == false) { |
121 |
|
|
if (isset($this->metier_instance->msg) |
122 |
|
|
&& !empty($this->metier_instance->msg)) { |
123 |
|
|
return $this->filterOutHtmlTags($this->metier_instance->msg); |
124 |
|
|
} |
125 |
|
|
return $this->GENERIC_ERROR; |
126 |
|
|
} |
127 |
|
|
return $this->OK; |
128 |
|
|
} |
129 |
|
|
|
130 |
mlimic |
522 |
|
131 |
|
|
/* |
132 |
|
|
* Executes the update of a record in DB by calling the |
133 |
|
|
* dbform::modifier() method. |
134 |
|
|
* |
135 |
|
|
* Precondition: $this->metier_instance is not null. |
136 |
|
|
* |
137 |
|
|
* @uses dbform::modifier() The method that does insertion of data |
138 |
|
|
* into a DB |
139 |
|
|
* @return string Returns 'OK' on success, error message set by the |
140 |
|
|
* application (if it was set), a generic error message otherwise. |
141 |
|
|
*/ |
142 |
mlimic |
573 |
protected function modifier($data, $msg_OK = null, $msg_KO = null) { |
143 |
mlimic |
515 |
// essai de la modification des donnees dans la base de donnees |
144 |
|
|
$ret = $this->metier_instance->modifier($data, $this->db, $this->DEBUG); |
145 |
|
|
if ($this->metier_instance->correct == false) { |
146 |
|
|
if (isset($this->metier_instance->msg) |
147 |
|
|
&& !empty($this->metier_instance->msg)) { |
148 |
mlimic |
573 |
$this->setMessage($this->metier_instance->msg); |
149 |
|
|
return $this->KO; |
150 |
mlimic |
515 |
} |
151 |
mlimic |
573 |
$this->setMessage($msg_KO); |
152 |
|
|
return $this->KO; |
153 |
mlimic |
515 |
} |
154 |
mlimic |
573 |
$this->setMessage($msg_OK); |
155 |
mlimic |
515 |
return $this->OK; |
156 |
|
|
} |
157 |
|
|
|
158 |
|
|
|
159 |
mlimic |
522 |
/* |
160 |
|
|
* Executes the insertion of a record into DB by calling the |
161 |
|
|
* dbform::supprimer() method. |
162 |
|
|
* |
163 |
|
|
* Precondition: $this->metier_instance is not null. |
164 |
|
|
* |
165 |
|
|
* @uses dbform::supprimer() The method that does insertion of data |
166 |
|
|
* into a DB |
167 |
|
|
* @return string Returns 'OK' on success, error message set by the |
168 |
|
|
* application (if it was set), a generic error message otherwise. |
169 |
|
|
*/ |
170 |
|
|
protected function supprimer(&$data) { |
171 |
mlimic |
515 |
// essai de la suppression des donnes dans la base de donnees |
172 |
|
|
$ret = $this->metier_instance->supprimer($data, $this->db, $this->DEBUG); |
173 |
|
|
if ($this->metier_instance->correct == false) { |
174 |
|
|
if (isset($this->metier_instance->msg) |
175 |
|
|
&& !empty($this->metier_instance->msg)) { |
176 |
|
|
return $this->filterOutHtmlTags($this->metier_instance->msg); |
177 |
|
|
} |
178 |
|
|
return $this->GENERIC_ERROR; |
179 |
|
|
} |
180 |
|
|
return $this->OK; |
181 |
|
|
} |
182 |
|
|
|
183 |
|
|
/* |
184 |
mlimic |
522 |
* This function is to be overwritten by the derived classes when a GET |
185 |
|
|
* request is received. |
186 |
|
|
* |
187 |
|
|
* @return bool Always returns 'KO' to indicate that by default, |
188 |
|
|
* extraction of data from the DB is not supported. |
189 |
mlimic |
515 |
*/ |
190 |
mlimic |
522 |
protected function extraire(&$data) { |
191 |
|
|
// |
192 |
mlimic |
515 |
return $this->KO; |
193 |
|
|
} |
194 |
|
|
|
195 |
mlimic |
540 |
|
196 |
mlimic |
541 |
/* |
197 |
|
|
* Formats error and debug info messages created on DB failure. |
198 |
|
|
* @param string $debuginfo The debug info returned from DB connection. |
199 |
|
|
* @param string $message The message returned from the failed DB attempt. |
200 |
|
|
* @return string Formatted error message. |
201 |
|
|
*/ |
202 |
mlimic |
540 |
protected function formatDBerror($debuginfo, $message) { |
203 |
|
|
return "DEBUG INFO: ".$debuginfo." DEBUG MESSAGE: ".$message; |
204 |
|
|
} |
205 |
mlimic |
515 |
|
206 |
mlimic |
541 |
|
207 |
|
|
/* |
208 |
|
|
* Sets the $msg attribute. Usefult for returning a message supplementary |
209 |
|
|
* to the one associated with the return code. |
210 |
|
|
* @param string $msg The message. |
211 |
|
|
*/ |
212 |
mlimic |
540 |
protected function setMessage($msg) { |
213 |
|
|
if ($msg) { |
214 |
|
|
$this->msg = $msg; |
215 |
|
|
} |
216 |
|
|
} |
217 |
|
|
|
218 |
mlimic |
541 |
|
219 |
|
|
/* |
220 |
|
|
* Returns the string stored in $msg attribute |
221 |
|
|
* @return string The value of $this->msg. |
222 |
|
|
*/ |
223 |
mlimic |
540 |
public function getMessage() { |
224 |
|
|
return $this->msg; |
225 |
|
|
} |
226 |
|
|
|
227 |
|
|
|
228 |
mlimic |
541 |
/* |
229 |
|
|
* Verifies the result object returned from a DB query for any |
230 |
|
|
* errors. If there was a DB error, the error message is stored |
231 |
|
|
* in the $msg attribute. |
232 |
|
|
* @param object $result DB result object. |
233 |
|
|
* @param string $msg The prefix to the constructed DB error message. |
234 |
|
|
* @return bool True on DB error, false otherwise. |
235 |
|
|
*/ |
236 |
|
|
protected function checkDBError($result, $msg = '') { |
237 |
mlimic |
540 |
if (database::isError($result, true)) { |
238 |
mlimic |
564 |
$this->addToLog("Error: $msg ". |
239 |
|
|
$this->formatDBerror($result->getDebugInfo(), |
240 |
|
|
$result->getMessage())); |
241 |
|
|
$this->setMessage('ERREUR DE LA BD : '. $msg); |
242 |
mlimic |
540 |
return true; |
243 |
|
|
} |
244 |
|
|
return false; |
245 |
|
|
} |
246 |
|
|
|
247 |
mlimic |
564 |
|
248 |
|
|
/* |
249 |
|
|
* Adds errors to log |
250 |
|
|
* @param string $message The message to add to log |
251 |
|
|
* @param string $type The logging mode |
252 |
|
|
* @todo what do we do with DB error messages if a REST call is made |
253 |
|
|
*/ |
254 |
|
|
function addToLog($message, $type = DEBUG_MODE) { |
255 |
|
|
// |
256 |
|
|
if (!defined('REST_REQUEST')) { |
257 |
|
|
logger::instance()->log("class ".get_class($this)." - ".$message, $type); |
258 |
|
|
} |
259 |
|
|
} |
260 |
mlimic |
577 |
|
261 |
|
|
|
262 |
|
|
|
263 |
|
|
/* |
264 |
|
|
* Verifies that a date falls inside of a date interval |
265 |
|
|
* @param string $date_str The string that should fall |
266 |
|
|
* within the interval |
267 |
|
|
* @param string $date_start_str The begining of the interval |
268 |
|
|
* @param string $date_end_str The end of the interval |
269 |
|
|
* @return book true if $date_str is found inside of the |
270 |
|
|
* interval, false otherwise |
271 |
|
|
*/ |
272 |
|
|
protected function dateInsideInterval($date_str, $date_start_str = null, |
273 |
|
|
$date_end_str = null) { |
274 |
|
|
$dates_str = array($date_start_str, $date_str, $date_end_str); |
275 |
|
|
if (count($dates_str) == 1) { |
276 |
|
|
return true; |
277 |
|
|
} |
278 |
|
|
$dates = array(); |
279 |
|
|
$prev_date = -1; |
280 |
|
|
for ($i = 0; $i < 3; $i++) { |
281 |
|
|
if ($dates_str[$i] == null) { |
282 |
|
|
$dates[] = null; |
283 |
|
|
continue; |
284 |
|
|
} |
285 |
|
|
$d = explode('/', $dates_str[$i]); |
286 |
|
|
$date = strtotime($d[2].'-'.$d[1].'-'.$d[0]); |
287 |
|
|
if ($i > 0 && $date < $prev_date) { |
288 |
|
|
return false; |
289 |
|
|
} |
290 |
|
|
$prev_date = $date; |
291 |
|
|
} |
292 |
|
|
return true; |
293 |
|
|
} |
294 |
|
|
|
295 |
mlimic |
564 |
|
296 |
mlimic |
515 |
}// fin classe |
297 |
|
|
?> |