From 0ae3672efe1f53591891ab9209dfcc7ecff8846a Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sun, 17 Dec 2006 04:07:35 +0000 Subject: [PATCH] - Cleaning up the API module - Renamed delete() to remove() --- api.php | 113 ++++++++++++++++++++++++-------------------------------- 1 file changed, 48 insertions(+), 65 deletions(-) diff --git a/api.php b/api.php index 7a841e6..4dc7a84 100644 --- a/api.php +++ b/api.php @@ -20,8 +20,7 @@ \*=====================================================================*/ /** -* Abstract Datamanger API -* api.php +* Abstract Datamanger API (api.php) * * @package ISSO */ @@ -88,7 +87,7 @@ if (!defined('REQ_AUTO')) * Abstract API * * Abstract class that is used as an API base for most common database interaction -* schemes. Creates a simple structure that holds data and can update, delete, and +* schemes. Creates a simple structure that holds data and can update, remove, and * insert. * * @author Blue Static @@ -97,14 +96,8 @@ if (!defined('REQ_AUTO')) * @package ISSO * */ -class API +abstract class BSApi { - /** - * Registry object - * @var object - */ - protected $registry = null; - /** * Fields: used for verification and sanitization * NAME => array(TYPE, REQUIRED, VERIFY METHOD (:self for self-named method), RELATION => array(FILE, CLASS IN FILE, ALTERNATE FIELD NAME)) @@ -119,7 +112,7 @@ class API public $values = array(); /** - * Fields that were manually set with set(), not by using set_existing() + * Fields that were manually set with set(), not by using setExisting() * @var array */ private $setfields = array(); @@ -170,19 +163,9 @@ class API /** * Constructor: cannot instantiate class directly */ - public function __construct(&$registry) + public function __construct() { - if (!is_subclass_of($this, 'API')) - { - trigger_error('Cannot instantiate the API module directly'); - } - - if (!is_object($registry)) - { - trigger_error('The passed registry is not an object'); - } - - $this->registry =& $registry; + BSRegister::RequiredModules(array('Db', 'Input')); } // ################################################################### @@ -217,7 +200,7 @@ class API * * @return array Array of errors */ - public function check_errors() + public function checkErrors() { if (sizeof($this->errors) < 1) { @@ -244,7 +227,7 @@ class API return; } - $this->values["$field"] = ($doclean ? $this->registry->clean($value, $this->fields["$field"][F_TYPE]) : $value); + $this->values["$field"] = ($doclean ? BSRegister::GetType('Input')->clean($value, $this->fields["$field"][F_TYPE]) : $value); $this->setfields["$field"] = $field; @@ -263,7 +246,7 @@ class API { if ($verify === false) { - $this->error(sprintf($this->registry->modules['localize']->string('Validation of %1$s failed'), $field)); + $this->error(sprintf(_('Validation of "%1$s" failed'), $field)); } else { @@ -280,7 +263,7 @@ class API * * @param mixed String with WHERE condition; array of fields to use for WHERE builder */ - public function set_condition($condition = '') + public function setCondition($condition = '') { if (is_array($condition) AND sizeof($condition) > 0) { @@ -290,11 +273,11 @@ class API { if (!$this->values["$field"]) { - trigger_error('The specified field `' . $field . '` for the condition could not be found as it is not set'); + trigger_error('The specified field "' . $field . '" for the condition could not be found as it is not set'); continue; } - $condbits[] = "$field = " . $this->prepare_field_for_sql($field); + $condbits[] = "$field = " . $this->_prepareFieldForSql($field); } $this->condition = implode(' AND ', $condbits); } @@ -314,7 +297,7 @@ class API continue; } - $this->condition = "$name = " . $this->prepare_field_for_sql($name); + $this->condition = "$name = " . $this->_prepareFieldForSql($name); } } @@ -329,7 +312,7 @@ class API /** * Sets existing data into $values where it's not already present */ - public function set_existing() + public function setExisting() { static $run; if ($run) @@ -363,16 +346,16 @@ class API trigger_error('Condition is empty: cannot fetch'); } - $this->run_action_method('pre_fetch'); + $this->_runActionMethod('pre_fetch'); - $result = $this->registry->modules[ISSO_DB_LAYER]->query_first("SELECT * FROM {$this->prefix}{$this->table} WHERE {$this->condition}"); + $result = BSRegister::GetType('Db')->queryFirst("SELECT * FROM {$this->prefix}{$this->table} WHERE {$this->condition}"); if (!$result) { - $this->error($this->registry->modules['localize']->string('No records were returned')); + $this->error(_('No records were returned')); return; } - $this->run_action_method('post_fetch'); + $this->_runActionMethod('post_fetch'); $this->objdata = $result; @@ -387,7 +370,7 @@ class API } } - $this->call_relations('fetch'); + $this->callRelations('fetch'); } // ################################################################### @@ -398,17 +381,17 @@ class API { $this->verify(); - $this->run_action_method('pre_insert'); + $this->_runActionMethod('pre_insert'); foreach ($this->setfields AS $field) { $fields[] = $field; - $values[] = $this->prepare_field_for_sql($field); + $values[] = $this->_prepareFieldForSql($field); } - $this->registry->modules[ISSO_DB_LAYER]->query("INSERT INTO {$this->prefix}{$this->table} (" . implode(',', $fields) . ") VALUES (" . implode(',', $values) . ")"); + BSRegister::GetType('Db')->query("INSERT INTO {$this->prefix}{$this->table} (" . implode(',', $fields) . ") VALUES (" . implode(',', $values) . ")"); - if (strcasecmp(ISSO_DB_LAYER, 'DB_PostgreSQL') == 0) + if (BSRegister::GetType('DbPostgreSql')) { foreach ($this->fields AS $field => $info) { @@ -419,14 +402,14 @@ class API } } - $this->insertid = $this->registry->modules[ISSO_DB_LAYER]->insert_id($this->prefix . $this->table, $autofield); + $this->insertid = BSRegister::GetType('Db')->insertId($this->prefix . $this->table, $autofield); } else { - $this->insertid = $this->registry->modules[ISSO_DB_LAYER]->insert_id(); + $this->insertid = BSRegister::GetType('Db')->insertId(); } - $this->run_action_method('post_insert'); + $this->_runActionMethod('post_insert'); } // ################################################################### @@ -440,42 +423,42 @@ class API trigger_error('Condition is empty: cannot update'); } - $this->run_action_method('pre_update'); + $this->_runActionMethod('pre_update'); foreach ($this->setfields AS $field) { - $updates[] = "$field = " . $this->prepare_field_for_sql($field); + $updates[] = "$field = " . $this->_prepareFieldForSql($field); } $updates = implode(', ', $updates); - $this->registry->modules[ISSO_DB_LAYER]->query("UPDATE {$this->prefix}{$this->table} SET $updates WHERE {$this->condition}"); + BSRegister::GetType('Db')->query("UPDATE {$this->prefix}{$this->table} SET $updates WHERE {$this->condition}"); - $this->run_action_method('post_update'); + $this->_runActionMethod('post_update'); } // ################################################################### /** * Deletes a record * - * @param bool Run API->set_existing()? + * @param bool Run API->setExisting()? */ - public function delete($runset = true) + public function remove($runset = true) { if ($this->condition == '') { - trigger_error('Condition is empty: cannot delete'); + trigger_error('Condition is empty: cannot remove'); } if ($runset) { - $this->set_existing(); + $this->setExisting(); } - $this->run_action_method('pre_delete'); + $this->_runActionMethod('pre_remove'); - $this->registry->modules[ISSO_DB_LAYER]->query("DELETE FROM {$this->prefix}{$this->table} WHERE {$this->condition}"); + BSRegister::GetType('Db')->query("DELETE FROM {$this->prefix}{$this->table} WHERE {$this->condition}"); - $this->run_action_method('post_delete'); + $this->_runActionMethod('post_remove'); } // ################################################################### @@ -490,7 +473,7 @@ class API { if (!isset($this->values["$name"])) { - $this->error(sprintf($this->registry->modules['localize']->string('Required field %1$s was not set'), $name)); + $this->error(sprintf(_('The required field "%1$s" was not set'), $name)); } } else if ($options[F_REQ] == REQ_SET) @@ -506,7 +489,7 @@ class API * * @param string Action to run */ - private function run_action_method($method) + private function _runActionMethod($method) { if (in_array($method, $this->norunners)) { @@ -523,7 +506,7 @@ class API * * @param string Operation to run */ - public function call_relations($method) + public function callRelations($method) { if (!is_array($this->dorelations) OR !in_array($method, $this->dorelations)) { @@ -538,16 +521,16 @@ class API continue; } - if (!file_exists($this->registry->get('apppath') . $info[F_RELATION][F_RELATION_FILE])) + if (!file_exists(BSRegister::GetApplicationPath() . $info[F_RELATION][F_RELATION_FILE])) { trigger_error("Could not load the relation file for field '$field'"); } - require_once($this->registry->get('apppath') . $info[F_RELATION][F_RELATION_FILE]); + require_once(BSRegister::GetApplicationPath() . $info[F_RELATION][F_RELATION_FILE]); $this->relations["$field"] = new $info[F_RELATION][F_RELATION_CLASS]($this->registry); $this->relations["$field"]->set(($info[F_RELATION][F_RELATION_ALTFIELD] ? $info[F_RELATION][F_RELATION_ALTFIELD] : $field), $value); - $this->relations["$field"]->set_condition(); + $this->relations["$field"]->setCondition(); $this->relations["$field"]->$method(); } } @@ -561,13 +544,13 @@ class API * * @return string Prepared value entry */ - private function prepare_field_for_sql($name) + private function _prepareFieldForSql($name) { $type = $this->fields["$name"][F_TYPE]; if ($type == TYPE_NOCLEAN OR $type == TYPE_STR OR $type == TYPE_STRUN) { - return "'" . $this->registry->db->escape_string($this->values["$name"]) . "'"; + return "'" . BSRegister::GetType('Db')->escapeString($this->values["$name"]) . "'"; } else if ($type == TYPE_BOOL) { @@ -575,7 +558,7 @@ class API } else if ($type == TYPE_BIN) { - return "'" . $this->registry->db->escape_binary($this->values["$name"]) . "'"; + return "'" . BSRegister::GetType('Db')->escapeBinary($this->values["$name"]) . "'"; } else { @@ -591,7 +574,7 @@ class API { if ($this->values["$field"] == 0) { - return sprintf($this->registry->modules['localize']->string('The field "%1$s" cannot be zero'), $field); + return sprintf(_('The field "%1$s" cannot be zero'), $field); } return true; @@ -605,7 +588,7 @@ class API { if (empty($this->values["$field"])) { - return sprintf($this->registry->modules['localize']->string('The field "%1$s" cannot be empty'), $field); + return sprintf(_('The field "%1$s" cannot be empty'), $field); } return true; -- 2.22.5