Bug 27070: Add cross_fields type to our searches
[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;
25 use C4::Output;
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         debug           => 1,
42     }
43 );
44
45 my $branches = Koha::Libraries->search( {}, { order_by => ['branchname'] } )->unblessed;
46
47 if ( $op eq 'add_form' ) {
48     my $desk;
49     if ($desk_id) {
50         $desk = Koha::Desks->find($desk_id);
51     }
52
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');
58
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' };
68         }
69     } else {
70         my $desk = Koha::Desk->new(
71             {
72                 desk_id       => $desk_id,
73                 desk_name    => $desk_name,
74                 branchcode   => $branchcode,
75             }
76         );
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' };
82         }
83     }
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; };
92
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' };
97     }
98     $op = 'list';
99 }
100
101 if ( $op eq 'list' || ! $op) {
102     my $desks = Koha::Desks->search( { desk_name => { -like => "%$searchfield%" } } );
103     $template->param( desks => $desks, );
104 }
105
106 $template->param(
107     desk_id      => $desk_id,
108     searchfield => $searchfield,
109     messages    => \@messages,
110     op          => $op,
111     branches    => $branches,
112 );
113
114 output_html_with_http_headers $input, $cookie, $template->output;