Bug 30230: (QA follow-up) Also add list_borrowers to the singular endpoint
[koha.git] / serials / serials-search.pl
1 #!/usr/bin/perl
2
3 # Copyright 2012 Koha Team
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
21 =head1 NAME
22
23 serials-search.pl
24
25 =head1 DESCRIPTION
26
27 this script is the search page for serials
28
29 =cut
30
31 use Modern::Perl;
32 use CGI qw ( -utf8 );
33 use C4::Auth qw( get_template_and_user );
34 use C4::Context;
35 use C4::Output qw( output_html_with_http_headers );
36 use C4::Serials qw( CloseSubscription ReopenSubscription SearchSubscriptions check_routing );
37 use Koha::AdditionalFields;
38
39 use Koha::DateUtils qw( dt_from_string );
40 use Koha::SharedContent;
41
42 my $query         = CGI->new;
43 my $title         = $query->param('title_filter') || '';
44 my $ISSN          = $query->param('ISSN_filter') || '';
45 my $EAN           = $query->param('EAN_filter') || '';
46 my $callnumber    = $query->param('callnumber_filter') || '';
47 my $publisher     = $query->param('publisher_filter') || '';
48 my $bookseller    = $query->param('bookseller_filter') || '';
49 my $biblionumber  = $query->param('biblionumber') || '';
50 my $branch        = $query->param('branch_filter') || '';
51 my $location      = $query->param('location_filter') || '';
52 my $expiration_date = $query->param('expiration_date_filter') || '';
53 my $routing       = $query->param('routing') || C4::Context->preference("RoutingSerials");
54 my $searched      = $query->param('searched') || 0;
55 my $mana      = $query->param('mana') || 0;
56 my @subscriptionids = $query->multi_param('subscriptionid');
57 my $op            = $query->param('op');
58 my $orig_total;
59
60 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
61     {
62         template_name   => "serials/serials-search.tt",
63         query           => $query,
64         type            => "intranet",
65         flagsrequired   => { serials => '*' },
66     }
67 );
68
69 if ( $op and $op eq "close" ) {
70     for my $subscriptionid ( @subscriptionids ) {
71         C4::Serials::CloseSubscription( $subscriptionid );
72     }
73 } elsif ( $op and $op eq "reopen" ) {
74     for my $subscriptionid ( @subscriptionids ) {
75         C4::Serials::ReopenSubscription( $subscriptionid );
76     }
77 }
78
79
80 my @additional_fields = Koha::AdditionalFields->search( { tablename => 'subscription', searchable => 1 } )->as_list;
81 my @additional_field_filters;
82 for my $field ( @additional_fields ) {
83     my $value = $query->param( 'additional_field_' . $field->id );
84     if ( defined $value and $value ne '' ) {
85         push @additional_field_filters, {
86             id => $field->id,
87             value => $value,
88         };
89     }
90 }
91
92 my @subscriptions;
93 my $mana_statuscode;
94 if ($searched) {
95     if ($mana) {
96         my $result = Koha::SharedContent::search_entities("subscription",{
97             title        => $title,
98             issn         => $ISSN,
99             ean          => $EAN,
100             publisher    => $publisher
101         });
102         $mana_statuscode = $result->{code};
103         @subscriptions = @{ $result->{data} };
104     }
105     else {
106         my $subscriptions = SearchSubscriptions(
107             {
108                 biblionumber => $biblionumber,
109                 title        => $title,
110                 issn         => $ISSN,
111                 ean          => $EAN,
112                 callnumber   => $callnumber,
113                 publisher    => $publisher,
114                 bookseller   => $bookseller,
115                 branch       => $branch,
116                 additional_fields => \@additional_field_filters,
117                 location     => $location,
118                 expiration_date => $expiration_date,
119             },
120             { results_limit => C4::Context->preference('SerialsSearchResultsLimit') }
121         );
122         @subscriptions = @{$subscriptions->{results}};
123         $orig_total = $subscriptions->{total};
124
125     }
126 }
127
128 if ($mana) {
129     $template->param(
130         subscriptions => \@subscriptions,
131         statuscode    => $mana_statuscode,
132         total         => scalar @subscriptions,
133         title_filter  => $title,
134         ISSN_filter   => $ISSN,
135         EAN_filter    => $EAN,
136         callnumber_filter => $callnumber,
137         publisher_filter => $publisher,
138         bookseller_filter  => $bookseller,
139         branch_filter => $branch,
140         location_filter => $location,
141         expiration_date_filter => $expiration_date,
142         done_searched => $searched,
143         routing       => $routing,
144         additional_field_filters => \@additional_field_filters,
145         additional_fields_for_subscription => \@additional_fields,
146         marcflavour   => (uc(C4::Context->preference("marcflavour"))),
147         mana => $mana,
148         search_only => 1
149     );
150 }
151 else {
152     # to toggle between create or edit routing list options
153     if ($routing) {
154         for my $subscription ( @subscriptions) {
155             $subscription->{routingedit} = check_routing( $subscription->{subscriptionid} );
156         }
157     }
158
159     my (@openedsubscriptions, @closedsubscriptions);
160     for my $sub ( @subscriptions ) {
161         unless ( $sub->{closed} ) {
162             push @openedsubscriptions, $sub
163                 unless $sub->{cannotdisplay};
164         } else {
165             push @closedsubscriptions, $sub
166                 unless $sub->{cannotdisplay};
167         }
168     }
169
170     my @branches = Koha::Libraries->search( {}, { order_by => ['branchcode'] } )->as_list;
171     my @branches_loop;
172     foreach my $b ( @branches ) {
173         my $selected = 0;
174         $selected = 1 if( defined $branch and $branch eq $b->branchcode );
175         push @branches_loop, {
176             branchcode  => $b->branchcode,
177             branchname  => $b->branchname,
178             selected    => $selected,
179         };
180     }
181
182     $template->param(
183         openedsubscriptions => \@openedsubscriptions,
184         closedsubscriptions => \@closedsubscriptions,
185         total         => @openedsubscriptions + @closedsubscriptions,
186         orig_total    => $orig_total,
187         title_filter  => $title,
188         ISSN_filter   => $ISSN,
189         EAN_filter    => $EAN,
190         callnumber_filter => $callnumber,
191         publisher_filter => $publisher,
192         bookseller_filter  => $bookseller,
193         branch_filter => $branch,
194         location_filter => $location,
195         expiration_date_filter => $expiration_date,
196         branches_loop => \@branches_loop,
197         done_searched => $searched,
198         routing       => $routing,
199         additional_field_filters => { map { $_->{id} => $_->{value} } @additional_field_filters },
200         additional_fields_for_subscription => \@additional_fields,
201         marcflavour   => (uc(C4::Context->preference("marcflavour"))),
202         mana => $mana
203     );
204 }
205 output_html_with_http_headers $query, $cookie, $template->output;