Bug 33568: Do not embed library names
[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 qw( get_template_and_user );
34 use C4::Output qw( output_html_with_http_headers );
35 use C4::Serials::Numberpattern qw(
36     AddSubscriptionNumberpattern
37     DelSubscriptionNumberpattern
38     GetSubscriptionNumberpattern
39     GetSubscriptionNumberpatternByName
40     GetSubscriptionNumberpatterns
41     GetSubscriptionsWithNumberpattern
42     ModSubscriptionNumberpattern
43 );
44 use C4::Serials::Frequency qw( GetSubscriptionFrequencies );
45
46 my @NUMBERPATTERN_FIELDS = qw/
47       label description numberingmethod displayorder
48       label1 label2 label3 add1 add2 add3 every1 every2 every3
49       setto1 setto2 setto3 whenmorethan1 whenmorethan2 whenmorethan3
50       numbering1 numbering2 numbering3 /;
51
52 my $input = CGI->new;
53 my ($template, $loggedinuser, $cookie, $flags) = get_template_and_user( {
54     template_name   => 'serials/subscription-numberpatterns.tt',
55     query           => $input,
56     type            => 'intranet',
57     flagsrequired   => { 'serials' => "*" }
58 } );
59
60 my $op = $input->param('op');
61
62 if($op && $op eq 'cud-savenew') {
63     my $label = $input->param('label');
64     my $numberpattern;
65     foreach(@NUMBERPATTERN_FIELDS) {
66         $numberpattern->{$_} = $input->param($_);
67         if(defined $numberpattern->{$_} and $numberpattern->{$_} eq '') {
68             $numberpattern->{$_} = undef;
69         }
70     }
71     my $numberpattern2 = GetSubscriptionNumberpatternByName($label);
72
73     if(!defined $numberpattern2) {
74         AddSubscriptionNumberpattern($numberpattern);
75     } else {
76         $op = 'new';
77         $template->param(error_existing_numberpattern => 1);
78         $template->param(%$numberpattern);
79     }
80 } elsif ($op && $op eq 'cud-savemod') {
81     my $id = $input->param('id');
82     my $label = $input->param('label');
83     my $numberpattern = GetSubscriptionNumberpattern($id);
84     my $mod_ok = 1;
85     if($numberpattern->{'label'} ne $label) {
86         my $numberpattern2 = GetSubscriptionNumberpatternByName($label);
87         if(defined $numberpattern2 && $id != $numberpattern2->{'id'}) {
88             $mod_ok = 0;
89         }
90     }
91     if($mod_ok) {
92         foreach(@NUMBERPATTERN_FIELDS) {
93             $numberpattern->{$_} = $input->param($_) || undef;
94             if(defined $numberpattern->{$_} and $numberpattern->{$_} eq '') {
95                 $numberpattern->{$_} = undef;
96             }
97         }
98         ModSubscriptionNumberpattern($numberpattern);
99     } else {
100         $op = 'cud-modify';
101         $template->param(error_existing_numberpattern => 1);
102     }
103 }
104
105 if($op && ($op eq 'new' || $op eq 'modify')) {
106     if($op eq 'modify') {
107         my $id = $input->param('id');
108         if(defined $id) {
109             my $numberpattern = GetSubscriptionNumberpattern($id);
110             $template->param(%$numberpattern);
111         } else {
112             $op = 'new';
113         }
114     }
115     my @frequencies = GetSubscriptionFrequencies();
116     my $languages = [ map {
117         {
118             language => $_->{iso639_2_code},
119             description => $_->{language_description} || $_->{language}
120         }
121     } @{ C4::Languages::getAllLanguages() } ];
122
123     $template->param(
124         $op => 1,
125         frequencies_loop => \@frequencies,
126         locales => $languages,
127     );
128     output_html_with_http_headers $input, $cookie, $template->output;
129     exit;
130 }
131
132 if($op && $op eq 'cud-del') {
133     my $id = $input->param('id');
134     if ($id) {
135         my $confirm = $input->param('confirm');
136         if ($confirm) {
137             DelSubscriptionNumberpattern($id);
138         } else {
139             my @subs = GetSubscriptionsWithNumberpattern($id);
140             if (@subs) {
141                 $template->param(
142                     id => $id,
143                     still_used => 1,
144                     subscriptions => \@subs
145                 );
146             } else {
147                 DelSubscriptionNumberpattern($id);
148             }
149         }
150     }
151 }
152
153 my @numberpatterns_loop = GetSubscriptionNumberpatterns();
154
155 $template->param(
156     numberpatterns_loop => \@numberpatterns_loop,
157 );
158
159 output_html_with_http_headers $input, $cookie, $template->output;