Bug 24421: Unit tests
[koha.git] / admin / additional-fields.pl
blobf2516d71cf145d37d285ab6df6fab2b0d5ab2e7a
1 #!/usr/bin/perl
3 # This file is part of Koha.
5 # Copyright 2013 BibLibre
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.
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.
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>.
19 use Modern::Perl;
20 use CGI;
21 use C4::Auth;
22 use C4::Koha;
23 use C4::Output;
24 use Koha::AdditionalFields;
26 my $input = new CGI;
28 my %flagsrequired;
29 $flagsrequired{parameters} = 'manage_additional_fields';
31 my $tablename = $input->param('tablename');
32 my $op = $input->param('op') // ( $tablename ? 'list' : 'list_tables' );
34 if( $op ne 'list_tables' ){
35 $flagsrequired{acquisition} = 'order_manage' if $tablename eq 'aqbasket';
36 $flagsrequired{serials} = 'edit_subscription' if $tablename eq 'subscription';
39 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
41 template_name => "admin/additional-fields.tt",
42 query => $input,
43 type => "intranet",
44 authnotrequired => 0,
45 flagsrequired => \%flagsrequired,
46 debug => 1,
50 my $field_id = $input->param('field_id');
51 my @messages;
53 if ( $op eq 'add' ) {
54 my $name = $input->param('name') // q{};
55 my $authorised_value_category = $input->param('authorised_value_category') // q{};
56 my $marcfield = $input->param('marcfield') // q{};
57 my $searchable = $input->param('searchable') ? 1 : 0;
58 if ( $field_id and $name ) {
59 my $updated = 0;
60 eval {
61 my $af = Koha::AdditionalFields->find($field_id);
62 $af->set({
63 name => $name,
64 authorised_value_category => $authorised_value_category,
65 marcfield => $marcfield,
66 searchable => $searchable,
67 });
68 $updated = $af->store ? 1 : 0;
70 push @messages, {
71 code => 'update',
72 number => $updated,
74 } elsif ( $name ) {
75 my $inserted = 0;
76 eval {
77 my $af = Koha::AdditionalField->new({
78 tablename => $tablename,
79 name => $name,
80 authorised_value_category => $authorised_value_category,
81 marcfield => $marcfield,
82 searchable => $searchable,
83 });
84 $inserted = $af->store ? 1 : 0;
86 push @messages, {
87 code => 'insert',
88 number => $inserted,
90 } else {
91 push @messages, {
92 code => 'insert',
93 number => 0,
96 $op = 'list';
99 if ( $op eq 'delete' ) {
100 my $deleted = 0;
101 eval {
102 my $af = Koha::AdditionalFields->find($field_id);
103 $deleted = $af->delete;
105 push @messages, {
106 code => 'delete',
107 number => $deleted,
109 $op = 'list';
112 if ( $op eq 'add_form' ) {
113 my $field;
114 if ( $field_id ) {
115 $field = Koha::AdditionalFields->find($field_id);
118 $tablename = $field->tablename if $field;
120 $template->param(
121 field => $field,
125 if ( $op eq 'list' ) {
126 my $fields = Koha::AdditionalFields->search( { tablename => $tablename } );
127 $template->param( fields => $fields );
130 $template->param(
131 op => $op,
132 tablename => $tablename,
133 messages => \@messages,
136 output_html_with_http_headers $input, $cookie, $template->output;