Merge branch 'bug_9900' into 3.12-master
[koha.git] / serials / subscription-frequencies.pl
1 #!/usr/bin/perl
2
3 # Copyright 2011 BibLibre SARL
4 # This file is part of Koha.
5 #
6 # Koha is free software; you can redistribute it and/or modify it under the
7 # terms of the GNU General Public License as published by the Free Software
8 # Foundation; either version 2 of the License, or (at your option) any later
9 # version.
10 #
11 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
12 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with Koha; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
19 =head1 NAME
20
21 subscription-frequencies.pl
22
23 =head1 DESCRIPTION
24
25 Manage subscription frequencies
26
27 =cut
28
29 use Modern::Perl;
30
31 use CGI;
32
33 use C4::Auth;
34 use C4::Output;
35 use C4::Serials;
36 use C4::Serials::Frequency;
37
38 my $input = new CGI;
39 my ($template, $loggedinuser, $cookie, $flags) = get_template_and_user( {
40     template_name   => 'serials/subscription-frequencies.tt',
41     query           => $input,
42     type            => 'intranet',
43     authnotrequired => 0,
44     flagsrequired   => { 'parameters' => 1 },
45     debug           => 1,
46 } );
47
48 my $op = $input->param('op');
49
50 if($op && ($op eq 'new' || $op eq 'modify')) {
51     my @units_loop;
52     push @units_loop, {val => $_} for (qw/ day week month year /);
53
54     if($op eq 'modify') {
55         my $frequencyid = $input->param('frequencyid');
56         my $frequency = GetSubscriptionFrequency($frequencyid);
57         foreach (@units_loop) {
58             if($frequency->{unit} and $_->{val} eq $frequency->{unit}) {
59                 $_->{selected} = 1;
60                 last;
61             }
62         }
63         $template->param( %$frequency );
64     }
65
66     $template->param(
67         units_loop => \@units_loop,
68         $op        => 1,
69     );
70     output_html_with_http_headers $input, $cookie, $template->output;
71     exit;
72 }
73
74 if($op && ($op eq 'savenew' || $op eq 'savemod')) {
75     my $frequency;
76     foreach (qw/ description unit issuesperunit unitsperissue displayorder /) {
77         $frequency->{$_} = $input->param($_);
78     }
79     $frequency->{unit} = undef if $frequency->{unit} eq '';
80     foreach (qw/issuesperunit unitsperissue/) {
81         $frequency->{$_} = 1 if $frequency->{$_} !~ /\d+/;
82     }
83     $frequency->{issuesperunit} = 1 if $frequency->{issuesperunit} < 1;
84     $frequency->{unitsperissue} = 1 if $frequency->{issuesperunit} != 1;
85
86     if($op eq 'savemod') {
87         $frequency->{id} = $input->param('id');
88         ModSubscriptionFrequency($frequency);
89     } else {
90         AddSubscriptionFrequency($frequency);
91     }
92 } elsif($op && $op eq 'del') {
93     my $frequencyid = $input->param('frequencyid');
94
95     if ($frequencyid) {
96         my $confirm = $input->param('confirm');
97         if ($confirm) {
98             DelSubscriptionFrequency($frequencyid);
99         } else {
100             my @subs = GetSubscriptionsWithFrequency($frequencyid);
101             if (@subs) {
102                 $template->param(
103                     frequencyid => $frequencyid,
104                     still_used => 1,
105                     subscriptions => \@subs
106                 );
107             } else {
108                 DelSubscriptionFrequency($frequencyid);
109             }
110         }
111     }
112 }
113
114
115 my @frequencies = GetSubscriptionFrequencies();
116
117 $template->param(frequencies_loop => \@frequencies);
118 $template->param($op => 1) if $op;
119
120 output_html_with_http_headers $input, $cookie, $template->output;