c7f7f07ddaf0a4894c17cdf5e53b9ab30915872f
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) 
Bernd Wurst Lizenzinfos in eigenes Modu...

Bernd Wurst authored 10 years ago

5) Written 2008-2014 by schokokeks.org Hosting, namely
Bernd Wurst Added license tags for CC0,...

Bernd Wurst authored 12 years ago

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/base.php');
18) require_once('inc/debug.php');
19) 
20) 
21) abstract class KeksData
22) {
23)   protected $default_table;
24)   
bernd Cookie einen eindeutigen Na...

bernd authored 16 years ago

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

bernd authored 16 years ago

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

bernd authored 16 years ago

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

bernd authored 16 years ago

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

bernd authored 16 years ago

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

bernd authored 16 years ago

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

bernd authored 16 years ago

49)     elseif (isset($this->$key))
50)       $this->$key = $value;
51)     else
52)       $this->data[$key] = $value;
53)   }
54) 
55)   protected function setup()
56)   {
57)     $fields = array();
58)     $res = db_query("DESCRIBE {$this->default_table}");
Bernd Wurst Umstellung auf PDO-Datenban...

Bernd Wurst authored 10 years ago

59)     while ($f = $res->fetch(PDO::FETCH_OBJ))
bernd Ein paar neue Klassen

bernd authored 16 years ago

60)     {
61)       $fields[$f->Field] = $f->Default;
62)     }
bernd schokokeks.org-Domain ist w...

bernd authored 16 years ago

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

bernd authored 16 years ago

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

bernd authored 16 years ago

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

bernd authored 16 years ago

67)   }
68) 
69) 
70)   function getData($fields, $restriction = NULL, $table = NULL)
71)   {
72)     $where = '';
73)     if ($restriction)
74)       $where = 'WHERE '.$restriction;
75)     if (! $table)
76)       $table = $this->default_table;
77)     if (is_array($fields))
78)       $fields = implode(',', $fields);
79)     
Bernd Wurst Einige Statements auf Prepa...

Bernd Wurst authored 10 years ago

80)     $res = db_query("SELECT {$fields} FROM {$table} {$where}", array()); // FIXME Übergebe leeren array um die Warnung zu unterdrücken
bernd Ein paar neue Klassen

bernd authored 16 years ago

81)     $return = array();
Bernd Wurst Umstellung auf PDO-Datenban...

Bernd Wurst authored 10 years ago

82)     while ($arr = $res->fetch())
bernd Ein paar neue Klassen

bernd authored 16 years ago

83)       array_push($return, $arr);
84)     return $return;
85)   }
86) 
87) 
88)   function loadByID($id)
89)   {
90)     $id = (int) $id;
bernd Cookie einen eindeutigen Na...

bernd authored 16 years ago

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

bernd authored 16 years ago

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

bernd authored 16 years ago

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

bernd authored 16 years ago

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

bernd authored 16 years ago

99)   function save()
100)   {
101)     $upd = array();
102)     foreach ($this->changes as $key => $value)
103)     {
Bernd Wurst Umstellung auf PDO-Datenban...

Bernd Wurst authored 10 years ago

104)       $value = db_escape_string($value);
bernd Cookie einen eindeutigen Na...

bernd authored 16 years ago

105)       array_push($upd, "`{$key}`='{$value}'");
106)     }
Bernd Wurst Einige Statements auf Prepa...

Bernd Wurst authored 10 years ago

107)     db_query("UPDATE {$this->default_table} SET ".implode(', ', $upd)." WHERE id=?", array($this->data['id']));
bernd Cookie einen eindeutigen Na...

bernd authored 16 years ago

108)   }
109)