Bug 30848: Add an ExpandCodedFields RecordProcessor filter
[koha.git] / admin / desks.pl
1 #! /usr/bin/perl
2
3 # Copyright (C) 2020 BULAC
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
21 use Modern::Perl;
22 use CGI qw ( -utf8 );
23 use C4::Context;
24 use C4::Auth qw( get_template_and_user );
25 use C4::Output qw( output_html_with_http_headers );
26
27 use Koha::Desks;
28
29 my $input       = CGI->new;
30 my $searchfield = $input->param('desk_name') // q||;
31 my $desk_id      = $input->param('desk_id') || '';
32 my $op          = $input->param('op') || 'list';
33 my @messages;
34
35 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
36     {   template_name   => "admin/desks.tt",
37         query           => $input,
38         type            => "intranet",
39         authnotrequired => 0,
40         flagsrequired   => { parameters => 'manage_libraries' },
41     }
42 );
43
44 my $branches = Koha::Libraries->search( {}, { order_by => ['branchname'] } )->unblessed;
45
46 if ( $op eq 'add_form' ) {
47     my $desk;
48     if ($desk_id) {
49         $desk = Koha::Desks->find($desk_id);
50     }
51
52     $template->param( desk => $desk, );
53 } elsif ( $op eq 'add_validate' ) {
54     my $desk_id       = $input->param('desk_id');
55     my $desk_name    = $input->param('desk_name');
56     my $branchcode   = $input->param('branchcode');
57
58     if (Koha::Desks->find($desk_id)) {
59         my $desk = Koha::Desks->find($desk_id);
60         $desk->desk_name($desk_name);
61         $desk->branchcode($branchcode);
62         eval { $desk->store; };
63         if ($@) {
64             push @messages, { type => 'error', code => 'error_on_update' };
65         } else {
66             push @messages, { type => 'message', code => 'success_on_update' };
67         }
68     } else {
69         my $desk = Koha::Desk->new(
70             {
71                 desk_id       => $desk_id,
72                 desk_name    => $desk_name,
73                 branchcode   => $branchcode,
74             }
75         );
76         eval { $desk->store; };
77         if ($@) {
78             push @messages, { type => 'error', code => 'error_on_insert' };
79         } else {
80             push @messages, { type => 'message', code => 'success_on_insert' };
81         }
82     }
83     $searchfield = q||;
84     $op          = 'list';
85 } elsif ( $op eq 'delete_confirm' ) {
86     my $desk = Koha::Desks->find($desk_id);
87     $template->param( desk => $desk, );
88 } elsif ( $op eq 'delete_confirmed' ) {
89     my $desk = Koha::Desks->find($desk_id);
90     my $deleted = eval { $desk->delete; };
91
92     if ( $@ or not $deleted ) {
93         push @messages, { type => 'error', code => 'error_on_delete' };
94     } else {
95         push @messages, { type => 'message', code => 'success_on_delete' };
96     }
97     $op = 'list';
98 }
99
100 if ( $op eq 'list' || ! $op) {
101     my $desks = Koha::Desks->search( { desk_name => { -like => "%$searchfield%" } } );
102     $template->param( desks => $desks, );
103 }
104
105 $template->param(
106     desk_id      => $desk_id,
107     searchfield => $searchfield,
108     messages    => \@messages,
109     op          => $op,
110     branches    => $branches,
111 );
112
113 output_html_with_http_headers $input, $cookie, $template->output;