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