Bug 7688: Change subscription numbering pattern and frequencies
[koha.git] / serials / subscription-numberpatterns.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-numberpatterns.pl
22
23 =head1 DESCRIPTION
24
25 Manage numbering patterns
26
27 =cut
28
29 use Modern::Perl;
30 use CGI;
31
32 use C4::Auth;
33 use C4::Output;
34 use C4::Serials::Numberpattern;
35 use C4::Serials::Frequency;
36
37 my $input = new CGI;
38 my ($template, $loggedinuser, $cookie, $flags) = get_template_and_user( {
39     template_name   => 'serials/subscription-numberpatterns.tt',
40     query           => $input,
41     type            => 'intranet',
42     authnotrequired => 0,
43     flagsrequired   => { 'parameters' => 1 }
44 } );
45
46 my $op = $input->param('op');
47
48 if($op && $op eq 'savenew') {
49     my $label = $input->param('label');
50     my $numberpattern;
51     foreach(qw/ label description numberingmethod displayorder
52       label1 label2 label3 add1 add2 add3 every1 every2 every3
53       setto1 setto2 setto3 whenmorethan1 whenmorethan2 whenmorethan3
54       numbering1 numbering2 numbering3 /) {
55         $numberpattern->{$_} = $input->param($_);
56         if($numberpattern->{$_} and $numberpattern->{$_} eq '') {
57             $numberpattern->{$_} = undef;
58         }
59     }
60     my $numberpattern2 = GetSubscriptionNumberpatternByName($label);
61
62     if(!defined $numberpattern2) {
63         AddSubscriptionNumberpattern($numberpattern);
64     } else {
65         $op = 'new';
66         $template->param(error_existing_numberpattern => 1);
67         $template->param(%$numberpattern);
68     }
69 } elsif ($op && $op eq 'savemod') {
70     my $id = $input->param('id');
71     my $label = $input->param('label');
72     my $numberpattern = GetSubscriptionNumberpattern($id);
73     my $mod_ok = 1;
74     if($numberpattern->{'label'} ne $label) {
75         my $numberpattern2 = GetSubscriptionNumberpatternByName($label);
76         if(defined $numberpattern2 && $id != $numberpattern2->{'id'}) {
77             $mod_ok = 0;
78         }
79     }
80     if($mod_ok) {
81         foreach(qw/ id label description numberingmethod displayorder
82           label1 label2 label3 add1 add2 add3 every1 every2 every3
83           setto1 setto2 setto3 whenmorethan1 whenmorethan2 whenmorethan3
84           numbering1 numbering2 numbering3 /) {
85             $numberpattern->{$_} = $input->param($_) || undef;
86         }
87         ModSubscriptionNumberpattern($numberpattern);
88     } else {
89         $op = 'modify';
90         $template->param(error_existing_numberpattern => 1);
91     }
92 }
93
94 if($op && ($op eq 'new' || $op eq 'modify')) {
95     if($op eq 'modify') {
96         my $id = $input->param('id');
97         if(defined $id) {
98             my $numberpattern = GetSubscriptionNumberpattern($id);
99             $template->param(%$numberpattern);
100         } else {
101             $op = 'new';
102         }
103     }
104     my @frequencies = GetSubscriptionFrequencies();
105     my @subtypes;
106     push @subtypes, { value => $_ } for (qw/ issues weeks months /);
107     my @locales = map {
108         chomp;
109         /^C|^POSIX$/ ? () : $_
110     } `locale -a`;
111
112     $template->param(
113         $op => 1,
114         frequencies_loop => \@frequencies,
115         subtypes_loop => \@subtypes,
116         locales => \@locales,
117         DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
118     );
119     output_html_with_http_headers $input, $cookie, $template->output;
120     exit;
121 }
122
123 if($op && $op eq 'del') {
124     my $id = $input->param('id');
125     if ($id) {
126         my $confirm = $input->param('confirm');
127         if ($confirm) {
128             DelSubscriptionNumberpattern($id);
129         } else {
130             my @subs = GetSubscriptionsWithNumberpattern($id);
131             if (@subs) {
132                 $template->param(
133                     id => $id,
134                     still_used => 1,
135                     subscriptions => \@subs
136                 );
137             } else {
138                 DelSubscriptionNumberpattern($id);
139             }
140         }
141     }
142 }
143
144 my @numberpatterns_loop = GetSubscriptionNumberpatterns();
145
146 $template->param(
147     numberpatterns_loop => \@numberpatterns_loop,
148 );
149
150 output_html_with_http_headers $input, $cookie, $template->output;