Added visibility information to all the functions
[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 __construct(&$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 * (PHP 4) Constructor
151 */
152 function API(&$registry)
153 {
154 $this->__construct($registry);
155 }
156
157 /**
158 * Constructs an error for the error handler to receive
159 *
160 * @param string Error message
161 */
162 function error($message)
163 {
164 call_user_func(APIError(), $message);
165 }
166
167 /**
168 * Sets a value, sanitizes it, and verifies it
169 *
170 * @access public
171 *
172 * @param string Field name
173 * @param mixed Value
174 * @param bool Do clean?
175 * @param bool Do verify?
176 */
177 function set($field, $value, $doclean = true, $doverify = true)
178 {
179 if (!isset($this->fields["$field"]))
180 {
181 trigger_error('Field `' . $field . '` is not valid', E_USER_WARNING);
182 return;
183 }
184
185 $this->values["$field"] = ($doclean ? $this->registry->clean($value, $this->fields["$field"][F_TYPE]) : $value);
186
187 $this->setfields["$field"] = $field;
188
189 if (isset($this->fields["$field"][F_VERIFY]) AND $doverify)
190 {
191 if ($this->fields["$field"][F_VERIFY] == ':self')
192 {
193 $verify = $this->{"verify_$field"}($field);
194 }
195 else
196 {
197 $verify = $this->{$this->fields["$field"][F_VERIFY]}($field);
198 }
199
200 if (!$verify)
201 {
202 $this->error(sprintf($this->registry->modules['localize']->string('Validation of %1$s failed'), $field));
203 }
204 }
205 }
206
207 /**
208 * Sets the condition to use in the WHERE clause; if not passed, then it calculates it
209 * from the REQ_AUTO field
210 *
211 * @access public
212 *
213 * @param string WHERE conditional bit
214 */
215 function set_condition($condition = '')
216 {
217 if ($condition != '')
218 {
219 $this->condition = $condition;
220 }
221 else
222 {
223 foreach ($this->fields AS $name => $options)
224 {
225 if ($options[F_REQ] == REQ_AUTO)
226 {
227 if (!$this->values["$name"])
228 {
229 trigger_error('Cannot determine condition from the REQ_AUTO field because it is not set', E_USER_WARNING);
230 continue;
231 }
232
233 $this->condition = "$name = " . (($options[F_TYPE] == TYPE_NOCLEAN OR $options[F_TYPE] == TYPE_STR OR $options[F_TYPE] == TYPE_STRUN) ? "'" . $this->values["$name"] . "'" : $this->values["$name"]);
234 }
235 }
236
237 if ($this->condition == '')
238 {
239 trigger_error('No REQ_AUTO fields are present and therefore the condition cannot be created', E_USER_WARNING);
240 }
241 }
242 }
243
244 /**
245 * Sets existing data into $values where it's not already present
246 *
247 * @access public
248 */
249 function set_existing()
250 {
251 static $run;
252 if ($run)
253 {
254 return;
255 }
256
257 $this->fetch();
258
259 foreach ($this->objdata AS $field => $value)
260 {
261 if (!isset($this->values["$field"]))
262 {
263 $this->values["$field"] = $value;
264 }
265 }
266
267 $run = true;
268 }
269
270 /**
271 * Fetches a record based on the condition
272 *
273 * @access public
274 */
275 function fetch()
276 {
277 if ($this->condition == '')
278 {
279 trigger_error('Condition is empty: cannot fetch', E_USER_ERROR);
280 }
281
282 $this->run_action_method('pre_fetch');
283
284 $result = $this->registry->modules['db_mysql']->query_first("SELECT * FROM {$this->prefix}{$this->table} WHERE {$this->condition}");
285 if (!$result)
286 {
287 $this->error($this->registry->modules['localize']->string('No records were returned'));
288 return;
289 }
290
291 $this->run_action_method('post_fetch');
292
293 $this->objdata = $result;
294 }
295
296 /**
297 * Inserts a record in the database
298 *
299 * @access public
300 */
301 function insert()
302 {
303 $this->verify();
304
305 $this->run_action_method('pre_insert');
306
307 foreach ($this->setfields AS $field)
308 {
309 $fields[] = $field;
310 $values[] = (($this->fields["$field"][F_TYPE] == TYPE_NOCLEAN OR $this->fields["$field"][F_TYPE] == TYPE_STR OR $this->fields["$field"][F_TYPE] == TYPE_STRUN) ? "'" . $this->values["$field"] . "'" : $this->values["$field"]);
311 }
312
313 $this->registry->modules['db_mysql']->query("INSERT INTO {$this->prefix}{$this->table} (" . implode(',', $fields) . ") VALUES (" . implode(',', $values) . ")");
314 $this->insertid = $this->registry->modules['db_mysql']->insert_id();
315
316 $this->run_action_method('post_insert');
317 }
318
319 /**
320 * Updates a record in the database using the data in $vaues
321 *
322 * @access public
323 */
324 function update()
325 {
326 if ($this->condition == '')
327 {
328 trigger_error('Condition is empty: cannot fetch', E_USER_ERROR);
329 }
330
331 $this->run_action_method('pre_update');
332
333 foreach ($this->setfields AS $field)
334 {
335 $updates[] = "$field = " . (($this->fields["$field"][F_TYPE] == TYPE_NOCLEAN OR $this->fields["$field"][F_TYPE] == TYPE_STR OR $this->fields["$field"][F_TYPE] == TYPE_STRUN) ? "'" . $this->values["$field"] . "'" : $this->values["$field"]);
336 }
337 $updates = implode(', ', $updates);
338
339 $this->registry->modules['db_mysql']->query("UPDATE {$this->prefix}{$this->table} SET $updates WHERE {$this->condition}");
340
341 $this->run_action_method('post_update');
342 }
343
344 /**
345 * Deletes a record
346 *
347 * @access public
348 */
349 function delete()
350 {
351 if ($this->condition == '')
352 {
353 trigger_error('Condition is empty: cannot fetch', E_USER_ERROR);
354 }
355
356 $this->set_existing();
357
358 $this->run_action_method('pre_delete');
359
360 $this->registry->modules['db_mysql']->query("DELETE FROM {$this->prefix}{$this->table} WHERE {$this->condition}");
361
362 $this->run_action_method('post_delete');
363 }
364
365 /**
366 * Verifies that all required fields are set
367 *
368 * @access private
369 */
370 function verify()
371 {
372 foreach ($this->fields AS $name => $options)
373 {
374 if ($options[F_REQ] == REQ_YES)
375 {
376 if (!isset($this->values["$name"]))
377 {
378 $this->error(sprintf($this->registry->modules['localize']->string('Required field %1$s was not set'), $name));
379 }
380 }
381 else if ($options[F_REQ] == REQ_SET)
382 {
383 $this->{"set_$name"}();
384 }
385 }
386 }
387
388 /**
389 * Runs a pre- or post-action method for database commands
390 *
391 * @access private
392 *
393 * @param string Action to run
394 */
395 function run_action_method($method)
396 {
397 if (in_array($method, $this->norunners))
398 {
399 return;
400 }
401
402 $actmethod = (method_exists($this, $method) ? $this->$method() : '');
403 }
404
405 /**
406 * Verify field: not a zero value
407 *
408 * @access protected
409 */
410 function verify_nozero($field)
411 {
412 if ($this->values["$field"] == 0)
413 {
414 return false;
415 }
416
417 return true;
418 }
419
420 /**
421 * Verify field: not empty
422 *
423 * @access protected
424 */
425 function verify_noempty($field)
426 {
427 if (empty($this->values["$field"]))
428 {
429 return false;
430 }
431
432 return true;
433 }
434 }
435
436 /**
437 * Setter and getter method for the API error reporting
438 * system. Passing a name will cause the set, no arguments
439 * will cause the get.
440 *
441 * @access public
442 *
443 * @param mixed Method name in callable form
444 *
445 * @return mixed Method name in callable form
446 */
447 function APIError($new = null)
448 {
449 static $caller;
450
451 if ($new !== null)
452 {
453 $caller = $new;
454 }
455
456 return $caller;
457 }
458
459 /*=====================================================================*\
460 || ###################################################################
461 || # $HeadURL$
462 || # $Id$
463 || ###################################################################
464 \*=====================================================================*/
465 ?>