]>
src.bluestatic.org Git - isso.git/blob - api.php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Iris Studios Shared Object Framework [#]version[#]
5 || # Copyright ©2002-[#]year[#] Iris Studios, Inc.
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.
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
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 \*=====================================================================*/
23 * Abstract Datamanger API
29 if (!defined('REQ_AUTO'))
32 * Auto-increasing value
34 define('REQ_AUTO', -1);
37 * Set by a cusotm set_*() function
42 * Index for cleaning type
47 * Index for requirement type
52 * Index for verification type
54 define('F_VERIFY', 2);
60 * Abstract class that is used as an API base for most common database interaction
61 * schemes. Creates a simple structure that holds data and can update, delete, and
64 * @author Iris Studios, Inc.
65 * @copyright Copyright ©2002 - [#]year[#], Iris Studios, Inc.
80 * Fields: used for verification and sanitization
81 * NAME => array(TYPE, REQUIRED, VERIFY METHOD (:self for self-named method))
85 var $fields = array();
88 * Values array: sanitized and verified field values
92 var $values = array();
95 * Fields that were manually set with set(), not by using set_existing()
99 var $setfields = array();
109 * The object table row; a fetched row that represents this instance
113 var $objdata = array();
116 * Insert ID from the insert() command
123 * Pre- and post-action method stoppers
127 var $norunners = array();
129 // ###################################################################
131 * Constructor: cannot instantiate class directly
133 function __construct(&$registry)
135 if (!is_subclass_of($this, 'API'))
137 trigger_error('Cannot instantiate the API module directly', E_USER_ERROR
);
140 if (!is_object($registry))
142 trigger_error('The passed registry is not an object', E_USER_ERROR
);
145 $this->registry
=& $registry;
148 // ###################################################################
150 * (PHP 4) Constructor
152 function API(&$registry)
154 $this->__construct($registry);
157 // ###################################################################
159 * Constructs an error for the error handler to receive
163 * @param string Error message
165 function error($message)
167 call_user_func(APIError(), $message);
170 // ###################################################################
172 * Sets a value, sanitizes it, and verifies it
176 * @param string Field name
178 * @param bool Do clean?
179 * @param bool Do verify?
181 function set($field, $value, $doclean = true, $doverify = true)
183 if (!isset($this->fields
["$field"]))
185 trigger_error('Field `' . $field . '` is not valid', E_USER_WARNING);
189 $this->values["$field"] = ($doclean ? $this->registry
->clean($value, $this->fields
["$field"][F_TYPE]) : $value);
191 $this->setfields["$field"] = $field;
193 if (isset($this->fields
["$field"][F_VERIFY]) AND $doverify)
195 if ($this->fields["$field"][F_VERIFY
] == ':self')
197 $verify = $this->{"verify_$field"}($field);
201 $verify = $this->{$this
->fields
["$field"][F_VERIFY
]}($field);
206 $this->error(sprintf($this->registry
->modules
['localize']->string('Validation of %1$s failed'), $field));
211 // ###################################################################
213 * Sets the condition to use in the WHERE clause; if not passed, then
214 * it calculates it from the REQ_AUTO field
218 * @param string WHERE conditional bit
220 function set_condition($condition = '')
222 if ($condition != '')
224 $this->condition
= $condition;
228 foreach ($this->fields
AS $name => $options)
230 if ($options[F_REQ
] == REQ_AUTO
)
232 if (!$this->values
["$name"])
234 trigger_error('Cannot determine condition from the REQ_AUTO field because it is not set', E_USER_WARNING);
238 $this->condition = "$name = " . $this->prepare_field_for_sql($name);
242 if ($this->condition == '')
244 trigger_error('No REQ_AUTO fields are present and therefore the condition cannot be created', E_USER_WARNING);
249 // ###################################################################
251 * Sets existing data into $values where it's not already present
255 function set_existing()
265 foreach ($this->objdata AS $field => $value)
267 if (!isset($this->values["$field"]))
269 $this->values
["$field"] = $value;
276 // ###################################################################
278 * Fetches a record based on the condition
284 if ($this->condition == '')
286 trigger_error('Condition is empty: cannot fetch', E_USER_ERROR);
289 $this->run_action_method('pre_fetch');
291 $result = $this->registry->modules[ISSO_DB_LAYER]->query_first("SELECT
* FROM {$this
->prefix
}{$this
->table
} WHERE {$this
->condition
}");
294 $this->error($this->registry->modules['localize']->string('No records were returned'));
298 $this->run_action_method('post_fetch');
300 $this->objdata = $result;
303 // ###################################################################
305 * Inserts a record in the database
313 $this->run_action_method('pre_insert');
315 foreach ($this->setfields AS $field)
318 $values[] = $this->prepare_field_for_sql($field);
321 $this->registry->modules[ISSO_DB_LAYER]->query("INSERT INTO {$this
->prefix
}{$this
->table
} (" . implode(',', $fields) . ") VALUES (" . implode(',', $values) . ")");
322 $this->insertid = $this->registry->modules[ISSO_DB_LAYER]->insert_id();
324 $this->run_action_method('post_insert');
327 // ###################################################################
329 * Updates a record in the database using the data in $vaues
335 if ($this->condition == '')
337 trigger_error('Condition is empty: cannot fetch', E_USER_ERROR);
340 $this->run_action_method('pre_update');
342 foreach ($this->setfields AS $field)
344 $updates[] = "$field = " . $this->prepare_field_for_sql($field);
346 $updates = implode(', ', $updates);
348 $this->registry->modules[ISSO_DB_LAYER]->query("UPDATE {$this
->prefix
}{$this
->table
} SET
$updates WHERE {$this
->condition
}");
350 $this->run_action_method('post_update');
353 // ###################################################################
361 if ($this->condition == '')
363 trigger_error('Condition is empty: cannot fetch', E_USER_ERROR);
366 $this->set_existing();
368 $this->run_action_method('pre_delete');
370 $this->registry->modules[ISSO_DB_LAYER]->query("DELETE FROM {$this
->prefix
}{$this
->table
} WHERE {$this
->condition
}");
372 $this->run_action_method('post_delete');
375 // ###################################################################
377 * Verifies that all required fields are set
383 foreach ($this->fields AS $name => $options)
385 if ($options[F_REQ] == REQ_YES)
387 if (!isset($this->values["$name"]))
389 $this->error(sprintf($this->registry
->modules
['localize']->string('Required field %1$s was not set'), $name));
392 else if ($options[F_REQ
] == REQ_SET
)
394 $this->{"set_$name"}();
399 // ###################################################################
401 * Runs a pre- or post-action method for database commands
405 * @param string Action to run
407 function run_action_method($method)
409 if (in_array($method, $this->norunners
))
414 $actmethod = (method_exists($this, $method) ? $this->$method() : '');
417 // ###################################################################
419 * Prepares a value for use in a SQL query; it encases and escapes
420 * strings and string-like values
424 * @param string Field name
426 * @return string Prepared value entry
428 function prepare_field_for_sql($name)
430 $type = $this->fields
["$name"][F_TYPE];
432 if ($type == TYPE_NOCLEAN OR $type == TYPE_STR OR $type == TYPE_STRUN)
434 return "'" . $this->registry->escape($this->values["$name"]) . "'";
436 else if ($type == TYPE_BOOL)
438 return (int)$this->values["$name"];
442 return $this->values
["$name"];
446 // ###################################################################
448 * Verify field: not a zero value
452 function verify_nozero($field)
454 if ($this->values["$field"] == 0)
462 // ###################################################################
464 * Verify field: not empty
468 function verify_noempty($field)
470 if (empty($this->values
["$field"]))
479 // ###################################################################
481 * Setter and getter method for the API error reporting system. Passing
482 * a name will cause the set, no arguments will cause the get.
486 * @param mixed Method name in callable form
488 * @return mixed Method name in callable form
490 function APIError($new = null)
502 /*=====================================================================*\
503 || ###################################################################
506 || ###################################################################
507 \*=====================================================================*/