Bug 16522: (follow-up) MARC display templates and get_marc_host fixes
[koha.git] / serials / subscription-frequencies.pl
1 #!/usr/bin/perl
2
3 # Copyright 2011-2013 Biblibre SARL
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 =head1 NAME
21
22 subscription-frequencies.pl
23
24 =head1 DESCRIPTION
25
26 Manage subscription frequencies
27
28 =cut
29
30 use Modern::Perl;
31
32 use CGI qw ( -utf8 );
33
34 use C4::Auth qw( get_template_and_user );
35 use C4::Output qw( output_html_with_http_headers );
36 use C4::Serials qw( GetSubscription ModSubscription DelSubscription );
37 use C4::Serials::Frequency;
38
39 my $input = CGI->new;
40 my ($template, $loggedinuser, $cookie, $flags) = get_template_and_user( {
41     template_name   => 'serials/subscription-frequencies.tt',
42     query           => $input,
43     type            => 'intranet',
44     flagsrequired   => { 'serials' => 1 },
45 } );
46
47 my $op = $input->param('op');
48
49 if($op && ($op eq 'new' || $op eq 'modify')) {
50     my @units_loop;
51     push @units_loop, {val => $_} for (qw/ day week month year /);
52
53     if($op eq 'modify') {
54         my $frequencyid = $input->param('frequencyid');
55         my $frequency = GetSubscriptionFrequency($frequencyid);
56         foreach (@units_loop) {
57             if($frequency->{unit} and $_->{val} eq $frequency->{unit}) {
58                 $_->{selected} = 1;
59                 last;
60             }
61         }
62         $template->param( %$frequency );
63     }
64
65     $template->param(
66         units_loop => \@units_loop,
67         $op        => 1,
68     );
69     output_html_with_http_headers $input, $cookie, $template->output;
70     exit;
71 }
72
73 if($op && ($op eq 'savenew' || $op eq 'savemod')) {
74     my $frequency;
75     foreach (qw/ description unit issuesperunit unitsperissue displayorder /) {
76         $frequency->{$_} = $input->param($_);
77     }
78     $frequency->{unit} = undef if $frequency->{unit} eq '';
79     foreach (qw/issuesperunit unitsperissue/) {
80         $frequency->{$_} = 1 if $frequency->{$_} !~ /\d+/;
81     }
82     $frequency->{issuesperunit} = 1 if $frequency->{issuesperunit} < 1;
83     $frequency->{unitsperissue} = 1 if $frequency->{issuesperunit} != 1;
84
85     if($op eq 'savemod') {
86         $frequency->{id} = $input->param('id');
87         ModSubscriptionFrequency($frequency);
88     } else {
89         AddSubscriptionFrequency($frequency);
90     }
91 } elsif($op && $op eq 'del') {
92     my $frequencyid = $input->param('frequencyid');
93
94     if ($frequencyid) {
95         my $confirm = $input->param('confirm');
96         if ($confirm) {
97             DelSubscriptionFrequency($frequencyid);
98         } else {
99             my @subs = GetSubscriptionsWithFrequency($frequencyid);
100             if (@subs) {
101                 $template->param(
102                     frequencyid => $frequencyid,
103                     still_used => 1,
104                     subscriptions => \@subs
105                 );
106             } else {
107                 DelSubscriptionFrequency($frequencyid);
108             }
109         }
110     }
111 }
112
113
114 my @frequencies = GetSubscriptionFrequencies();
115
116 $template->param(frequencies_loop => \@frequencies);
117 $template->param($op => 1) if $op;
118
119 output_html_with_http_headers $input, $cookie, $template->output;