Bug 12478: starting authority search in 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
38 my $query = new CGI;
39 my $dbh   = C4::Context->dbh;
40 my $op           = $query->param('op')           || '';
41 my $authtypecode = $query->param('authtypecode') || '';
42 my $authid       = $query->param('authid')       || '';
43
44 my ( $template, $loggedinuser, $cookie );
45
46 my $authority_types = Koha::Authority::Types->search( {}, { order_by => ['authtypetext'] } );
47
48 if ( $op eq "delete" ) {
49     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
50         {
51             template_name   => "authorities/authorities-home.tt",
52             query           => $query,
53             type            => 'intranet',
54             authnotrequired => 0,
55             flagsrequired   => { catalogue => 1 },
56             debug           => 1,
57         }
58     );
59     &DelAuthority( $authid, 1 );
60
61     if ( $query->param('operator') ) {
62         # query contains search params so perform search
63         $op = "do_search";
64     }
65     else {
66         $op = '';
67     }
68 }
69 if ( $op eq "do_search" ) {
70     my $marclist  = $query->param('marclist')  || '';
71     my $and_or    = $query->param('and_or')    || '';
72     my $excluding = $query->param('excluding') || '';
73     my $operator  = $query->param('operator')  || '';
74     my $orderby   = $query->param('orderby')   || '';
75     my $value     = $query->param('value')     || '';
76
77     my $startfrom      = $query->param('startfrom')      || 1;
78     my $resultsperpage = $query->param('resultsperpage') || 20;
79
80     my $builder  = Koha::SearchEngine::QueryBuilder->new();
81     my $searcher = Koha::SearchEngine::Search->new({index => 'authorities'});
82     my $search_query = $builder->build_authorities_query_compat(
83         [$marclist], [$and_or], [$excluding], [$operator],
84         [$value], $authtypecode, $orderby
85     );
86     $startfrom = $startfrom // 0;
87     my ( $results, $total ) =
88       $searcher->search_auth_compat( $search_query, $startfrom + 1,
89         $resultsperpage );
90
91     #my ( $results, $total ) = SearchAuthorities(
92     #    [$marclist],  [$and_or],
93     #    [$excluding], [$operator],
94     #    [$value], ( $startfrom - 1 ) * $resultsperpage,
95     #    $resultsperpage, $authtypecode,
96     #    $orderby
97     #);
98
99
100     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
101         {
102             template_name   => "authorities/searchresultlist.tt",
103             query           => $query,
104             type            => 'intranet',
105             authnotrequired => 0,
106             flagsrequired   => { catalogue => 1 },
107             debug           => 1,
108         }
109     );
110
111     # search history
112     if (C4::Context->preference('EnableSearchHistory')) {
113         if ( $startfrom == 1) {
114             my $path_info = $query->url(-path_info=>1);
115             my $query_cgi_history = $query->url(-query=>1);
116             $query_cgi_history =~ s/^$path_info\?//;
117             $query_cgi_history =~ s/;/&/g;
118
119             C4::Search::History::add({
120                 userid => $loggedinuser,
121                 sessionid => $query->cookie("CGISESSID"),
122                 query_desc => $value,
123                 query_cgi => $query_cgi_history,
124                 total => $total,
125                 type => "authority",
126             });
127         }
128     }
129
130     $template->param(
131         marclist       => $marclist,
132         and_or         => $and_or,
133         excluding      => $excluding,
134         operator       => $operator,
135         orderby        => $orderby,
136         value          => $value,
137         authtypecode   => $authtypecode,
138         startfrom      => $startfrom,
139         resultsperpage => $resultsperpage,
140     );
141
142     # we must get parameters once again. Because if there is a mainentry, it
143     # has been replaced by something else during the search, thus the links
144     # next/previous would not work anymore
145
146     # construction of the url of each page
147     my $value_url = uri_escape_utf8($value);
148     my $base_url = "authorities-home.pl?"
149       ."marclist=$marclist"
150       ."&amp;and_or=$and_or"
151       ."&amp;excluding=$excluding"
152       ."&amp;operator=$operator"
153       ."&amp;value=$value_url"
154       ."&amp;resultsperpage=$resultsperpage"
155       ."&amp;type=intranet"
156       ."&amp;op=do_search"
157       ."&amp;authtypecode=$authtypecode"
158       ."&amp;orderby=$orderby";
159
160     my $from = ( $startfrom - 1 ) * $resultsperpage + 1;
161     my $to;
162     if ( !defined $total ) {
163         $total = 0;
164     }
165
166     if ( $total < $startfrom * $resultsperpage ) {
167         $to = $total;
168     }
169     else {
170         $to = $startfrom * $resultsperpage;
171     }
172
173     $template->param( result => $results ) if $results;
174
175     $template->param(
176         pagination_bar => pagination_bar(
177             $base_url,  int( $total / $resultsperpage ) + 1,
178             $startfrom, 'startfrom'
179         ),
180         total     => $total,
181         from      => $from,
182         to        => $to,
183         isEDITORS => $authtypecode eq 'EDITORS',
184     );
185
186 }
187 if ( $op eq '' ) {
188     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
189         {
190             template_name   => "authorities/authorities-home.tt",
191             query           => $query,
192             type            => 'intranet',
193             authnotrequired => 0,
194             flagsrequired   => { catalogue => 1 },
195             debug           => 1,
196         }
197     );
198
199 }
200
201 $template->param(
202     authority_types => $authority_types,
203     op            => $op,
204 );
205
206 $template->{VARS}->{marcflavour} = C4::Context->preference("marcflavour");
207
208 # Print the page
209 output_html_with_http_headers $query, $cookie, $template->output;