Bug 27846: members folder
[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 qw ( -utf8 );
32
33 use C4::Auth;
34 use C4::Output;
35 use C4::Serials::Numberpattern;
36 use C4::Serials::Frequency;
37
38 my $input = CGI->new;
39 my ($template, $loggedinuser, $cookie, $flags) = get_template_and_user( {
40     template_name   => 'serials/subscription-numberpatterns.tt',
41     query           => $input,
42     type            => 'intranet',
43     flagsrequired   => { 'serials' => 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 $languages = [ map {
106         {
107             language => $_->{iso639_2_code},
108             description => $_->{language_description} || $_->{language}
109         }
110     } @{ C4::Languages::getAllLanguages() } ];
111
112     $template->param(
113         $op => 1,
114         frequencies_loop => \@frequencies,
115         locales => $languages,
116     );
117     output_html_with_http_headers $input, $cookie, $template->output;
118     exit;
119 }
120
121 if($op && $op eq 'del') {
122     my $id = $input->param('id');
123     if ($id) {
124         my $confirm = $input->param('confirm');
125         if ($confirm) {
126             DelSubscriptionNumberpattern($id);
127         } else {
128             my @subs = GetSubscriptionsWithNumberpattern($id);
129             if (@subs) {
130                 $template->param(
131                     id => $id,
132                     still_used => 1,
133                     subscriptions => \@subs
134                 );
135             } else {
136                 DelSubscriptionNumberpattern($id);
137             }
138         }
139     }
140 }
141
142 my @numberpatterns_loop = GetSubscriptionNumberpatterns();
143
144 $template->param(
145     numberpatterns_loop => \@numberpatterns_loop,
146 );
147
148 output_html_with_http_headers $input, $cookie, $template->output;