Bug 11441: (follow-up) improve utility help text
[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
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
21 use strict;
22 use warnings;
23
24 use CGI;
25 use C4::Output;
26 use C4::Auth;
27 use C4::Context;
28 use C4::AuthoritiesMarc;
29 use C4::Acquisition;
30 use C4::Koha;
31
32 my $query        = new CGI;
33 my $op           = $query->param('op') || '';
34 my $authtypecode = $query->param('authtypecode') || '';
35 my $index        = $query->param('index') || '';
36 my $tagid        = $query->param('tagid') || '';
37 my $source       = $query->param('source') || '';
38 my $relationship = $query->param('relationship') || '';
39
40 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
41     {
42         template_name => ( $op eq 'do_search' )
43         ? 'authorities/searchresultlist-auth.tt'
44         : 'authorities/auth_finder.tt',
45         query           => $query,
46         type            => 'intranet',
47         authnotrequired => 0,
48         flagsrequired   => { catalogue => 1 },
49     }
50 );
51
52 # Authority types loop
53 my $authtypes = C4::Koha::getauthtypes();
54 my @authtypesloop;
55 foreach my $thisauthtype ( keys %$authtypes ) {
56     my %row = (
57         value        => $thisauthtype,
58         selected     => ( $thisauthtype eq $authtypecode ),
59         authtypetext => $authtypes->{$thisauthtype}{'authtypetext'},
60         index        => $index,
61     );
62     push @authtypesloop, \%row;
63 }
64
65 # If search form posted
66 if ( $op eq "do_search" ) {
67     my @marclist  = $query->param('marclist');
68     my @and_or    = $query->param('and_or');
69     my @excluding = $query->param('excluding');
70     my @operator  = $query->param('operator');
71     my @value     = (
72         $query->param('value_mainstr') || undef,
73         $query->param('value_main')    || undef,
74         $query->param('value_any')     || undef,
75         $query->param('value_match')   || undef
76     );
77     my $orderby        = $query->param('orderby')        || '';
78     my $startfrom      = $query->param('startfrom')      || 0;
79     my $resultsperpage = $query->param('resultsperpage') || 20;
80
81     my ( $results, $total ) =
82       SearchAuthorities( \@marclist, \@and_or, \@excluding, \@operator, \@value,
83         $startfrom * $resultsperpage,
84         $resultsperpage, $authtypecode, $orderby );
85
86     # If an authority heading is repeated, add an arrayref to those repetions
87     # First heading -- Second heading
88     for my $heading (@$results) {
89         my @repets = split / -- /, $heading->{summary};
90         if ( @repets > 1 ) {
91             my @repets_loop;
92             for ( my $i = 0 ; $i < @repets ; $i++ ) {
93                 push @repets_loop,
94                   { index => $index, repet => $i + 1, value => $repets[$i] };
95             }
96             $heading->{repets} = \@repets_loop;
97         }
98     }
99
100     # multi page display gestion
101     my $displaynext = 0;
102     my $displayprev = $startfrom;
103     if ( ( $total - ( ( $startfrom + 1 ) * ($resultsperpage) ) ) > 0 ) {
104         $displaynext = 1;
105     }
106
107     my @field_data = ();
108
109 # get marclist again, as the previous one has been modified by catalogsearch (mainentry replaced by field name)
110     my @marclist_ini = $query->param('marclist');
111     for ( my $i = 0 ; $i <= $#marclist ; $i++ ) {
112         push @field_data, { term => "marclist",  val => $marclist_ini[$i] };
113         push @field_data, { term => "and_or",    val => $and_or[$i] };
114         push @field_data, { term => "excluding", val => $excluding[$i] };
115         push @field_data, { term => "operator",  val => $operator[$i] };
116     }
117
118     push @field_data,
119       { term => "value_mainstr", val => $query->param('value_mainstr') || "" };
120     push @field_data,
121       { term => "value_main", val => $query->param('value_main') || "" };
122     push @field_data,
123       { term => "value_any", val => $query->param('value_any') || "" };
124     push @field_data,
125       { term => "value_match", val => $query->param('value_match') || "" };
126
127     my @numbers = ();
128     if ( $total > $resultsperpage ) {
129         for ( my $i = 1 ; $i < $total / $resultsperpage + 1 ; $i++ ) {
130             if ( $i < 16 ) {
131                 my $highlight = 0;
132                 ( $startfrom == ( $i - 1 ) ) && ( $highlight = 1 );
133                 push @numbers,
134                   {
135                     number     => $i,
136                     highlight  => $highlight,
137                     searchdata => \@field_data,
138                     startfrom  => ( $i - 1 )
139                   };
140             }
141         }
142     }
143
144     my $from = $startfrom * $resultsperpage + 1;
145     my $to;
146     if ( $total < ( ( $startfrom + 1 ) * $resultsperpage ) ) {
147         $to = $total;
148     }
149     else {
150         $to = ( ( $startfrom + 1 ) * $resultsperpage );
151     }
152
153     $template->param( result => $results ) if $results;
154     $template->param(
155         orderby          => $orderby,
156         startfrom        => $startfrom,
157         displaynext      => $displaynext,
158         displayprev      => $displayprev,
159         resultsperpage   => $resultsperpage,
160         startfromnext    => $startfrom + 1,
161         startfromprev    => $startfrom - 1,
162         searchdata       => \@field_data,
163         total            => $total,
164         from             => $from,
165         to               => $to,
166         numbers          => \@numbers,
167         operator_mainstr => ( @operator > 0 && $operator[0] )
168         ? $operator[0]
169         : '',
170         operator_main  => ( @operator > 1 && $operator[1] ) ? $operator[1] : '',
171         operator_any   => ( @operator > 2 && $operator[2] ) ? $operator[2] : '',
172         operator_match => ( @operator > 3 && $operator[3] ) ? $operator[3] : '',
173     );
174 }
175 else {
176
177     # special case for UNIMARC field 210c builder
178     my $resultstring = $query->param('result') || '';
179     $template->param( resultstring => $resultstring, );
180 }
181
182 $template->param(
183     op            => $op,
184     value_mainstr => $query->param('value_mainstr') || '',
185     value_main    => $query->param('value_main') || '',
186     value_any     => $query->param('value_any') || '',
187     value_match   => $query->param('value_match') || '',
188     tagid         => $tagid,
189     index         => $index,
190     authtypesloop => \@authtypesloop,
191     authtypecode  => $authtypecode,
192     source        => $source,
193     relationship  => $relationship,
194 );
195
196 # Print the page
197 output_html_with_http_headers $query, $cookie, $template->output;
198
199 # Local Variables:
200 # tab-width: 4
201 # End: