1 |
<?php |
2 |
|
3 |
/* |
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 |
|
15 |
|
16 |
|
17 |
require_once ("../obj/utils.class.php"); |
18 |
require_once ("../obj/om_utilisateur.class.php"); |
19 |
|
20 |
class MetierManager { |
21 |
|
22 |
|
23 |
/**#@+ |
24 |
* @access protected |
25 |
* @var string |
26 |
*/ |
27 |
var $KO = 'KO'; |
28 |
var $OK = 'OK'; |
29 |
var $NA = 'NA'; // not apllicable |
30 |
var $DEBUG = 0; |
31 |
var $GENERIC_ERROR = "Error during request processing"; |
32 |
|
33 |
/* |
34 |
* Constructeur. |
35 |
* |
36 |
* Sets 'collectivite', and 'login' in the $_SESSION. |
37 |
* @todo The value stored in the $_SESSION['collectivite'] needs to |
38 |
* be obtained rather than hardcoded. |
39 |
* @todo The value stored in the $_SESSION['login'] could be obtained |
40 |
* rather than being hardcoded to "somelogin". |
41 |
* @uses utils Uses the obj/utils.class.php to establish a DB connection. |
42 |
* @uses application::connectDatabase() In this function the execution |
43 |
* could terminate if the connection to DB cannot be established. The |
44 |
* important thing to prevent this is to set the $_SESSION['login'] to |
45 |
* value that is not empty and that is bogus. |
46 |
*/ |
47 |
protected function __construct() { |
48 |
session_start(); |
49 |
// Setter les variables de $_SESSION pour la connexion avec BD |
50 |
$_SESSION['collectivite'] = 1; // Arles |
51 |
// login has to be set to some value, i.e. it must not be empty, |
52 |
// and $_SESSION['justlogin'] MUST NOT be set, otherwise |
53 |
// isAuthenticated() inside of om_application will fail, and the |
54 |
// the script will stop executing |
55 |
$_SESSION['login'] = "somelogin"; |
56 |
// Genere une connexion a la base de donnees |
57 |
$this->f = new utils("nohtml"); |
58 |
// set the type of the DB to use |
59 |
$this->f->db->phptype = 'pgsql'; |
60 |
// save the reference to DB |
61 |
$this->db = $this->f->db; |
62 |
$this->msg = ''; |
63 |
}// fin constructeur |
64 |
|
65 |
|
66 |
/* |
67 |
* Destructor |
68 |
* |
69 |
* Unsets the instance of the utils class. |
70 |
* Unsets in the $_SESSION the key=>value pairs that were set in |
71 |
* the constructor. |
72 |
*/ |
73 |
protected function __destruct() { |
74 |
unset($this->f); |
75 |
unset($_SESSION['login']); |
76 |
unset($_SESSION['collectivite']); |
77 |
} |
78 |
|
79 |
|
80 |
/* |
81 |
* Checks that the data received in the request is valid. |
82 |
* |
83 |
* To be overridden in the derived classes. |
84 |
* @return bool Always return true. |
85 |
*/ |
86 |
protected function requestValid() { |
87 |
// A etre implemente dans les classes derivees |
88 |
return true; |
89 |
} |
90 |
|
91 |
|
92 |
/* |
93 |
* Filters out the HTML tags from a string. |
94 |
* |
95 |
* @param string $var The string from which to filter out the HTML tags. |
96 |
* @return string Returns $var without the HTML tags. |
97 |
*/ |
98 |
private function filterOutHtmlTags($var) { |
99 |
$pattern = '/<[\/]*[\sa-zA-Z0-9="]*[\/]*>/'; |
100 |
$replacement = ''; |
101 |
return preg_replace($pattern, $replacement, $var); |
102 |
} |
103 |
|
104 |
|
105 |
/* |
106 |
* Executes the insertion of a record into DB by calling the |
107 |
* dbform::ajouter() method. |
108 |
* |
109 |
* Precondition: $this->metier_instance is not null. |
110 |
* |
111 |
* @uses dbform::ajouter() The method that does insertion of data |
112 |
* into a DB |
113 |
* @return string Returns 'OK' on success, error message set by the |
114 |
* application (if it was set), a generic error message otherwise. |
115 |
*/ |
116 |
protected function ajouter(&$data) { |
117 |
// essai d'ajout des donnees dans la base de donnees |
118 |
$this->metier_instance->ajouter($data, $this->db, $this->DEBUG); |
119 |
if ($this->metier_instance->correct == false) { |
120 |
if (isset($this->metier_instance->msg) |
121 |
&& !empty($this->metier_instance->msg)) { |
122 |
return $this->filterOutHtmlTags($this->metier_instance->msg); |
123 |
} |
124 |
return $this->GENERIC_ERROR; |
125 |
} |
126 |
return $this->OK; |
127 |
} |
128 |
|
129 |
|
130 |
/* |
131 |
* Executes the update of a record in DB by calling the |
132 |
* dbform::modifier() method. |
133 |
* |
134 |
* Precondition: $this->metier_instance is not null. |
135 |
* |
136 |
* @uses dbform::modifier() The method that does insertion of data |
137 |
* into a DB |
138 |
* @return string Returns 'OK' on success, error message set by the |
139 |
* application (if it was set), a generic error message otherwise. |
140 |
*/ |
141 |
protected function modifier(&$data) { |
142 |
// essai de la modification des donnees dans la base de donnees |
143 |
$ret = $this->metier_instance->modifier($data, $this->db, $this->DEBUG); |
144 |
if ($this->metier_instance->correct == false) { |
145 |
if (isset($this->metier_instance->msg) |
146 |
&& !empty($this->metier_instance->msg)) { |
147 |
return $this->filterOutHtmlTags($this->metier_instance->msg); |
148 |
} |
149 |
return $this->GENERIC_ERROR; |
150 |
} |
151 |
return $this->OK; |
152 |
} |
153 |
|
154 |
|
155 |
/* |
156 |
* Executes the insertion of a record into DB by calling the |
157 |
* dbform::supprimer() method. |
158 |
* |
159 |
* Precondition: $this->metier_instance is not null. |
160 |
* |
161 |
* @uses dbform::supprimer() The method that does insertion of data |
162 |
* into a DB |
163 |
* @return string Returns 'OK' on success, error message set by the |
164 |
* application (if it was set), a generic error message otherwise. |
165 |
*/ |
166 |
protected function supprimer(&$data) { |
167 |
// essai de la suppression des donnes dans la base de donnees |
168 |
$ret = $this->metier_instance->supprimer($data, $this->db, $this->DEBUG); |
169 |
if ($this->metier_instance->correct == false) { |
170 |
if (isset($this->metier_instance->msg) |
171 |
&& !empty($this->metier_instance->msg)) { |
172 |
return $this->filterOutHtmlTags($this->metier_instance->msg); |
173 |
} |
174 |
return $this->GENERIC_ERROR; |
175 |
} |
176 |
return $this->OK; |
177 |
} |
178 |
|
179 |
/* |
180 |
* This function is to be overwritten by the derived classes when a GET |
181 |
* request is received. |
182 |
* |
183 |
* @return bool Always returns 'KO' to indicate that by default, |
184 |
* extraction of data from the DB is not supported. |
185 |
*/ |
186 |
protected function extraire(&$data) { |
187 |
// |
188 |
return $this->KO; |
189 |
} |
190 |
|
191 |
|
192 |
/* |
193 |
* Formats error and debug info messages created on DB failure. |
194 |
* @param string $debuginfo The debug info returned from DB connection. |
195 |
* @param string $message The message returned from the failed DB attempt. |
196 |
* @return string Formatted error message. |
197 |
*/ |
198 |
protected function formatDBerror($debuginfo, $message) { |
199 |
return "DEBUG INFO: ".$debuginfo." DEBUG MESSAGE: ".$message; |
200 |
} |
201 |
|
202 |
|
203 |
/* |
204 |
* Sets the $msg attribute. Usefult for returning a message supplementary |
205 |
* to the one associated with the return code. |
206 |
* @param string $msg The message. |
207 |
*/ |
208 |
protected function setMessage($msg) { |
209 |
if ($msg) { |
210 |
$this->msg = $msg; |
211 |
} |
212 |
} |
213 |
|
214 |
|
215 |
/* |
216 |
* Returns the string stored in $msg attribute |
217 |
* @return string The value of $this->msg. |
218 |
*/ |
219 |
public function getMessage() { |
220 |
return $this->msg; |
221 |
} |
222 |
|
223 |
|
224 |
/* |
225 |
* Verifies the result object returned from a DB query for any |
226 |
* errors. If there was a DB error, the error message is stored |
227 |
* in the $msg attribute. |
228 |
* @param object $result DB result object. |
229 |
* @param string $msg The prefix to the constructed DB error message. |
230 |
* @return bool True on DB error, false otherwise. |
231 |
*/ |
232 |
protected function checkDBError($result, $msg = '') { |
233 |
if (database::isError($result, true)) { |
234 |
$this->setMessage($msg.$this->formatDBerror($result->getDebugInfo(), |
235 |
$result->getMessage())); |
236 |
return true; |
237 |
} |
238 |
return false; |
239 |
} |
240 |
|
241 |
}// fin classe |
242 |
?> |