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 |
|
|
require_once ("../obj/om_utilisateur.class.php"); |
19 |
|
|
|
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 |
515 |
$_SESSION['login'] = "somelogin"; |
57 |
|
|
// 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 |
|
|
protected function modifier(&$data) { |
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 |
|
|
return $this->filterOutHtmlTags($this->metier_instance->msg); |
149 |
|
|
} |
150 |
|
|
return $this->GENERIC_ERROR; |
151 |
|
|
} |
152 |
|
|
return $this->OK; |
153 |
|
|
} |
154 |
|
|
|
155 |
|
|
|
156 |
mlimic |
522 |
/* |
157 |
|
|
* Executes the insertion of a record into DB by calling the |
158 |
|
|
* dbform::supprimer() method. |
159 |
|
|
* |
160 |
|
|
* Precondition: $this->metier_instance is not null. |
161 |
|
|
* |
162 |
|
|
* @uses dbform::supprimer() The method that does insertion of data |
163 |
|
|
* into a DB |
164 |
|
|
* @return string Returns 'OK' on success, error message set by the |
165 |
|
|
* application (if it was set), a generic error message otherwise. |
166 |
|
|
*/ |
167 |
|
|
protected function supprimer(&$data) { |
168 |
mlimic |
515 |
// essai de la suppression des donnes dans la base de donnees |
169 |
|
|
$ret = $this->metier_instance->supprimer($data, $this->db, $this->DEBUG); |
170 |
|
|
if ($this->metier_instance->correct == false) { |
171 |
|
|
if (isset($this->metier_instance->msg) |
172 |
|
|
&& !empty($this->metier_instance->msg)) { |
173 |
|
|
return $this->filterOutHtmlTags($this->metier_instance->msg); |
174 |
|
|
} |
175 |
|
|
return $this->GENERIC_ERROR; |
176 |
|
|
} |
177 |
|
|
return $this->OK; |
178 |
|
|
} |
179 |
|
|
|
180 |
|
|
/* |
181 |
mlimic |
522 |
* This function is to be overwritten by the derived classes when a GET |
182 |
|
|
* request is received. |
183 |
|
|
* |
184 |
|
|
* @return bool Always returns 'KO' to indicate that by default, |
185 |
|
|
* extraction of data from the DB is not supported. |
186 |
mlimic |
515 |
*/ |
187 |
mlimic |
522 |
protected function extraire(&$data) { |
188 |
|
|
// |
189 |
mlimic |
515 |
return $this->KO; |
190 |
|
|
} |
191 |
|
|
|
192 |
mlimic |
540 |
|
193 |
mlimic |
541 |
/* |
194 |
|
|
* Formats error and debug info messages created on DB failure. |
195 |
|
|
* @param string $debuginfo The debug info returned from DB connection. |
196 |
|
|
* @param string $message The message returned from the failed DB attempt. |
197 |
|
|
* @return string Formatted error message. |
198 |
|
|
*/ |
199 |
mlimic |
540 |
protected function formatDBerror($debuginfo, $message) { |
200 |
|
|
return "DEBUG INFO: ".$debuginfo." DEBUG MESSAGE: ".$message; |
201 |
|
|
} |
202 |
mlimic |
515 |
|
203 |
mlimic |
541 |
|
204 |
|
|
/* |
205 |
|
|
* Sets the $msg attribute. Usefult for returning a message supplementary |
206 |
|
|
* to the one associated with the return code. |
207 |
|
|
* @param string $msg The message. |
208 |
|
|
*/ |
209 |
mlimic |
540 |
protected function setMessage($msg) { |
210 |
|
|
if ($msg) { |
211 |
|
|
$this->msg = $msg; |
212 |
|
|
} |
213 |
|
|
} |
214 |
|
|
|
215 |
mlimic |
541 |
|
216 |
|
|
/* |
217 |
|
|
* Returns the string stored in $msg attribute |
218 |
|
|
* @return string The value of $this->msg. |
219 |
|
|
*/ |
220 |
mlimic |
540 |
public function getMessage() { |
221 |
|
|
return $this->msg; |
222 |
|
|
} |
223 |
|
|
|
224 |
|
|
|
225 |
mlimic |
541 |
/* |
226 |
|
|
* Verifies the result object returned from a DB query for any |
227 |
|
|
* errors. If there was a DB error, the error message is stored |
228 |
|
|
* in the $msg attribute. |
229 |
|
|
* @param object $result DB result object. |
230 |
|
|
* @param string $msg The prefix to the constructed DB error message. |
231 |
|
|
* @return bool True on DB error, false otherwise. |
232 |
|
|
*/ |
233 |
|
|
protected function checkDBError($result, $msg = '') { |
234 |
mlimic |
540 |
if (database::isError($result, true)) { |
235 |
mlimic |
564 |
$this->addToLog("Error: $msg ". |
236 |
|
|
$this->formatDBerror($result->getDebugInfo(), |
237 |
|
|
$result->getMessage())); |
238 |
|
|
$this->setMessage('ERREUR DE LA BD : '. $msg); |
239 |
mlimic |
540 |
return true; |
240 |
|
|
} |
241 |
|
|
return false; |
242 |
|
|
} |
243 |
|
|
|
244 |
mlimic |
564 |
|
245 |
|
|
/* |
246 |
|
|
* Adds errors to log |
247 |
|
|
* @param string $message The message to add to log |
248 |
|
|
* @param string $type The logging mode |
249 |
|
|
* @todo what do we do with DB error messages if a REST call is made |
250 |
|
|
*/ |
251 |
|
|
function addToLog($message, $type = DEBUG_MODE) { |
252 |
|
|
// |
253 |
|
|
if (!defined('REST_REQUEST')) { |
254 |
|
|
logger::instance()->log("class ".get_class($this)." - ".$message, $type); |
255 |
|
|
} |
256 |
|
|
} |
257 |
|
|
|
258 |
mlimic |
515 |
}// fin classe |
259 |
|
|
?> |