Convert script to strict mode
[infodrom.org/service.infodrom.org] / class / databasetable.class.php
1 <?php
2
3 abstract class DatabaseTable {
4   protected $idcolumn = 'id';
5   protected $db;
6   protected $table;
7   protected $data;
8   protected $id;
9   protected $rootPath;
10
11   public function __construct($table, $id=false, $column_or_db=false, $column=false)
12   {
13     global $db;
14
15     $this->table = $table;
16
17     if ($column_or_db && is_object($column_or_db)) {
18       $this->db = $column_or_db;
19       $col = $column;
20     } else {
21       $this->db = $db;
22       $col = $column_or_db;
23     }
24
25     if ($id && !$col) $this->load($id);
26     if ($id && $col) $this->loadByColumn($id,$col);
27
28     $path = substr($_SERVER['HTTP_REFERER'], strpos($_SERVER['HTTP_REFERER'], $_SERVER['SERVER_NAME'])+strlen($_SERVER['SERVER_NAME'])+1);
29
30     $pos = 0;
31     $this->rootPath = '';
32     while (($pos = strpos($path, '/', $pos)) !== false) {
33       $this->rootPath .= '../';
34       $pos++;
35     }
36   }
37
38   protected function postLoad() {}
39
40   protected function load($id)
41   {
42     $sql = sprintf("SELECT * FROM %s WHERE %s = %d LIMIT 1",
43                    $this->table, $this->idcolumn, $id);
44     $this->data = $this->db->fetchObject($sql);
45     if ($this->data) {
46       $idcolumn = $this->idcolumn;
47       $this->id = $this->data->$idcolumn;
48       $this->postLoad();
49       return true;
50     }
51     return false;
52   }
53
54   protected function loadByColumn($id,$column)
55   {
56     $sql = sprintf("SELECT * FROM %s WHERE %s = %s",
57            $this->table, $column, $this->db->quote($id));
58     $this->data = $this->db->fetchObject($sql);
59     if ($this->data) {
60       $idcolumn = $this->idcolumn;
61       $this->id = $this->data->$idcolumn;
62       $this->postLoad();
63     }
64   }
65
66   public function id()
67   {
68     return $this->id;
69   }
70
71   public function fetch()
72   {
73     return $this->data;
74   }
75
76   public function modify($column, $value=false, $id=false)
77   {
78     if (!is_array($column) && $value !== false)
79       $column = array($column => $value);
80     elseif (!is_array($column) || $value !== false)
81       throw Exception('modify called with wrong parameters');
82
83     if ($this->db->update($this->table, $column, $this->idcolumn.'='.($id === false ? $this->id : $id)))
84       return $id === false ? $this->load($this->id) : true;
85     else
86       return false;
87   }
88
89   public function delete($id=false)
90   {
91     $this->db->delete($this->table, $this->idcolumn, $id === false ? $this->id : $id);
92   }
93
94   public function quote($string) { return $this->db->quote($string); }
95   public function execute($sql) { return $this->db->execute($sql); }
96   public function fetchValue($sql) { return $this->db->fetchValue($sql); }
97   public function fetchAssoc($sql) { return $this->db->fetchAssoc($sql); }
98   public function fetchAssocList($sql) { return $this->db->fetchAssocList($sql); }
99   public function fetchObject($sql) { return $this->db->fetchObject($sql); }
100   public function fetchObjectList($sql) { return $this->db->fetchObjectList($sql); }
101 }