Bug 23432: (QA follow-up) Add a unit test
[koha.git] / admin / desks.pl
blob352ff8ba50bfaa893417f096f529dc8f55256e47
1 #! /usr/bin/perl
3 # Copyright (C) 2020 BULAC
5 # This file is part of Koha.
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.
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.
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>.
21 use Modern::Perl;
22 use CGI qw ( -utf8 );
23 use C4::Context;
24 use C4::Auth;
25 use C4::Output;
27 use Koha::Desks;
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;
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 debug => 1,
45 my $branches = Koha::Libraries->search( {}, { order_by => ['branchname'] } )->unblessed;
47 if ( $op eq 'add_form' ) {
48 my $desk;
49 if ($desk_id) {
50 $desk = Koha::Desks->find($desk_id);
53 $template->param( desk => $desk, );
54 } elsif ( $op eq 'add_validate' ) {
55 my $desk_id = $input->param('desk_id');
56 my $desk_name = $input->param('desk_name');
57 my $branchcode = $input->param('branchcode');
59 if (Koha::Desks->find($desk_id)) {
60 my $desk = Koha::Desks->find($desk_id);
61 $desk->desk_name($desk_name);
62 $desk->branchcode($branchcode);
63 eval { $desk->store; };
64 if ($@) {
65 push @messages, { type => 'error', code => 'error_on_update' };
66 } else {
67 push @messages, { type => 'message', code => 'success_on_update' };
69 } else {
70 my $desk = Koha::Desk->new(
72 desk_id => $desk_id,
73 desk_name => $desk_name,
74 branchcode => $branchcode,
77 eval { $desk->store; };
78 if ($@) {
79 push @messages, { type => 'error', code => 'error_on_insert' };
80 } else {
81 push @messages, { type => 'message', code => 'success_on_insert' };
84 $searchfield = q||;
85 $op = 'list';
86 } elsif ( $op eq 'delete_confirm' ) {
87 my $desk = Koha::Desks->find($desk_id);
88 $template->param( desk => $desk, );
89 } elsif ( $op eq 'delete_confirmed' ) {
90 my $desk = Koha::Desks->find($desk_id);
91 my $deleted = eval { $desk->delete; };
93 if ( $@ or not $deleted ) {
94 push @messages, { type => 'error', code => 'error_on_delete' };
95 } else {
96 push @messages, { type => 'message', code => 'success_on_delete' };
98 $op = 'list';
101 if ( $op eq 'list' || ! $op) {
102 my $desks = Koha::Desks->search( { desk_name => { -like => "%$searchfield%" } } );
103 $template->param( desks => $desks, );
106 $template->param(
107 desk_id => $desk_id,
108 searchfield => $searchfield,
109 messages => \@messages,
110 op => $op,
111 branches => $branches,
114 output_html_with_http_headers $input, $cookie, $template->output;