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