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