Thread: [PHPVortex-Commit] phpvortex TB_Base.class.php,1.3,1.4
Brought to you by:
nop144666
From: Thiago R. <nop...@us...> - 2004-09-30 19:34:47
|
Update of /cvsroot/phpvortex/phpvortex In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv5481 Modified Files: TB_Base.class.php Log Message: Added a lot of functionality to TB_Base. Still a lot to do, but it is already working Index: TB_Base.class.php =================================================================== RCS file: /cvsroot/phpvortex/phpvortex/TB_Base.class.php,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** TB_Base.class.php 29 Sep 2004 21:01:39 -0000 1.3 --- TB_Base.class.php 30 Sep 2004 19:34:38 -0000 1.4 *************** *** 11,14 **** --- 11,45 ---- /** + * No Error + * + * Error codes for TB_* + */ + define('TB_ERR_OK', 0); + /** + * Inconsistency detected in a field. + * + * Error codes for TB_* + */ + define('TB_ERR_INCONSIST', 1); + /** + * No Primary Key found. + * + * Error codes for TB_* + */ + define('TB_ERR_NOPKEY', 2); + /** + * Empty parameter or RecordSet. + * + * Error codes for TB_* + */ + define('TB_ERR_EMPTY', 3); + /** + * Database error (use {@link DB_Base::Error()} to discover which error). + * + * Error codes for TB_* + */ + define('TB_ERR_DB', 4); + + /** * Base class for tables in databases. * *************** *** 25,28 **** --- 56,87 ---- /** + * Table name. + * @var string + */ + var $name; + + /** + * Table name in the database. + * Default = {@link $name} + * + * @var string + */ + var $name_db; + + /** + * Label of the table for forms and listings. + * Default = {@link $name} + * + * @var string + */ + var $label; + + /** + * Last error in the object. + * @var int + */ + var $error = TB_ERR_OK; + + /** * Array containing all the fields of the table (FT_* classes). * @var array *************** *** 44,47 **** --- 103,246 ---- { $this->db =& $db; + is_null($this->name_db) and $this->name_db = $this->name; + is_null($this->label) and $this->label = $this->name; + } + + /** + * Get the last error message. + * + * @return string Returns the last error message, or FALSE if no error. + */ + function Error() + { + if ($this->error == TB_ERR_OK) return FALSE; + return $this->error; + } + + /** + * Set the internal to a record to show/edit, or a blank one. + * + * @param array $data Array containing the primary key(s) to the table as 'field' => 'value'. + * @param bool $pkonly Use only the pkey's in the search? + * @return bool Returns TRUE on success, FALSE on error. + */ + function Seek($data = NULL, $pkonly = TRUE) + { + if (empty($data)) { + $this->data = array(); + return TRUE; + } + $where = ''; + foreach ($this->fields as $field) if (!$pkonly || $field->pkey) { + if (isset($data[$field->name_form]) && ($key = $field->Consist($data)) !== FALSE) $where .= (strlen($where)?' AND ':'')."{$field->name_db} = $key"; + } + if (!strlen($where)) { + $this->data = array(); + $this->error = TB_ERR_NOPKEY; + return FALSE; + } + $sql = "SELECT * FROM {$this->name_db} WHERE $where"; + dv(3, 'SEEK SELECT', $sql); + if (!($rs = $this->db->Query($sql))) { + $this->data = array(); + $this->error = TB_ERR_DB; + return FALSE; + } + if (($this->data = $rs->Row()) === FALSE) { + $this->data = array(); + $this->error = TB_ERR_EMPTY; + return FALSE; + } + $rs->Close(); + return TRUE; + } + + /** + * INSERT or UPDATE the data to the database. + * + * @param array $data Array containing all the data to save as 'field' => 'value'. + * @return bool Returns TRUE on success, FALSE on error. + */ + function Save($data) + { + if (empty($data)) { + $this->error = TB_ERR_EMPTY; + return FALSE; + } + $values = array(); + $where = ''; + foreach ($this->fields as $field) { + if (($value = $field->Consist($data)) === FALSE) { + $this->error = TB_ERR_INCONSIST; + return FALSE; + } + if ($field->pkey) { + $where .= (strlen($where)?' AND ':'')."{$field->name_db} = $value"; + } else { + $values[$field->name_db] = $value; + } + } + if (empty($where)) { + if (empty($values)) { + $this->error = TB_ERR_EMPTY; + return FALSE; + } + if (!$this->db->Insert($this->name_db, $values)) { + $this->error = TB_ERR_DB; + return FALSE; + } + return TRUE; + } + if (empty($values)) { + $this->error = TB_ERR_EMPTY; + return FALSE; + } + if (!($rs = $this->db->Query("SELECT COUNT(*) AS cnt FROM {$this->name_db} WHERE $where"))) { + $this->error = TB_ERR_DB; + return FALSE; + } + if (($row = $rs->Row()) === FALSE) { + $this->error = TB_ERR_DB; + return FALSE; + } + $rs->Close(); + if ($row['cnt'] > 0) { + if (!$this->db->Update($this->name_db, $values, $where)) { + $this->error = TB_ERR_DB; + return FALSE; + } + } else { + if (!($rs =& $this->db->Insert($this->name_db, $values))) { + $this->error = TB_ERR_DB; + return FALSE; + } + $id = $rs->LastId(); + return (($id > 0)?$id:TRUE); + } + return TRUE; + } + + /** + * Delete a record from the table. + * + * @param array $data Array containing the primary key(s) to the table as 'field' => 'value', or NULL to delete the current record. + * @return bool Returns TRUE on success, FALSE on error. + */ + function Delete($data = NULL) + { + if (!empty($data)) if (!$this->Seek($data)) return FALSE; + $where = ''; + foreach ($this->fields as $field) if ($field->pkey) { + if (isset($this->data[$field->name_db])) $where .= (strlen($where)?' AND ':'')."{$field->name_db} = ".$field->ConsistFormat($this->data[$field->name_db]); + } + if (!strlen($where)) { + $this->error = TB_ERR_NOPKEY; + return FALSE; + } + if (!$this->db->Delete($this->name_db, $where)) { + $this->error = TB_ERR_DB; + return FALSE; + } + return TRUE; } } |