Bug 15774: (QA follow-up) Make sure that tablename is correctly saved
[koha.git] / admin / additional-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   => "admin/additional-fields.tt",
31         query           => $input,
32         type            => "intranet",
33         authnotrequired => 0,
34         flagsrequired   => { parameters => 1 },
35         debug           => 1,
36     }
37 );
38
39 my $tablename = $input->param('tablename');
40 my $op = $input->param('op') // ( $tablename ? 'list' : 'list_tables' );
41 my $field_id = $input->param('field_id');
42 my @messages;
43
44 if ( $op eq 'add' ) {
45     my $name = $input->param('name') // q{};
46     my $authorised_value_category = $input->param('authorised_value_category') // q{};
47     my $marcfield = $input->param('marcfield') // q{};
48     my $searchable = $input->param('searchable') ? 1 : 0;
49     if ( $field_id and $name ) {
50         my $updated = 0;
51         eval {
52             my $af = Koha::AdditionalField->new({
53                 id => $field_id,
54                 name => $name,
55                 authorised_value_category => $authorised_value_category,
56                 marcfield => $marcfield,
57                 searchable => $searchable,
58             });
59             $updated = $af->update;
60         };
61         push @messages, {
62             code => 'update',
63             number => $updated,
64         };
65     } elsif ( $name ) {
66         my $inserted = 0;
67         eval {
68             my $af = Koha::AdditionalField->new({
69                 tablename => $tablename,
70                 name => $name,
71                 authorised_value_category => $authorised_value_category,
72                 marcfield => $marcfield,
73                 searchable => $searchable,
74             });
75             $inserted = $af->insert;
76         };
77         push @messages, {
78             code => 'insert',
79             number => $inserted,
80         };
81     } else {
82         push @messages, {
83             code => 'insert',
84             number => 0,
85         };
86     }
87     $op = 'list';
88 }
89
90 if ( $op eq 'delete' ) {
91     my $deleted = 0;
92     eval {
93         my $af = Koha::AdditionalField->new( { id => $field_id } );
94         $deleted = $af->delete;
95         $deleted = 0 if $deleted eq '0E0';
96     };
97     push @messages, {
98         code => 'delete',
99         number => $deleted,
100     };
101     $op = 'list';
102 }
103
104 if ( $op eq 'add_form' ) {
105     my $field;
106     if ( $field_id ) {
107         $field = Koha::AdditionalField->new( { id => $field_id } )->fetch;
108     }
109
110     $tablename = $field->{tablename} if $field;
111
112     $template->param(
113         field => $field,
114     );
115 }
116
117 if ( $op eq 'list' ) {
118     my $fields = Koha::AdditionalField->all( { tablename => $tablename } );
119     $template->param( fields => $fields );
120 }
121
122 $template->param(
123     op => $op,
124     tablename => $tablename,
125     messages => \@messages,
126 );
127
128 output_html_with_http_headers $input, $cookie, $template->output;