]> dev.renevier.net Git - syp.git/blob - inc/db/anydb.php
596561cbc9027c15f89cbaf51aae9b1b000046b6
[syp.git] / inc / db / anydb.php
1 <?php
2 /* Copyright (c) 2009 Arnaud Renevier, Inc, published under the modified BSD
3    license. */
4
5 class feature {
6     private $id = null;
7     private $lon = null;
8     private $lat = null;
9     private $imgpath = null;
10     private $title = null;
11     private $description = null;
12     private $date = 0;
13     private $user = null;
14
15     const err_lonlat_invalid = 1;
16
17     function __construct ($id, $lon, $lat, $imgpath, $title, $description, $date, $user) {
18         $this->imgpath = $imgpath;
19
20         // id
21         if (isset ($id)) {
22             $this->id = $id;
23         }
24
25         // title
26         $this->title = $title;
27
28         // description
29         $this->description = $description;
30
31         // date
32         $this->date = $date;
33
34         // user
35         $this->user = $user;
36
37         // longitude
38         if (!isset ($lon) || !is_numeric ($lon) ||
39              ($lon < -180) || ($lon > 180)) {
40             throw new Exception ($this->err_lonlat_invalid);
41         }
42         $this->lon = $lon;
43
44         // latitude
45         if (!isset ($lat) || !is_numeric ($lat) ||
46              ($lat < -90) || ($lat > 90)) {
47             throw new Exception ($this->err_lonlat_invalid);
48         }
49         $this->lat = $lat;
50     }
51
52     public function __get ($attr) {
53         if (isset ($this->$attr)) return $this->$attr;
54             else throw new Exception ('Unknow attribute '.$attr);
55     }
56
57     public function __set ($attr,$value) {
58         throw new Exception ('properties can only be set in constructor');
59     }
60
61 }
62
63 interface anydbConnection {
64     const err_driver_unavailable = 1;
65     const err_connection = 2;
66     const err_unknown_database = 3;
67     const err_query = 3;
68
69     /*
70      * connect to database; That method may be called multiple times.
71      */
72     public function connect($host, $user, $pwd, $dbname, $dbprefix);
73
74     /*
75      * return true if users table already exists
76      */
77     public function users_table_exists();
78
79     /*
80      * create users table; 
81      * throws an err_query error in case users table already exists.
82      */
83     public function create_users_table();
84
85     /*
86      * return true if items table already exists
87      */
88     public function items_table_exists();
89
90     /*
91      * create items table;
92      * throws an err_query error in case items table already exists.
93      */
94     public function create_items_table();
95
96     /*
97      * set password $pwd for user $usrname. 
98      * If $usrname does not exist:
99      *    - if $create_if_not_exists is true: create user.
100      *    - if $create_if_not_exists is false: throws an err_query error.
101      */
102     public function setpwd($usrname, $pwd, $create_if_not_exists);
103
104     /*
105      * check that $pwd_md5 is md5 for $username password.
106      */
107     public function checkpwdmd5($usrname, $pwd_md5);
108
109     /*
110      * saves feature in database. If feature has an id, feature will be updated
111      * in database; otherwise it will be created. Returns saved feature
112      */
113     public function save_feature($feature);
114
115     /*
116      * delete feature from database. Returns true in case of success, even if
117      * image was not referenced in the database.
118      */
119     public function delete_feature($feature);
120
121     /*
122      * Returns feature with given id. If none exists, returns null.
123      */
124     public function getfeature($id);
125
126     /*
127      * returns an array of features managed by $user. If $user is undefined or
128      * if user is "admin", return all available features.
129      */
130     public function listfeatures($user);
131
132     /*
133      * returns the most recent features sorted by date. If $num_features is not
134      * defined or is null, returns all features sorted by date.
135      */
136     public function mostrecentfeatures($num_features);
137
138     /*
139      * returns true if a feature with imgpath exists
140      */
141     public function imgpath_exists($imgpath);
142
143     /*
144      * get name of database backend
145      */
146     public function getdbname();
147 }
148 ?>