Update make-form.php to fix errors.

Add auto-generated articles.php.

Add select_is_published() to common.php (for auto-generated forms).
pull/193/head
Michael R Sweet 20 years ago
parent ead5f1cdf0
commit 866c5c9673
  1. 332
      www/articles.php
  2. 52
      www/data/make-form.php
  3. 35
      www/phplib/common.php

@ -1,20 +1,344 @@
<?php
//
// "$Id: articles.php,v 1.1 2004/05/17 20:28:52 mike Exp $"
// "$Id: articles.php,v 1.2 2004/05/18 12:02:02 mike Exp $"
//
// Web form for the article table...
//
//
// Include necessary headers...
//
include_once "phplib/html.php";
include_once "phplib/common.php";
// Get command-line options...
//
// Usage: article.php [operation]
//
// Operations:
//
// D# - Delete Article
// L = List all
// L# = List Article #
// M# = Modify Article #
// N = Create new Article
if ($argc)
{
$op = $argv[0][0];
$id = (int)substr($argv[0], 1);
if ($op != 'D' && $op != 'L' && $op != 'M' && $op != 'N')
{
html_header("Article Error");
print("<p>Bad command '$op'!\n");
html_footer();
exit();
}
if (($op == 'D' || $op == 'M') && !$id)
{
html_header("Article Error");
print("<p>Command '$op' requires an ID!\n");
html_footer();
exit();
}
if ($op == 'N' && $id)
{
html_header("Article Error");
print("<p>Command '$op' may not have an ID!\n");
html_footer();
exit();
}
}
else
{
$op = 'L';
$id = 0;
}
switch ($op)
{
case 'D' : // Delete Article
if ($REQUEST_METHOD == "POST")
{
db_query("DELETE FROM article WHERE id = $id");
header("Location: $PHP_SELF?L");
}
else
{
$result = db_query("SELECT * FROM article WHERE id = $id");
if (db_count($result) != 1)
{
print("<p><b>Error:</b> Article #$id was not found!</p>\n");
html_footer();
exit();
}
$row = db_next($result);
html_header("Delete Article #$id");
html_start_links(1);
html_link("Return to Article List", "$PHP_SELF?L");
html_link("View Article #$id</A>", "$PHP_SELF?L$id");
html_link("Modify Article #$id</A>", "$PHP_SELF?M$id");
html_end_links();
print("<h1>Delete Article #$id</h1>\n");
print("<form method='post' action='$PHP_SELF?D$id'>"
."<p><table width='100%' cellpadding='5' cellspacing='0' border='0'>\n");
if (!$row['is_published'])
print("<tr><th align='center' colspan='2'>This Article is "
."currently hidden from public view.</td></tr>\n");
$temp = htmlspecialchars($row["title"]);
print("<tr><th align='right'>Title:</th><td class='left'>$temp</td></tr>\n");
$temp = htmlspecialchars($row["abstract"]);
print("<tr><th align='right'>Abstract:</th><td class='left'>$temp</td></tr>\n");
$temp = htmlspecialchars($row["contents"]);
print("<tr><th align='right'>Contents:</th><td class='left'>$temp</td></tr>\n");
print("<tr><th colspan='2'>"
."<input type='submit' value='Confirm Delete Article'></th></tr>\n");
print("</table></p></form>\n");
html_footer();
}
break;
case 'L' : // List (all) Article(s)
if ($id)
{
html_header("Article #$id");
$result = db_query("SELECT * FROM article WHERE id = $id");
if (db_count($result) != 1)
{
print("<p><b>Error:</b> Article #$id was not found!</p>\n");
html_footer();
exit();
}
$row = db_next($result);
html_start_links(1);
html_link("Return to Article List", "$PHP_SELF?L");
html_link("Modify Article</A>", "$PHP_SELF?M$id");
html_link("Delete Article #$id</A>", "$PHP_SELF?D$id");
html_end_links();
print("<h1>Article #$id</h1>\n");
print("<p><table width='100%' cellpadding='5' cellspacing='0' "
."border='0'>\n");
if (!$row['is_published'])
print("<tr><th align='center' colspan='2'>This Article is "
."currently hidden from public view.</td></tr>\n");
$temp = htmlspecialchars($row['title']);
print("<tr><th align='right'>Title:</th><td class='left'>$temp</td></tr>\n");
$temp = htmlspecialchars($row['abstract']);
print("<tr><th align='right'>Abstract:</th><td class='left'>$temp</td></tr>\n");
$temp = htmlspecialchars($row['contents']);
print("<tr><th align='right'>Contents:</th><td class='left'>$temp</td></tr>\n");
print("</table></p>\n");
db_free($result);
}
else
{
html_header("Article List");
html_start_links(1);
html_link("New Article", "$PHP_SELF?N");
html_end_links();
$result = db_query("SELECT * FROM article");
$count = db_count($result);
print("<h1>Article List</h1>\n");
if ($count == 0)
{
print("<p>No Articles found.</p>\n");
html_footer();
exit();
}
html_start_table(array("Title","Abstract","Contents"));
while ($row = db_next($result))
{
html_start_row();
$id = $row['id'];
$temp = htmlspecialchars($row['title']);
print("<td class='center'><a href='$PHP_SELF?L$id' "
."alt='Article #$id'>"
."$temp</a></td>");
$temp = htmlspecialchars($row['abstract']);
print("<td class='center'><a href='$PHP_SELF?L$id' "
."alt='Article #$id'>"
."$temp</a></td>");
$temp = htmlspecialchars($row['contents']);
print("<td class='center'><a href='$PHP_SELF?L$id' "
."alt='Article #$id'>"
."$temp</a></td>");
html_end_row();
}
html_end_table();
}
html_footer();
break;
case 'M' : // Modify Article
if ($REQUEST_METHOD == "POST")
{
$date = time();
$is_published = db_escape($_POST["IS_PUBLISHED"]);
$title = db_escape($_POST["TITLE"]);
$abstract = db_escape($_POST["ABSTRACT"]);
$contents = db_escape($_POST["CONTENTS"]);
db_query("UPDATE article SET "
."is_published = $is_published, "
."title = '$title', "
."abstract = '$abstract', "
."contents = '$contents', "
."modify_date = $date, "
."modify_user = '$LOGIN_USER' "
."WHERE id = $id");
header("Location: $PHP_SELF?L$id");
}
else
{
html_header("Modify Article #$id");
html_start_links(1);
html_link("Return to Article List", "$PHP_SELF?L");
html_link("Article #$id", "$PHP_SELF?L$id");
html_end_links();
print("<h1>Modify Article #$id</h1>\n");
$result = db_query("SELECT * FROM article WHERE id = $id");
if (db_count($result) != 1)
{
print("<p><b>Error:</b> Article #$id was not found!</p>\n");
html_footer();
exit();
}
$row = db_next($result);
print("<form method='post' action='$PHP_SELF?M$id'>"
."<p><table width='100%' cellpadding='5' cellspacing='0' border='0'>\n");
print("<tr><th align='right'>Published:</th><td>");
select_is_published($row['is_published']);
print("</td></tr>\n");
$temp = htmlspecialchars($row['title'], ENT_QUOTES);
print("<tr><th align='right'>Title:</th>"
."<td><input type='text' name='TITLE' "
."value='$temp' size='40'></td></tr>\n");
$temp = htmlspecialchars($row['abstract'], ENT_QUOTES);
print("<tr><th align='right'>Abstract:</th>"
."<td><input type='text' name='ABSTRACT' "
."value='$temp' size='40'></td></tr>\n");
$temp = htmlspecialchars($row['contents'], ENT_QUOTES);
print("<tr><th align='right'>Contents:</th>"
."<td><textarea name='CONTENTS' "
."cols='80' rows='10' wrap='virtual'>"
."$temp</textarea></td></tr>\n");
print("<tr><th colspan='2'>"
."<input type='submit' value='Update Article'></th></tr>\n");
print("</table></p></form>\n");
html_footer();
}
break;
case 'N' : // Post new Article
if ($REQUEST_METHOD == "POST")
{
$date = time();
$is_published = db_escape($_POST["IS_PUBLISHED"]);
$title = db_escape($_POST["TITLE"]);
$abstract = db_escape($_POST["ABSTRACT"]);
$contents = db_escape($_POST["CONTENTS"]);
db_query("INSERT INTO article VALUES(NULL,"
."$is_published,"
."'$title',"
."'$abstract',"
."'$contents',"
."$date,'$LOGIN_USER',$date,'$LOGIN_USER')");
$id = db_insert_id();
header("Location: $PHP_SELF?L$id");
break;
}
html_header("New Article");
html_start_links(1);
html_link("Return to Article List", "$PHP_SELF?L");
html_end_links();
print("<h1>New Article</h1>\n");
print("<form method='post' action='$PHP_SELF?N'>"
."<p><table width='100%' cellpadding='5' cellspacing='0' border='0'>\n");
print("<tr><th align='right'>Published:</th><td>");
select_is_published();
print("</td></tr>\n");
print("<tr><th align='right'>Title:</th>"
."<td><input type='text' name='TITLE' "
."size='40'></td></tr>\n");
print("<tr><th align='right'>Abstract:</th>"
."<td><input type='text' name='ABSTRACT' "
."size='40'></td></tr>\n");
print("<tr><th align='right'>Contents:</th>"
."<td><textarea name='CONTENTS' "
."cols='80' rows='10' wrap='virtual'>"
."</textarea></td></tr>\n");
print("<tr><th colspan='2'>"
."<input type='submit' value='Create Article'></th></tr>\n");
print("</table></p></form>\n");
html_header("Title");
html_footer();
break;
}
html_footer();
//
// End of "$Id: articles.php,v 1.1 2004/05/17 20:28:52 mike Exp $".
// End of "$Id: articles.php,v 1.2 2004/05/18 12:02:02 mike Exp $".
//
?>

@ -1,4 +1,4 @@
#!/usr/bin/php
#!/usr/bin/php -q
<?php
// Make sure that the module is loaded...
@ -120,12 +120,13 @@ print(" \$row = db_next(\$result);\n");
print("\n");
print(" html_header(\"Delete $tname #\$id\");\n");
print("\n");
print(" html_startlinks(1);\n");
print(" html_start_links(1);\n");
print(" html_link(\"Return to $tname List\", \"\$PHP_SELF?L\");\n");
print(" html_link(\"View $tname #\$id</A>\", \"\$PHP_SELF?L\$id\");\n");
print(" html_link(\"Modify $tname #\$id</A>\", \"\$PHP_SELF?M\$id\");\n");
print(" html_endlinks();\n");
print(" html_end_links();\n");
print("\n");
print(" print(\"<h1>Delete $tname #\$id</h1>\\n\");\n");
print(" print(\"<form method='post' action='\$PHP_SELF?D\$id'>\"\n");
print(" .\"<p><table width='100%' cellpadding='5' cellspacing='0' border='0'>\\n\");\n");
print("\n");
@ -152,7 +153,7 @@ while ($row = sqlite_fetch_array($result))
$name = ucwords(str_replace('_', ' ', $row['name']));
print(" \$temp = htmlspecialchars(\$row[\"$row[name]\"]);\n");
print(" print(\"<tr><th class='right'>$name:</th>"
print(" print(\"<tr><th align='right'>$name:</th>"
."<td class='left'>\$temp</td></tr>\\n\");\n");
print("\n");
break;
@ -182,12 +183,13 @@ print(" }\n");
print("\n");
print(" \$row = db_next(\$result);\n");
print("\n");
print(" html_startlinks(1);\n");
print(" html_start_links(1);\n");
print(" html_link(\"Return to $tname List\", \"\$PHP_SELF?L\");\n");
print(" html_link(\"Modify $tname</A>\", \"\$PHP_SELF?M\$id\");\n");
print(" html_link(\"Delete $tname #\$id</A>\", \"\$PHP_SELF?D\$id\");\n");
print(" html_endlinks();\n");
print(" html_end_links();\n");
print("\n");
print(" print(\"<h1>$tname #\$id</h1>\\n\");\n");
print(" print(\"<p><table width='100%' cellpadding='5' cellspacing='0' \"\n");
print(" .\"border='0'>\\n\");\n");
print("\n");
@ -213,7 +215,7 @@ while ($row = sqlite_fetch_array($result))
$name = ucwords(str_replace('_', ' ', $row['name']));
print(" \$temp = htmlspecialchars(\$row['$row[name]']);\n");
print(" print(\"<tr><th class='right'>$name:</th>"
print(" print(\"<tr><th align='right'>$name:</th>"
."<td class='left'>\$temp</td></tr>\\n\");\n");
print("\n");
break;
@ -221,19 +223,20 @@ while ($row = sqlite_fetch_array($result))
}
print(" print(\"</table></p>\\n\");\n");
print(" mysql_free_result(\$result);\n");
print(" db_free(\$result);\n");
print(" }\n");
print(" else\n");
print(" {\n");
print(" html_header(\"$tname List\");\n");
print("\n");
print(" html_startlinks(1);\n");
print(" html_start_links(1);\n");
print(" html_link(\"New $tname\", \"\$PHP_SELF?N\");\n");
print(" html_endlinks();\n");
print(" html_end_links();\n");
print("\n");
print(" \$result = db_query(\"SELECT * FROM $table\");\n");
print(" \$count = db_count(\$result);\n");
print("\n");
print(" print(\"<h1>$tname List</h1>\\n\");\n");
print(" if (\$count == 0)\n");
print(" {\n");
print(" print(\"<p>No ${tname}s found.</p>\\n\");\n");
@ -261,7 +264,7 @@ while ($row = sqlite_fetch_array($result))
$name = ucwords(str_replace('_', ' ', $row['name']));
if ($firsttime)
{
print(",\"$name\"");
print("\"$name\"");
$firsttime = 0;
}
else
@ -281,6 +284,9 @@ while ($row = sqlite_fetch_array($result))
switch ($row['name'])
{
case "id" :
print(" \$id = \$row['id'];\n\n");
break;
case "create_date" :
case "create_user" :
case "modify_date" :
@ -290,7 +296,7 @@ while ($row = sqlite_fetch_array($result))
default :
print(" \$temp = htmlspecialchars(\$row['$row[name]']);\n");
print(" print(\"<td class='center'><a href='\$PHP_SELF?L\$row->id' \"\n");
print(" print(\"<td class='center'><a href='\$PHP_SELF?L\$id' \"\n");
print(" .\"alt='$tname #\$id'>\"\n");
print(" .\"\$temp</a></td>\");\n");
print("\n");
@ -360,13 +366,14 @@ print(" header(\"Location: \$PHP_SELF?L\$id\");\n");
print(" }\n");
print(" else\n");
print(" {\n");
print(" html_header(\"$tname #\$id\");\n");
print(" html_header(\"Modify $tname #\$id\");\n");
print("\n");
print(" html_startlinks(1);\n");
print(" html_start_links(1);\n");
print(" html_link(\"Return to $tname List\", \"\$PHP_SELF?L\");\n");
print(" html_link(\"$tname #\$id\", \"\$PHP_SELF?L\$id\");\n");
print(" html_endlinks();\n");
print(" html_end_links();\n");
print("\n");
print(" print(\"<h1>Modify $tname #\$id</h1>\\n\");\n");
print(" \$result = db_query(\"SELECT * FROM $table WHERE id = \$id\");\n");
print(" if (db_count(\$result) != 1)\n");
print(" {\n");
@ -380,7 +387,7 @@ print("\n");
print(" print(\"<form method='post' action='\$PHP_SELF?M\$id'>\"\n");
print(" .\"<p><table width='100%' cellpadding='5' cellspacing='0' border='0'>\\n\");\n");
print("\n");
print(" print(\"<tr><th class='right'>Published:</th><td>\");\n");
print(" print(\"<tr><th align='right'>Published:</th><td>\");\n");
print(" select_is_published(\$row['is_published']);\n");
print(" print(\"</td></tr>\\n\");\n");
print("\n");
@ -401,7 +408,7 @@ while ($row = sqlite_fetch_array($result))
$form = strtoupper($row['name']);
$name = ucwords(str_replace('_', ' ', $row['name']));
print(" \$temp = htmlspecialchars(\$row['$row[name]'], ENT_QUOTES);\n");
print(" print(\"<tr><th class='right'>$name:</th>\"\n");
print(" print(\"<tr><th align='right'>$name:</th>\"\n");
if ($row['type'] == "TEXT")
{
@ -453,7 +460,7 @@ while ($row = sqlite_fetch_array($result))
}
print("\n");
print(" db_query(\"INSERT INTO $table VALUES(0,\"\n");
print(" db_query(\"INSERT INTO $table VALUES(NULL,\"\n");
sqlite_seek($result, 0);
while ($row = sqlite_fetch_array($result))
@ -484,14 +491,15 @@ print(" }\n");
print("\n");
print(" html_header(\"New $tname\");\n");
print("\n");
print(" html_startlinks(1);\n");
print(" html_start_links(1);\n");
print(" html_link(\"Return to $tname List\", \"\$PHP_SELF?L\");\n");
print(" html_endlinks();\n");
print(" html_end_links();\n");
print("\n");
print(" print(\"<h1>New $tname</h1>\\n\");\n");
print(" print(\"<form method='post' action='\$PHP_SELF?N'>\"\n");
print(" .\"<p><table width='100%' cellpadding='5' cellspacing='0' border='0'>\\n\");\n");
print("\n");
print(" print(\"<tr><th class='right'>Published:</th><td>\");\n");
print(" print(\"<tr><th align='right'>Published:</th><td>\");\n");
print(" select_is_published();\n");
print(" print(\"</td></tr>\\n\");\n");
print("\n");
@ -512,7 +520,7 @@ while ($row = sqlite_fetch_array($result))
$form = strtoupper($row['name']);
$name = ucwords(str_replace('_', ' ', $row['name']));
print(" print(\"<tr><th class='right'>$name:</th>\"\n");
print(" print(\"<tr><th align='right'>$name:</th>\"\n");
if ($row['type'] == "TEXT")
{

@ -1,15 +1,16 @@
<?
//
// "$Id: common.php,v 1.2 2004/05/17 20:28:52 mike Exp $"
// "$Id: common.php,v 1.3 2004/05/18 12:02:02 mike Exp $"
//
// Common utility functions for PHP pages...
//
// Contents:
//
// quote_text() - Quote a string...
// sanitize_email() - Convert an email address to something a SPAMbot
// can't read...
// sanitize_text() - Sanitize text.
// quote_text() - Quote a string...
// sanitize_email() - Convert an email address to something a SPAMbot
// can't read...
// sanitize_text() - Sanitize text.
// select_is_published() - Do a <select> for the "is published" field...
//
@ -290,6 +291,28 @@ sanitize_text($text) // I - Original text
//
// End of "$Id: common.php,v 1.2 2004/05/17 20:28:52 mike Exp $".
// 'select_is_published()' - Do a <select> for the "is published" field...
//
function
select_is_published($is_published = 1) // I - Default state
{
print("<select name='IS_PUBLISHED'>");
if ($is_published)
{
print("<option value='0'>No</option>");
print("<option value='1' selected>Yes</option>");
}
else
{
print("<option value='0' selected>No</option>");
print("<option value='1'>Yes</option>");
}
print("</select>");
}
//
// End of "$Id: common.php,v 1.3 2004/05/18 12:02:02 mike Exp $".
//
?>

Loading…
Cancel
Save