Clean up before final commits
[koha.git] / authorities / auth_finder.pl
1 #!/usr/bin/perl
2 # WARNING: 4-character tab stops here
3
4 # Copyright 2000-2002 Katipo Communications
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA  02111-1307 USA
20
21 use strict;
22
23 use CGI;
24 use C4::Auth;
25 use C4::Context;
26 use C4::Search;
27 use C4::Interface::CGI::Output;
28 use C4::AuthoritiesMarc;
29 use C4::Koha; # XXX subfield_is_koha_internal_p
30
31 my $query=new CGI;
32 my $op = $query->param('op');
33 my $authtypecode = $query->param('authtypecode');
34 my $index = $query->param('index');
35 my $resultstring = $query->param('result');
36 my $dbh = C4::Context->dbh;
37
38 my $startfrom=$query->param('startfrom');
39 $startfrom=0 if(!defined $startfrom);
40 my ($template, $loggedinuser, $cookie);
41 my $resultsperpage;
42
43 my $authtypes = getauthtypes;
44 my @authtypesloop;
45 foreach my $thisauthtype (keys %$authtypes) {
46         my $selected = 1 if $thisauthtype eq $authtypecode;
47         my %row =(value => $thisauthtype,
48                                 selected => $selected,
49                                 authtypetext => $authtypes->{$thisauthtype}{'authtypetext'},
50                           index => $index,
51                         );
52         push @authtypesloop, \%row;
53 }
54
55 if ($op eq "do_search") {
56         my @marclist = $query->param('marclist');
57         
58         my @operator = $query->param('operator');
59         my @value = $query->param('value');
60
61         $resultsperpage= $query->param('resultsperpage');
62         $resultsperpage = 10 ;
63
64         my ($results,$total) = authoritysearch($dbh, \@marclist, \@operator, \@value,$startfrom*$resultsperpage, $resultsperpage,$authtypecode);# $orderby);
65
66         ($template, $loggedinuser, $cookie)
67                 = get_template_and_user({template_name => "authorities/searchresultlist-auth.tmpl",
68                                 query => $query,
69                                 type => 'intranet',
70                                 authnotrequired => 0,
71                                 flagsrequired => {borrowers => 1},
72                                 flagsrequired => {catalogue => 1},
73                                 debug => 1,
74                                 });
75
76         # multi page display gestion
77         my $displaynext=0;
78         my $displayprev=$startfrom;
79         if(($total - (($startfrom+1)*($resultsperpage))) > 0 ) {
80                 $displaynext = 1;
81         }
82
83         my @field_data = ();
84
85
86         my @marclist_ini = $query->param('marclist'); # get marclist again, as the previous one has been modified by catalogsearch (mainentry replaced by field name
87         for(my $i = 0 ; $i <= $#marclist ; $i++) {
88                 push @field_data, { term => "marclist", val=>$marclist_ini[$i] };
89                 push @field_data, { term => "operator", val=>$operator[$i] };
90                 push @field_data, { term => "value", val=>$value[$i] };
91         }
92
93         my @numbers = ();
94
95         if ($total>$resultsperpage) {
96                 for (my $i=1; $i<$total/$resultsperpage+1; $i++) {
97                         if ($i<16) {
98                         my $highlight=0;
99                         ($startfrom==($i-1)) && ($highlight=1);
100                         push @numbers, { number => $i,
101                                         highlight => $highlight ,
102                                         searchdata=> \@field_data,
103                                         startfrom => ($i-1)};
104                         }
105         }
106         }
107
108         my $from = $startfrom*$resultsperpage+1;
109         my $to;
110
111         if($total < (($startfrom+1)*$resultsperpage)) {
112                 $to = $total;
113         } else {
114                 $to = (($startfrom+1)*$resultsperpage);
115         }
116         $template->param(result => $results) if $results;
117         $template->param(index => $query->param('index')."");
118         $template->param(startfrom=> $startfrom,
119                                                         displaynext=> $displaynext,
120                                                         displayprev=> $displayprev,
121                                                         resultsperpage => $resultsperpage,
122                                                         startfromnext => $startfrom+1,
123                                                         startfromprev => $startfrom-1,
124                                                           index => $index,
125                                                         searchdata=>\@field_data,
126                                                         total=>$total,
127                                                         from=>$from,
128                                                         to=>$to,
129                                                         numbers=>\@numbers,
130                                                         authtypecode =>$authtypecode,
131                                                         resultstring =>$value[0],
132                                                         );
133 } else {
134         ($template, $loggedinuser, $cookie)
135                 = get_template_and_user({template_name => "authorities/auth_finder.tmpl",
136                                 query => $query,
137                                 type => 'intranet',
138                                 authnotrequired => 0,
139                                 flagsrequired => {catalogue => 1},
140                                 debug => 1,
141                                 });
142
143         $template->param(index=>$query->param('index')."",
144                                         resultstring => $resultstring,
145                                         );
146 }
147
148 $template->param(authtypesloop => \@authtypesloop,
149                                 authtypecode => $authtypecode,
150                                 nonav=>"1",);
151
152 # Print the page
153 output_html_with_http_headers $query, $cookie, $template->output;
154
155 # Local Variables:
156 # tab-width: 4
157 # End: