#! /usr/bin/perl

BEGIN { require "../../perl/lib/Sitedefs.pm"; }

#######################################################################
# Program:    makeindex.cgi
# Date:       
# Programmer: Laurie Dickinson
# Purpose:    Generates a browsable index, including page title and
#             description, for a query on subject and doctype.  The
#             index will be alphabetized by page title and will 
#             only include menu or top-level pages to a subsection.  
# Input:      a query including at most one value for each of subject
#             and doctype, for a total of at least zero and at most
#             two parameters.
# Output:     An alphabetized list, by title, of all top-level pages
#             (i.e., pages in which SU_Index is set to "yes") which match
#             the query.  If the number of parameters is:
#               -- 0, a full alphabetized list of index files will be
#                  displayed.
#               -- 1, the results will be broken down into the categories
#             represented by the other parameter type, i.e., if the
#             user requests entries on SU_Subject = Geometry, the
#             results will be separated under headings for SU_Doctype.
#             Conversely, if the user requests articles, the results
#             will be divided into the various subjects.  
#               -- 2, specific results for entries which match a
#                  subject/doctype pairing will be displayed.
#             Note: If a category is empty, the category heading will 
#                   not appear.
# Logic:      
#             1.  Define and initialize local variables.
#             2.  Get sent parameters.
#             3.  Display Header
#             4.  If two parameters.
#                 a.  Execute swish-e with parameters and SU_Index=yes
#                 b.  Retrieve Title & Description for each entry
#                 c.  Alphabetize by Title.
#                 d.  Display Output.
#             5.  If one parameter.
#                 a.  If searchtype=SU_Doctype, do for each SU_Subject:
#                     i.  Execute swish-e (with parameter and SU_Index=yes)
#                     ii. If non-empty:
#                         a.  Retrieve Title & Description for each entry
#                         b.  Alphabetize by Title.
#                         c.  display Output, with subject header.
#                 b.  If searchtype=SU_Subject, do for each SU_Doctype:
#                     i.  Execute swish-e (with parameter and SU_Index=yes)
#                     ii. If non-empty:
#                         a.  Retrieve Title & Description for each entry
#                         b.  Alphabetize by Title.
#                         c.  display Output, with subject header.
#             6.  If no parameters:
#                 a.  Execute swish-e with SU_Index=yes
#                 b.  Retrieve Title & Description for each entry
#                 c.  Alphabetize by Title.
#                 d.  Display Output.
#             7.  Display footer.
#
#######################################################################

use ErrorLog;
use CGITracker;
use Exception;
use strict;
use Keywords;
use PageTemplate;
use searchutils;

#- swish-e Variables --------------------------------------------#

#Path to SWISH-E executable
my $swish = "$Sitedefs::SUROOT/local/swish-e/$Sitedefs::OSTYPE/swish-e";

#Path to the SWISH-E index
my $index = "$Sitedefs::SUROOT/local/swish-e/scienceu.index";

my $navfile = "$Sitedefs::ROOTPATH/library/navigation.html";

#- Main Program ------------------------------------------------------#

#define local variables
my ($tpl, $q, $key, $contents, $pagetitle, $subject, $doctype);

$tpl = new PageTemplate;
$q = new CGITracker;

if ($q->param('SU_Doctype') && $q->param('SU_Subject')) { 
#
# Sent SU_Doctype and SU_Subject --> browse by document 
#                                    type & subject
#

    $subject = $q->param('SU_Subject');
    $doctype = $q->param('SU_Doctype');
    $contents = $contents.&searchutils::getContents($subject, $doctype, "", $swish, $index, "desc");
    $pagetitle = $Keywords::SU_KeyLabels{$q->param('SU_Subject')}."\: ".$Keywords::SU_Doctype{$q->param('SU_Doctype')};
}
elsif ($q->param('SU_Doctype')) {
#
# Sent SU_Doctype --> browse by document type
#
    $doctype = $q->param('SU_Doctype');

    my @keys = reverse sort keys %Keywords::SU_Subject2;
    $contents = "";
    while (@keys) {
	$subject = pop(@keys);
# 	$contents = $contents."Key = ".$subject."<p>";
	$contents = $contents.&searchutils::getContents($subject, $doctype, $Keywords::SU_KeyLabels{$subject}, $swish, $index, "desc");
    }

    $pagetitle = "Browse by Type: ".$Keywords::SU_Doctype{$q->param('SU_Doctype')};

}
elsif ($q->param('SU_Subject')) {
#
# Sent SU_Subject --> browse by subject
#

    $subject = $q->param('SU_Subject');

    my @keys = reverse sort keys %Keywords::SU_Doctype;
    $contents = "";
    while (@keys) {
	$doctype = pop(@keys);
#	$contents = $contents."Key = ".$doctype."<p>";
	$contents=$contents.&searchutils::getContents($subject, $doctype, $Keywords::SU_Doctype{$doctype}, $swish, $index, "desc");
    }

    $pagetitle = "Browse by Subject: ".$Keywords::SU_KeyLabels{$q->param('SU_Subject')};

}
else {
#
# No search parameters --> browse all 
#

    $pagetitle = "Browse All";
    $subject = "";
    $doctype = "";
    $contents = $contents.searchutils::getContents($subject, $doctype, "", $swish, $index, "nodesc");

}

&printPageTop($pagetitle, $navfile, $q, $tpl);
$q->Print(q|
<table width="100%" cellpadding="15">
<tr>
<td valign="top">|);

$q->Print($contents);

$q->Print(q|
</td>
<td valign="top" align="right">

<iframe src="http://rcm.amazon.com/e/cm?t=sienceu-20&o=1&p=10&l=st1&mode=books&search=science%20projects&fc1=&=1&lc1=&lt1=_blank&bg1=FFCC66&f=ifr" marginwidth="0" marginheight="0" width="120" height="450" border="0" frameborder="0" style="border:none;" scrolling="no"></iframe>

</td>
</tr>
</table>
|);

&printPageBottom($q, $tpl);





