Bug 30477: Add new UNIMARC installer translation files
[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 'add' ) {
51     my $name = $input->param('name') // q{};
52     my $authorised_value_category = $input->param('authorised_value_category') // q{};
53     my $marcfield = $input->param('marcfield') // q{};
54     my $searchable = $input->param('searchable') ? 1 : 0;
55     if ( $field_id and $name ) {
56         my $updated = 0;
57         eval {
58             my $af = Koha::AdditionalFields->find($field_id);
59             $af->set({
60                 name => $name,
61                 authorised_value_category => $authorised_value_category,
62                 marcfield => $marcfield,
63                 searchable => $searchable,
64             });
65             $updated = $af->store ? 1 : 0;
66         };
67         push @messages, {
68             code => 'update',
69             number => $updated,
70         };
71     } elsif ( $name ) {
72         my $inserted = 0;
73         eval {
74             my $af = Koha::AdditionalField->new({
75                 tablename => $tablename,
76                 name => $name,
77                 authorised_value_category => $authorised_value_category,
78                 marcfield => $marcfield,
79                 searchable => $searchable,
80             });
81             $inserted = $af->store ? 1 : 0;
82         };
83         push @messages, {
84             code => 'insert',
85             number => $inserted,
86         };
87     } else {
88         push @messages, {
89             code => 'insert',
90             number => 0,
91         };
92     }
93     $op = 'list';
94 }
95
96 if ( $op eq 'delete' ) {
97     my $deleted = 0;
98     eval {
99         my $af = Koha::AdditionalFields->find($field_id);
100         $deleted = $af->delete;
101     };
102     push @messages, {
103         code => 'delete',
104         number => $deleted,
105     };
106     $op = 'list';
107 }
108
109 if ( $op eq 'add_form' ) {
110     my $field;
111     if ( $field_id ) {
112         $field = Koha::AdditionalFields->find($field_id);
113     }
114
115     $tablename = $field->tablename if $field;
116
117     $template->param(
118         field => $field,
119     );
120 }
121
122 if ( $op eq 'list' ) {
123     my $fields = Koha::AdditionalFields->search( { tablename => $tablename } );
124     $template->param( fields => $fields );
125 }
126
127 $template->param(
128     op => $op,
129     tablename => $tablename,
130     messages => \@messages,
131 );
132
133 output_html_with_http_headers $input, $cookie, $template->output;