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 * Pre- and post-action method stoppers
129 var $norunners = array();
132 * Constructor: cannot instantiate class directly
134 function API(&$registry)
136 if (!is_subclass_of($this, 'API'))
138 trigger_error('Cannot instantiate the API module directly', E_USER_ERROR
);
141 if (!is_object($registry))
143 trigger_error('The passed registry is not an object', E_USER_ERROR
);
146 $this->registry
=& $registry;
150 * Constructs an error for the error handler to receive
152 * @param string Error message
154 function error($message)
156 call_user_func(APIError(), $message);
160 * Sets a value, sanitizes it, and verifies it
162 * @param string Field name
164 * @param bool Do clean?
165 * @param bool Do verify?
167 function set($field, $value, $doclean = true
, $doverify = true
)
169 if (!isset($this->fields
["$field"]))
171 trigger_error('Field `' . $field . '` is not valid', E_USER_WARNING
);
175 $this->values
["$field"] = ($doclean ?
$this->registry
->clean($value, $this->fields
["$field"][F_TYPE
]) : $value);
177 $this->setfields
[] = $field;
179 if (isset($this->fields
["$field"][F_VERIFY
]) AND $doverify)
181 if ($this->fields
["$field"][F_VERIFY
] == ':self')
183 $verify = $this->{"verify_$field"}($field);
187 $verify = $this->{$this->fields
["$field"][F_VERIFY
]}($field);
193 trigger_error('Validation of `' . $field . '` failed', E_USER_ERROR
);
199 * Sets the condition to use in the WHERE clause; if not passed, then it calculates it
200 * from the REQ_AUTO field
202 * @param string WHERE conditional bit
204 function set_condition($condition = '')
206 if ($condition != '')
208 $this->condition
= $condition;
212 foreach ($this->fields
AS $name => $options)
214 if ($options[F_REQ
] == REQ_AUTO
)
216 if (!$this->values
["$name"])
218 trigger_error('Cannot determine condition from the REQ_AUTO field because it is not set', E_USER_WARNING
);
222 $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"]);
226 if ($this->condition
== '')
228 trigger_error('No REQ_AUTO fields are present and therefore the condition cannot be created', E_USER_WARNING
);
234 * Sets existing data into $values where it's not already present
236 function set_existing()
246 foreach ($this->objdata
AS $field => $value)
248 if (!isset($this->values
["$field"]))
250 $this->values
["$field"] = $value;
258 * Fetches a record based on the condition
262 if ($this->condition
== '')
264 trigger_error('Condition is empty: cannot fetch', E_USER_ERROR
);
267 $this->run_action_method('pre_fetch');
269 $result = $this->registry
->modules
['db_mysql']->query_first("SELECT * FROM {$this->prefix}{$this->table} WHERE {$this->condition}");
273 trigger_error('No record was returned', E_USER_ERROR
);
277 $this->run_action_method('post_fetch');
279 $this->objdata
= $result;
283 * Inserts a record in the database
289 $this->run_action_method('pre_insert');
291 foreach ($this->setfields
AS $field)
294 $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"]);
297 $this->registry
->modules
['db_mysql']->query("INSERT INTO {$this->prefix}{$this->table} (" . implode(',', $fields) . ") VALUES (" . implode(',', $values) . ")");
298 $this->insertid
= $this->registry
->modules
['db_mysql']->insert_id();
300 $this->run_action_method('post_insert');
304 * Updates a record in the database using the data in $vaues
308 if ($this->condition
== '')
310 trigger_error('Condition is empty: cannot fetch', E_USER_ERROR
);
313 $this->run_action_method('pre_update');
315 foreach ($this->setfields
AS $field)
317 $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"]);
319 $updates = implode(', ', $updates);
321 $this->registry
->modules
['db_mysql']->query("UPDATE {$this->prefix}{$this->table} SET $updates WHERE {$this->condition}");
323 $this->run_action_method('post_update');
331 if ($this->condition
== '')
333 trigger_error('Condition is empty: cannot fetch', E_USER_ERROR
);
336 $this->set_existing();
338 $this->run_action_method('pre_delete');
340 $this->registry
->modules
['db_mysql']->query("DELETE FROM {$this->prefix}{$this->table} WHERE {$this->condition}");
342 $this->run_action_method('post_delete');
346 * Verifies that all required fields are set
350 foreach ($this->fields
AS $name => $options)
352 if ($options[F_REQ
] == REQ_YES
)
354 if (!isset($this->values
["$name"]))
357 trigger_error('Field `' . $name . '` was not set', E_USER_ERROR
);
360 else if ($options[F_REQ
] == REQ_SET
)
362 $this->{"set_$name"}();
368 * Runs a pre- or post-action method for database commands
370 * @param string Action to run
372 function run_action_method($method)
374 if (in_array($method, $this->norunners
))
379 $actmethod = (method_exists($this, $method) ?
$this->$method() : '');
383 * Verify field: not a zero value
385 function verify_nozero($field)
387 if ($this->values
["$field"] == 0)
396 * Verify field: not empty
398 function verify_noempty($field)
400 if (empty($this->values
["$field"]))
410 * Setter and getter method for the API error reporting
411 * system. Passing a name will cause the set, no arguments
412 * will cause the get.
414 * @param mixed Method name in callable form
416 * @return mixed Method name in callable form
418 function APIError($new = null
)
430 /*=====================================================================*\
431 || ###################################################################
434 || ###################################################################
435 \*=====================================================================*/