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);
47 * Set by a cusotm set_*() function
55 * Abstract class that is used as an API base for most common database interaction
56 * schemes. Creates a simple structure that holds data and can update, delete, and
59 * @author Iris Studios, Inc.
60 * @copyright Copyright ©2002 - [#]year[#], Iris Studios, Inc.
74 * Fields: used for verification and sanitization
75 * NAME => array(TYPE, REQUIRED, VERIFY METHOD (:self for self-named method))
78 var $fields = array();
81 * Values array: sanitized and verified field values
84 var $values = array();
87 * Fields that were manually set with set(), not by using set_existing()
90 var $setfields = array();
99 * The object table row; a fetched row that represents this instance
102 var $objdata = array();
105 * Insert ID from the insert() command
111 * Constructor: cannot instantiate class directly
113 function API(&$registry)
115 if (!is_subclass_of($this, 'API'))
117 trigger_error('Cannot instantiate the API module directly', E_USER_ERROR
);
120 if (!is_object($registry))
122 trigger_error('The passed registry is not an object', E_USER_ERROR
);
125 $this->registry
=& $registry;
129 * Sets a value, sanitizes it, and verifies it
131 * @param string Field name
133 * @param bool Do clean?
134 * @param bool Do verify?
136 function set($field, $value, $doclean = true, $doverify = true)
138 if (!isset($this->fields
["$field"]))
140 trigger_error('Field `' . $field . '` is not valid', E_USER_WARNING);
144 $this->values["$field"] = ($doclean ? $this->registry
->clean($value, $this->fields
["$field"][0]) : $value);
146 $this->setfields[] = $field;
148 if (isset($this->fields["$field"][2]) AND $doverify)
150 if ($this->fields
["$field"][2] == ':self')
152 $verify = $this->{"verify_$field"}($field);
156 $verify = $this->{$this->fields["$field"][2]}($field);
162 trigger_error('Validation of `' . $field . '` failed', E_USER_ERROR);
168 * Sets the condition to use in the WHERE clause; if not passed, then it calculates it
169 * from the REQ_AUTO field
171 * @param string WHERE conditional bit
173 function set_condition($condition = '')
175 if ($condition != '')
177 $this->condition = $condition;
181 foreach ($this->fields AS $name => $options)
183 if ($options[1] == REQ_AUTO)
185 if (!$this->values["$name"])
187 trigger_error('Cannot determine condition from the REQ_AUTO field because it is not set', E_USER_WARNING
);
191 $this->condition
= "$name = " . (($options[0] == TYPE_NOCLEAN
OR $options[0] == TYPE_STR
) ? "'" . $this->values
["$name"] . "'" : $this->values["$name"]);
195 if ($this->condition == '')
197 trigger_error('No REQ_AUTO fields are present
and therefore the condition cannot be created
', E_USER_WARNING);
203 * Sets existing data into $values where it's not already present
205 function set_existing()
215 foreach ($this->objdata
AS $field => $value)
217 if (!isset($this->values
["$field"]))
219 $this->values["$field"] = $value;
227 * Fetches a record based on the condition
231 if ($this->condition
== '')
233 trigger_error('Condition is empty: cannot fetch', E_USER_ERROR
);
236 $actmethod = (method_exists($this, 'pre_fetch') ? $this->pre_fetch() : '');
238 $result = $this->registry
->modules
['db_mysql']->query_first("SELECT * FROM {$this->prefix}{$this->table} WHERE {$this->condition}");
242 trigger_error('No record was returned', E_USER_ERROR
);
246 $actmethod = (method_exists($this, 'post_fetch') ? $this->post_fetch() : '');
248 $this->objdata
= $result;
252 * Inserts a record in the database
258 $actmethod = (method_exists($this, 'pre_insert') ? $this->pre_insert() : '');
260 foreach ($this->setfields
AS $field)
263 $values[] = (($this->fields
["$field"][0] == TYPE_NOCLEAN OR $this->fields["$field"][0] == TYPE_STR
) ? "'" . $this->values
["$field"] . "'" : $this->values["$field"]);
266 $this->registry->modules['db_mysql
']->query("INSERT INTO {$this->prefix}{$this->table} (" . implode(',', $fields) . ") VALUES (" . implode(',', $values) . ")");
267 $this->insertid = $this->registry->modules['db_mysql
']->insert_id();
269 $actmethod = (method_exists($this, 'post_insert
') ? $this->post_insert() : '');
273 * Updates a record in the database using the data in $vaues
277 if ($this->condition == '')
279 trigger_error('Condition is
empty: cannot fetch
', E_USER_ERROR);
282 $this->set_existing();
285 $actmethod = (method_exists($this, 'pre_update
') ? $this->pre_update() : '');
287 foreach ($this->setfields AS $field)
289 $updates[] = "$field = " . (($this->fields["$field"][0] == TYPE_NOCLEAN OR $this->fields["$field"][0] == TYPE_STR) ? "'" . $this->values["$field"] . "'" : $this->values
["$field"]);
291 $updates = implode(', ', $updates);
293 $this->registry->modules['db_mysql']->query("UPDATE {$this
->prefix
}{$this
->table
} SET
$updates WHERE {$this
->condition
}");
295 $actmethod = (method_exists($this, 'post_update') ? $this->post_update() : '');
303 if ($this->condition == '')
305 trigger_error('Condition is empty: cannot fetch', E_USER_ERROR);
308 $this->set_existing();
310 $actmethod = (method_exists($this, 'pre_delete') ? $this->pre_delete() : '');
312 $this->registry->modules['db_mysql']->query("DELETE FROM {$this
->prefix
}{$this
->table
} WHERE {$this
->condition
}");
314 $actmethod = (method_exists($this, 'post_delete') ? $this->post_delete() : '');
318 * Verifies that all required fields are set
322 foreach ($this->fields AS $name => $options)
324 if ($options[1] == REQ_YES)
326 if (!isset($this->values["$name"]))
329 trigger_error('Field `' . $name . '` was not set', E_USER_ERROR
);
332 else if ($options[1] == REQ_SET
)
334 $this->{"set_$name"}();
340 * Verify field: not a zero value
342 function verify_nozero($field)
344 if ($this->values
["$field"] == 0)
353 * Verify field: not empty
355 function verify_noempty($field)
357 if (empty($this->values["$field"]))
366 /*=====================================================================*\
367 || ###################################################################
370 || ###################################################################
371 \*=====================================================================*/