adding Content-Disposition to header to allow for browsers to recognize
[koha.git] / search.pl
index 4b0fc76..53d1f8d 100755 (executable)
--- a/search.pl
+++ b/search.pl
@@ -1,7 +1,6 @@
 #!/usr/bin/perl
-#script to provide intranet (librarian) advanced search facility
-
 
+# $Id$
 # Copyright 2000-2002 Katipo Communications
 #
 # This file is part of Koha.
 # 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;
-use C4::Search;
+require Exporter;
 use CGI;
+use C4::Auth;
+use HTML::Template;
+use C4::Context;
+use C4::Search;
 use C4::Output;
+use C4::Interface::CGI::Output;
+
+my $query=new CGI;
+my $type=$query->param('type');
+
+my $startfrom=$query->param('startfrom');
+($startfrom) || ($startfrom=0);
+
+my $subject=$query->param('subject');
+# if it's a subject we need to use the subject.tmpl
+my ($template, $loggedinuser, $cookie);
+if ($subject) {
+       ($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, $loggedinuser, $cookie)
+               = get_template_and_user({template_name => "catalogue/searchresults.tmpl",
+                            query => $query,
+                            type => "intranet",
+                            authnotrequired => 0,
+                            flagsrequired => {catalogue => 1},
+                            debug => 1,
+                            });
+}
 
-my $env;
-my $input = new CGI;
-print $input->header;
-#print $input->dump;
-
-#whether it is called from the opac or the intranet
-my $type=$input->param('type');if ($type eq ''){
-  $type = 'intra';
+# %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
+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 };
 }
 
-my $ttype=$input->param('ttype');
+$template->param(FORMINPUTS => \@forminputs);
 
-#setup colours                 
-my $main;
-my $secondary;
+# whats this for?
+# I think it is (or was) a search from the "front" page...   [st]
+$search{'front'}=$query->param('front');
 
-if ($type eq 'opac'){
-  $main='#99cccc';    
-  $secondary='#efe5ef';
+my $num=10;
+my @results;
+my $count;
+if (my $subject=$query->param('subjectitems')) {
+    my $blah;
+    @results=subsearch(\$blah,$subject);
+    $count=$#results+1;
 } else {
-  $main='#cccc99';
-  $secondary='#ffffcc';
-}       
-
-#print $input->Dump;
-my $blah;
-my %search;
-
-#build hash of users input
-my $title=validate($input->param('title'));
-$search{'title'}=$title;
-
-my $keyword=validate($input->param('keyword'));
-$search{'keyword'}=$keyword;
-
-$search{'front'}=validate($input->param('front'));
-
-my $author=validate($input->param('author'));
-$search{'author'}=$author;
-
-my $illustrator=validate($input->param('illustrator'));
-$search{'illustrator'}=$illustrator;
-
-my $subject=validate($input->param('subject'));
-$search{'subject'}=$subject;
-
-my $itemnumber=validate($input->param('item'));
-$search{'item'}=$itemnumber;
-
-my $isbn=validate($input->param('isbn'));
-$search{'isbn'}=$isbn;
-
-my $datebefore=validate($input->param('date-before'));
-$search{'date-before'};
-
-my $class=$input->param('class');
-$search{'class'}=$class;
-
-$search{'ttype'}=$ttype;
-
-my $dewey=validate($input->param('dewey'));
-$search{'dewey'}=$dewey;
+    ($count,@results)=catalogsearch(\%env,'',\%search,$num,$startfrom);
+}
 
-my $branch=validate($input->param('branch'));
-$search{'branch'}=$branch;
+#my $resultsarray=\@results;
+my $resultsarray;
 
-my @results;
-my $offset=$input->param('offset');
-if ($offset eq ''){
-  $offset=0;
+foreach my $result (@results) {
+    $result->{'authorhtmlescaped'}=$result->{'author'};
+    $result->{'authorhtmlescaped'}=~s/ /%20/g;
+    ($result->{'copyrightdate'}==0) && ($result->{'copyrightdate'}='');
+    ($type eq 'opac') ? ($result->{'opac'}=1) : ($result->{'opac'}=0);
+    push (@$resultsarray, $result);
 }
-my $num=$input->param('num');
-if ($num eq ''){
-  $num=10;
+($resultsarray) || (@$resultsarray=());
+my $search="num=20";
+my $searchdesc='';
+if ($search{"keyword"}) {
+    $search .= "&keyword=$search{keyword}";
+    $searchdesc.="keyword $search{keyword}, ";
 }
-print startpage();
-print startmenu($type);
-#print $type;
-#print $search{'ttype'};
-if ($type eq 'intra'){
-  print mkheadr(1,'Catalogue Search Results');
-} elsif ($type eq 'catmain'){
-  print mkheadr(1,'Catalogue Maintenance');
-} else {
-  print mkheadr(1,'Opac Search Results');
+if (my $subjectitems=$query->param('subjectitems')){
+    $search .= "&subjectitems=$subjectitems";
+    $searchdesc.="subject $subjectitems, ";
 }
-print center();
-my $count;
-my @results;
-if ($itemnumber ne '' || $isbn ne ''){
-    ($count,@results)=&CatSearch(\$blah,'precise',\%search,$num,$offset);
-} else {
-  if ($subject ne ''){
-    ($count,@results)=&CatSearch(\$blah,'subject',\%search,$num,$offset);
-  } else {
-    if ($keyword ne ''){
-      ($count,@results)=&KeywordSearch(\$blah,'intra',\%search,$num,$offset);
-    }elsif ($title ne '' || $author ne '' || $illustrator ne '' || $dewey ne '' || $class ne '') {
-      ($count,@results)=&CatSearch(\$blah,'loose',\%search,$num,$offset);
-    }
-  }
+if ($subject){
+    $search .= "&subject=$subject";
+    $searchdesc.="subject $subject, ";
 }
-print "You searched on ";
-while ( my ($key, $value) = each %search) {                                 
-  if ($value ne '' && $key ne 'ttype'){
-    $value=~ s/\\//g;
-    print bold("$key $value,");
-  }                          
+if ($search{"author"}){
+    $search .= "&author=$search{author}";
+    $searchdesc.="author $search{author}, ";
 }
-print " $count results found";
-my $offset2=$num+$offset;
-my $dispnum=$offset+1;
-print "<br> Results $dispnum to $offset2 displayed";
-print mktablehdr;
-if ($type ne 'opac'){
-  if ($subject ne ''){
-   print mktablerow(1,$main,'<b>SUBJECT</b>','/images/background-mem.gif');
-  } elsif ($illustrator ne '') {
-   print mktablerow(7,$main,'<b>TITLE</b>','<b>AUTHOR</b>', '<b>ILLUSTRATOR<b>', bold('&copy;'),'<b>COUNT</b>',bold('LOCATION'),'','/images/background-mem.gif');
-  } else {
-   print mktablerow(6,$main,'<b>TITLE</b>','<b>AUTHOR</b>',bold('&copy;'),'<b>COUNT</b>',bold('LOCATION'),'','/images/background-mem.gif');
-  }
-} else {
-  if ($subject ne ''){
-   print mktablerow(6,$main,'<b>SUBJECT</b>',' &nbsp; ',' &nbsp; ');
-  } elsif ($illustrator ne '') {
-   print mktablerow(7,$main,'<b>TITLE</b>','<b>AUTHOR</b>','<b>ILLUSTRATOR</b>', bold('&copy;'),'<b>COUNT</b>',bold('BRANCH'),'');
-  } else {
-   print mktablerow(6,$main,'<b>TITLE</b>','<b>AUTHOR</b>',bold('&copy;'),'<b>COUNT</b>',bold('BRANCH'),'');
-  }
+if ($search{"class"}){
+    $search .= "&class=$search{class}";
+    $searchdesc.="class $search{class}, ";
 }
-my $count2=@results;
-if ($keyword ne '' && $offset > 0){
-  $count2=$count-$offset;
-  if ($count2 > 10){
-    $count2=10;
-  }
+if ($search{"title"}){
+    $search .= "&title=$search{title}";
+    $searchdesc.="title $search{title}, ";
 }
-#print $count2;
-my $i=0;
-my $colour=1;
-while ($i < $count2){
-#    print $results[$i]."\n";
-#    my @stuff=split('\t',$results[$i]);
-    my $result=$results[$i];
-    $result->{'title'}=~ s/\`/\\\'/g;
-    my $title2=$result->{'title'};
-    $title2=~ s/ /%20/g;
-    my $location='';
-    my $itemcount;
-    if ($subject eq ''){
-      $result->{'title'}=mklink("/cgi-bin/koha/detail.pl?type=$type&bib=$result->{'biblionumber'}&title=$title2",$result->{'title'});
-      my $word=$result->{'author'};
-      $word=~ s/([a-z]) +([a-z])/$1%20$2/ig;
-      $word=~ s/  //g;
-      $word=~ s/ /%20/g;
-      $word=~ s/\,/\,%20/g;
-      $word=~ s/\n//g;
-      my $url="/cgi-bin/koha/search.pl?author=$word&type=$type";
-      $result->{'author'}=mklink($url,$result->{'author'});
-      my ($count,$lcount,$nacount,$fcount,$scount,$lostcount,$mending,$transit,$ocount)=itemcount($env,$result->{'biblionumber'},$type);
-      $itemcount=$count;
-      ####
-      # Fix this chunk below, remove all hardcoded branch references
-      # need to fix itemcount as well
-      ###
-      if ($nacount > 0){
-        $location=$location."On Loan";
-       if ($nacount >1 ){                                                                                                         
-         $location=$location." ($nacount)";                                                                                            
-         }                                                                                                                         
-        $location.=" ";
-      }
-      if ($lcount > 0){
-         $location=$location."Levin";
-         if ($lcount >1 ){                                                                                                         
-         $location=$location." ($lcount)";                                                                                            
-         }                                                                                                                         
-        $location.=" ";
-      }
-      if ($fcount > 0){
-        $location=$location."Foxton";
-         if ($fcount >1 ){                                                                                                         
-         $location=$location." ($fcount)";                                                                                            
-         }                                                                                                                         
-        $location.=" ";        
-      }
-      if ($scount > 0){
-        $location=$location."Shannon";
-         if ($scount >1 ){                                                                                                         
-         $location=$location." ($scount)";                                                                                            
-         }                                                                                                                         
-        $location.=" ";        
-      }
-      if ($lostcount > 0){
-        $location=$location."Lost";
-         if ($lostcount >1 ){                                                                                                         
-         $location=$location." ($lostcount)";                                                                                            
-         }                                                                                                                         
-        $location.=" ";        
-      }
-      if ($mending > 0){
-        $location=$location."Mending";
-         if ($mending >1 ){                                                                                                         
-         $location=$location." ($mending)";                                                                                            
-         }                                                                                                                         
-        $location.=" ";        
-      }
-      if ($transit > 0){
-        $location=$location."In Transiit";
-         if ($transit >1 ){                                                                                                         
-         $location=$location." ($transit)";                                                                                            
-         }                                                                                                                         
-        $location.=" ";        
-      }
-      if ($ocount > 0){
-        $location=$location."On Order";
-         if ($ocount >1 ){                                                                                                         
-         $location=$location." ($ocount)";                                                                                            
-         }                                                                                                                         
-        $location.=" ";        
-      }
-      
-#      if ($type ne 'opac'){
-#        $result->{'request'}=mklink("/cgi-bin/koha/request.pl?bib=$stuff[2]","Request");
-#      }
-    } else {
-      my $word=$result->{'subject'};
-      $word=~ s/ /%20/g;
-      
-        $result->{'title'}=mklink("/cgi-bin/koha/subjectsearch.pl?subject=$word&type=$type",$result->{'subject'});
-
-    }
-
-    if ($colour == 1){
-      if ($illustrator) {
-         print mktablerow(7,$secondary,$result->{'title'},$result->{'author'},$result->{'illus'},$result->{'copyrightdate'},$itemcount,$location);
-      } else {
-         print mktablerow(6,$secondary,$result->{'title'},$result->{'author'},$result->{'copyrightdate'},$itemcount,$location);
-      }
-      $colour=0;
-    } else {
-      if ($illustrator) {
-         print mktablerow(7,'white',$result->{'title'},$result->{'author'},$result->{'illus'},$result->{'copyrightdate'},$itemcount,$location);
-      } else {
-         print mktablerow(6,'white',$result->{'title'},$result->{'author'},$result->{'copyrightdate'},$itemcount,$location);
-      }
-      $colour=1;
-    }
-    $i++;
+if ($search{"dewey"}){
+    $search .= "&dewey=$search{dewey}";
+    $searchdesc.="dewey $search{dewey}, ";
 }
-$offset=$num+$offset;
-if ($type ne 'opac'){
-    if ($illustrator) {
-        print mktablerow(7,$main,' &nbsp; ',' &nbsp; ',' &nbsp;',' &nbsp;','','','','/images/background-mem.gif');
-    } else {
-        print mktablerow(6,$main,' &nbsp; ',' &nbsp; ',' &nbsp;',' &nbsp;','','','/images/background-mem.gif');
-    }
-} else {
- if ($illustrator) {
-     print mktablerow(7,$main,' &nbsp; ',' &nbsp; ',' &nbsp;',' &nbsp; ','', '','');
- } else {
-     print mktablerow(6,$main,' &nbsp; ',' &nbsp; ',' &nbsp;',' &nbsp; ','','');
- }
+if ($search{"illustrator"}){
+    $search .= "&illustrator=$search{illustrator}";
+    $searchdesc.="illustrator $search{illustrator}, ";
 }
-print mktableft();
-my $search;
-
-    $search="num=$num&offset=$offset&type=$type";
-    if ($subject ne ''){
-      $subject=~ s/ /%20/g;
-      $search=$search."&subject=$subject";
-    }
-    if ($title ne ''){
-      $title=~ s/ /%20/g;
-      $search=$search."&title=$title";
-    }
-    if ($author ne ''){
-      $author=~ s/ /%20/g;
-      $search=$search."&author=$author";
-    }
-    if ($keyword ne ''){
-      $keyword=~ s/ /%20/g;
-      $search=$search."&keyword=$keyword";
-    }
-    if ($class ne ''){
-      $keyword=~ s/ /%20/g;
-      $search=$search."&class=$class";
-    }
-    if ($dewey ne ''){
-      $search=$search."&dewey=$dewey";
-    }
-    $search.="&ttype=$ttype";    
-if ($offset < $count){    
-    my $stuff=mklink("/cgi-bin/koha/search.pl?$search",'Next');
-    print $stuff;
+if ($search{"itemnumber"}){
+    $search .= "&itemnumber=$search{itemnumber}";
+    $searchdesc.="barcode $search{itemnumber}, ";
 }
-print "<br>";
-my $pages=$count/10;
-$pages++;
-for (my $i=1;$i<$pages;$i++){
-  my $temp=$i*10;
-  $temp=$temp-10;
-  $search=~ s/offset=[0-9]+/offset=$temp/;
-  my $stuff=mklink("/cgi-bin/koha/search.pl?$search",$i);
-  print "$stuff ";
+$search.="&ttype=$search{ttype}";
+
+$search=~ s/ /%20/g;
+$template->param(startfrom => $startfrom+1);
+($startfrom+$num<=$count) ? ($template->param(endat => $startfrom+$num)) : ($template->param(endat => $count));
+$template->param(numrecords => $count);
+my $nextstartfrom=($startfrom+$num<$count) ? ($startfrom+$num) : (-1);
+my $prevstartfrom=($startfrom-$num>=0) ? ($startfrom-$num) : (-1);
+$template->param(nextstartfrom => $nextstartfrom);
+my $displaynext=1;
+my $displayprev=0;
+($nextstartfrom==-1) ? ($displaynext=0) : ($displaynext=1);
+($prevstartfrom==-1) ? ($displayprev=0) : ($displayprev=1);
+$template->param(displaynext => $displaynext);
+$template->param(displayprev => $displayprev);
+($type eq 'opac') ? ($template->param(opac => 1)) : ($template->param(opac => 0));
+$template->param(prevstartfrom => $prevstartfrom);
+$template->param(search => $search);
+$template->param(searchdesc => $searchdesc);
+$template->param(SEARCH_RESULTS => $resultsarray);
+#$template->param(includesdir => $includes);
+
+my @numbers = ();
+if ($count>10) {
+    for (my $i=1; $i<$count/10+1; $i++) {
+       if ($i<16) {
+           if ($search{"title"})
+           {
+               push @forminputs, { line => "title=$search{title}"};
+           }
+           my $highlight=0;
+           ($startfrom==($i-1)*10) && ($highlight=1);
+           my $formelements='';
+           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))};
+       }
+    }
 }
-  
-print endcenter();
-print endmenu($type);
-print endpage();
 
+$template->param(numbers => \@numbers);
+if (C4::Context->boolean_preference('marc') eq '1') {
+       $template->param(script => "MARCdetail.pl");
+} else {
+       $template->param(script => "detail.pl");
+}
 
-sub validate {
-  my ($input)=@_;
-  $input=~ s/\<[a-z]+\>//gi;
-  $input=~ s/\<\/[a-z]+\>//gi;
-  $input=~ s/\<//g;
-  $input=~ s/\>//g;
-  $input=~ s/^%//g;
-  return($input);
+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;
 }