--- /dev/null
+<?php
+
+abstract class DatabaseTable {
+ protected $idcolumn = 'id';
+ protected $blobcolumns = array();
+ protected $autosave = true;
+ protected $userid;
+ protected $db;
+ protected $table;
+ protected $data;
+ protected $update;
+ protected $id;
+ protected $errorMsg = NULL;
+
+ public function __construct($table, $id=false, $column_or_db=false, $column=false)
+ {
+ global $db;
+
+ $this->table = $table;
+
+ if (!empty($_SESSION['userid']))
+ $this->userid = $_SESSION['userid'];
+
+ if ($column_or_db && is_object($column_or_db)) {
+ $this->db = $column_or_db;
+ $col = $column;
+ } else {
+ $this->db = $db;
+ $col = $column_or_db;
+ }
+
+ if ($id && !$col) $this->load($id);
+ if ($id && $col) $this->loadByColumn($id,$col);
+ $this->update = array();
+ }
+
+ public function __destruct()
+ {
+ if ($this->autosave) $this->save();
+ }
+
+ protected function postLoad() {}
+
+ protected function load($id)
+ {
+ $sql = sprintf("SELECT * FROM %s WHERE %s = %d LIMIT 1",
+ $this->table, $this->idcolumn, $id);
+ $this->data = $this->db->fetchObject($sql);
+ if ($this->data) {
+ $idcolumn = $this->idcolumn;
+ $this->id = $this->data->$idcolumn;
+ $this->postLoad();
+ return true;
+ }
+ return false;
+ }
+
+ protected function loadByColumn($id,$column)
+ {
+ $sql = sprintf("SELECT * FROM %s WHERE %s = %s",
+ $this->table, $column, $this->db->quote($id));
+ $this->data = $this->db->fetchObject($sql);
+ if ($this->data) {
+ $idcolumn = $this->idcolumn;
+ $this->id = $this->data->$idcolumn;
+ $this->postLoad();
+ }
+ }
+
+ protected function resetError()
+ {
+ $this->errorMsg = NULL;
+ }
+
+ protected function setError($msg, $return=false)
+ {
+ $this->errorMsg = $msg;
+ return $return;
+ }
+
+ public function getError()
+ {
+ return $this->errorMsg;
+ }
+
+ public function id()
+ {
+ $this->resetError();
+ return $this->id;
+ }
+
+ public function fetch()
+ {
+ $this->resetError();
+ if (is_object($this->data))
+ return clone $this->data;
+ $this->setError('Record not loaded');
+ return $this->data;
+ }
+
+ protected function modify($column, $value=false, $id=false)
+ {
+ $this->resetError();
+ if (!is_array($column) && $value !== false) {
+ if ($column == $this->idcolumn) return $this->setError('Cannot modify id column');
+ if (in_array($column, $this->blobcolumns)) {
+ $sql = sprintf("UPDATE %s SET %s = :data,sys_edit = now(), sys_user_id = %d WHERE %s = %d",
+ $this->table, $column,
+ $this->userid,
+ $this->idcolumn,
+ $id === false ? $this->id : $id);
+ if ($this->db->executeParameters($sql, array(array(':data', $value))))
+ return $id === false ? $this->load($this->id) : true;
+ else return $this->setError('Cannot modify record');
+ } else {
+ $sql = sprintf("UPDATE %s SET %s = %s,sys_edit = now(), sys_user_id = %d WHERE %s = %d",
+ $this->table, $column,
+ $this->db->quote($value),
+ $this->userid,
+ $this->idcolumn,
+ $id === false ? $this->id : $id);
+ if ($this->db->execute($sql))
+ return $id === false ? $this->load($this->id) : true;
+ else return $this->setError('Cannot modify record');
+ }
+ } elseif (is_array($column) && $value === false) {
+ $blobs = array();
+ $values = array('sys_user_id' => $this->userid,
+ 'sys_edit' => 'now()');
+ $values = array_merge($values, $column);
+ $set = array();
+ foreach ($values as $col => $val) {
+ if ($col == 'id') continue;
+ if (in_array($col, $this->blobcolumns))
+ $blobs[] = array(':'.$col, $val, 'blob');
+ else
+ $set[] = sprintf('%s = %s', $col, $this->db->quote($val));
+ }
+
+ if (count($blobs)) {
+ $blobset = array();
+ foreach ($blobs as $blob)
+ $blobset[] = sprintf('%s = %s', substr($blob[0],1), $blob[0]);
+ $sql = sprintf("UPDATE %s\nSET %s\nWHERE %s = %d",
+ $this->table,
+ implode(', ', $blobset),
+ $this->idcolumn,
+ $id === false ? $this->id : $id);
+ $this->db->executeParameters($sql, $blobs);
+ }
+
+ $sql = sprintf("UPDATE %s\nSET %s\nWHERE %s = %d",
+ $this->table,
+ implode(', ', $set),
+ $this->idcolumn,
+ $id === false ? $this->id : $id);
+ if ($this->db->execute($sql))
+ return $id === false ? $this->load($this->id) : true;
+ else
+ return $this->setError('Cannot modify record');
+ }
+
+ die(get_class($this).': Cannot modify object');
+ return false;
+ }
+
+ public function get($name)
+ {
+ $this->resetError();
+ return $this->data->$name;
+ }
+
+ public function set($name, $value=false)
+ {
+
+ if ((is_array($name) || is_object($name)) && $value === false) {
+ foreach ($name as $k => $v) {
+ if (strtolower($k) == $this->idcolumn) continue;
+
+ if (!is_object($this->data)) $this->data = new stdClass();
+
+ $this->data->$k = $v;
+ $this->update[$k] = $v;
+ }
+ } else {
+ if (strtolower($name) == $this->idcolumn) return $this->setError('Cannot set id column');
+
+ if (!is_object($this->data)) $this->data = new stdClass();
+
+ $this->data->$name = $value;
+ $this->update[$name] = $value;
+ }
+ return true;
+ }
+
+ public function save()
+ {
+ $this->resetError();
+ if (!count($this->update)) return true;
+
+ if ($this->id) {
+ if ($this->modify($this->update)) {
+ $this->update = array();
+ return true;
+ } else {
+ return $this->setError('Cannot save modifications');
+ }
+ } else {
+ if (!array_key_exists('sys_edit', $this->update))
+ $this->update['sys_edit'] = 'now()';
+ if (!array_key_exists('sys_user_id', $this->update))
+ $this->update['sys_user_id'] = $this->userid;
+ if ($this->db->insertInto($this->table, $this->update)) {
+ $this->update = array();
+ $this->load($this->db->lastInsertId());
+ return true;
+ } else {
+ return $this->setError('Cannot save new record');
+ }
+ }
+
+ return false;
+ }
+
+}