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
52 * Index for cleaning type
57 * Index for requirement type
62 * Index for verification type
64 define('F_VERIFY', 2);
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
74 * @author Iris Studios, Inc.
75 * @copyright Copyright ©2002 - [#]year[#], Iris Studios, Inc.
89 * Fields: used for verification and sanitization
90 * NAME => array(TYPE, REQUIRED, VERIFY METHOD (:self for self-named method))
93 var $fields = array();
96 * Values array: sanitized and verified field values
99 var $values = array();
102 * Fields that were manually set with set(), not by using set_existing()
105 var $setfields = array();
114 * The object table row; a fetched row that represents this instance
117 var $objdata = array();
120 * Insert ID from the insert() command
126 * Constructor: cannot instantiate class directly
128 function API(&$registry)
130 if (!is_subclass_of($this, 'API'))
132 trigger_error('Cannot instantiate the API module directly', E_USER_ERROR
);
135 if (!is_object($registry))
137 trigger_error('The passed registry is not an object', E_USER_ERROR
);
140 $this->registry
=& $registry;
144 * Sets a value, sanitizes it, and verifies it
146 * @param string Field name
148 * @param bool Do clean?
149 * @param bool Do verify?
151 function set($field, $value, $doclean = true, $doverify = true)
153 if (!isset($this->fields
["$field"]))
155 trigger_error('Field `' . $field . '` is not valid', E_USER_WARNING);
159 $this->values["$field"] = ($doclean ? $this->registry
->clean($value, $this->fields
["$field"][F_TYPE]) : $value);
161 $this->setfields[] = $field;
163 if (isset($this->fields["$field"][F_VERIFY
]) AND $doverify)
165 if ($this->fields
["$field"][F_VERIFY] == ':self')
167 $verify = $this->{"verify_$field"}($field);
171 $verify = $this->{$this->fields["$field"][F_VERIFY]}($field);
177 trigger_error('Validation of `' . $field . '` failed', E_USER_ERROR);
183 * Sets the condition to use in the WHERE clause; if not passed, then it calculates it
184 * from the REQ_AUTO field
186 * @param string WHERE conditional bit
188 function set_condition($condition = '')
190 if ($condition != '')
192 $this->condition = $condition;
196 foreach ($this->fields AS $name => $options)
198 if ($options[F_REQ] == REQ_AUTO)
200 if (!$this->values["$name"])
202 trigger_error('Cannot determine condition from the REQ_AUTO field because it is not set', E_USER_WARNING
);
206 $this->condition
= "$name = " . (($options[F_TYPE
] == TYPE_NOCLEAN
OR $options[F_TYPE
] == TYPE_STR
) ? "'" . $this->values
["$name"] . "'" : $this->values["$name"]);
210 if ($this->condition == '')
212 trigger_error('No REQ_AUTO fields are present
and therefore the condition cannot be created
', E_USER_WARNING);
218 * Sets existing data into $values where it's not already present
220 function set_existing()
230 foreach ($this->objdata
AS $field => $value)
232 if (!isset($this->values
["$field"]))
234 $this->values["$field"] = $value;
242 * Fetches a record based on the condition
246 if ($this->condition
== '')
248 trigger_error('Condition is empty: cannot fetch', E_USER_ERROR
);
251 $actmethod = (method_exists($this, 'pre_fetch') ? $this->pre_fetch() : '');
253 $result = $this->registry
->modules
['db_mysql']->query_first("SELECT * FROM {$this->prefix}{$this->table} WHERE {$this->condition}");
257 trigger_error('No record was returned', E_USER_ERROR
);
261 $actmethod = (method_exists($this, 'post_fetch') ? $this->post_fetch() : '');
263 $this->objdata
= $result;
267 * Inserts a record in the database
273 $actmethod = (method_exists($this, 'pre_insert') ? $this->pre_insert() : '');
275 foreach ($this->setfields
AS $field)
278 $values[] = (($this->fields
["$field"][F_TYPE] == TYPE_NOCLEAN OR $this->fields["$field"][F_TYPE
] == TYPE_STR
) ? "'" . $this->values
["$field"] . "'" : $this->values["$field"]);
281 $this->registry->modules['db_mysql
']->query("INSERT INTO {$this->prefix}{$this->table} (" . implode(',', $fields) . ") VALUES (" . implode(',', $values) . ")");
282 $this->insertid = $this->registry->modules['db_mysql
']->insert_id();
284 $actmethod = (method_exists($this, 'post_insert
') ? $this->post_insert() : '');
288 * Updates a record in the database using the data in $vaues
292 if ($this->condition == '')
294 trigger_error('Condition is
empty: cannot fetch
', E_USER_ERROR);
297 $actmethod = (method_exists($this, 'pre_update
') ? $this->pre_update() : '');
299 foreach ($this->setfields AS $field)
301 $updates[] = "$field = " . (($this->fields["$field"][F_TYPE] == TYPE_NOCLEAN OR $this->fields["$field"][F_TYPE] == TYPE_STR) ? "'" . $this->values["$field"] . "'" : $this->values
["$field"]);
303 $updates = implode(', ', $updates);
305 $this->registry->modules['db_mysql']->query("UPDATE {$this
->prefix
}{$this
->table
} SET
$updates WHERE {$this
->condition
}");
307 $actmethod = (method_exists($this, 'post_update') ? $this->post_update() : '');
315 if ($this->condition == '')
317 trigger_error('Condition is empty: cannot fetch', E_USER_ERROR);
320 $this->set_existing();
322 $actmethod = (method_exists($this, 'pre_delete') ? $this->pre_delete() : '');
324 $this->registry->modules['db_mysql']->query("DELETE FROM {$this
->prefix
}{$this
->table
} WHERE {$this
->condition
}");
326 $actmethod = (method_exists($this, 'post_delete') ? $this->post_delete() : '');
330 * Verifies that all required fields are set
334 foreach ($this->fields AS $name => $options)
336 if ($options[F_REQ] == REQ_YES)
338 if (!isset($this->values["$name"]))
341 trigger_error('Field `' . $name . '` was not set', E_USER_ERROR
);
344 else if ($options[F_REQ
] == REQ_SET
)
346 $this->{"set_$name"}();
352 * Verify field: not a zero value
354 function verify_nozero($field)
356 if ($this->values
["$field"] == 0)
365 * Verify field: not empty
367 function verify_noempty($field)
369 if (empty($this->values["$field"]))
378 /*=====================================================================*\
379 || ###################################################################
382 || ###################################################################
383 \*=====================================================================*/