- Run action methods using run_action_method()
[isso.git] / api.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Iris Studios Shared Object Framework [#]version[#]
5 || # Copyright ©2002-[#]year[#] Iris Studios, Inc.
6 || #
7 || # This program is free software; you can redistribute it and/or modify
8 || # it under the terms of the GNU General Public License as published by
9 || # the Free Software Foundation; version [#]gpl[#] of the License.
10 || #
11 || # This program is distributed in the hope that it will be useful, but
12 || # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 || # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 || # more details.
15 || #
16 || # You should have received a copy of the GNU General Public License along
17 || # with this program; if not, write to the Free Software Foundation, Inc.,
18 || # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 || ###################################################################
20 \*=====================================================================*/
21
22 /**
23 * Abstract Datamanger API
24 * api.php
25 *
26 * @package ISSO
27 */
28
29 if (!defined('REQ_YES'))
30 {
31 /**
32 * Yes, required
33 */
34 define('REQ_YES', 1);
35
36 /**
37 * No, not required
38 */
39 define('REQ_NO', 0);
40
41 /**
42 * Auto-increasing value
43 */
44 define('REQ_AUTO', -1);
45
46 /**
47 * Set by a cusotm set_*() function
48 */
49 define('REQ_SET', 2);
50
51 /**
52 * Index for cleaning type
53 */
54 define('F_TYPE', 0);
55
56 /**
57 * Index for requirement type
58 */
59 define('F_REQ', 1);
60
61 /**
62 * Index for verification type
63 */
64 define('F_VERIFY', 2);
65 }
66
67 /**
68 * Abstract API
69 *
70 * Abstract class that is used as an API base for most common database interaction
71 * schemes. Creates a simple structure that holds data and can update, delete, and
72 * insert.
73 *
74 * @author Iris Studios, Inc.
75 * @copyright Copyright ©2002 - [#]year[#], Iris Studios, Inc.
76 * @version $Revision$
77 * @package ISSO
78 *
79 */
80 class API
81 {
82 /**
83 * Registry object
84 * @var object
85 */
86 var $registry = null;
87
88 /**
89 * Fields: used for verification and sanitization
90 * NAME => array(TYPE, REQUIRED, VERIFY METHOD (:self for self-named method))
91 * @var array
92 */
93 var $fields = array();
94
95 /**
96 * Values array: sanitized and verified field values
97 * @var array
98 */
99 var $values = array();
100
101 /**
102 * Fields that were manually set with set(), not by using set_existing()
103 * @var array
104 */
105 var $setfields = array();
106
107 /**
108 * WHERE condition
109 * @var string
110 */
111 var $condition = '';
112
113 /**
114 * The object table row; a fetched row that represents this instance
115 * @var array
116 */
117 var $objdata = array();
118
119 /**
120 * Insert ID from the insert() command
121 * @var integer
122 */
123 var $insertid = 0;
124
125 /**
126 * Pre- and post-action method stoppers
127 * @var array
128 */
129 var $norunners = array();
130
131 /**
132 * Constructor: cannot instantiate class directly
133 */
134 function API(&$registry)
135 {
136 if (!is_subclass_of($this, 'API'))
137 {
138 trigger_error('Cannot instantiate the API module directly', E_USER_ERROR);
139 }
140
141 if (!is_object($registry))
142 {
143 trigger_error('The passed registry is not an object', E_USER_ERROR);
144 }
145
146 $this->registry =& $registry;
147 }
148
149 /**
150 * Sets a value, sanitizes it, and verifies it
151 *
152 * @param string Field name
153 * @param mixed Value
154 * @param bool Do clean?
155 * @param bool Do verify?
156 */
157 function set($field, $value, $doclean = true, $doverify = true)
158 {
159 if (!isset($this->fields["$field"]))
160 {
161 trigger_error('Field `' . $field . '` is not valid', E_USER_WARNING);
162 return;
163 }
164
165 $this->values["$field"] = ($doclean ? $this->registry->clean($value, $this->fields["$field"][F_TYPE]) : $value);
166
167 $this->setfields[] = $field;
168
169 if (isset($this->fields["$field"][F_VERIFY]) AND $doverify)
170 {
171 if ($this->fields["$field"][F_VERIFY] == ':self')
172 {
173 $verify = $this->{"verify_$field"}($field);
174 }
175 else
176 {
177 $verify = $this->{$this->fields["$field"][F_VERIFY]}($field);
178 }
179
180 if (!$verify)
181 {
182 // <ERR:CVT>
183 trigger_error('Validation of `' . $field . '` failed', E_USER_ERROR);
184 }
185 }
186 }
187
188 /**
189 * Sets the condition to use in the WHERE clause; if not passed, then it calculates it
190 * from the REQ_AUTO field
191 *
192 * @param string WHERE conditional bit
193 */
194 function set_condition($condition = '')
195 {
196 if ($condition != '')
197 {
198 $this->condition = $condition;
199 }
200 else
201 {
202 foreach ($this->fields AS $name => $options)
203 {
204 if ($options[F_REQ] == REQ_AUTO)
205 {
206 if (!$this->values["$name"])
207 {
208 trigger_error('Cannot determine condition from the REQ_AUTO field because it is not set', E_USER_WARNING);
209 continue;
210 }
211
212 $this->condition = "$name = " . (($options[F_TYPE] == TYPE_NOCLEAN OR $options[F_TYPE] == TYPE_STR) ? "'" . $this->values["$name"] . "'" : $this->values["$name"]);
213 }
214 }
215
216 if ($this->condition == '')
217 {
218 trigger_error('No REQ_AUTO fields are present and therefore the condition cannot be created', E_USER_WARNING);
219 }
220 }
221 }
222
223 /**
224 * Sets existing data into $values where it's not already present
225 */
226 function set_existing()
227 {
228 static $run;
229 if ($run)
230 {
231 return;
232 }
233
234 $this->fetch();
235
236 foreach ($this->objdata AS $field => $value)
237 {
238 if (!isset($this->values["$field"]))
239 {
240 $this->values["$field"] = $value;
241 }
242 }
243
244 $run = true;
245 }
246
247 /**
248 * Fetches a record based on the condition
249 */
250 function fetch()
251 {
252 if ($this->condition == '')
253 {
254 trigger_error('Condition is empty: cannot fetch', E_USER_ERROR);
255 }
256
257 $this->run_action_method('pre_fetch');
258
259 $result = $this->registry->modules['db_mysql']->query_first("SELECT * FROM {$this->prefix}{$this->table} WHERE {$this->condition}");
260 if (!$result)
261 {
262 // <ERR:CVT>
263 trigger_error('No record was returned', E_USER_ERROR);
264 return;
265 }
266
267 $this->run_action_method('post_fetch');
268
269 $this->objdata = $result;
270 }
271
272 /**
273 * Inserts a record in the database
274 */
275 function insert()
276 {
277 $this->verify();
278
279 $this->run_action_method('pre_insert');
280
281 foreach ($this->setfields AS $field)
282 {
283 $fields[] = $field;
284 $values[] = (($this->fields["$field"][F_TYPE] == TYPE_NOCLEAN OR $this->fields["$field"][F_TYPE] == TYPE_STR) ? "'" . $this->values["$field"] . "'" : $this->values["$field"]);
285 }
286
287 $this->registry->modules['db_mysql']->query("INSERT INTO {$this->prefix}{$this->table} (" . implode(',', $fields) . ") VALUES (" . implode(',', $values) . ")");
288 $this->insertid = $this->registry->modules['db_mysql']->insert_id();
289
290 $this->run_action_method('post_insert');
291 }
292
293 /**
294 * Updates a record in the database using the data in $vaues
295 */
296 function update()
297 {
298 if ($this->condition == '')
299 {
300 trigger_error('Condition is empty: cannot fetch', E_USER_ERROR);
301 }
302
303 $this->run_action_method('pre_update');
304
305 foreach ($this->setfields AS $field)
306 {
307 $updates[] = "$field = " . (($this->fields["$field"][F_TYPE] == TYPE_NOCLEAN OR $this->fields["$field"][F_TYPE] == TYPE_STR) ? "'" . $this->values["$field"] . "'" : $this->values["$field"]);
308 }
309 $updates = implode(', ', $updates);
310
311 $this->registry->modules['db_mysql']->query("UPDATE {$this->prefix}{$this->table} SET $updates WHERE {$this->condition}");
312
313 $this->run_action_method('post_update');
314 }
315
316 /**
317 * Deletes a record
318 */
319 function delete()
320 {
321 if ($this->condition == '')
322 {
323 trigger_error('Condition is empty: cannot fetch', E_USER_ERROR);
324 }
325
326 $this->set_existing();
327
328 $this->run_action_method('pre_delete');
329
330 $this->registry->modules['db_mysql']->query("DELETE FROM {$this->prefix}{$this->table} WHERE {$this->condition}");
331
332 $this->run_action_method('post_delete');
333 }
334
335 /**
336 * Verifies that all required fields are set
337 */
338 function verify()
339 {
340 foreach ($this->fields AS $name => $options)
341 {
342 if ($options[F_REQ] == REQ_YES)
343 {
344 if (!isset($this->values["$name"]))
345 {
346 // <ERR:CVT>
347 trigger_error('Field `' . $name . '` was not set', E_USER_ERROR);
348 }
349 }
350 else if ($options[F_REQ] == REQ_SET)
351 {
352 $this->{"set_$name"}();
353 }
354 }
355 }
356
357 /**
358 * Runs a pre- or post-action method for database commands
359 *
360 * @param string Action to run
361 */
362 function run_action_method($method)
363 {
364 if (in_array($method, $this->norunners))
365 {
366 return;
367 }
368
369 $actmethod = (method_exists($this, $method) ? $this->$method() : '');
370 }
371
372 /**
373 * Verify field: not a zero value
374 */
375 function verify_nozero($field)
376 {
377 if ($this->values["$field"] == 0)
378 {
379 return false;
380 }
381
382 return true;
383 }
384
385 /**
386 * Verify field: not empty
387 */
388 function verify_noempty($field)
389 {
390 if (empty($this->values["$field"]))
391 {
392 return false;
393 }
394
395 return true;
396 }
397 }
398
399 /*=====================================================================*\
400 || ###################################################################
401 || # $HeadURL$
402 || # $Id$
403 || ###################################################################
404 \*=====================================================================*/
405 ?>