Bug 12478: auth search works in the staff client
[koha.git] / authorities / authorities-home.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
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use strict;
21 use warnings;
22
23 use CGI qw ( -utf8 );
24 use URI::Escape;
25 use C4::Auth;
26
27 use C4::Context;
28 use C4::Auth;
29 use C4::Output;
30 use C4::AuthoritiesMarc;
31 use C4::Acquisition;
32 use C4::Koha;
33 use C4::Biblio;
34 use C4::Search::History;
35
36 use Koha::Authority::Types;
37 use Koha::SearchEngine::Search;
38 use Koha::SearchEngine::QueryBuilder;
39
40 my $query = new CGI;
41 my $dbh   = C4::Context->dbh;
42 my $op           = $query->param('op')           || '';
43 my $authtypecode = $query->param('authtypecode') || '';
44 my $authid       = $query->param('authid')       || '';
45
46 my ( $template, $loggedinuser, $cookie );
47
48 my $authority_types = Koha::Authority::Types->search( {}, { order_by => ['authtypetext'] } );
49
50 if ( $op eq "delete" ) {
51     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
52         {
53             template_name   => "authorities/authorities-home.tt",
54             query           => $query,
55             type            => 'intranet',
56             authnotrequired => 0,
57             flagsrequired   => { catalogue => 1 },
58             debug           => 1,
59         }
60     );
61     &DelAuthority( $authid, 1 );
62
63     if ( $query->param('operator') ) {
64         # query contains search params so perform search
65         $op = "do_search";
66     }
67     else {
68         $op = '';
69     }
70 }
71 if ( $op eq "do_search" ) {
72     my $marclist  = $query->param('marclist')  || '';
73     my $and_or    = $query->param('and_or')    || '';
74     my $excluding = $query->param('excluding') || '';
75     my $operator  = $query->param('operator')  || '';
76     my $orderby   = $query->param('orderby')   || '';
77     my $value     = $query->param('value')     || '';
78
79     my $startfrom      = $query->param('startfrom')      || 1;
80     my $resultsperpage = $query->param('resultsperpage') || 20;
81
82     my $builder  = Koha::SearchEngine::QueryBuilder->new();
83     my $searcher = Koha::SearchEngine::Search->new({index => 'authorities'});
84     my $search_query = $builder->build_authorities_query_compat(
85         [$marclist], [$and_or], [$excluding], [$operator],
86         [$value], $authtypecode, $orderby
87     );
88     $startfrom = $startfrom // 0;
89     my ( $results, $total ) =
90       $searcher->search_auth_compat( $search_query, $startfrom,
91         $resultsperpage );
92     #my ( $results, $total ) = SearchAuthorities(
93     #    [$marclist],  [$and_or],
94     #    [$excluding], [$operator],
95     #    [$value], ( $startfrom - 1 ) * $resultsperpage,
96     #    $resultsperpage, $authtypecode,
97     #    $orderby
98     #);
99
100
101     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
102         {
103             template_name   => "authorities/searchresultlist.tt",
104             query           => $query,
105             type            => 'intranet',
106             authnotrequired => 0,
107             flagsrequired   => { catalogue => 1 },
108             debug           => 1,
109         }
110     );
111
112     # search history
113     if (C4::Context->preference('EnableSearchHistory')) {
114         if ( $startfrom == 1) {
115             my $path_info = $query->url(-path_info=>1);
116             my $query_cgi_history = $query->url(-query=>1);
117             $query_cgi_history =~ s/^$path_info\?//;
118             $query_cgi_history =~ s/;/&/g;
119
120             C4::Search::History::add({
121                 userid => $loggedinuser,
122                 sessionid => $query->cookie("CGISESSID"),
123                 query_desc => $value,
124                 query_cgi => $query_cgi_history,
125                 total => $total,
126                 type => "authority",
127             });
128         }
129     }
130
131     $template->param(
132         marclist       => $marclist,
133         and_or         => $and_or,
134         excluding      => $excluding,
135         operator       => $operator,
136         orderby        => $orderby,
137         value          => $value,
138         authtypecode   => $authtypecode,
139         startfrom      => $startfrom,
140         resultsperpage => $resultsperpage,
141     );
142
143     # we must get parameters once again. Because if there is a mainentry, it
144     # has been replaced by something else during the search, thus the links
145     # next/previous would not work anymore
146
147     # construction of the url of each page
148     my $value_url = uri_escape_utf8($value);
149     my $base_url = "authorities-home.pl?"
150       ."marclist=$marclist"
151       ."&amp;and_or=$and_or"
152       ."&amp;excluding=$excluding"
153       ."&amp;operator=$operator"
154       ."&amp;value=$value_url"
155       ."&amp;resultsperpage=$resultsperpage"
156       ."&amp;type=intranet"
157       ."&amp;op=do_search"
158       ."&amp;authtypecode=$authtypecode"
159       ."&amp;orderby=$orderby";
160
161     my $from = ( $startfrom - 1 ) * $resultsperpage + 1;
162     my $to;
163     if ( !defined $total ) {
164         $total = 0;
165     }
166
167     if ( $total < $startfrom * $resultsperpage ) {
168         $to = $total;
169     }
170     else {
171         $to = $startfrom * $resultsperpage;
172     }
173
174     $template->param( result => $results ) if $results;
175
176     $template->param(
177         pagination_bar => pagination_bar(
178             $base_url,  int( $total / $resultsperpage ) + 1,
179             $startfrom, 'startfrom'
180         ),
181         total     => $total,
182         from      => $from,
183         to        => $to,
184         isEDITORS => $authtypecode eq 'EDITORS',
185     );
186
187 }
188 if ( $op eq '' ) {
189     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
190         {
191             template_name   => "authorities/authorities-home.tt",
192             query           => $query,
193             type            => 'intranet',
194             authnotrequired => 0,
195             flagsrequired   => { catalogue => 1 },
196             debug           => 1,
197         }
198     );
199
200 }
201
202 $template->param(
203     authority_types => $authority_types,
204     op            => $op,
205 );
206
207 $template->{VARS}->{marcflavour} = C4::Context->preference("marcflavour");
208
209 # Print the page
210 output_html_with_http_headers $query, $cookie, $template->output;