Bob's Good Stuff Lists

Powered by Gossamer Threads

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:

  1. 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 = ();
  2. add.cgi

    1. ** 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 ();
                }
            }
        }
    2. ** 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);
    3. ** 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;
            }
          }
        }
  3. nph-build.cgi

    1. 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";
        }
    2. 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;
      }

  4. site_html_templates.pl

    1. 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);
          }
        }
    2. ** 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
        });
    3. 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);
        }
    4. 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);
          }
        }
    5. 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>.

*:=+=:*:=+=:*:=+=:*:=+=:*:=+=:*:=+=:*:=+=:*:=+=:*:=+=:*:=+=:*:=+=:*

This Site Hosted By
Orphanage Services
Powered By Apache