Bug 27948: (QA follow-up) Remove unused $disclaimer code
[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::AdditionalFields;
25
26 my $input = CGI->new;
27
28 my %flagsrequired;
29 $flagsrequired{parameters} = 'manage_additional_fields';
30
31 my $tablename = $input->param('tablename');
32 my $op = $input->param('op') // ( $tablename ? 'list' : 'list_tables' );
33
34 if( $op ne 'list_tables' ){
35     $flagsrequired{acquisition} = 'order_manage' if $tablename eq 'aqbasket';
36     $flagsrequired{serials} = 'edit_subscription' if $tablename eq 'subscription';
37 }
38
39 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
40     {
41         template_name   => "admin/additional-fields.tt",
42         query           => $input,
43         type            => "intranet",
44         flagsrequired   => \%flagsrequired,
45     }
46 );
47
48 my $field_id = $input->param('field_id');
49 my @messages;
50
51 if ( $op eq 'add' ) {
52     my $name = $input->param('name') // q{};
53     my $authorised_value_category = $input->param('authorised_value_category') // q{};
54     my $marcfield = $input->param('marcfield') // q{};
55     my $searchable = $input->param('searchable') ? 1 : 0;
56     if ( $field_id and $name ) {
57         my $updated = 0;
58         eval {
59             my $af = Koha::AdditionalFields->find($field_id);
60             $af->set({
61                 name => $name,
62                 authorised_value_category => $authorised_value_category,
63                 marcfield => $marcfield,
64                 searchable => $searchable,
65             });
66             $updated = $af->store ? 1 : 0;
67         };
68         push @messages, {
69             code => 'update',
70             number => $updated,
71         };
72     } elsif ( $name ) {
73         my $inserted = 0;
74         eval {
75             my $af = Koha::AdditionalField->new({
76                 tablename => $tablename,
77                 name => $name,
78                 authorised_value_category => $authorised_value_category,
79                 marcfield => $marcfield,
80                 searchable => $searchable,
81             });
82             $inserted = $af->store ? 1 : 0;
83         };
84         push @messages, {
85             code => 'insert',
86             number => $inserted,
87         };
88     } else {
89         push @messages, {
90             code => 'insert',
91             number => 0,
92         };
93     }
94     $op = 'list';
95 }
96
97 if ( $op eq 'delete' ) {
98     my $deleted = 0;
99     eval {
100         my $af = Koha::AdditionalFields->find($field_id);
101         $deleted = $af->delete;
102     };
103     push @messages, {
104         code => 'delete',
105         number => $deleted,
106     };
107     $op = 'list';
108 }
109
110 if ( $op eq 'add_form' ) {
111     my $field;
112     if ( $field_id ) {
113         $field = Koha::AdditionalFields->find($field_id);
114     }
115
116     $tablename = $field->tablename if $field;
117
118     $template->param(
119         field => $field,
120     );
121 }
122
123 if ( $op eq 'list' ) {
124     my $fields = Koha::AdditionalFields->search( { tablename => $tablename } );
125     $template->param( fields => $fields );
126 }
127
128 $template->param(
129     op => $op,
130     tablename => $tablename,
131     messages => \@messages,
132 );
133
134 output_html_with_http_headers $input, $cookie, $template->output;