/[openfoncier]/trunk/services/metier/metiermanager.php
ViewVC logotype

Contents of /trunk/services/metier/metiermanager.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 573 - (show annotations)
Wed Oct 31 16:23:31 2012 UTC (12 years, 3 months ago) by mlimic
File size: 8519 byte(s)
ticket 76 Version II - The consultation class is used
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 $BAD_DATA = 'BAD_DATA';
31 var $DEBUG = 0;
32 var $GENERIC_ERROR = "Error during request processing";
33
34 /*
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 protected function __construct() {
49 session_start();
50 // Setter les variables de $_SESSION pour la connexion avec BD
51 $_SESSION['collectivite'] = 1; // Arles
52 // 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 $_SESSION['login'] = "somelogin";
57 // Genere une connexion a la base de donnees
58 $this->f = new utils("nohtml");
59 // set the type of the DB to use
60 $this->f->db->phptype = 'pgsql';
61 // save the reference to DB
62 $this->db = $this->f->db;
63 $this->msg = '';
64 }// fin constructeur
65
66
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 protected function __destruct() {
75 unset($this->f);
76 unset($_SESSION['login']);
77 unset($_SESSION['collectivite']);
78 }
79
80
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 // A etre implemente dans les classes derivees
89 return true;
90 }
91
92
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 $pattern = '/<[\/]*[\sa-zA-Z0-9="]*[\/]*>/';
101 $replacement = '';
102 return preg_replace($pattern, $replacement, $var);
103 }
104
105
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 // 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
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, $msg_OK = null, $msg_KO = null) {
143 // 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 $this->setMessage($this->metier_instance->msg);
149 return $this->KO;
150 }
151 $this->setMessage($msg_KO);
152 return $this->KO;
153 }
154 $this->setMessage($msg_OK);
155 return $this->OK;
156 }
157
158
159 /*
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 // 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 * 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 */
190 protected function extraire(&$data) {
191 //
192 return $this->KO;
193 }
194
195
196 /*
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 protected function formatDBerror($debuginfo, $message) {
203 return "DEBUG INFO: ".$debuginfo." DEBUG MESSAGE: ".$message;
204 }
205
206
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 protected function setMessage($msg) {
213 if ($msg) {
214 $this->msg = $msg;
215 }
216 }
217
218
219 /*
220 * Returns the string stored in $msg attribute
221 * @return string The value of $this->msg.
222 */
223 public function getMessage() {
224 return $this->msg;
225 }
226
227
228 /*
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 if (database::isError($result, true)) {
238 $this->addToLog("Error: $msg ".
239 $this->formatDBerror($result->getDebugInfo(),
240 $result->getMessage()));
241 $this->setMessage('ERREUR DE LA BD : '. $msg);
242 return true;
243 }
244 return false;
245 }
246
247
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
261 }// fin classe
262 ?>

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26