2247ba05f34e79602a47107d4d94ad87ccd1154c
bernd Ein paar neue Klassen

bernd authored 16 years ago

1) <?php
Bernd Wurst Added license tags for CC0,...

Bernd Wurst authored 12 years ago

2) /*
3) This file belongs to the Webinterface of schokokeks.org Hosting
4) 
5) Written 2008-2012 by schokokeks.org Hosting, namely
6)   Bernd Wurst <bernd@schokokeks.org>
7)   Hanno Böck <hanno@schokokeks.org>
8) 
9) To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty.
10) 
11) You should have received a copy of the CC0 Public Domain Dedication along with this software. If not, see 
12) http://creativecommons.org/publicdomain/zero/1.0/
13) 
14) Nevertheless, in case you use a significant part of this code, we ask (but not require, see the license) that you keep the authors' names in place and return your changes to the public. We would be especially happy if you tell us what you're going to do with this code.
15) */
bernd Ein paar neue Klassen

bernd authored 16 years ago

16) 
17) require_once('inc/db_connect.php');
18) require_once('inc/base.php');
19) require_once('inc/debug.php');
20) 
21) 
22) abstract class KeksData
23) {
24)   protected $default_table;
25)   
bernd Cookie einen eindeutigen Na...

bernd authored 16 years ago

26)   protected $raw_data = array();
bernd Ein paar neue Klassen

bernd authored 16 years ago

27)   protected $data = array();
bernd Cookie einen eindeutigen Na...

bernd authored 16 years ago

28)   protected $changes = array();
bernd Ein paar neue Klassen

bernd authored 16 years ago

29) 
30)   function __get($key)
31)   {
32)     if (array_key_exists($key, $this->data))
33)       return $this->data[$key];
34)     elseif (isset($this->$key))
35)       return $this->$key;
36)     // else werfe fehler
37)   }
38) 
39)   function __set($key, $value)
40)   {
bernd Cookie einen eindeutigen Na...

bernd authored 16 years ago

41)     if (array_key_exists($key, $this->raw_data))
42)     {
43)       $this->raw_data[$key] = $value;
44)       $this->changes[$key] = $value;
45)       $this->parse($this->raw_data);
46)     }
47)     elseif (array_key_exists($key, $this->data))
bernd schokokeks.org-Domain ist w...

bernd authored 16 years ago

48)       $this->data[$key] = $value;
49)       // return false;
bernd Ein paar neue Klassen

bernd authored 16 years ago

50)     elseif (isset($this->$key))
51)       $this->$key = $value;
52)     else
53)       $this->data[$key] = $value;
54)   }
55) 
56)   protected function setup()
57)   {
58)     $fields = array();
59)     $res = db_query("DESCRIBE {$this->default_table}");
60)     while ($f = mysql_fetch_object($res))
61)     {
62)       $fields[$f->Field] = $f->Default;
63)     }
bernd schokokeks.org-Domain ist w...

bernd authored 16 years ago

64)     $this->raw_data = $fields;
65)     $this->raw_data['id'] = NULL;
bernd Ein paar neue Klassen

bernd authored 16 years ago

66)     $this->data = $fields;
bernd Domain-Klasse benutzen

bernd authored 16 years ago

67)     $this->data['id'] = NULL;
bernd Ein paar neue Klassen

bernd authored 16 years ago

68)   }
69) 
70) 
71)   function getData($fields, $restriction = NULL, $table = NULL)
72)   {
73)     $where = '';
74)     if ($restriction)
75)       $where = 'WHERE '.$restriction;
76)     if (! $table)
77)       $table = $this->default_table;
78)     if (is_array($fields))
79)       $fields = implode(',', $fields);
80)     
81)     $res = db_query("SELECT {$fields} FROM {$table} {$where}");
82)     $return = array();
83)     while ($arr = mysql_fetch_assoc($res))
84)       array_push($return, $arr);
85)     return $return;
86)   }
87) 
88) 
89)   function loadByID($id)
90)   {
91)     $id = (int) $id;
bernd Cookie einen eindeutigen Na...

bernd authored 16 years ago

92)     DEBUG("requested to load ID »{$id}«");
bernd Ein paar neue Klassen

bernd authored 16 years ago

93)     $res = $this->getData('*', "id={$id} LIMIT 1");
bernd Domain-Klasse benutzen

bernd authored 16 years ago

94)     if (count($res) < 1)
95)       return false;
bernd Ein paar neue Klassen

bernd authored 16 years ago

96)     $this->parse($res[0]);
97)   }
98) 
99) 
bernd Cookie einen eindeutigen Na...

bernd authored 16 years ago

100)   function save()
101)   {
102)     $upd = array();
103)     foreach ($this->changes as $key => $value)
104)     {
105)       $value = mysql_real_escape_string($value);
106)       array_push($upd, "`{$key}`='{$value}'");
107)     }
108)     db_query("UPDATE {$this->default_table} SET ".implode(', ', $upd)." WHERE id={$this->data['id']};");
109)   }
110)