]>
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_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 __construct(&$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 * (PHP 4) Constructor
152 function API(&$registry)
154 $this->__construct($registry);
158 * Constructs an error for the error handler to receive
160 * @param string Error message
162 function error($message)
164 call_user_func(APIError(), $message);
168 * Sets a value, sanitizes it, and verifies it
172 * @param string Field name
174 * @param bool Do clean?
175 * @param bool Do verify?
177 function set($field, $value, $doclean = true, $doverify = true)
179 if (!isset($this->fields
["$field"]))
181 trigger_error('Field `' . $field . '` is not valid', E_USER_WARNING);
185 $this->values["$field"] = ($doclean ? $this->registry
->clean($value, $this->fields
["$field"][F_TYPE]) : $value);
187 $this->setfields["$field"] = $field;
189 if (isset($this->fields
["$field"][F_VERIFY]) AND $doverify)
191 if ($this->fields["$field"][F_VERIFY
] == ':self')
193 $verify = $this->{"verify_$field"}($field);
197 $verify = $this->{$this
->fields
["$field"][F_VERIFY
]}($field);
202 $this->error(sprintf($this->registry
->modules
['localize']->string('Validation of %1$s failed'), $field));
208 * Sets the condition to use in the WHERE clause; if not passed, then it calculates it
209 * from the REQ_AUTO field
213 * @param string WHERE conditional bit
215 function set_condition($condition = '')
217 if ($condition != '')
219 $this->condition
= $condition;
223 foreach ($this->fields
AS $name => $options)
225 if ($options[F_REQ
] == REQ_AUTO
)
227 if (!$this->values
["$name"])
229 trigger_error('Cannot determine condition from the REQ_AUTO field because it is not set', E_USER_WARNING);
233 $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"]);
237 if ($this->condition
== '')
239 trigger_error('No REQ_AUTO fields are present and therefore the condition cannot be created', E_USER_WARNING
);
245 * Sets existing data into $values where it's not already present
249 function set_existing()
259 foreach ($this->objdata
AS $field => $value)
261 if (!isset($this->values
["$field"]))
263 $this->values["$field"] = $value;
271 * Fetches a record based on the condition
277 if ($this->condition
== '')
279 trigger_error('Condition is empty: cannot fetch', E_USER_ERROR
);
282 $this->run_action_method('pre_fetch');
284 $result = $this->registry
->modules
['db_mysql']->query_first("SELECT * FROM {$this->prefix}{$this->table} WHERE {$this->condition}");
287 $this->error($this->registry
->modules
['localize']->string('No records were returned'));
291 $this->run_action_method('post_fetch');
293 $this->objdata
= $result;
297 * Inserts a record in the database
305 $this->run_action_method('pre_insert');
307 foreach ($this->setfields
AS $field)
310 $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"]);
313 $this->registry
->modules
['db_mysql']->query("INSERT INTO {$this->prefix}{$this->table} (" . implode(',', $fields) . ") VALUES (" . implode(',', $values) . ")");
314 $this->insertid
= $this->registry
->modules
['db_mysql']->insert_id();
316 $this->run_action_method('post_insert');
320 * Updates a record in the database using the data in $vaues
326 if ($this->condition
== '')
328 trigger_error('Condition is empty: cannot fetch', E_USER_ERROR
);
331 $this->run_action_method('pre_update');
333 foreach ($this->setfields
AS $field)
335 $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"]);
337 $updates = implode(', ', $updates);
339 $this->registry
->modules
['db_mysql']->query("UPDATE {$this->prefix}{$this->table} SET $updates WHERE {$this->condition}");
341 $this->run_action_method('post_update');
351 if ($this->condition
== '')
353 trigger_error('Condition is empty: cannot fetch', E_USER_ERROR
);
356 $this->set_existing();
358 $this->run_action_method('pre_delete');
360 $this->registry
->modules
['db_mysql']->query("DELETE FROM {$this->prefix}{$this->table} WHERE {$this->condition}");
362 $this->run_action_method('post_delete');
366 * Verifies that all required fields are set
372 foreach ($this->fields
AS $name => $options)
374 if ($options[F_REQ
] == REQ_YES
)
376 if (!isset($this->values
["$name"]))
378 $this->error(sprintf($this->registry->modules['localize']->string('Required field %1$s was not set'), $name));
381 else if ($options[F_REQ] == REQ_SET)
383 $this->{"set_$name"}();
389 * Runs a pre- or post-action method for database commands
393 * @param string Action to run
395 function run_action_method($method)
397 if (in_array($method, $this->norunners))
402 $actmethod = (method_exists($this, $method) ? $this->$method() : '');
406 * Verify field: not a zero value
410 function verify_nozero($field)
412 if ($this->values["$field"] == 0)
421 * Verify field: not empty
425 function verify_noempty($field)
427 if (empty($this->values
["$field"]))
437 * Setter and getter method for the API error reporting
438 * system. Passing a name will cause the set, no arguments
439 * will cause the get.
443 * @param mixed Method name in callable form
445 * @return mixed Method name in callable form
447 function APIError($new = null)
459 /*=====================================================================*\
460 || ###################################################################
463 || ###################################################################
464 \*=====================================================================*/