summaryrefslogtreecommitdiff
path: root/db_record.class
blob: a716117b669d0ebcdb604abe76995c56900970bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
<?php
/*
**	$Id$
**
**	it_db_record.class - A database record based on it_db_table and it_db
**
**	ITools - the Internet Tools Library
**
**	Copyright (C) 1995-2003 by the ITools Authors.
**	This program is free software; you can redistribute it and/or
**	modify it under the terms of either the GNU General Public License
**	or the GNU Lesser General Public License, as published by the Free
**	Software Foundation. See http://www.gnu.org/licenses/ for details.
*/

/**
 * Functions to handle MySQL records
 * @see it_db, it_db_table
 */
class it_db_record
{
	var $table;		/* Table object */
	var $handle;		/* SQL handle for select() / fetch_next() */
	var $keyfieldname;	/* Name of primary key field */
	var $key;		/* Key of loaded record or empty if no record is loaded */
	var $data = array();	/* Data fields */


/**
 * Initialize internal data (table object and name of key field)
 * @param $table Underlying table object (class it_db_table)
 * @param $keyfieldname Optional name of primary key field, defaults to 'ID'
 */
function it_db_record(&$table, $keyfieldname='ID')
{
	$this->table = &$table;
	$this->keyfieldname = $keyfieldname;
}


/**
 * Change active key field
 * @param $keyfieldname Optional name of primary key field, defaults to 'ID'
 */
function set_key_field($keyfieldname='ID')
{
	$this->keyfieldname = $keyfieldname;
	$this->key = isset($this->data[$keyfieldname]) ? $this->data[$keyfieldname] : '';
}


/**
 * Select a set of records of this table according to an array of selection
 * and sorting criteria.
 * Use fetch_next() to iterate through the result set<br>
 * Example:<br>
 * $record->select(array('Type' => 'bar', 'Date >=' => '1999-01-01',
 *    '-Date <' => 'NOW()', 'User NI' => 'chris'), 'ORDER BY Date');
 *
 * @param $params optional array of fieldname => value tupels.
 * These are ANDed to form a WHERE clause.
 * fieldname can contain an operator (separated by space), the
 * default operator is '='. The special operator 'NI' specifies
 * that the argument must be contained in a comma-separated list.
 *
 * @param $sql Optional SQL addendum added after $params (ORDER BY etc.)
 * @param $fields Optional field list to select (defaults to '*')
 * @return Number of rows that matched this query
 * @see it_db_record::fetch_next, it_db_table::construct_sql_clause
 *
 */
function select($params='', $sql='', $fields='*')
{
	$this->handle = $this->table->safe_sql_select($this->table->construct_sql_clause($params, $sql), $fields);
	return $this->table->db->num_rows($this->handle);
}


/**
 * Post-process data after reading a record.
 * This is a stub-function that can be overloaded.
 */
function _read_post_process()
{
}


/**
 * Pre-process data before writing a record.
 * This is a stub-function that can be overloaded.
 * @param $tags Reference to update/create tags, can be modified as needed
 */
function _write_pre_process(&$tags)
{
}


/**
 * Fetch the next record of a select() query
 * @return true if a record was successfully fetched, false otherwise
 * @see it_db_record::select
 */
function fetch_next()
{
	return $this->fetch($this->handle);
}


/**
 * Fetch the next record from an SQL result into this object.
 * If no more records are pending, clear the object's data.
 * @param $sqlresult Result of table::sql_select() or similar
 * @return 1 if a record was successfully fetched, 0 otherwise
 */
function fetch(&$sqlresult)
{
	$result = 0;

	if ($this->data = $this->table->db->fetch_assoc($sqlresult))
	{
		$this->key = $this->data[$this->keyfieldname];
		$result = 1;
	}
	else
	{
		$this->key = "";	/* "No record is loaded" */
		$this->data = array();	/* Clear local record data */
	}

	$this->_read_post_process();

	return  $result;
}


/**
 * Read a record from table
 * @param $key key field value of the desired row
 * @return 1 on success, 0 otherwise
 */
function read($key)
{
	$result = 0;

	if ($key != "")
	{
		$result = $this->fetch($this->table->safe_sql_select("WHERE $this->keyfieldname='" . mysql_real_escape_string($key) . "'"));
	}
	else
	{
		$this->key = "";	/* "No record is loaded" */
		$this->data = array();	/* Clear local record data */
	}

	$this->_read_post_process();

	return $result;
}


/**
 * Create a new record in the table
 * @see it_db_record::record_create, it_db_record::safe_create
 * @param $tags array of Fieldname/Value pairs
 * @param $replace use REPLACE (1) instead of INSERT (otherwise)
 * @return MySQL result (0 means failure)
 */
function create($tags, $replace=0)
{
	/* Pre-processing, $tags is passed by reference and may be modified here */
	$this->_write_pre_process($tags);

	$sql = $replace ? "REPLACE" : "INSERT";
	$sql .= " INTO " . $this->table->name . " (";
	$sql2 = '';

	unset($this->data);
	for ($sep=""; $ti = each($tags); $sep=",")
	{
		/* If the field name starts with '-', the value is taken as raw, no escaping
		   is done and no quotes are put around it. */
		if (substr($ti[0], 0, 1) == '-')
		{
			$ti[0] = substr($ti[0], 1);	/* Strip that '-' sign */
			$val = $ti[1];
		}
		else
			$val = isset($ti[1]) ? "'".mysql_real_escape_string($ti[1])."'" : 'NULL';

		$this->data[$ti[0]] = $ti[1];	/* Store data for later use */

		$sql .= $sep . $ti[0];
		$sql2 .= "$sep$val";
	}

	$sql .= ") VALUES ($sql2)";

	if ($result = $this->table->db->sql_query($sql))
	{
		/* If the key element was not given, it means it's an AUTO_INCREMENT thingy. */
		if (($this->key = $this->data[$this->keyfieldname]) == "")
		{
			$result = $this->table->db->safe_sql_query("SELECT LAST_INSERT_ID()");
			$this->key = mysql_result($result, 0, 0);
		}

		/* We need all fields from record, not only those we initialised */
		$this->read($this->key);
	}

	return $result;
}


/**
 * Safe version of create (doesn't return in case of failure)
 * @see it_db_record::create, it_db_record::record_create
 * @param $tags array of Fieldname/Value pairs
 * @param $replace use REPLACE (1) instead of INSERT (otherwise)
 * @return MySQL result (0 means failure)
 */
function safe_create($tags, $replace=0)
{
	if ($result = $this->create($tags, $replace))
		return $result;
	else
		fail("it_db_record::safe_create() failed: " . $this->table->db->error());
}


/**
 * Update a record in the table
 * @param $tags array of Fieldname/Value pairs
 * @return MySQL result (0 means failure)
 */
function update($tags)
{
	/* Pre-processing, $tags is passed by reference and may be modified here */
	$this->_write_pre_process($tags);

	if ($this->key == "")
		it::fatal("it_db_record::update(): can't update undefined record");

	/* If we have nothing to do, return instead of performing an invalid SQL query */
	if (!count($tags))
		return true;

	$sql = "UPDATE " . $this->table->name . " SET ";

	for ($sep="", $raw=0; $ti = each($tags); $sep=",")
	{
		/* If the field name starts with '-', the value is taken as raw, no escaping
		   is done and no quotes are put around it. */
		if (substr($ti[0], 0, 1) == '-')
		{
			$ti[0] = substr($ti[0], 1);	/* Strip that '-' sign */
			$val = $ti[1];
			$raw++;
		}
		else
			$val = "'".mysql_real_escape_string($ti[1])."'";

		$sql .= $sep.$ti[0].'='.$val;

		if ($ti[0] == $this->key)
		{
			if ($this->data[$this->keyfieldname] != $ti[0])
				it::fatal("$sql: trying to change key from $this->key.");
		}

		$this->data[$ti[0]] = $ti[1];
	}

	$sql .= " WHERE " . $this->keyfieldname . "='" . mysql_real_escape_string($this->key) . "'";

	/* debug("it_db_record::update(): $sql"); */
	$ret = $this->table->db->safe_sql_query($sql);

	/* Only re-read record if necessary (for performance), but always do post-processing. */
	if ($raw)
		$this->read($this->key);
	else
		$this->_read_post_process();

	return $ret;
}


/**
 * Delete the current record from the database
 * @param $key keyfield value of the record to be deleted. If this is missing, the current record is deleted. 
 * @return MySQL result (0 means failure)
 */
function delete($key="")
{
	if (!$key) $key = $this->key;

	if (($this->table->name == "") || ($this->keyfieldname == "") || ($key == ""))
		it::fatal("it_db_record::delete(): no record\n");

	$sql = "DELETE FROM " . $this->table->name . " WHERE ". $this->keyfieldname . "='" . mysql_real_escape_string($key) . "'";
	if ($result = $this->table->db->sql_query($sql))
	{
		$this->key = "";
		$this->data = array();
	}
	return $result;
}


/**
 * Return a field of a record.
 * @param $field field name
 * @return field value. If the field does not exists, issue an
 * error message and terminate program execution.
 */
function safe_get_field($field)
{
	if ($field)
	{
		if (isset($this->data[$field]))
			return $this->data[$field];
		else it::fatal("it_db_record::safe_get_field(): field \"$field\" not present in record.");
	}
	else it::fatal("it_db_record::safe_get_field(): empty field name");
}


/**
 * Output all fields of a record in (ugly) html format
 */
function dump_html()
{
	echo "<br>Dump of table " . $this->table->name . ", record " . $this->keyfieldname . " = \"" . $this->key . "\":<br><pre>\n";
	reset($this->data); next($this->data);
	while(list($key, $value) = each($this->data))
	{
		if ($key > 0) continue;	/* $$$ ugly */
		echo $key . " = \"" . htmlspecialchars($value) . "\"\n";
	}
	echo "</pre><br>\n";
}


/**
 * Edit a record of a table. Create a new record if necessary.<br>
 * Example:<br>
 * $record->edit($id, array('ID' => $id));
 *
 * @param $key key value of the row to edit
 * @param $fixfields Array of fields not editable (with fixed values)
 * @return 1=changed, 2=created, -1=deleted, 0=nothing changed
 */
function edit($key, $fixfields = array())
{
	$returnresult = 0;
	$numfields = $this->table->num_fields();

	if ($_REQUEST['_COMMAND'])
	{
		/*
		**  If the key field is changed, $key will carry a wrong value, so we
		**  take the correct one from $_RECORD_KEY_VALUE
		*/
		$key = $_REQUEST['_RECORD_KEY_VALUE'];

		switch($_REQUEST['_COMMAND'])
		{
		case "EDIT":
			$this->read($key);
			$newdata = array();
			$changes = 0;
			for ($i=0; $i < $numfields; ++$i)
			{
				$fieldname = $this->table->fieldnames[$i];

				/* Assumes field order is fixed */
				if (isset($fixfields[$fieldname]))
					$newvalue = $fixfields[$fieldname];
				else if (is_array($_REQUEST["_RECORD_$fieldname"]))
					$newvalue = join(',', $_REQUEST["_RECORD_$fieldname"]);
				else
					$newvalue = $_REQUEST["_RECORD_$fieldname"];
	
				if ($this->data[$fieldname] != $newvalue)
				{
					$newdata[$fieldname] = $newvalue;
					$changes = 1;
				}
			}

			if ($changes)
			{
				if ($this->update($newdata))
				{
					echo "<b>Änderung erfolgreich durchgef&uuml;hrt.</b><br>\n";
					$returnresult = 1;
				}
				else
					fail("Update failed.");
			}
			else echo "<b>Keine Änderungen.</b><br>\n";
			break;

		case "CREATE":
			$newdata = array();
			for ($i=0; $i < $numfields; ++$i)
			{
				$fieldname = $this->table->fieldnames[$i];
				
				if (isset($fixfields[$fieldname]))
					$newvalue = $fixfields[$fieldname];
				else if (is_array($_REQUEST["_RECORD_$fieldname"]))
					$newvalue = join(',', $_REQUEST["_RECORD_$fieldname"]);
				else
					$newvalue = $_REQUEST["_RECORD_$fieldname"];

				$newdata[$fieldname] = $newvalue;
			}

			if ($this->create($newdata))
			{
				$key = $this->key;
				echo "<b>Datensatz erzeugt.</b><br>\n";
				$returnresult = 2;
			}
			else
				fail("Datensatz konnte nicht erzeugt werden.");
			break;

		case "DELETE":
			if ($this->delete($key))
			{
				echo "<b>Datensatz gelöscht.</b><br>\n";
				$returnresult = -1;
			}
			else
				fail("Datensatz konnte nicht gelöscht werden.");
			break;

		default:
			fail("Unknown _COMMAND \"{$_REQUEST['_COMMAND']}\"");
			/* NOT REACHED */
		}
	}

	if ($key != "")
	{
		$this->read($key);
		$cmd = "EDIT";
		$buttontext = "Änderungen übernehmen";
	}
	else
	{
		$cmd = "CREATE";
		$buttontext = "Datensatz erzeugen";
	}

	echo "<form action=\"\" method=\"post\"><input type=\"hidden\" name=\"_COMMAND\" value=\"$cmd\"><input type=\"hidden\" name=\"_RECORD_KEY_NAME\" value=\"$this->keyfieldname\"><input type=\"hidden\" name=\"_RECORD_KEY_VALUE\" value=\"$this->key\">\n";
	echo "<table border=1 cellspacing=0 cellpadding=4>\n";
	for ($i=0; $i < $numfields; ++$i)
	{
		$fieldname = $this->table->fieldnames[$i];

		/* Do not display fix fields */
		if (isset($fixfields[$fieldname]))
			continue;

		$fieldflags = $this->table->fieldflags[$i];
		$fieldlen  = $this->table->fieldlengths[$i];
		$size = min($fieldlen, 65);

		if ($fieldname == $this->keyfieldname)
			echo "<tr><th align=right>$fieldname</th>";
		else
			echo "<tr><td align=right>$fieldname</td>";

		if (strstr($fieldflags, "blob"))
		{
			if (strlen($this->data["$fieldname"]) < 256)
				echo "<td><textarea name=\"_RECORD_$fieldname\" rows=4 cols=56 wrap=\"physical\">" . htmlspecialchars($this->data["$fieldname"]) . "</textarea></td></tr>\n";
			else
				echo "<td><textarea name=\"_RECORD_$fieldname\" rows=10 cols=75 wrap=\"physical\">" . htmlspecialchars($this->data["$fieldname"]) . "</textarea></td></tr>\n";
		}
		else if (strstr($fieldflags, "enum"))
		{
			$result = $this->table->db->safe_sql_query("SHOW columns FROM ". $this->table->name . " LIKE '$fieldname'");
			$options = split("'", mysql_result($result, 0, 1));

			$vals = explode(',', $this->data[$fieldname]);

			for ($j = 0; $j < count($vals); $j++)
				$values[$vals[$j]] = 1;

			if (strstr($options[0], 'set'))
				$attr = 'multiple';
			else
				$attr = 'set="1"';

			echo "<td><select name=\"_RECORD_${fieldname}[]\" $attr>";
			for ($j=1; $options[$j]; $j+=2)
			{
				$selected = $values[$options[$j]] ? " selected" : "";
				echo "<option$selected>", $options[$j], "</option>";
			}
			echo "</select></td></tr>\n";
		}
		else
			echo "<td><input type=\"text\" size=$size name=\"_RECORD_$fieldname\" maxlength=\"$fieldlen\" value=\"" . htmlspecialchars($this->data["$fieldname"]) . "\"></td></tr>\n";
	}
	echo "</table><br><input type=\"submit\" value=\"$buttontext\"></form>\n";

	if ($this->key != "")
		echo "<form action=\"\" method=\"post\"><input type=\"hidden\" name=\"_COMMAND\" value=\"DELETE\"><input type=\"hidden\" name=\"_RECORD_KEY_NAME\" value=\"$this->keyfieldname\"><input type=\"hidden\" name=\"_RECORD_KEY_VALUE\" value=\"$this->key\"><input type=\"submit\" value=\"Datensatz löschen\"></form>\n";

	return $returnresult;
}

} /* End class it_db_record */
?>