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