5b6608a5cd219480470d5e83f537c339db535415
[srvgit] / 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;
34 use C4::Context;
35 use C4::Koha qw( GetAuthorisedValues );
36 use C4::Output;
37 use C4::Serials;
38 use Koha::AdditionalFields;
39
40 use Koha::DateUtils;
41 use Koha::SharedContent;
42
43 my $query         = CGI->new;
44 my $title         = $query->param('title_filter') || '';
45 my $ISSN          = $query->param('ISSN_filter') || '';
46 my $EAN           = $query->param('EAN_filter') || '';
47 my $callnumber    = $query->param('callnumber_filter') || '';
48 my $publisher     = $query->param('publisher_filter') || '';
49 my $bookseller    = $query->param('bookseller_filter') || '';
50 my $biblionumber  = $query->param('biblionumber') || '';
51 my $branch        = $query->param('branch_filter') || '';
52 my $location      = $query->param('location_filter') || '';
53 my $expiration_date = $query->param('expiration_date_filter') || '';
54 my $routing       = $query->param('routing') || C4::Context->preference("RoutingSerials");
55 my $searched      = $query->param('searched') || 0;
56 my $mana      = $query->param('mana') || 0;
57 my @subscriptionids = $query->multi_param('subscriptionid');
58 my $op            = $query->param('op');
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 } );
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 $expiration_date_dt = $expiration_date ? dt_from_string( $expiration_date ) : undef;
93 my @subscriptions;
94 my $mana_statuscode;
95 if ($searched){
96     if ($mana) {
97         my $result = Koha::SharedContent::search_entities("subscription",{
98             title        => $title,
99             issn         => $ISSN,
100             ean          => $EAN,
101             publisher    => $publisher
102         });
103         $mana_statuscode = $result->{code};
104         @subscriptions = @{ $result->{data} };
105     }
106     else {
107         @subscriptions = SearchSubscriptions(
108         {
109             biblionumber => $biblionumber,
110             title        => $title,
111             issn         => $ISSN,
112             ean          => $EAN,
113             callnumber   => $callnumber,
114             publisher    => $publisher,
115             bookseller   => $bookseller,
116             branch       => $branch,
117             additional_fields => \@additional_field_filters,
118             location     => $location,
119             expiration_date => $expiration_date_dt,
120         });
121     }
122 }
123
124 if ($mana) {
125     $template->param(
126         subscriptions => \@subscriptions,
127         statuscode    => $mana_statuscode,
128         total         => scalar @subscriptions,
129         title_filter  => $title,
130         ISSN_filter   => $ISSN,
131         EAN_filter    => $EAN,
132         callnumber_filter => $callnumber,
133         publisher_filter => $publisher,
134         bookseller_filter  => $bookseller,
135         branch_filter => $branch,
136         location_filter => $location,
137         expiration_date_filter => $expiration_date_dt,
138         done_searched => $searched,
139         routing       => $routing,
140         additional_field_filters => \@additional_field_filters,
141         additional_fields_for_subscription => \@additional_fields,
142         marcflavour   => (uc(C4::Context->preference("marcflavour"))),
143         mana => $mana,
144         search_only => 1
145     );
146 }
147 else
148 {
149     # to toggle between create or edit routing list options
150     if ($routing) {
151         for my $subscription ( @subscriptions) {
152             $subscription->{routingedit} = check_routing( $subscription->{subscriptionid} );
153         }
154     }
155
156     my (@openedsubscriptions, @closedsubscriptions);
157     for my $sub ( @subscriptions ) {
158         unless ( $sub->{closed} ) {
159             push @openedsubscriptions, $sub
160                 unless $sub->{cannotdisplay};
161         } else {
162             push @closedsubscriptions, $sub
163                 unless $sub->{cannotdisplay};
164         }
165     }
166
167     my @branches = Koha::Libraries->search( {}, { order_by => ['branchcode'] } );
168     my @branches_loop;
169     foreach my $b ( @branches ) {
170         my $selected = 0;
171         $selected = 1 if( defined $branch and $branch eq $b->branchcode );
172         push @branches_loop, {
173             branchcode  => $b->branchcode,
174             branchname  => $b->branchname,
175             selected    => $selected,
176         };
177     }
178
179     $template->param(
180         openedsubscriptions => \@openedsubscriptions,
181         closedsubscriptions => \@closedsubscriptions,
182         total         => @openedsubscriptions + @closedsubscriptions,
183         title_filter  => $title,
184         ISSN_filter   => $ISSN,
185         EAN_filter    => $EAN,
186         callnumber_filter => $callnumber,
187         publisher_filter => $publisher,
188         bookseller_filter  => $bookseller,
189         branch_filter => $branch,
190         location_filter => $location,
191         expiration_date_filter => $expiration_date_dt,
192         branches_loop => \@branches_loop,
193         done_searched => $searched,
194         routing       => $routing,
195         additional_field_filters => { map { $_->{id} => $_->{value} } @additional_field_filters },
196         additional_fields_for_subscription => \@additional_fields,
197         marcflavour   => (uc(C4::Context->preference("marcflavour"))),
198         mana => $mana
199     );
200 }
201 output_html_with_http_headers $query, $cookie, $template->output;