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