Script to add or edit a review
[koha.git] / search.pl
index 73dbaca..53d1f8d 100755 (executable)
--- a/search.pl
+++ b/search.pl
@@ -1,9 +1,6 @@
 #!/usr/bin/perl
 
-use HTML::Template;
-
-
-
+# $Id$
 # Copyright 2000-2002 Katipo Communications
 #
 # This file is part of Koha.
@@ -21,93 +18,106 @@ use HTML::Template;
 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
 # Suite 330, Boston, MA  02111-1307 USA
 
+# $Log$
+# Revision 1.36  2005/06/20 14:39:11  tipaul
+# synch'ing 2.2 and head
+#
+# Revision 1.35.2.1  2005/05/27 10:02:50  hdl
+# Bug Fixing : Using old search.pl with subjectitems In normal mode display.
+# Problem : Changing Page doesn't work.
+#
+# Revision 1.35  2004/04/07 22:43:04  rangi
+# Fix for bug 217
+#
+# Revision 1.34  2004/02/11 08:35:31  tipaul
+# synch'ing 2.0.0 branch and head
+#
+# Revision 1.33  2003/12/19 17:28:03  tipaul
+# fix for #683
+#
+# Revision 1.32.2.2  2004/01/13 17:33:39  tipaul
+# removing useless (& buggy here) checkauth
+#
+# Revision 1.32.2.1  2003/12/19 17:28:42  tipaul
+# fix for 683
+#
+# Revision 1.32  2003/06/11 18:37:55  tonnesen
+# Using boolean_preference instead of preference for 'marc' setting
+#
+# Revision 1.31  2003/05/11 07:31:37  rangi
+# Removing duplicate use C4::Auth
+#
+
 use strict;
 require Exporter;
-
 use CGI;
+use C4::Auth;
+use HTML::Template;
 use C4::Context;
 use C4::Search;
-use C4::Auth;
 use C4::Output;
+use C4::Interface::CGI::Output;
 
 my $query=new CGI;
 my $type=$query->param('type');
-(-e "opac") && ($type='opac');
-
-my ($loggedinuser, $cookie, $sessionID) = checkauth($query, ($type eq 'opac') ? (1) : (0));
-
 
 my $startfrom=$query->param('startfrom');
 ($startfrom) || ($startfrom=0);
 
 my $subject=$query->param('subject');
-my $template;
 # if it's a subject we need to use the subject.tmpl
+my ($template, $loggedinuser, $cookie);
 if ($subject) {
-       $template = gettemplate("catalogue/subject.tmpl",0);
+       ($template, $loggedinuser, $cookie)
+               = get_template_and_user({template_name => "catalogue/subject.tmpl",
+                            query => $query,
+                            type => "intranet",
+                            authnotrequired => 0,
+                            flagsrequired => {catalogue => 1},
+                            debug => 1,
+                            });
 } else {
-       $template = gettemplate("catalogue/searchresults.tmpl", 0);
+       ($template, $loggedinuser, $cookie)
+               = get_template_and_user({template_name => "catalogue/searchresults.tmpl",
+                            query => $query,
+                            type => "intranet",
+                            authnotrequired => 0,
+                            flagsrequired => {catalogue => 1},
+                            debug => 1,
+                            });
 }
 
-my $env;
-$env->{itemcount}=1;
+# %env
+# Additional parameters for &catalogsearch
+my %env = (
+       itemcount       => 1,   # If set to 1, &catalogsearch enumerates
+                               # the results found and returns the number
+                               # of items found, where they're located,
+                               # etc.
+       );
 
 # get all the search variables
 # we assume that C4::Search will validate these values for us
-# FIXME - This whole section, up to the &catalogsearch call, is crying
-# out for
-#      foreach $search_term (qw(keyword subject author ...))
-#      { ... }
-my %search;
-my $keyword=$query->param('keyword');
-$search{'keyword'}=$keyword;
-
-$search{'subject'}=$subject;
-my $author=$query->param('author');
-$search{'author'}=$author;
-$search{'authoresc'}=$author;
-#$search{'authorhtmlescaped'}=~s/ /%20/g;
-my $illustrator=$query->param('illustrator');
-$search{'param'}=$illustrator;
-my $itemnumber=$query->param('itemnumber');
-$search{'itemnumber'}=$itemnumber;
-my $isbn=$query->param('isbn');
-$search{'isbn'}=$isbn;
-my $datebefore=$query->param('date-before');
-$search{'date-before'}=$datebefore;
-my $class=$query->param('class');
-$search{'class'}=$class;
-my $dewey=$query->param('dewey');
-$search{'dewey'}=$dewey;
-my $branch=$query->param('branch');
-$search{'branch'}=$branch;
-my $title=$query->param('title');
-$search{'title'}=$title;
-my $abstract=$query->param('abstract');
-$search{'abstract'}=$abstract;
-my $publisher=$query->param('publisher');
-$search{'publisher'}=$publisher;
-
-my $ttype=$query->param('ttype');
-$search{'ttype'}=$ttype;
-
-my $forminputs;
-($keyword) && (push @$forminputs, { line => "keyword=$keyword"});
-($subject) && (push @$forminputs, { line => "subject=$subject"});
-($author) && (push @$forminputs, { line => "author=$author"});
-($illustrator) && (push @$forminputs, { line => "illustrator=$illustrator"});
-($itemnumber) && (push @$forminputs, { line => "itemnumber=$itemnumber"});
-($isbn) && (push @$forminputs, { line => "isbn=$isbn"});
-($datebefore) && (push @$forminputs, { line => "date-before=$datebefore"});
-($class) && (push @$forminputs, { line => "class=$class"});
-($dewey) && (push @$forminputs, { line => "dewey=$dewey"});
-($branch) && (push @$forminputs, { line => "branch=$branch"});
-($title) && (push @$forminputs, { line => "title=$title"});
-($ttype) && (push @$forminputs, { line => "ttype=$ttype"});
-($abstract) && (push @$forminputs, { line => "abstract=$abstract"});
-($publisher) && (push @$forminputs, { line => "publisher=$publisher"});
-($forminputs) || (@$forminputs=());
-$template->param(FORMINPUTS => $forminputs);
+my %search;                    # Search terms. If the key is "author",
+                               # then $search{author} is the author the
+                               # user is looking for.
+
+my @forminputs;                        # This is used in the form template.
+
+foreach my $term (qw(keyword subject author illustrator itemnumber
+                    isbn date-before class dewey branch title abstract
+                    publisher ttype subjectitems))
+{
+       my $value = $query->param($term);
+       next unless defined $value && $value ne "";
+                               # Skip blank search terms
+       $search{$term} = $value;
+       push @forminputs, {     term => $term,
+                                                       value =>$value };
+}
+
+$template->param(FORMINPUTS => \@forminputs);
+
 # whats this for?
 # I think it is (or was) a search from the "front" page...   [st]
 $search{'front'}=$query->param('front');
@@ -120,7 +130,7 @@ if (my $subject=$query->param('subjectitems')) {
     @results=subsearch(\$blah,$subject);
     $count=$#results+1;
 } else {
-    ($count,@results)=catalogsearch($env,'',\%search,$num,$startfrom);
+    ($count,@results)=catalogsearch(\%env,'',\%search,$num,$startfrom);
 }
 
 #my $resultsarray=\@results;
@@ -136,35 +146,43 @@ foreach my $result (@results) {
 ($resultsarray) || (@$resultsarray=());
 my $search="num=20";
 my $searchdesc='';
-if ($keyword){
-    $search=$search."&keyword=$keyword";
-    $searchdesc.="keyword $keyword, ";
+if ($search{"keyword"}) {
+    $search .= "&keyword=$search{keyword}";
+    $searchdesc.="keyword $search{keyword}, ";
 }
 if (my $subjectitems=$query->param('subjectitems')){
-    $search=$search."&subjectitems=$subjectitems";
+    $search .= "&subjectitems=$subjectitems";
     $searchdesc.="subject $subjectitems, ";
 }
 if ($subject){
-    $search=$search."&subject=$subject";
+    $search .= "&subject=$subject";
     $searchdesc.="subject $subject, ";
 }
-if ($author){
-    $search=$search."&author=$author";
-    $searchdesc.="author $author, ";
+if ($search{"author"}){
+    $search .= "&author=$search{author}";
+    $searchdesc.="author $search{author}, ";
+}
+if ($search{"class"}){
+    $search .= "&class=$search{class}";
+    $searchdesc.="class $search{class}, ";
+}
+if ($search{"title"}){
+    $search .= "&title=$search{title}";
+    $searchdesc.="title $search{title}, ";
 }
-if ($class){
-    $search=$search."&class=$class";
-    $searchdesc.="class $class, ";
+if ($search{"dewey"}){
+    $search .= "&dewey=$search{dewey}";
+    $searchdesc.="dewey $search{dewey}, ";
 }
-if ($title){
-    $search=$search."&title=$title";
-    $searchdesc.="title $title, ";
+if ($search{"illustrator"}){
+    $search .= "&illustrator=$search{illustrator}";
+    $searchdesc.="illustrator $search{illustrator}, ";
 }
-if ($dewey){
-    $search=$search."&dewey=$dewey";
-    $searchdesc.="dewey $dewey, ";
+if ($search{"itemnumber"}){
+    $search .= "&itemnumber=$search{itemnumber}";
+    $searchdesc.="barcode $search{itemnumber}, ";
 }
-$search.="&ttype=$ttype";
+$search.="&ttype=$search{ttype}";
 
 $search=~ s/ /%20/g;
 $template->param(startfrom => $startfrom+1);
@@ -185,30 +203,43 @@ $template->param(search => $search);
 $template->param(searchdesc => $searchdesc);
 $template->param(SEARCH_RESULTS => $resultsarray);
 #$template->param(includesdir => $includes);
-$template->param(loggedinuser => $loggedinuser);
 
-my $numbers;
-@$numbers=();
+my @numbers = ();
 if ($count>10) {
     for (my $i=1; $i<$count/10+1; $i++) {
        if ($i<16) {
-           ($title) && (push @$forminputs, { line => "title=$title"});
+           if ($search{"title"})
+           {
+               push @forminputs, { line => "title=$search{title}"};
+           }
            my $highlight=0;
            ($startfrom==($i-1)*10) && ($highlight=1);
            my $formelements='';
-           foreach (@$forminputs) {
+           foreach (@forminputs) {
                my $line=$_->{line};
                $formelements.="$line&";
            }
            $formelements=~s/ /+/g;
-           push @$numbers, { number => $i, highlight => $highlight , FORMELEMENTS => $formelements, FORMINPUTS => $forminputs, startfrom => ($i-1)*10, opac => (($type eq 'opac') ? (1) : (0))};
+           push @numbers, { number => $i, highlight => $highlight , FORMELEMENTS => $formelements, FORMINPUTS => \@forminputs, startfrom => ($i-1)*10, opac => (($type eq 'opac') ? (1) : (0))};
        }
     }
 }
 
-$template->param(numbers => $numbers);
-
-
-
-print $query->header(-cookie => $cookie), $template->output;
+$template->param(numbers => \@numbers);
+if (C4::Context->boolean_preference('marc') eq '1') {
+       $template->param(script => "MARCdetail.pl");
+} else {
+       $template->param(script => "detail.pl");
+}
 
+if ($search{"itemnumber"} && $count == 1){
+    # if its a barcode search by definition we will only have one result.
+    # And if we have a result
+    # lets jump straight to the detail.pl page
+    print $query->redirect("/cgi-bin/koha/detail.pl?type=intra&bib=$results[0]->{'biblionumber'}");
+}
+else {
+    # otherwise
+    # Print the page
+    output_html_with_http_headers $query, $cookie, $template->output;
+}