class/keksdata.php
e4c9511b
 <?php
 
 require_once('inc/db_connect.php');
 require_once('inc/base.php');
 require_once('inc/debug.php');
 
 
 abstract class KeksData
 {
   protected $default_table;
   
2e004ad3
   protected $raw_data = array();
e4c9511b
   protected $data = array();
2e004ad3
   protected $changes = array();
e4c9511b
 
   function __get($key)
   {
     if (array_key_exists($key, $this->data))
       return $this->data[$key];
     elseif (isset($this->$key))
       return $this->$key;
     // else werfe fehler
   }
 
   function __set($key, $value)
   {
2e004ad3
     if (array_key_exists($key, $this->raw_data))
     {
       $this->raw_data[$key] = $value;
       $this->changes[$key] = $value;
       $this->parse($this->raw_data);
     }
     elseif (array_key_exists($key, $this->data))
d6195c0e
       $this->data[$key] = $value;
       // return false;
e4c9511b
     elseif (isset($this->$key))
       $this->$key = $value;
     else
       $this->data[$key] = $value;
   }
 
   protected function setup()
   {
     $fields = array();
     $res = db_query("DESCRIBE {$this->default_table}");
     while ($f = mysql_fetch_object($res))
     {
       $fields[$f->Field] = $f->Default;
     }
d6195c0e
     $this->raw_data = $fields;
     $this->raw_data['id'] = NULL;
e4c9511b
     $this->data = $fields;
c0d71b32
     $this->data['id'] = NULL;
e4c9511b
   }
 
 
   function getData($fields, $restriction = NULL, $table = NULL)
   {
     $where = '';
     if ($restriction)
       $where = 'WHERE '.$restriction;
     if (! $table)
       $table = $this->default_table;
     if (is_array($fields))
       $fields = implode(',', $fields);
     
     $res = db_query("SELECT {$fields} FROM {$table} {$where}");
     $return = array();
     while ($arr = mysql_fetch_assoc($res))
       array_push($return, $arr);
     return $return;
   }
 
 
   function loadByID($id)
   {
     $id = (int) $id;
2e004ad3
     DEBUG("requested to load ID »{$id}«");
e4c9511b
     $res = $this->getData('*', "id={$id} LIMIT 1");
c0d71b32
     if (count($res) < 1)
       return false;
e4c9511b
     $this->parse($res[0]);
   }
 
 
2e004ad3
   function save()
   {
     $upd = array();
     foreach ($this->changes as $key => $value)
     {
       $value = mysql_real_escape_string($value);
       array_push($upd, "`{$key}`='{$value}'");
     }
     db_query("UPDATE {$this->default_table} SET ".implode(', ', $upd)." WHERE id={$this->data['id']};");
   }
 
e4c9511b
   abstract function parse($data);
 
 }
 
 ?>