$message";
}
$db = new Database;
if (!$db->connect()) {
notice('Failed to connect to the database');
$db = NULL;
} else
$action = array_key_exists('action', $_POST) ? $_POST['action'] : NULL;
function execute_query_and_expect_one_row_to_be_affected($query, $params, $success_message, $failure_message) {
global $db;
foreach ($params as &$param) {
if ($param == '')
$param = NULL;
}
$affected_rows = $db->query_and_get_affected_rows($query, $params);
if ($affected_rows) {
assert('$affected_rows == 1');
notice($success_message);
return true;
}
notice($failure_message);
return false;
}
function update_field($table, $prefix, $field_name) {
global $db;
if (!array_key_exists('id', $_POST) || !array_key_exists($field_name, $_POST))
return FALSE;
$id = intval($_POST['id']);
$prefixed_field_name = $prefix . $field_name;
$id_field_name = $prefix ? $prefix . '_id' : 'id';
execute_query_and_expect_one_row_to_be_affected("UPDATE $table SET $prefixed_field_name = \$2 WHERE $id_field_name = \$1",
array($id, $_POST[$field_name]),
"Updated the $prefix $id",
"Could not update $prefix $id");
return TRUE;
}
class AdministrativePage {
private $table;
private $prefix;
private $column_to_be_ordered_by;
private $column_info;
function __construct($db, $table, $prefix, $column_info) {
$this->db = $db;
$this->table = $table;
$this->prefix = $prefix ? $prefix . '_' : '';
$this->column_info = $column_info;
}
private function name_to_titlecase($name) {
return ucwords(str_replace('_', ' ', $name));
}
private function column_label($name) {
return array_get($this->column_info[$name], 'label', $this->name_to_titlecase($name));
}
private function render_form_control_for_column($editing_mode, $name, $value = '', $show_update_button_if_needed = FALSE, $size = NULL) {
if ($editing_mode == 'text') {
echo <<< END
END;
if ($show_update_button_if_needed) {
echo <<< END
Update
END;
}
return;
}
if ($editing_mode == 'url') {
if (!$size)
$size = 70;
echo <<< END
END;
return;
}
$sizeIfExits = $size ? " size=\"$size\"" : '';
echo <<< END
END;
}
function render_table($column_to_be_ordered_by) {
$column_names = array_keys($this->column_info);
$labels = array();
foreach ($column_names as $name) {
if (array_get($this->column_info[$name], 'pre_insertion'))
continue;
array_push($labels, htmlspecialchars($this->column_label($name)));
}
$headers = join('
', $labels);
echo <<< END
ID $headers
END;
assert(ctype_alnum_underscore($column_to_be_ordered_by));
$rows = $this->db->fetch_table($this->table, $this->prefix . $column_to_be_ordered_by);
if ($rows) {
foreach ($rows as $row) {
$id = intval($row[$this->prefix . 'id']);
echo "\n$id \n";
foreach ($column_names as $name) {
if (array_get($this->column_info[$name], 'pre_insertion'))
continue;
$custom = array_get($this->column_info[$name], 'custom');
if ($custom) {
echo "";
$custom($row);
echo " \n";
continue;
}
$value = htmlspecialchars($row[$this->prefix . $name], ENT_QUOTES);
$editing_mode = array_get($this->column_info[$name], 'editing_mode');
if (!$editing_mode) {
echo "$value \n";
continue;
}
echo <<< END
\n";
}
echo " \n";
}
}
echo <<< END
END;
}
function render_form_to_add($title = NULL) {
if (!$title) # Can't use the table name since it needs to be singular.
$title = 'New ' . $this->name_to_titlecase($this->prefix);
echo <<< END
END;
}
}
?>