File:  [LON-CAPA] / loncom / interface / lonsearchcat.pm
Revision 1.11: download - view: text, annotated - select for diffs
Thu Mar 15 13:23:15 2001 UTC (23 years, 1 month ago) by harris41
Branches: MAIN
CVS tags: HEAD
modularizing the code and adding close button

# The LearningOnline Network
# Search Catalog
#
# 03/08/2001 Scott Harrison
#
package Apache::lonsearchcat;

use strict;
use Apache::Constants qw(:common);
use Apache::lonnet();
use Apache::File();
use CGI qw(:standard);

my %language;
my $scrout;
my %metadatafields;
my %cprtag;
my %mimetag;

sub handler {
    my $r = shift;

# -------------------------------------- see if called from an interactive mode
    map {
       my ($name, $value) = split(/=/,$_);
       $value =~ tr/+/ /;
       $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
       if ($name eq 'catalogmode') {
	   $ENV{'form.'.$name}=$value;
       }
    } (split(/&/,$ENV{'QUERY_STRING'}));

    $r->content_type('text/html');
    $r->send_http_header;
    return OK if $r->header_only;

    %metadatafields=();

    my $hidden='';
    $hidden=<<END if $ENV{'form.catalogmode'} eq 'interactive';
<input type='hidden' name='catalogmode' value='interactive'>
END

# ------------------------------------------------ First, check out environment
    $metadatafields{'owner'}=$ENV{'user.name'}.'@'.$ENV{'user.domain'};

# --------------------------------- Compute various listings of metadata values
    
    %language=();
    $language{'any'}='Any language';
    {
	my $fh=Apache::File->new($r->dir_config('lonTabDir').'/language.tab');
	map {
	    $_=~/(\w+)\s+([\w\s\-]+)/;
	    $language{$1}=$2;
	} <$fh>;
    }

    %cprtag=();
    $cprtag{'any'}='Any copyright/distribution';
    {
	my $fh=Apache::File->new($r->dir_config('lonIncludes').'/copyright.tab');
	map {
	    $_=~/(\w+)\s+([\w\s\-]+)/;
	    $cprtag{$1}=$2;
	} <$fh>;
    }

    %mimetag=();
    $mimetag{'any'}='Any type';
    {
	my $fh=Apache::File->new($r->dir_config('lonTabDir').'/filetypes.tab');
	map {
	    $_=~/(\w+)\s+(\w+)\s+([\w\s\-]+)/;
	    $mimetag{$1}=".$1 $3";
	} <$fh>;
    }

    if ($ENV{'form.basicsubmit'} eq 'SEARCH') {
	return &basicsearch($r,$ENV{'form.basicexp'});
    }

    $scrout=''; # building a part of screen output
    $scrout.=&searchphrasefield('Limit by title','title',
			$ENV{'form.title'});

    $scrout.=&searchphrasefield('Limit by author','author',
			$ENV{'form.author'});

    $scrout.=&searchphrasefield('Limit by subject','subject',
			$ENV{'form.subject'});

    $scrout.=&searchphrasefield('Limit by keywords','keywords',
			$ENV{'form.keywords'});

    $scrout.=&searchphrasefield('Limit by URL','url',
			$ENV{'form.url'});

    $scrout.=&searchphrasefield('Limit by version','version',
			$ENV{'form.version'});

    $scrout.=&searchphrasefield('Limit by notes','notes',
			$ENV{'form.notes'});

    $scrout.=&searchphrasefield('Limit by abstract','abstract',
			$ENV{'form.abstract'});

    $ENV{'form.mime'}='notxxx' unless length($ENV{'form.mime'});
    $scrout.=&selectbox('Limit by MIME type','mime',
			$ENV{'form.mime'},%mimetag);

    $ENV{'form.language'}='any' unless length($ENV{'form.language'});

    $scrout.=&selectbox('Limit by language','language',
			$ENV{'form.language'},%language);
    

# ------------------------------------------------ Compute date selection boxes
    $scrout.=<<CREATIONDATESTART;
<p>
<font color="#800000" face="helvetica"><b>LIMIT BY CREATION DATE RANGE:</b>
</font>
<br>
between:
CREATIONDATESTART
    $scrout.=&dateboxes('creationdatestart',1,1,1976,
			$ENV{'form.creationdatestart_month'},
			$ENV{'form.creationdatestart_day'},
			$ENV{'form.creationdatestart_year'},
			);
    $scrout.=<<CREATIONDATEEND;
and:
CREATIONDATEEND
    $scrout.=&dateboxes('creationdateend',12,31,2051,
			$ENV{'form.creationdateend_month'},
			$ENV{'form.creationdateend_day'},
			$ENV{'form.creationdateend_year'},
			);
    $scrout.="</p>";

    $scrout.=<<LASTREVISIONDATESTART;
<p>
<font color="#800000" face="helvetica"><b>LIMIT BY LAST REVISION DATE RANGE:
</b></font>
<br>between:
LASTREVISIONDATESTART
    $scrout.=&dateboxes('lastrevisiondatestart',1,1,1976,
			$ENV{'form.lastrevisiondatestart_month'},
			$ENV{'form.lastrevisiondatestart_day'},
			$ENV{'form.lastrevisiondatestart_year'},
			);
    $scrout.=<<LASTREVISIONDATEEND;
and:
LASTREVISIONDATEEND
    $scrout.=&dateboxes('lastrevisiondateend',12,31,2051,
			$ENV{'form.lastrevisiondateend_month'},
			$ENV{'form.lastrevisiondateend_day'},
			$ENV{'form.lastrevisiondateend_year'},
			);
    $scrout.='</p>';

    $scrout.=&searchphrasefield('Limit by publisher/owner','owner',
				$ENV{'form.owner'});
#			$metadatafields{'owner'});

    $ENV{'form.copyright'}='any' unless length($ENV{'form.copyright'});
    $scrout.=&selectbox('Limit by copyright/distribution','copyright',
			$ENV{'form.copyright'},%cprtag);

# ---------------------------------------------------------------- Print screen
    $r->print(<<ENDDOCUMENT);
<html>
<head>
<title>The LearningOnline Network with CAPA</title>
</head>
<body bgcolor="#FFFFFF">
<img align=right src=/adm/lonIcons/lonlogos.gif>
<h1>Search Catalog</h1>
<form method="post" action="/adm/searchcat">
$hidden
<hr>
<h3>Basic Search</h3>
<p>
Enter terms or phrases separated by search operators
such as AND or OR then press SEARCH below.  Terms should be specific
to the title, author, subject, notes, or abstract information associated
with a resource.
<br>
ENDDOCUMENT
    $r->print(&simpletextfield('basicexp',$ENV{'form.basicexp'}));
    $r->print(' ');
    $r->print(&simplecheckbox('titleonly',$ENV{'form.titleonly'}));
    $r->print('<font color="#800000">Title only</font> ');
    $r->print(&simplecheckbox('allversions',$ENV{'form.allversions'}));
    $r->print(<<ENDDOCUMENT);
<font color="#800000">Search historic archives</font>
<br>
<input type="submit" name="basicsubmit" value="SEARCH">
<input type="reset" name="reset" value="RESET">
<input type="button" name="close" value="CLOSE" onClick="self.close()">
</p>
<hr>
<h3>Advanced Search</h3>
$scrout
<p>
<input type="submit" name="advancedsubmit" value="SEARCH">
<input type="reset" name="reset" value="RESET">
<input type="button" name="close" value="CLOSE" onClick="self.close()">
</p>
</form>
</body>
</html>
ENDDOCUMENT
    return OK;
} 

# --------------------------------------------------------- Various form fields

sub textfield {
    my ($title,$name,$value)=@_;
    return "\n<p><b>$title:</b><br>".
           '<input type=text name="'.$name.'" size=80 value="'.$value.'">';
}

sub simpletextfield {
    my ($name,$value)=@_;
    return '<input type=text name="'.$name.'" size=20 value="'.$value.'">';
}

sub simplecheckbox {
    my ($name,$value)=@_;
    my $checked='';
    $checked="CHECKED" if $value eq 'on';
    return '<input type=checkbox name="'.$name.'" '. $checked . '>';
}

sub searchphrasefield {
    my ($title,$name,$value)=@_;
    my $instruction=<<END;
Enter terms or phrases separated by search operators such
as AND or OR.
END
    my $uctitle=uc($title);
    return "\n<p><font color=\"#800000\" face=\"helvetica\"><b>$uctitle:</b>".
	   "</FONT> $instruction<br>".
           '<input type=text name="'.$name.'" size=80 value="'.$value.'">';
}

sub dateboxes {
    my ($name,$defaultmonth,$defaultday,$defaultyear,
	$currentmonth,$currentday,$currentyear)=@_;
    ($defaultmonth,$defaultday,$defaultyear)=('','','');
    my $month=<<END;
<select name="${name}_month">
<option value='$defaultmonth'> </option>
<option value="1">January</option>
<option value="2">February</option>
<option value="3">March</option>
<option value="4">April</option>
<option value="5">May</option>
<option value="6">June</option>
<option value="7">July</option>
<option value="8">August</option>
<option value="9">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
END
    $month=~s/(\"$currentmonth\")/$1 SELECTED/ if length($currentmonth);
    my $day=<<END;
<select name="${name}_day">
<option value='$defaultday'> </option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
</select>
END
    $day=~s/(\"$currentday\")/$1 SELECTED/ if length($currentday);
    my $year=<<END;
<select name="${name}_year">
<option value='$defaultyear'> </option>
<option value="1976">1976</option>
<option value="1977">1977</option>
<option value="1978">1978</option>
<option value="1979">1979</option>
<option value="1980">1980</option>
<option value="1981">1981</option>
<option value="1982">1982</option>
<option value="1983">1983</option>
<option value="1984">1984</option>
<option value="1985">1985</option>
<option value="1986">1986</option>
<option value="1987">1987</option>
<option value="1988">1988</option>
<option value="1989">1989</option>
<option value="1990">1990</option>
<option value="1991">1991</option>
<option value="1992">1992</option>
<option value="1993">1993</option>
<option value="1994">1994</option>
<option value="1995">1995</option>
<option value="1996">1996</option>
<option value="1997">1997</option>
<option value="1998">1998</option>
<option value="1999">1999</option>
<option value="2000">2000</option>
<option value="2001">2001</option>
<option value="2002">2002</option>
<option value="2003">2003</option>
<option value="2004">2004</option>
<option value="2005">2005</option>
<option value="2006">2006</option>
<option value="2007">2007</option>
<option value="2008">2008</option>
<option value="2009">2009</option>
<option value="2010">2010</option>
<option value="2011">2011</option>
<option value="2012">2012</option>
<option value="2013">2013</option>
<option value="2014">2014</option>
<option value="2015">2015</option>
<option value="2016">2016</option>
<option value="2017">2017</option>
<option value="2018">2018</option>
<option value="2019">2019</option>
<option value="2020">2020</option>
<option value="2021">2021</option>
<option value="2022">2022</option>
<option value="2023">2023</option>
<option value="2024">2024</option>
<option value="2025">2025</option>
<option value="2026">2026</option>
<option value="2027">2027</option>
<option value="2028">2028</option>
<option value="2029">2029</option>
<option value="2030">2030</option>
<option value="2031">2031</option>
<option value="2032">2032</option>
<option value="2033">2033</option>
<option value="2034">2034</option>
<option value="2035">2035</option>
<option value="2036">2036</option>
<option value="2037">2037</option>
<option value="2038">2038</option>
<option value="2039">2039</option>
<option value="2040">2040</option>
<option value="2041">2041</option>
<option value="2042">2042</option>
<option value="2043">2043</option>
<option value="2044">2044</option>
<option value="2045">2045</option>
<option value="2046">2046</option>
<option value="2047">2047</option>
<option value="2048">2048</option>
<option value="2049">2049</option>
<option value="2050">2050</option>
<option value="2051">2051</option>
</select>
END
    $year=~s/(\"$currentyear\")/$1 SELECTED/ if length($currentyear);
    return "$month$day$year";
}

sub selectbox {
    my ($title,$name,$value,%options)=@_;
    my $uctitle=uc($title);
    my $selout="\n<p><font color=\"#800000\" face=\"helvetica\"><b>$uctitle:".
	"</b></font><br>".'<select name="'.$name.'">';
    map {
        $selout.='<option value="'.$_.'"';
        if ($_ eq $value) { $selout.=' selected'; }
        $selout.='>'.$options{$_}.'</option>';
    } sort keys %options;
    return $selout.'</select>';
}

# --------------------------------------------------- Performing a basic search
sub basicsearch {
    my ($r,$expression)=@_;

    my $query=$expression;
    $query="select * from metadata where concat(title,\"    \",author) like '\%$expression\%'";
    my $reply=&Apache::lonnet::reply("querysend:$query",'msul3');

    my @results;

    my $replyfile='';
    $reply=~/^([\.\w]+)$/; # must do since 'use strict' checks for tainting
    $replyfile=$r->dir_config('lonDaemons').'/tmp/'.$1;
    $reply=~/(.*?)\_/;
    my $hostname=$1;

    {
	while (1) {
	    last if -e $replyfile;
	    sleep 1;
	}
	# QUESTION: how should I handle this error condition..
	# I'm sure there is syntax elsewhere I can use..
	my $fh=Apache::File->new($replyfile) or
	       ($r->print('file cannot be opened') and return OK);
       @results=<$fh>;
    }

    my $compiledresult='';

    foreach my $result (@results) {
	my ($title,$author,$subject,$url,$keywords,$version,
	    $notes,$abstract,$mime,$lang,
	    $creationdate,$lastrevisiondate,$owner,$copyright
	    )=map {&Apache::lonnet::unescape($_)} (split(/\,/,$result));
	my $shortabstract=$abstract;
	$shortabstract=substr($abstract,0,200) if length($abstract)>200;
	$compiledresult.=<<END;
<p>
END
	$compiledresult.=<<END if $ENV{'form.catalogmode'} eq 'interactive';
<font size='-1'><INPUT TYPE="button" NAME="returnvalues" VALUE="SELECT"
onClick="javascript:select_data('$title','$url')">
</font>
<br>
END
        $compiledresult.=<<END;
<b>URL: </b> $url<br>
<b>Title:</b> $title<br>
<b>Author(s):</b> $author<br>
<b>Subject:</b> $subject<br>
<b>Keyword(s):</b> $keywords<br>
<b>Notes:</b> $notes<br>
<b>Abstract:</b> $shortabstract<br>
<b>MIME Type:</b> $mimetag{$mime}<br>
<b>Language:</b> $language{$lang}<br>
<b>Creation Date:</b> $creationdate<br>
<b>Last Revision Date:</b> $lastrevisiondate<br>
<b>Publisher/Owner:</b> $owner<br>
<b>Copyright/Distribution:</b> $copyright<br>
<b>Repository Location:</b> $hostname
</p>
END
}

    unless ($compiledresult) {
	$compiledresult="There were no results that matched your query";
    }

    # make query information persistent to allow for subsequent revision
    my $persistent='';
    map {
	if (/^form\./ && !/submit/) {
	    my $name=$_;
	    my $key=$name;
	    $name=~s/^form\.//;
	    $persistent.=<<END;
<INPUT TYPE='hidden' NAME='$name' VALUE='$ENV{$key}'>
END
        }
    } (keys %ENV);

    $r->print(<<BEGINNING);
<html>
<head>
<title>The LearningOnline Network with CAPA</title>
BEGINNING
    $r->print(<<SCRIPT) if $ENV{'form.catalogmode'} eq 'interactive';
<script>
    function select_data(title,url) {
	changeTitle(title);
	changeURL(url);
    }
    function changeTitle(val) {
	if (opener.inf.document.forms.resinfo.elements.t) {
	    opener.inf.document.forms.resinfo.elements.t.value=val;
	}
    }
    function changeURL(val) {
	if (opener.inf.document.forms.resinfo.elements.u) {
	    opener.inf.document.forms.resinfo.elements.u.value=val;
	}
    }
</script>
SCRIPT
    $r->print(<<RESULTS);
</head>
<body bgcolor="#ffffff">
<img align=right src=/adm/lonIcons/lonlogos.gif>
<h1>Search Catalog</h1>
<form method="post" action="/adm/searchcat">
<input type='button' value='Revise search request'
onClick='this.form.submit();'>
<input type='button' value='CLOSE'
onClick='self.close();'>
$persistent
<hr>
<h3>Search Query</h3>
<p>
<b>Basic search:</b> $expression
</p>
<h3>Search Results</h3>
$compiledresult
</body>
</html>
RESULTS

    return OK;
}

1;
__END__

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>