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_YES'))
42 * Auto-increasing value
44 define('REQ_AUTO', -1);
50 * Abstract class that is used as an API base for most common database interaction
51 * schemes. Creates a simple structure that holds data and can update, delete, and
54 * @author Iris Studios, Inc.
55 * @copyright Copyright ©2002 - [#]year[#], Iris Studios, Inc.
69 * Fields: used for verification and sanitization
70 * NAME => array(TYPE, REQUIRED, VERIFY METHOD (:self for self-named method))
73 var $fields = array();
76 * Values array: sanitized and verified field values
79 var $values = array();
82 * Fields that were manually set with set(), not by using set_existing()
85 var $setfields = array();
94 * The object table row; a fetched row that represents this instance
97 var $objdata = array();
100 * Insert ID from the insert() command
106 * Constructor: cannot instantiate class directly
108 function API(&$registry)
110 if (!is_subclass_of($this, 'API'))
112 trigger_error('Cannot instantiate the API module directly', E_USER_ERROR
);
115 if (!is_object($registry))
117 trigger_error('The passed registry is not an object', E_USER_ERROR
);
120 $this->registry
=& $registry;
124 * Sets a value, sanitizes it, and verifies it
126 * @param string Field name
128 * @param bool Do clean?
129 * @param bool Do verify?
131 function set($field, $value, $doclean = true
, $doverify = true
)
133 if (!isset($this->fields
["$field"]))
135 trigger_error('Field `' . $field . '` is not valid', E_USER_WARNING
);
139 $this->values
["$field"] = ($doclean ?
$this->registry
->clean($value, $this->fields
["$field"][0]) : $value);
141 $this->setfields
[] = $field;
143 if (isset($this->fields
["$field"][2]) AND $doverify)
145 if ($this->fields
["$field"][2] == ':self')
147 $verify = $this->{"verify_$field"}($field);
151 $verify = $this->{$this->fields
["$field"][2]}($field);
157 trigger_error('Validation of `' . $field . '` failed', E_USER_ERROR
);
163 * Sets the condition to use in the WHERE clause; if not passed, then it calculates it
164 * from the REQ_AUTO field
166 * @param string WHERE conditional bit
168 function set_condition($condition = '')
170 if ($condition != '')
172 $this->condition
= $condition;
176 foreach ($this->fields
AS $name => $options)
178 if ($options[1] == REQ_AUTO
)
180 if (!$this->values
["$name"])
182 trigger_error('Cannot determine condition from the REQ_AUTO field because it is not set', E_USER_WARNING
);
186 $this->condition
= "$name = " . (($options[0] == TYPE_NOCLEAN
OR $options[0] == TYPE_STR
) ?
"'" . $this->values
["$name"] . "'" : $this->values
["$name"]);
190 if ($this->condition
== '')
192 trigger_error('No REQ_AUTO fields are present and therefore the condition cannot be created', E_USER_WARNING
);
198 * Sets existing data into $values where it's not already present
200 function set_existing()
210 foreach ($this->objdata
AS $field => $value)
212 if (!isset($this->values
["$field"]))
214 $this->values
["$field"] = $value;
222 * Fetches a record based on the condition
226 if ($this->condition
== '')
228 trigger_error('Condition is empty: cannot fetch', E_USER_ERROR
);
231 $actmethod = (method_exists($this, 'pre_fetch') ?
$this->pre_fetch() : '');
233 $result = $this->registry
->modules
['db_mysql']->query_first("SELECT * FROM {$this->prefix}{$this->table} WHERE {$this->condition}");
237 trigger_error('No record was returned', E_USER_ERROR
);
241 $actmethod = (method_exists($this, 'post_fetch') ?
$this->post_fetch() : '');
243 $this->objdata
= $result;
247 * Inserts a record in the database
253 $actmethod = (method_exists($this, 'pre_insert') ?
$this->pre_insert() : '');
255 foreach ($this->setfields
AS $field)
258 $values[] = (($this->fields
["$field"][0] == TYPE_NOCLEAN
OR $this->fields
["$field"][0] == TYPE_STR
) ?
"'" . $this->values
["$field"] . "'" : $this->values
["$field"]);
261 $this->registry
->modules
['db_mysql']->query("INSERT INTO {$this->prefix}{$this->table} (" . implode(',', $fields) . ") VALUES (" . implode(',', $values) . ")");
262 $this->insertid
= $this->registry
->modules
['db_mysql']->insert_id();
264 $actmethod = (method_exists($this, 'post_insert') ?
$this->post_insert() : '');
268 * Updates a record in the database using the data in $vaues
272 if ($this->condition
== '')
274 trigger_error('Condition is empty: cannot fetch', E_USER_ERROR
);
277 $this->set_existing();
280 $actmethod = (method_exists($this, 'pre_update') ?
$this->pre_update() : '');
282 foreach ($this->setfields
AS $field)
284 $updates[] = "$field = " . (($this->fields
["$field"][0] == TYPE_NOCLEAN
OR $this->fields
["$field"][0] == TYPE_STR
) ?
"'" . $this->values
["$field"] . "'" : $this->values
["$field"]);
286 $updates = implode(', ', $updates);
288 $this->registry
->modules
['db_mysql']->query("UPDATE {$this->prefix}{$this->table} SET $updates WHERE {$this->condition}");
290 $actmethod = (method_exists($this, 'post_update') ?
$this->post_update() : '');
298 if ($this->condition
== '')
300 trigger_error('Condition is empty: cannot fetch', E_USER_ERROR
);
303 $this->set_existing();
305 $actmethod = (method_exists($this, 'pre_delete') ?
$this->pre_delete() : '');
307 $this->registry
->modules
['db_mysql']->query("DELETE FROM {$this->prefix}{$this->table} WHERE {$this->condition}");
309 $actmethod = (method_exists($this, 'post_delete') ?
$this->post_delete() : '');
313 * Verifies that all required fields are set
317 foreach ($this->fields
AS $name => $options)
319 if ($options[1] == REQ_YES
)
321 if (!isset($this->values
["$name"]))
324 trigger_error('Field `' . $name . '` was not set', E_USER_ERROR
);
331 * Verify field: not a zero value
333 function verify_nozero($field)
335 if ($this->values
["$field"] == 0)
344 * Verify field: not empty
346 function verify_noempty($field)
348 if (empty($this->values
["$field"]))
357 /*=====================================================================*\
358 || ###################################################################
361 || ###################################################################
362 \*=====================================================================*/