| 1 |
<?php |
<?php |
| 2 |
// $Id: object_driver.module,v 1.8 2007/04/08 16:23:32 pwolanin Exp $ |
// $Id: object_driver.module,v 1.9 2007/04/08 16:45:03 pwolanin Exp $ |
| 3 |
|
|
| 4 |
/** |
/** |
| 5 |
* hooks this module looks for (hook -> module name, classhook -> class name): |
* hooks this module looks for (hook -> module name, classhook -> class name): |
| 459 |
$obj_def = call_user_func($class .'_class_define'); |
$obj_def = call_user_func($class .'_class_define'); |
| 460 |
$table = isset($obj_def['table']) ? $obj_def['table'] : $class; |
$table = isset($obj_def['table']) ? $obj_def['table'] : $class; |
| 461 |
|
|
| 462 |
$obj = db_fetch_object(db_query("SELECT * FROM {%s} WHERE %s = %d", $table, $id_name, $id)); |
$obj = db_fetch_object(db_query("SELECT * FROM {". $table ."} WHERE %s = %d", $id_name, $id)); |
| 463 |
|
|
| 464 |
} |
} |
| 465 |
if (isset($obj->$id_name)) { |
if (isset($obj->$id_name)) { |
| 478 |
|
|
| 479 |
function object_driver_save(&$obj) { |
function object_driver_save(&$obj) { |
| 480 |
$class = $obj->class; |
$class = $obj->class; |
|
|
|
| 481 |
$id_name = $class .'_id'; |
$id_name = $class .'_id'; |
| 482 |
$obj->is_new = FALSE; |
// Is this a new object? |
| 483 |
|
$obj->is_new = empty($obj->$id_name); |
| 484 |
|
|
|
if (empty($obj->$id_name)) { |
|
|
// Insert a new object |
|
|
$obj->is_new = TRUE; |
|
|
$obj->$id_name = db_next_id("{$table}_id"); |
|
|
} |
|
|
|
|
| 485 |
if (function_exists($class .'_save')) { |
if (function_exists($class .'_save')) { |
| 486 |
call_user_func($class .'_save', $obj); |
call_user_func($class .'_save', $obj); |
| 487 |
} |
} |
| 488 |
else { // Default functionality |
else { // Default functionality |
| 489 |
$obj_def = call_user_func($class .'_class_define'); |
$obj_def = call_user_func($class .'_class_define'); |
| 490 |
$table = isset($obj_def['table']) ? $obj_def['table'] : $class; |
$table = isset($obj_def['table']) ? $obj_def['table'] : $class; |
| 491 |
|
|
| 492 |
|
if ($obj->is_new) { |
| 493 |
|
// Insert a new object |
| 494 |
|
$obj->$id_name = db_next_id("{". $table ."}_id"); |
| 495 |
|
} |
| 496 |
|
|
| 497 |
$column_names = array(); |
$column_names = array(); |
| 498 |
$column_placeholders = array(); |
$column_placeholders = array(); |
| 499 |
$column_assignments = array(); |
$column_assignments = array(); |
| 513 |
} |
} |
| 514 |
$data[] = $obj->$id_name; |
$data[] = $obj->$id_name; |
| 515 |
if ($obj->is_new) { |
if ($obj->is_new) { |
| 516 |
db_query("INSERT INTO {$table} (". implode(', ', $column_names) .", $id_name) VALUES (". implode(', ', $column_placeholders) .', %d)', $data); |
db_query("INSERT INTO {". $table ."} (". implode(', ', $column_names) .", $id_name) VALUES (". implode(', ', $column_placeholders) .', %d)', $data); |
| 517 |
} |
} |
| 518 |
else { |
else { |
| 519 |
db_query("UPDATE {$table} SET ". implode(', ', $column_assignments) ." WHERE $id_name = %d", $data); |
db_query("UPDATE {". $table ."} SET ". implode(', ', $column_assignments) ." WHERE $id_name = %d", $data); |
| 520 |
} |
} |
| 521 |
} |
} |
| 522 |
if ($obj->is_new) { |
if ($obj->is_new) { |
| 565 |
$table = isset($obj_def['table']) ? $obj_def['table'] : $class; |
$table = isset($obj_def['table']) ? $obj_def['table'] : $class; |
| 566 |
$id_name = $class .'_id'; |
$id_name = $class .'_id'; |
| 567 |
|
|
| 568 |
db_query("DELETE FROM {%s} WHERE %s = %d", $table, $id_name, $obj->$id_name); |
db_query("DELETE FROM {". $table ."} WHERE %s = %d", $id_name, $obj->$id_name); |
| 569 |
if (isset($obj->type)) { |
if (isset($obj->type)) { |
| 570 |
if (function_exists($obj->type .'_'. $class .'_delete')) { |
if (function_exists($obj->type .'_'. $class .'_delete')) { |
| 571 |
call_user_func($obj->type .'_'. $class .'_delete', $obj); |
call_user_func($obj->type .'_'. $class .'_delete', $obj); |