Bug 16777: Correct intranet search alias
[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;
34 use C4::Branch;
35 use C4::Context;
36 use C4::Koha qw( GetAuthorisedValues );
37 use C4::Output;
38 use C4::Serials;
39 use Koha::AdditionalField;
40
41 use Koha::DateUtils;
42
43 my $query         = new CGI;
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 @subscriptionids = $query->multi_param('subscriptionid');
57 my $op            = $query->param('op');
58
59 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
60     {
61         template_name   => "serials/serials-search.tt",
62         query           => $query,
63         type            => "intranet",
64         authnotrequired => 0,
65         flagsrequired   => { serials => '*' },
66         debug           => 1,
67     }
68 );
69
70 if ( $op and $op eq "close" ) {
71     for my $subscriptionid ( @subscriptionids ) {
72         C4::Serials::CloseSubscription( $subscriptionid );
73     }
74 } elsif ( $op and $op eq "reopen" ) {
75     for my $subscriptionid ( @subscriptionids ) {
76         C4::Serials::ReopenSubscription( $subscriptionid );
77     }
78 }
79
80
81 my $additional_fields = Koha::AdditionalField->all( { tablename => 'subscription', searchable => 1 } );
82 my $additional_field_filters;
83 for my $field ( @$additional_fields ) {
84     my $filter_value = $query->param('additional_field_' . $field->{id} . '_filter');
85     if ( defined $filter_value and $filter_value ne q|| ) {
86         $additional_field_filters->{ $field->{name} } = {
87             value => $filter_value,
88             authorised_value_category => $field->{authorised_value_category},
89         };
90     }
91     if ( $field->{authorised_value_category} ) {
92         $field->{authorised_value_choices} = GetAuthorisedValues( $field->{authorised_value_category} );
93     }
94 }
95
96 my $expiration_date_dt = $expiration_date ? dt_from_string( $expiration_date ) : undef;
97 my @subscriptions;
98 if ($searched){
99     @subscriptions = SearchSubscriptions(
100         {
101             biblionumber => $biblionumber,
102             title        => $title,
103             issn         => $ISSN,
104             ean          => $EAN,
105             callnumber   => $callnumber,
106             publisher    => $publisher,
107             bookseller   => $bookseller,
108             branch       => $branch,
109             additional_fields => [ map{ { name => $_, value => $additional_field_filters->{$_}{value}, authorised_value_category => $additional_field_filters->{$_}{authorised_value_category} } } keys %$additional_field_filters ],
110             location     => $location,
111             expiration_date => $expiration_date_dt,
112         }
113     );
114 }
115
116 # to toggle between create or edit routing list options
117 if ($routing) {
118     for my $subscription ( @subscriptions) {
119         $subscription->{routingedit} = check_routing( $subscription->{subscriptionid} );
120     }
121 }
122
123 my (@openedsubscriptions, @closedsubscriptions);
124 for my $sub ( @subscriptions ) {
125     unless ( $sub->{closed} ) {
126         push @openedsubscriptions, $sub
127             unless $sub->{cannotdisplay};
128     } else {
129         push @closedsubscriptions, $sub
130             unless $sub->{cannotdisplay};
131     }
132 }
133
134 my $branches = GetBranches();
135 my @branches_loop;
136 foreach (sort keys %$branches){
137     my $selected = 0;
138     $selected = 1 if( defined $branch and $branch eq $_ );
139     push @branches_loop, {
140         branchcode  => $_,
141         branchname  => $branches->{$_}->{'branchname'},
142         selected    => $selected,
143     };
144 }
145
146
147 $template->param(
148     openedsubscriptions => \@openedsubscriptions,
149     closedsubscriptions => \@closedsubscriptions,
150     total         => @openedsubscriptions + @closedsubscriptions,
151     title_filter  => $title,
152     ISSN_filter   => $ISSN,
153     EAN_filter    => $EAN,
154     callnumber_filter => $callnumber,
155     publisher_filter => $publisher,
156     bookseller_filter  => $bookseller,
157     branch_filter => $branch,
158     location_filter => $location,
159     expiration_date_filter => $expiration_date_dt,
160     branches_loop => \@branches_loop,
161     done_searched => $searched,
162     routing       => $routing,
163     additional_field_filters => $additional_field_filters,
164     additional_fields_for_subscription => $additional_fields,
165     marcflavour   => (uc(C4::Context->preference("marcflavour")))
166 );
167
168 output_html_with_http_headers $query, $cookie, $template->output;