Links 2.0 Category Select List Mod
Version 1.3 -- 19 August 1999
Copyright © 1999 Connors
All Rights Reserved
Some people want to prevent the addition of links to top-level categories while others want to do the same but with a variety of different categories at a variety of depths. The standard Links scripts creates a select list of all categories to use for the purpose of specifying the category a link is to be added to but has no way of eliminating categories that should not have links added.This mod addresses that issue. Once installed, the Links administrator can decide which categories to not allow users to add links under regardless of the category level. This is accomplished by simply adding category names to an array, @exclude_categories, which will be in links.cfg.
This mod may be used for personal use by users of the Gossamer Threads, Inc's Links v2.0 script. All other rights to the code and documentation for this mod are reserved, under copyright, by the author. Redistribution or republication of this mod and its instructions, without permission, is prohibited.
The following should be noted about this mod:
- This modification is based on the latest version of Links v2.0 dated 3/22/99. If you do not have the latest distribution, you should obtain and install it before applying this mod.
- It creates a file in the admin/data directory that contains the new category select list to display, minus the categories specified in @exclude_categories.
- There is no necessity to modify templates to use this mod. All changes are made in links.cfg, nph-build.cgi, site_html.pl (if not using templates), and site_html_templates.pl (if using templates).
- For the purpose of this mod, all instructions below for site_html_templates.pl should be considered identical for site_html.pl
- After installing this mod, you have to run nph-build.cgi at least once in order to create the new category select drop-down menu before it will be available to the add form.
- This mod may not work with future releases of Links v2 (for that matter, it may not even be needed in future releases of Links v2), but I will try to keep it updated.
- Items changed from the previous version of this mod are marked with a double asterisk (**). This version of the mod (v1.3, 19 August 1999) will now display an error message if the user tries to add a resource to an excluded category. The add form will be redisplayed with all entries still completed (except the category) and they will be instructed to select a category for the resource in the drop-down category selection list, which will only list non-excluded categories. No other error checking is done until a valid category is selected.
- links.cfg
At the bottom of the Build Options section (just before the Date Routines section), add the following:# Exclude Categories: a list of categories you do not want people to # add categories in. Entries are contained within single quotes and # separated by commas. Any change to this variable requires a rebuild # of pages in order for the change to take effect. @exclude_categories = ();- add.cgi
- ** sub main
In the previous version of this mod (v1.2, 5 August 1999), you were instructed to make a change in this routine. If you did that, you need to restore it to its original condition. If you did not install the previous version of this mod, skip this step, and continue with step 2.b. below..
Change:# Otherwise we are displaying the form (in site_html.pl). else { if ($db_single_category) { my %is_valid = map { $_ => 1 } &category_list; $ENV{'HTTP_REFERER'} =~ s,/[^/]+\.[^/]+$,,; $ENV{'HTTP_REFERER'} =~ m,$build_root_url/(.+?)/?$,; if ($is_valid{$1}) { if (@exclude_categories) { &site_html_add_form (); } else { &site_html_add_form ($1); } } else { &site_html_add_form; } } else { &site_html_add_form (); } } }to read:# Otherwise we are displaying the form (in site_html.pl). else { if ($db_single_category) { my %is_valid = map { $_ => 1 } &category_list; $ENV{'HTTP_REFERER'} =~ s,/[^/]+\.[^/]+$,,; $ENV{'HTTP_REFERER'} =~ m,$build_root_url/(.+?)/?$,; $is_valid{$1} ? &site_html_add_form ($1) : &site_html_add_form (); } else { &site_html_add_form (); } } }- ** sub process_form
Change:# Validate the form input.. $status = &validate_record(%in);to read:# Validate the form input.. &check_exclude($in{'Category'}); $status = &validate_record(%in);- ** Add the following subroutine
to the end of the script:sub check_exclude { # Don't allow adds to excluded categories. my $bad_category = shift; if (@exclude_categories) { $found = 0; foreach (@exclude_categories) { ($bad_category eq $_) and $found++ and last; } if ($found) { delete $in{'Category'}; &site_html_add_failure ("You may not add links to the $bad_category category. Select a category below."); exit; } } }- nph-build.cgi
- sub build_all
Add the following someplace before the first build_page call (right before the line, # Generate detailed view pages, is a good place):# Create Category Select List if appropriate if (@exclude_categories) { print "Building Category Select List . . .\n"; &build_category_select_list; print "\tDone\n\n"; } else { print "No Categories to Exclude\n"; print "Category Select List not built\n"; print "Default category list will be used\n\n"; }Someplace before the end of the script, include the following subroutine:sub build_category_select_list { # -------------------------------------------------------- # This subroutine will build a category select list of only desired subcategories. my ($subcat, @rootcat); my $cat_select_list = $db_script_path . "/data/cat_sel.txt"; print "\tOpening file: $cat_select_list\n"; open (SELECT, ">$cat_select_list") or &cgierr ("unable to open $cat_select_list. Reason: $!"); print SELECT qq|<select name="Category">\n <option>---\n|; foreach $subcat (sort keys %category) { if ($subcat !~ /^\s*$/) { push (@rootcat, $subcat); } } $category = &site_html_cat_select_list (@rootcat) if ($#rootcat >= 0); print SELECT $category; print SELECT qq|</select>\n|; print "\tClosing file.\n"; close SELECT; }
- site_html_templates.pl
- sub site_html_add_form
Change:$category ? ($category = qq~$category <input type=hidden name="Category" value="$category">~) : ($category = &build_select_field ("Category", "$in{'Category'}"));to read:if ($category) { ($category = qq~$category<input type=hidden name="Category" value="$category">~); } else { if (!@exclude_categories) { ($category = &build_select_field ("Category")); } else { ($category = &get_cat_select_list); } }- ** sub site_html_add_failure
Change:$in{'Category'} ? ($in{'Category'} = qq~<input type=hidden name="Category" value="$in{'Category'}">$in{'Category'}~) : ($in{'Category'} = &build_select_field ("Category"));to read:if (defined $in{'Category'}) { $category = qq~$in{'Category'}~; } else { if (!@exclude_categories) { $category = &build_select_field ("Category"); } else { $category = &get_cat_select_list; } }Change:print &load_template ('add_error.html', { error => $errormsg, %in, %globals });to read:print &load_template ('add_error.html', { error => $errormsg, %in, Category => $category, %globals });- sub site_html_modify_form
Change:my $category = &build_select_field ("Category", "$in{'Category'}");to read:if (!@exclude_categories) { ($category = &build_select_field ("Category")); } else { ($category = &get_cat_select_list); }- sub site_html_modify_failure
Change:$in{'Category'} = &build_select_field ("Category", $in{'Category'});to read:if ($in{'Category'}) { ($in{'Category'} = qq~$in{'Category'}<input type=hidden name="Category" value="$in{'Category'}">~); } else { if (!@exclude_categories) { ($in{'Category'} = &build_select_field ("Category")); } else { ($in{'Category'} = &get_cat_select_list); } }Add the following two subroutines just before the end of site_html_templates.pl:sub site_html_cat_select_list { # -------------------------------------------------------- # This routine builds the options for the category select list. my (@subcat) = @_; my $output; CAT: foreach $subcat (sort @subcat) { if (@exclude_categories) { foreach (@exclude_categories) { ($subcat eq $_) and next CAT; } } $category_name = &build_clean($subcat); $output .= qq| <option value="$subcat">$category_name\n|; } return $output; } sub get_cat_select_list { # -------------------------------------------------------- # This routine retrieves the category select list. my $cat_select_list; open (SELECT, "<$db_script_path/data/cat_sel.txt") or &cgierr("unable to open $db_script_path/data/cat_sel.txt. Reason: $!"); while (<SELECT> ) { $cat_select_list .= $_; } close SELECT; return $cat_select_list; }That is all there is to it. If you have any problems with this mod, please post your problem in the Links Modifications Forum or send email to me at linksmanager@bobsgoodstufflists.net/a>.