search on MARC datas.
[koha.git] / search.marc / search.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20 use strict;
21 require Exporter;
22 use CGI;
23 use C4::Auth;
24 use HTML::Template;
25 use C4::Context;
26 use C4::Search;
27 use C4::Auth;
28 use C4::Output;
29 use C4::Biblio;
30 use C4::SearchMarc;
31
32 my $query=new CGI;
33 my $type=$query->param('type');
34 my $op = $query->param('op');
35 #$type="opac" unless $type;
36
37 my $dbh = C4::Context->dbh;
38
39 my ($loggedinuser, $cookie, $sessionID) = checkauth($query, ($type eq 'opac') ? (1) : (0));
40
41 my $startfrom=$query->param('startfrom');
42 ($startfrom) || ($startfrom=0);
43 my ($template, $loggedinuser, $cookie);
44
45 if ($op eq "do_search") {
46         my @marclist = $query->param('marclist');
47         my @and_or = $query->param('and_or');
48         my @excluding = $query->param('excluding');
49         my @operator = $query->param('operator');
50         my @value = $query->param('value');
51         # builds tag and subfield arrays
52         my @tags;
53         my @subfields;
54         foreach my $marc (@marclist) {
55                 push @tags, substr($marc,0,3);
56                 push @subfields, substr($marc,3,1);
57         }
58         my @results = catalogsearch($dbh, \@tags, \@subfields, \@and_or, 
59                                                                                         \@excluding, \@operator, \@value, 
60                                                                                         $startfrom, 20);
61         ($template, $loggedinuser, $cookie)
62                 = get_template_and_user({template_name => "search.marc/result.tmpl",
63                                 query => $query,
64                                 type => $type,
65                                 authnotrequired => 0,
66                                 flagsrequired => {catalogue => 1},
67                                 debug => 1,
68                                 });
69         $template->param(loggedinuser => $loggedinuser,
70                                                         result => \@results);
71
72 } else {
73         ($template, $loggedinuser, $cookie)
74                 = get_template_and_user({template_name => "search.marc/search.tmpl",
75                                 query => $query,
76                                 type => $type,
77                                 authnotrequired => 0,
78                                 flagsrequired => {catalogue => 1},
79                                 debug => 1,
80                                 });
81         $template->param(loggedinuser => $loggedinuser);
82         my $tagslib;
83         if ($type eq "opac") {
84                 $tagslib = &MARCgettagslib($dbh,1);
85         } else {
86                 $tagslib = &MARCgettagslib($dbh,1);
87         }
88         my @marcarray;
89         push @marcarray,"";
90         for (my $tabloop = 0; $tabloop<=9;$tabloop++) {
91         push @marcarray,"--------------------------------------- $tabloop ---------------------------------------";
92                 foreach my $tag (sort(keys (%{$tagslib}))) {
93                         foreach my $subfield (sort(keys %{$tagslib->{$tag}})) {
94                                 next if ($subfield eq 'lib'); # skip lib and tabs, which are koha internal
95                                 next if ($subfield eq 'tab');
96                                 next unless ($tagslib->{$tag}->{$subfield}->{tab} eq $tabloop);
97                                 push @marcarray, "$tag$subfield - $tagslib->{$tag}->{$subfield}->{lib}";
98                         }
99                 }
100         }
101         my $marclist = CGI::scrolling_list(-name=>"marclist",
102                                         -values=> \@marcarray,
103                                         -size=>1,
104                                         -multiple=>0,
105                                         -onChange => "sql_update()",
106                                         );
107         $template->param("marclist" => $marclist);
108 }
109 # Print the page
110 print $query->header(-cookie => $cookie), $template->output;