I was bugged, so here is the script as it stands today. No fancy features,
new_cat.html file. It should be easy enough to add the <%error%> tag if you
want it.
Code:
You need to create a new table, using MySQLMan:
#
# Table structure for table 'New_Cat'
#
CREATE TABLE New_Cat (
ID int(11) DEFAULT '0' NOT NULL auto_increment,
Parent_Category varchar(150) DEFAULT '' NOT NULL ,
New_Category varchar(25) DEFAULT '' NOT NULL ,
Suggest_Date date DEFAULT '0000-00-00' NOT NULL ,
PRIMARY KEY (ID),
KEY ID(ID),
UNIQUE ID_2 (ID)
);
Then, you need to make the .def file. Start with the template below, but ***REMEMBER*** to
copy the header from one of your other DEF files, but change the 'package' and $db_table lines
to "New_Cat" from whatever tablename you used. Copy this to your .defs directory.
###########
# Auto Generated Config File.
# Created on: Tue Oct 3 20:37:51 2000
#
package Links::DBSQL::New_Cat;
$db_name = '';
$db_host = '';
$db_driver = 'mysql';
$db_user = '';
$db_pass = '';
$db_key = 'ID';
$db_table = 'New_Cat';
$db_track = '1';
$attach_dir = '';
%db_def = (
ID => ['1', 'INT', '10', '20', '1', '0', '^\d*\.?\d*$'],
Parent_Category => ['2', 'CHAR', '40', '150', '1', '', ''],
New_Category => ['3', 'CHAR', '25', '25', '1', '', ''],
Suggest_Date => ['4', 'DATE', '20', '20', '1', 'NOW', '^\d{4}\-\d{2}\-\d{2}$']
);
%db_select_fields = (
);
%db_checkbox_fields = (
);
%db_radio_fields = (
);
1;
========
Now, you need to create the forms: new_cat.html, new_cat_success.html (I use new_cat.html for the error
report at this time, without any report of an 'error')
This is the new_cat.html form. (basically the same for the new_cat_error.html as well!)
<form action="<%db_cgi_url%>/new_cat.cgi" method="POST">
<table border ="0" cellspacing="5" cellpadding="0">
<tr><td align="right" valign="top"><font face="Arial" size="2"><B>Category:</B></font></td>
<td><%Category%></td></tr>
<tr><td align="right" valign="top"><font face="Arial" size="2"><B>New Category to add:</B></font></td>
<td><input name="New_Category" value="" size="40"></td></tr>
<tr><td></td><td><input type="SUBMIT" name="New_Cat" value="submit"></td></tr>
</table>
</form>
This is the ADD CATEGORY SUCCESS page: (new_cat_success.html)
<p><font face="Arial" size="2">We have received the following suggestion:</font></p>
<TABLE>
<TR>
<TD ALIGN=RIGHT><FONT FACE=ARIAL SIZE=3><B> ID:</B></FONT></TD><TD ALIGN=LEFT> <%ID%></TD>
</TR><TR>
<TD ALIGN=RIGHT><FONT FACE=ARIAL SIZE=3><B> Parent:</B></FONT></TD><TD ALIGN=LEFT> <%Parent_Category%></TD>
</TR><TR>
<TD ALIGN=RIGHT><FONT FACE=ARIAL SIZE=3><B>New Category:</B></FONT></TD><TD ALIGN=LEFT> <%New_Category%></TD>
</TR><TR>
<TD ALIGN=RIGHT><FONT FACE=ARIAL SIZE=3><B> Date:</B></FONT></TD><TD ALIGN=LEFT> <%Suggest_Date%></TD>
</TR><TR>
</TABLE>
This is the routine you need to add to the EXPORT list in DB_UTILS.pm, and at the end of the file before the 1;
sub get_category_list_top {
# --------------------------------------------------------
# Builds a <select> list of category name to category id.
# It includes a choice '0' for "Top Level" additions.
#
my $value = shift;
my $fname = shift || 'CategoryID';
my $mult = shift || '';
$mult and ($mult = "MULTIPLE SIZE=4");
my ($query, $sth, $id, $name, $output, $time);
$output = $CATEGORY_LIST{$value,$fname};
$time = time();
if (!$output) {
if (! $CATDB) {
$CATDB = new Links::DBSQL $LINKS{admin_root_path} . "/defs/Category.def";
}
$query = qq|
SELECT ID, Name
FROM Category
ORDER BY Name
|;
$sth = $CATDB->prepare ($query);
$sth->execute() or die "Can't Execute: $DBI::errstr";
$output = "<select $mult name='$fname'><option value=''>----";
$output .= "<option value='0'>Top Level";
while (($id, $name) = $sth->fetchrow_array) {
($id == $value) ? ($output .= "<option value='$id' SELECTED>$name") : ($output .= "<option value='$id'>$name");
}
$output .= "</select>";
$sth->finish;
$CATEGORY_LIST{$value,$fname} = $output;
}
return $output;
}
The three routines to add to the EXPORT list in HTML_Templates.pm and to the end of that file before the 1;
&site_html_new_cat_form &site_html_new_cat_success &site_html_new_cat_failure
sub site_html_new_cat_form {
# --------------------------------------------------------
# This routine determines how the add form page will look like.
#
my ($tags, $dynamic) = @_;
my $template = defined $dynamic ? $dynamic->param('t') : undef;
(ref $tags eq 'HASH') or croak "HTML_TEMPLATES: Argument '$tags' must be hash reference";
defined $dynamic and &load_user ($dynamic, $tags);
my $output = &load_template ('new_cat.html', {
%$tags,
%GLOBALS
}, undef, $template);
defined $dynamic and &clean_output($dynamic, \$output);
print $output;
}
sub site_html_new_cat_success {
# --------------------------------------------------------
# This routine determines how the add success page will look like.
#
my ($tags, $dynamic) = @_;
my $template = defined $dynamic ? $dynamic->param('t') : undef;
(ref $tags eq 'HASH') or croak "HTML_TEMPLATES: Argument '$tags' must be hash reference";
defined $dynamic and &load_user ($dynamic, $tags);
my $output = &load_template ('new_cat_success.html', {
%$tags,
%GLOBALS
}, undef, $template);
defined $dynamic and &clean_output($dynamic, \$output);
print $output;
}
sub site_html_new_cat_failure {
# --------------------------------------------------------
# This routine determines how the add failure page will look like.
#
my ($tags, $dynamic) = @_;
my $template = defined $dynamic ? $dynamic->param('t') : undef;
(ref $tags eq 'HASH') or croak "HTML_TEMPLATES: Argument '$tags' must be hash reference";
defined $dynamic and &load_user ($dynamic, $tags);
my $output = &load_template ('new_cat_error.html', {
%$tags,
%GLOBALS
}, undef, $template);
defined $dynamic and &clean_output($dynamic, \$output);
print $output;
}
And, finally, the new_cat.cgi file itself:
#!/usr/bin/perl
# ==============================================================
# --------------------------------------------
# Links SQL -- Suggest a new category script
# --------------------------------------------
# Original script and include modules Copyright 1999
Gossamer Threads Inc.
# This mod, written by PUGDOG (R) Enterprises, Inc.
#
# By using this program you agree to indemnify
Gossamer Threads Inc.
# & PUGDOG(R) Enterprises, Inc. from any liability WHATSOEVER.
#
# ==============================================================
# Load required modules.
# ---------------------------------------------------
use strict;
use vars qw/%in/;
use CGI ();
use CGI::Carp qw/fatalsToBrowser/;
use lib 'admin';
use Links;
use Links::DB_Utils;
use Links::HTML_Templates;
use Links::DBSQL;
$|++;
&main();
sub main {
# ---------------------------------------------------
# Create CGI object and figure out what to do.
#
my $in = new CGI;
my $dynamic = $in->param('d') ? $in : undef;
%in = %{&cgi_to_hash ($in)}; ## legacy from another mod, but it arguably makes doing _some_ things easier.
## The CGI object is "gone" and there is now a hash %in, and values $in{key}
print $in->header('text/html');
if (($in{'New_Cat'}) && ($in{'New_Category'})) { ## if the submit button was pressed and New_Category has a value
&process_form (\%in, $dynamic);
}
else { ## the form is incomplete
my ($name, $category);
my $id = $in{'CategoryID'};
# If we don't have an id, and can't generate a list, let's send the user a message.
## kept in for future use to limit suggestions to being _in_ that category: ie: no db_gen_category_list
if (! $id and ! $LINKS{db_gen_category_list}) {
&site_html_error ( { error => "Please go to the category you want to add to, and click add from there." }, $dynamic);
}
else {
if ($id) {
$name = &get_category_name ($id);
$category = "$name <input type=hidden name='CategoryID' value='$id'>";
}
else {
$category = &get_category_list_top (); ## new sub, adds 'Top Level' as a category option.
}
&site_html_new_cat_form ({ Category => $category, %in }, $dynamic)
}
}
}
# ==============================================================
sub process_form {
# --------------------------------------------------------
my ($in, $dynamic) = @_;
my ($new_cat, $category);
# Connect to the database.
$new_cat = new Links::DBSQL $LINKS{admin_root_path} . "/defs/New_Cat.def";
# Set date variable to today's date.
$in{'Suggest_Date'} = $new_cat->get_date(); ## today's date, mysql format.
# Convert the CategoryID into a CategoryName
$in{'Parent_Category'} = &get_category_name ($in{'CategoryID'}); ##$in{'CategoryID'} comes to us via the CGI process
# Store the current LinkID (not really useful for anything, but helps with debugging at this point.
$in{'ID'} = $new_cat->add_record ( \%in ); ## if successful, $id is the database record number of the new record.
# $in{'New_Category'} was passed from the form, and checked for non-nullness before being passed to this routine.
## the only 4 tags the form is expecting are the 4 database fields, now all 4 are defined.
if ($in{'ID'}) { # Send the visitor to the success page.
&site_html_new_cat_success ( { %in }, $dynamic);
}
else {
if ($in->param('CategoryID')) { ## if we have a parent category picked, assign it.
$in{'Category'} = $in{'Parent_Category'};
}
else { ## generate the category list again.
$in{'Category'} = &get_category_list_top();
}
&site_html_new_cat_failure( { error => $Links::DBSQL::error, %in }, $dynamic);
}
}
====
As far as I know, this is all you have to do!
Put the .cgi file in the /cgi-bin/links directory, and the other stuff where it belongs.