Bug 15774: Use Koha::Object(s) for 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 ( $template, $loggedinuser, $cookie ) = get_template_and_user(
30     {
31         template_name   => "admin/additional-fields.tt",
32         query           => $input,
33         type            => "intranet",
34         authnotrequired => 0,
35         flagsrequired   => { parameters => 1 },
36         debug           => 1,
37     }
38 );
39
40 my $tablename = $input->param('tablename');
41 my $op = $input->param('op') // ( $tablename ? 'list' : 'list_tables' );
42 my $field_id = $input->param('field_id');
43 my @messages;
44
45 if ( $op eq 'add' ) {
46     my $name = $input->param('name') // q{};
47     my $authorised_value_category = $input->param('authorised_value_category') // q{};
48     my $marcfield = $input->param('marcfield') // q{};
49     my $searchable = $input->param('searchable') ? 1 : 0;
50     if ( $field_id and $name ) {
51         my $updated = 0;
52         eval {
53             my $af = Koha::AdditionalFields->find($field_id);
54             $af->set({
55                 name => $name,
56                 authorised_value_category => $authorised_value_category,
57                 marcfield => $marcfield,
58                 searchable => $searchable,
59             });
60             $updated = $af->store ? 1 : 0;
61         };
62         push @messages, {
63             code => 'update',
64             number => $updated,
65         };
66     } elsif ( $name ) {
67         my $inserted = 0;
68         eval {
69             my $af = Koha::AdditionalField->new({
70                 tablename => $tablename,
71                 name => $name,
72                 authorised_value_category => $authorised_value_category,
73                 marcfield => $marcfield,
74                 searchable => $searchable,
75             });
76             $inserted = $af->store ? 1 : 0;
77         };
78         push @messages, {
79             code => 'insert',
80             number => $inserted,
81         };
82     } else {
83         push @messages, {
84             code => 'insert',
85             number => 0,
86         };
87     }
88     $op = 'list';
89 }
90
91 if ( $op eq 'delete' ) {
92     my $deleted = 0;
93     eval {
94         my $af = Koha::AdditionalFields->find($field_id);
95         $deleted = $af->delete;
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::AdditionalFields->find($field_id);
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::AdditionalFields->search( { 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;