Bug 19528: Fix a few typos like corrosponding
[koha.git] / serials / add_fields.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Copyright 2013 BibLibre
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 3 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, see <http://www.gnu.org/licenses>.
18
19 use Modern::Perl;
20 use CGI;
21 use C4::Auth;
22 use C4::Koha;
23 use C4::Output;
24 use Koha::AdditionalField;
25
26 my $input = new CGI;
27
28 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
29     {
30         template_name   => "serials/add_fields.tt",
31         query           => $input,
32         type            => "intranet",
33         authnotrequired => 0,
34         flagsrequired   => { serials => '*' },
35         debug           => 1,
36     }
37 );
38
39 my $op = $input->param('op') // 'list';
40 my $field_id = $input->param('field_id');
41 my @messages;
42
43 if ( $op eq 'add' ) {
44     my $name = $input->param('name') // q{};
45     my $authorised_value_category = $input->param('authorised_value_category') // q{};
46     my $marcfield = $input->param('marcfield') // q{};
47     my $searchable = $input->param('searchable') ? 1 : 0;
48     if ( $field_id and $name ) {
49         my $updated = 0;
50         eval {
51             my $af = Koha::AdditionalField->new({
52                 id => $field_id,
53                 name => $name,
54                 authorised_value_category => $authorised_value_category,
55                 marcfield => $marcfield,
56                 searchable => $searchable,
57             });
58             $updated = $af->update;
59         };
60         push @messages, {
61             code => 'update',
62             number => $updated,
63         };
64     } elsif ( $name ) {
65         my $inserted = 0;
66         eval {
67             my $af = Koha::AdditionalField->new({
68                 tablename => 'subscription',
69                 name => $name,
70                 authorised_value_category => $authorised_value_category,
71                 marcfield => $marcfield,
72                 searchable => $searchable,
73             });
74             $inserted = $af->insert;
75         };
76         push @messages, {
77             code => 'insert',
78             number => $inserted,
79         };
80     } else {
81         push @messages, {
82             code => 'insert',
83             number => 0,
84         };
85     }
86     $op = 'list';
87 }
88
89 if ( $op eq 'delete' ) {
90     my $deleted = 0;
91     eval {
92         my $af = Koha::AdditionalField->new( { id => $field_id } );
93         $deleted = $af->delete;
94         $deleted = 0 if $deleted eq '0E0';
95     };
96     push @messages, {
97         code => 'delete',
98         number => $deleted,
99     };
100     $op = 'list';
101 }
102
103 if ( $op eq 'add_form' ) {
104     my $field;
105     if ( $field_id ) {
106         $field = Koha::AdditionalField->new( { id => $field_id } )->fetch;
107     }
108
109     $template->param(
110         field => $field,
111     );
112 }
113
114 if ( $op eq 'list' ) {
115     my $fields = Koha::AdditionalField->all( { tablename => 'subscription' } );
116     $template->param( fields => $fields );
117 }
118
119 $template->param(
120     op => $op,
121     messages => \@messages,
122 );
123
124 output_html_with_http_headers $input, $cookie, $template->output;