Bug 11226: subscription frequencies and numbering patterns should be editable with...
[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   => { 'serials' => 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
109     my $languages = [ map {
110         {
111             language => $_->{iso639_2_code},
112             description => $_->{language_description} || $_->{language}
113         }
114     } @{ C4::Languages::getAllLanguages() } ];
115
116     $template->param(
117         $op => 1,
118         frequencies_loop => \@frequencies,
119         subtypes_loop => \@subtypes,
120         locales => $languages,
121     );
122     output_html_with_http_headers $input, $cookie, $template->output;
123     exit;
124 }
125
126 if($op && $op eq 'del') {
127     my $id = $input->param('id');
128     if ($id) {
129         my $confirm = $input->param('confirm');
130         if ($confirm) {
131             DelSubscriptionNumberpattern($id);
132         } else {
133             my @subs = GetSubscriptionsWithNumberpattern($id);
134             if (@subs) {
135                 $template->param(
136                     id => $id,
137                     still_used => 1,
138                     subscriptions => \@subs
139                 );
140             } else {
141                 DelSubscriptionNumberpattern($id);
142             }
143         }
144     }
145 }
146
147 my @numberpatterns_loop = GetSubscriptionNumberpatterns();
148
149 $template->param(
150     numberpatterns_loop => \@numberpatterns_loop,
151 );
152
153 output_html_with_http_headers $input, $cookie, $template->output;