Handle non-latin1 charsets
[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::Charset;
30 use C4::Biblio;
31 use C4::SearchMarc;
32
33 my $query=new CGI;
34 my $type=$query->param('type');
35 my $op = $query->param('op');
36 #$type="opac" unless $type;
37
38 my $dbh = C4::Context->dbh;
39
40 my ($loggedinuser, $cookie, $sessionID) = checkauth($query, ($type eq 'opac') ? (1) : (0));
41
42 my $startfrom=$query->param('startfrom');
43 ($startfrom) || ($startfrom=0);
44 my ($template, $loggedinuser, $cookie);
45
46 if ($op eq "do_search") {
47         my @marclist = $query->param('marclist');
48         my @and_or = $query->param('and_or');
49         my @excluding = $query->param('excluding');
50         my @operator = $query->param('operator');
51         my @value = $query->param('value');
52         # builds tag and subfield arrays
53         my @tags;
54         my @subfields;
55         foreach my $marc (@marclist) {
56                 push @tags, substr($marc,0,3);
57                 push @subfields, substr($marc,3,1);
58         }
59         my @results = catalogsearch($dbh, \@tags, \@subfields, \@and_or, 
60                                                                                         \@excluding, \@operator, \@value, 
61                                                                                         $startfrom, 20);
62         ($template, $loggedinuser, $cookie)
63                 = get_template_and_user({template_name => "search.marc/result.tmpl",
64                                 query => $query,
65                                 type => $type,
66                                 authnotrequired => 0,
67                                 flagsrequired => {catalogue => 1},
68                                 debug => 1,
69                                 });
70         $template->param(loggedinuser => $loggedinuser,
71                                                         result => \@results);
72
73 } else {
74         ($template, $loggedinuser, $cookie)
75                 = get_template_and_user({template_name => "search.marc/search.tmpl",
76                                 query => $query,
77                                 type => $type,
78                                 authnotrequired => 0,
79                                 flagsrequired => {catalogue => 1},
80                                 debug => 1,
81                                 });
82         $template->param(loggedinuser => $loggedinuser);
83         my $tagslib;
84         if ($type eq "opac") {
85                 $tagslib = &MARCgettagslib($dbh,1);
86         } else {
87                 $tagslib = &MARCgettagslib($dbh,1);
88         }
89         my @marcarray;
90         push @marcarray,"";
91         for (my $tabloop = 0; $tabloop<=9;$tabloop++) {
92         push @marcarray,"--------------------------------------- $tabloop ---------------------------------------";
93                 foreach my $tag (sort(keys (%{$tagslib}))) {
94                         foreach my $subfield (sort(keys %{$tagslib->{$tag}})) {
95                                 next if ($subfield eq 'lib'); # skip lib and tabs, which are koha internal
96                                 next if ($subfield eq 'tab');
97                                 next unless ($tagslib->{$tag}->{$subfield}->{tab} eq $tabloop);
98                                 push @marcarray, "$tag$subfield - $tagslib->{$tag}->{$subfield}->{lib}";
99                         }
100                 }
101         }
102         my $marclist = CGI::scrolling_list(-name=>"marclist",
103                                         -values=> \@marcarray,
104                                         -size=>1,
105                                         -multiple=>0,
106                                         -onChange => "sql_update()",
107                                         );
108         $template->param("marclist" => $marclist);
109 }
110 # Print the page
111 print $query->header(
112     -type => guesstype($template->output),
113     -cookie => $cookie
114 ), $template->output;