<?
// Include my local files
require_once("local.inc");
require_once("common.inc");
// Include the library
require_once("b-forms/b-forms.inc");
require_once("b-forms/layout.inc");
init_db(TRUE); // Initialize the database link in the global $blog_link variable
// Define the form structure
$form = new Form("denied.html", TRUE);
$bl = & new BaseLayout();
$tl = & new TableLayout(FALSE);
// Single row block "entry"
$block = & new Block("entry");
$block->add_property(new TextProperty("title", "Title", "", TRUE, 128), new TextBox(80));
$block->add_property(new DateProperty("post_date", "Post date", "", FALSE));
$block->add_property(new TextProperty("brief", "Intro", "", TRUE, 10000), new TextArea(80, 15));
$block->add_property(new TextProperty("full", "Content", "", FALSE, 100000), new TextArea(80, 25));
$block->add_property(new LayoutElement("Post in topics"), new InlineBlock(&$bl, "topic", &$tl));
$block -> add_property(new ButtonProperty("save", "Save", TRUE));
$block -> add_property(new ButtonProperty("cancel", "Cancel"));
$block -> add_property(new ButtonProperty("delete", "Delete"));
$form -> add_block($block);
//Multi-row block "topic"
$block = & new Block("topic", TRUE);
$block -> add_property(new CheckBoxProperty("include_fl", "", FALSE));
$block -> add_property(new TextProperty("name", "Name", "", TRUE, 64), new TextDisplay());
$form -> add_block($block);
// Define triggers
function entry_on_open() {
global $form, $blog_link, $HTTP_GET_VARS;
if (isset($HTTP_GET_VARS["entry"])) {
$query = sprintf(
"SELECT entries.id, ". //0
" entries.title, ". //1
" entries.brief, ". //2
" entries.post_date, ". //3
" entries.full ". //4
"FROM entries ".
"WHERE entries.id = %d ",
$HTTP_GET_VARS["entry"]);
$result = @mysql_query($query, $blog_link);
$num_rows = mysql_num_rows($result);
if (!$num_rows) {
header("Location: ".$form->_denied_target);
close_db();
exit;
}
$row = mysql_fetch_row($result);
$form->entry->append(RS_OLD);
$form->entry->id = $row[0];
$form->entry->title = $row[1];
$form->entry->brief = $row[2];
$form->entry->post_date = $row[3];
$form->entry->full = $row[4];
}
}
// The following trigger will be executed AFTER the entry_on_open trigger
// since block entry is added to the form before block topic.
function topic_on_open() {
global $form, $blog_link;
$query = sprintf(
"SELECT id, name, topic_id ". // 0, 1, 2
"FROM topics ".
"LEFT JOIN entry_topics ON topic_id = id AND entry_id = %d ".
"ORDER BY name",
$form->entry->id);
// Because of the left join, topic_id column will be null for those
// topics for which there is no row in the entry_topics for this entry.
// And it will be not null, if the entry is already included in that topic.
//
// When the form is opened for creation, the $form->entry->id will be null,
// sprintf() will convert it into 0, and no entry_topics will be found.
$result = mysql_query($query, $blog_link);
$num_rows = mysql_num_rows($result);
for ($i=0; $i<$num_rows; $i++) {
$row = mysql_fetch_row($result);
if ($row[2]) { // Entry already belongs to the topic
$form->topic->append(RS_OLD);
$form->topic->include_fl[$i] = TRUE;
}
else { // NULL, Entry does not belong to the topic
$form->topic->append(RS_NEW);
}
$form->topic->id[$i]=$row[0];
$form->topic->name[$i]=$row[1];
}
}
function entry_cancel_on_action($rownum = -1) {
close_db();
header("Location: /examples/");
exit;
}
function entry_save_on_action($rownum = -1) {
global $blog_link, $domain, $form, $post_date;
$post_date = $form->entry->post_date;
if ($post_date == "")
$post_date = "SYSDATE()";
else
$post_date = "'$post_date'";
if ($form->save()) {
close_db();
header("Location: /examples/");
exit;
}
}
function entry_on_update($rownum = -1) {
global $form, $blog_link, $post_date;
$query = sprintf(
"UPDATE entries ".
"SET title = '%s', ".
" brief = '%s', ".
" full = '%s', ".
" post_date = %s ".
"WHERE id = %d",
mysql_escape_string($form->entry->title),
mysql_escape_string($form->entry->brief),
mysql_escape_string($form->entry->full),
$post_date,
$form->entry->id);
mysql_query($query, $blog_link);
}
function entry_on_insert($rownum = -1) {
global $form, $blog_link, $post_date;
$query = sprintf(
"INSERT INTO entries ".
" (title, brief, full, post_date) ".
"VALUES ('%s', '%s', '%s', %s)",
mysql_escape_string($form->entry->title),
mysql_escape_string($form->entry->brief),
mysql_escape_string($form->entry->full),
$post_date);
mysql_query($query, $blog_link);
// We need to retrieve the autoincrement id assigned to our entry.
// It will be required for saving the topics of this entry
$form->entry->id = mysql_insert_id($blog_link);
}
// This trigger will only be called for records, that are in status RS_INSERT
// and these are records that previously did not exist in the form, thus the
// checkbox was originally off, and now the record has changed, thus the
// checkbox is on.
function topic_on_insert($rownum = -1) {
global $form, $blog_link;
$query = sprintf(
"INSERT INTO entry_topics (entry_id, topic_id) VALUES (%d, %d)",
$form->entry->id,
$form->topic->id[$rownum]);
mysql_query($query, $blog_link);
}
// This trigger will only be called for records in status RS_UPDATE, and these
// are those records that were originally in the database, thus the checkbox was
// on. Since the record has changed to be in status RS_UPDATE, the checkbox must
// be off now. So this is a record to be deleted. However, will will not delete it now,
// - just build a list of ids.
function topic_on_update($rownum = -1) {
global $form, $delete_list, $join;
$delete_list .= $join.$form->topic->id[$rownum];
$join = ', ';
}
function form_post_save() {
global $form, $blog_link, $delete_list;
// And this is the place where we should delete the topics than require
// deletion
if ($delete_list) {
$query = sprintf(
"DELETE FROM entry_topics ".
"WHERE entry_id = %d ".
"AND topic_id IN (%s)",
$form->entry->id,
$delete_list);
mysql_query($query, $blog_link);
}
}
function entry_delete_on_action($rownum = -1) {
global $blog_link, $form, $domain;
$query = sprintf(
"DELETE FROM entry_topics ".
"WHERE entry_id = %d ",
$form->entry->id);
mysql_query($query, $blog_link);
$query = sprintf(
"DELETE FROM entries ".
"WHERE id = %d ",
$form->entry->id);
mysql_query($query, $blog_link);
close_db();
header("Location: /examples/");
exit;
}
function entry_pre_display($rownum = -1) {
global $form;
if (!$form->entry->is_record_existing())
$form->entry->_properties["delete"]->visible = FALSE;
}
function topic_name_pre_render($rownum) {
global $form;
echo '<label for="'.
$form->topic->_properties['include_fl']->get_form_name($rownum).
'">';
}
function topic_name_post_render($rownum) {
echo '</label>';
}
// Do the main processing
$form->process();
// Generate the HTML code for the form
echo "<html><body><head>\n";
echo "<link rel=\"stylesheet\" media=\"screen, projection\" type=\"text/css\" href=\"layout.css\" />\n";
echo "</head><h1>";
if ($form->entry->is_record_existing())
echo "Edit";
else
echo "Create";
echo " entry</h1>\n";
if (isset($error)) echo "<h2>$error</h2>\n";
$form->start_form();
$bl->show_block("entry");
$form->end_form();
echo "</body></html>\n";
close_db();
?>
|