- Moved rendition of API::do_clean() to ISSO::clean()
[isso.git] / api.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Iris Studios Shared Object Framework [#]version[#]
5 || # Copyright ©2002-[#]year[#] Iris Studios, Inc.
6 || #
7 || # This program is free software; you can redistribute it and/or modify
8 || # it under the terms of the GNU General Public License as published by
9 || # the Free Software Foundation; version [#]gpl[#] of the License.
10 || #
11 || # This program is distributed in the hope that it will be useful, but
12 || # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 || # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 || # more details.
15 || #
16 || # You should have received a copy of the GNU General Public License along
17 || # with this program; if not, write to the Free Software Foundation, Inc.,
18 || # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 || ###################################################################
20 \*=====================================================================*/
21
22 /**
23 * Abstract Datamanger API
24 * api.php
25 *
26 * @package ISSO
27 */
28
29 if (!defined('REQ_YES'))
30 {
31 /**
32 * Yes, required
33 */
34 define('REQ_YES', 1);
35
36 /**
37 * No, not required
38 */
39 define('REQ_NO', 0);
40
41 /**
42 * Auto-increasing value
43 */
44 define('REQ_AUTO', -1);
45 }
46
47 /**
48 * Abstract API
49 *
50 * Abstract class that is used as an API base for most common database interaction
51 * schemes. Creates a simple structure that holds data and can update, delete, and
52 * insert.
53 *
54 * @author Iris Studios, Inc.
55 * @copyright Copyright ©2002 - [#]year[#], Iris Studios, Inc.
56 * @version $Revision$
57 * @package ISSO
58 *
59 */
60 class API
61 {
62 /**
63 * Registry object
64 * @var object
65 */
66 var $registry = null;
67
68 /**
69 * Fields: used for verification and sanitization
70 * NAME => array(TYPE, REQUIRED, VERIFY METHOD (:self for self-named method))
71 * @var array
72 */
73 var $fields = array();
74
75 /**
76 * Values array: sanitized and verified field values
77 * @var array
78 */
79 var $values = array();
80
81 /**
82 * Fields that were manually set with set(), not by using set_existing()
83 * @var array
84 */
85 var $setfields = array();
86
87 /**
88 * WHERE condition
89 * @var string
90 */
91 var $condition = '';
92
93 /**
94 * The object table row; a fetched row that represents this instance
95 * @var array
96 */
97 var $objdata = array();
98
99 /**
100 * Insert ID from the insert() command
101 * @var integer
102 */
103 var $insertid = 0;
104
105 /**
106 * Constructor: cannot instantiate class directly
107 */
108 function API(&$registry)
109 {
110 if (!is_subclass_of($this, 'API'))
111 {
112 trigger_error('Cannot instantiate the API module directly', E_USER_ERROR);
113 }
114
115 if (!is_object($registry))
116 {
117 trigger_error('The passed registry is not an object', E_USER_ERROR);
118 }
119
120 $this->registry =& $registry;
121 }
122
123 /**
124 * Sets a value, sanitizes it, and verifies it
125 *
126 * @param string Field name
127 * @param mixed Value
128 * @param bool Do clean?
129 * @param bool Do verify?
130 */
131 function set($field, $value, $doclean = true, $doverify = true)
132 {
133 if (!isset($this->fields["$field"]))
134 {
135 trigger_error('Field `' . $field . '` is not valid', E_USER_WARNING);
136 return;
137 }
138
139 $this->values["$field"] = ($doclean ? $this->registry->clean($value, $this->fields["$field"][0]) : $value);
140
141 $this->setfields[] = $field;
142
143 if (isset($this->fields["$field"][2]) AND $doverify)
144 {
145 if ($this->fields["$field"][2] == ':self')
146 {
147 $verify = $this->{"verify_$field"}($field);
148 }
149 else
150 {
151 $verify = $this->{$this->fields["$field"][2]}($field);
152 }
153
154 if (!$verify)
155 {
156 // <ERR:CVT>
157 trigger_error('Validation of `' . $field . '` failed', E_USER_ERROR);
158 }
159 }
160 }
161
162 /**
163 * Sets the condition to use in the WHERE clause; if not passed, then it calculates it
164 * from the REQ_AUTO field
165 *
166 * @param string WHERE conditional bit
167 */
168 function set_condition($condition = '')
169 {
170 if ($condition != '')
171 {
172 $this->condition = $condition;
173 }
174 else
175 {
176 foreach ($this->fields AS $name => $options)
177 {
178 if ($options[1] == REQ_AUTO)
179 {
180 if (!$this->values["$name"])
181 {
182 trigger_error('Cannot determine condition from the REQ_AUTO field because it is not set', E_USER_WARNING);
183 continue;
184 }
185
186 $this->condition = "$name = " . (($options[0] == TYPE_NOCLEAN OR $options[0] == TYPE_STR) ? "'" . $this->values["$name"] . "'" : $this->values["$name"]);
187 }
188 }
189
190 if ($this->condition == '')
191 {
192 trigger_error('No REQ_AUTO fields are present and therefore the condition cannot be created', E_USER_WARNING);
193 }
194 }
195 }
196
197 /**
198 * Sets existing data into $values where it's not already present
199 */
200 function set_existing()
201 {
202 static $run;
203 if ($run)
204 {
205 return;
206 }
207
208 $this->fetch();
209
210 foreach ($this->objdata AS $field => $value)
211 {
212 if (!isset($this->values["$field"]))
213 {
214 $this->values["$field"] = $value;
215 }
216 }
217
218 $run = true;
219 }
220
221 /**
222 * Fetches a record based on the condition
223 */
224 function fetch()
225 {
226 if ($this->condition == '')
227 {
228 trigger_error('Condition is empty: cannot fetch', E_USER_ERROR);
229 }
230
231 $actmethod = (method_exists($this, 'pre_fetch') ? $this->pre_fetch() : '');
232
233 $result = $this->registry->modules['db_mysql']->query_first("SELECT * FROM {$this->prefix}{$this->table} WHERE {$this->condition}");
234 if (!$result)
235 {
236 // <ERR:CVT>
237 trigger_error('No record was returned', E_USER_ERROR);
238 return;
239 }
240
241 $actmethod = (method_exists($this, 'post_fetch') ? $this->post_fetch() : '');
242
243 $this->objdata = $result;
244 }
245
246 /**
247 * Inserts a record in the database
248 */
249 function insert()
250 {
251 $this->verify();
252
253 $actmethod = (method_exists($this, 'pre_insert') ? $this->pre_insert() : '');
254
255 foreach ($this->setfields AS $field)
256 {
257 $fields[] = $field;
258 $values[] = (($this->fields["$field"][0] == TYPE_NOCLEAN OR $this->fields["$field"][0] == TYPE_STR) ? "'" . $this->values["$field"] . "'" : $this->values["$field"]);
259 }
260
261 $this->registry->modules['db_mysql']->query("INSERT INTO {$this->prefix}{$this->table} (" . implode(',', $fields) . ") VALUES (" . implode(',', $values) . ")");
262 $this->insertid = $this->registry->modules['db_mysql']->insert_id();
263
264 $actmethod = (method_exists($this, 'post_insert') ? $this->post_insert() : '');
265 }
266
267 /**
268 * Updates a record in the database using the data in $vaues
269 */
270 function update()
271 {
272 if ($this->condition == '')
273 {
274 trigger_error('Condition is empty: cannot fetch', E_USER_ERROR);
275 }
276
277 $this->set_existing();
278 $this->verify();
279
280 $actmethod = (method_exists($this, 'pre_update') ? $this->pre_update() : '');
281
282 foreach ($this->setfields AS $field)
283 {
284 $updates[] = "$field = " . (($this->fields["$field"][0] == TYPE_NOCLEAN OR $this->fields["$field"][0] == TYPE_STR) ? "'" . $this->values["$field"] . "'" : $this->values["$field"]);
285 }
286 $updates = implode(', ', $updates);
287
288 $this->registry->modules['db_mysql']->query("UPDATE {$this->prefix}{$this->table} SET $updates WHERE {$this->condition}");
289
290 $actmethod = (method_exists($this, 'post_update') ? $this->post_update() : '');
291 }
292
293 /**
294 * Deletes a record
295 */
296 function delete()
297 {
298 if ($this->condition == '')
299 {
300 trigger_error('Condition is empty: cannot fetch', E_USER_ERROR);
301 }
302
303 $this->set_existing();
304
305 $actmethod = (method_exists($this, 'pre_delete') ? $this->pre_delete() : '');
306
307 $this->registry->modules['db_mysql']->query("DELETE FROM {$this->prefix}{$this->table} WHERE {$this->condition}");
308
309 $actmethod = (method_exists($this, 'post_delete') ? $this->post_delete() : '');
310 }
311
312 /**
313 * Verifies that all required fields are set
314 */
315 function verify()
316 {
317 foreach ($this->fields AS $name => $options)
318 {
319 if ($options[1] == REQ_YES)
320 {
321 if (!isset($this->values["$name"]))
322 {
323 // <ERR:CVT>
324 trigger_error('Field `' . $name . '` was not set', E_USER_ERROR);
325 }
326 }
327 }
328 }
329
330 /**
331 * Verify field: not a zero value
332 */
333 function verify_nozero($field)
334 {
335 if ($this->values["$field"] == 0)
336 {
337 return false;
338 }
339
340 return true;
341 }
342
343 /**
344 * Verify field: not empty
345 */
346 function verify_noempty($field)
347 {
348 if (empty($this->values["$field"]))
349 {
350 return false;
351 }
352
353 return true;
354 }
355 }
356
357 /*=====================================================================*\
358 || ###################################################################
359 || # $HeadURL$
360 || # $Id$
361 || ###################################################################
362 \*=====================================================================*/
363 ?>