Sorted "use" statements in esthetic order.
[koha_gimpoz] / search.pl
index 4ca5e15..19ae914 100755 (executable)
--- a/search.pl
+++ b/search.pl
 #!/usr/bin/perl
-#script to provide intranet (librarian) advanced search facility
-#modified 9/11/1999 by chris@katipo.co.nz
-#adding an extra comment to play with CVS (Si, 19/11/99)
-#modified 29/12/99 by chris@katipo.co.nz to be usavle by opac as well
-#modified by chris 10/11/00 to fix dewey search
+
+# Copyright 2000-2002 Katipo Communications
+#
+# This file is part of Koha.
+#
+# Koha is free software; you can redistribute it and/or modify it under the
+# terms of the GNU General Public License as published by the Free Software
+# Foundation; either version 2 of the License, or (at your option) any later
+# version.
+#
+# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along with
+# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
+# Suite 330, Boston, MA  02111-1307 USA
 
 use strict;
-use C4::Search;
+require Exporter;
 use CGI;
+use HTML::Template;
+use C4::Context;
+use C4::Search;
+use C4::Auth;
 use C4::Output;
 
-my $env;
-my $input = new CGI;
-print $input->header;
-#print $input->dump;
-#whether it is called from the opac of the intranet                                                            
-my $type=$input->param('type');                                                  
-if ($type eq ''){
-  $type = 'intra';
+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
+if ($subject) {
+       $template = gettemplate("catalogue/subject.tmpl",0);
+} else {
+       $template = gettemplate("catalogue/searchresults.tmpl", 0);
 }
-my $ttype=$input->param('ttype');
-#setup colours                                                                                                 
-my $main;                                                                                                      
-my $secondary;                                                                                                 
-if ($type eq 'opac'){                                                                                          
-  $main='#99cccc';    
-  $secondary='#efe5ef';
-} else {                                                                                                       
-  $main='#cccc99';                                                                                             
-  $secondary='#ffffcc';                                                                                        
-}       
 
-#print $input->dump;
-my $blah;
+my $env;
+$env->{itemcount}=1;
+
+# 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;
-#build hash of users input
-my $title=validate($input->param('title'));
-$search{'title'}=$title;
-my $keyword=validate($input->param('keyword'));
+my $keyword=$query->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'));
+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=validate($input->param('date-before'));
-$search{'date-before'};
-my $class=$input->param('class');
+my $datebefore=$query->param('date-before');
+$search{'date-before'}=$datebefore;
+my $class=$query->param('class');
 $search{'class'}=$class;
-$search{'ttype'}=$ttype;
-my $dewey=validate($input->param('dewey'));
+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);
+# whats this for?
+# I think it is (or was) a search from the "front" page...   [st]
+$search{'front'}=$query->param('front');
+
+my $num=10;
 my @results;
-my $offset=$input->param('offset');
-if ($offset eq ''){
-  $offset=0;
+my $count;
+if (my $subject=$query->param('subjectitems')) {
+    my $blah;
+    @results=subsearch(\$blah,$subject);
+    $count=$#results+1;
+} else {
+    ($count,@results)=catalogsearch($env,'',\%search,$num,$startfrom);
+}
+
+#my $resultsarray=\@results;
+my $resultsarray;
+
+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 ($keyword){
+    $search=$search."&keyword=$keyword";
+    $searchdesc.="keyword $keyword, ";
 }
-print startpage();
-print startmenu($type);
-#print $search{'ttype'};
-if ($type ne 'opac'){
-  print mkheadr(1,'Catalogue Search Results');
-} else {
-  print mkheadr(1,'Opac Search Results');
+if (my $subjectitems=$query->param('subjectitems')){
+    $search=$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 ''){
-#      print "hey";
-      ($count,@results)=&KeywordSearch(\$blah,'intra',\%search,$num,$offset);
-    } elsif ($search{'front'} ne '') {
-    ($count,@results)&FrontSearch(\$blah,'intra',\%search,$num,$offset);
-#    print "hey";
-    }elsif ($title ne '' || $author ne '' || $illustrator ne '' || $dewey ne '' || $class ne '') {
-      ($count,@results)=&CatSearch(\$blah,'loose',\%search,$num,$offset);
-#            print "hey";
-    }
-  }
+if ($subject){
+    $search=$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 ($author){
+    $search=$search."&author=$author";
+    $searchdesc.="author $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 ($class){
+    $search=$search."&class=$class";
+    $searchdesc.="class $class, ";
+}
+if ($title){
+    $search=$search."&title=$title";
+    $searchdesc.="title $title, ";
 }
-my $count2=@results;
-if ($keyword ne '' && $offset > 0){
-  $count2=$count-$offset;
-  if ($count2 > 10){
-    $count2=10;
-  }
+if ($dewey){
+    $search=$search."&dewey=$dewey";
+    $searchdesc.="dewey $dewey, ";
 }
-#print $count2;
-my $i=0;
-my $colour=1;
-while ($i < $count2){
-#    print $results[$i]."\n";
-    my @stuff=split('\t',$results[$i]);
-    $stuff[1]=~ s/\`/\\\'/g;
-    my $title2=$stuff[1];
-    $title2=~ s/ /%20/g;
-    if ($subject eq ''){
-#      print $stuff[0];
-      $stuff[1]=mklink("/cgi-bin/koha/detail.pl?type=$type&bib=$stuff[2]&title=$title2",$stuff[1]);
-      my $word=$stuff[0];
-#      print $word;
-      $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";
-      $stuff[7]=$stuff[5];
-      $stuff[5]='';
-      $stuff[0]=mklink($url,$stuff[0]);
-      my ($count,$lcount,$nacount,$fcount,$scount,$lostcount,$mending,$transit,$ocount)=itemcount($env,$stuff[2],$type);
-      $stuff[4]=$count;
-      if ($nacount > 0){
-        $stuff[5]=$stuff[5]."On Loan";
-       if ($nacount >1 ){                                                                                                         
-         $stuff[5]=$stuff[5]." ($nacount)";                                                                                            
-         }                                                                                                                         
-        $stuff[5].=" ";
-      }
-      if ($lcount > 0){
-         $stuff[5]=$stuff[5]."Levin";
-         if ($lcount >1 ){                                                                                                         
-         $stuff[5]=$stuff[5]." ($lcount)";                                                                                            
-         }                                                                                                                         
-        $stuff[5].=" ";
-      }
-      if ($fcount > 0){
-        $stuff[5]=$stuff[5]."Foxton";
-         if ($fcount >1 ){                                                                                                         
-         $stuff[5]=$stuff[5]." ($fcount)";                                                                                            
-         }                                                                                                                         
-        $stuff[5].=" ";        
-      }
-      if ($scount > 0){
-        $stuff[5]=$stuff[5]."Shannon";
-         if ($scount >1 ){                                                                                                         
-         $stuff[5]=$stuff[5]." ($scount)";                                                                                            
-         }                                                                                                                         
-        $stuff[5].=" ";        
-      }
-      if ($lostcount > 0){
-        $stuff[5]=$stuff[5]."Lost";
-         if ($lostcount >1 ){                                                                                                         
-         $stuff[5]=$stuff[5]." ($lostcount)";                                                                                            
-         }                                                                                                                         
-        $stuff[5].=" ";        
-      }
-      if ($mending > 0){
-        $stuff[5]=$stuff[5]."Mending";
-         if ($mending >1 ){                                                                                                         
-         $stuff[5]=$stuff[5]." ($mending)";                                                                                            
-         }                                                                                                                         
-        $stuff[5].=" ";        
-      }
-      if ($transit > 0){
-        $stuff[5]=$stuff[5]."In Transiit";
-         if ($transit >1 ){                                                                                                         
-         $stuff[5]=$stuff[5]." ($transit)";                                                                                            
-         }                                                                                                                         
-        $stuff[5].=" ";        
-      }
-      if ($ocount > 0){
-        $stuff[5]=$stuff[5]."On Order";
-         if ($ocount >1 ){                                                                                                         
-         $stuff[5]=$stuff[5]." ($ocount)";                                                                                            
-         }                                                                                                                         
-        $stuff[5].=" ";        
-      }
-      
-      if ($type ne 'opac'){
-        $stuff[6]=mklink("/cgi-bin/koha/request.pl?bib=$stuff[2]","Request");
-      }
-    } else {
-      my $word=$stuff[1];
-      $word=~ s/ /%20/g;
-      
-        $stuff[1]=mklink("/cgi-bin/koha/subjectsearch.pl?subject=$word&type=$type",$stuff[1]);
+$search.="&ttype=$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);
+$template->param(loggedinuser => $loggedinuser);
 
-    if ($colour == 1){
-      if ($illustrator) {
-         print mktablerow(7,$secondary,$stuff[1],$stuff[0],$stuff[7],$stuff[3],$stuff[4],$stuff[5],$stuff[6]);
-      } else {
-         print mktablerow(6,$secondary,$stuff[1],$stuff[0],$stuff[3],$stuff[4],$stuff[5],$stuff[6]);
-      }
-      $colour=0;
-    } else {
-      if ($illustrator) {
-         print mktablerow(7,'white',$stuff[1],$stuff[0],$stuff[7],$stuff[3],$stuff[4],$stuff[5],$stuff[6]);
-      } else {
-         print mktablerow(6,'white',$stuff[1],$stuff[0],$stuff[3],$stuff[4],$stuff[5],$stuff[6]);
-      }
-      $colour=1;
-    }
-    $i++;
-}
-$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');
+my $numbers;
+@$numbers=();
+if ($count>10) {
+    for (my $i=1; $i<$count/10+1; $i++) {
+       if ($i<16) {
+           ($title) && (push @$forminputs, { line => "title=$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))};
+       }
     }
-} else {
- if ($illustrator) {
-     print mktablerow(7,$main,' &nbsp; ',' &nbsp; ',' &nbsp;',' &nbsp; ','', '','');
- } else {
-     print mktablerow(6,$main,' &nbsp; ',' &nbsp; ',' &nbsp;',' &nbsp; ','','');
- }
 }
-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;
-}
-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 ";
-}
-  
-print endcenter();
-print endmenu($type);
-print endpage();
+$template->param(numbers => $numbers);
 
 
-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);
-}
+
+print $query->header(-cookie => $cookie), $template->output;
+