Bug 23681: (QA follow-up) Proper handling of default option
[koha.git] / admin / curbside_pickup.pl
1 #! /usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19 use CGI qw ( -utf8 );
20 use C4::Context;
21 use C4::Auth qw( get_template_and_user );
22 use C4::Output qw( output_html_with_http_headers );
23
24 use Koha::CurbsidePickupPolicies;
25 use Koha::Libraries;
26
27 my $input       = CGI->new;
28 my $op          = $input->param('op') || 'list';
29 my @messages;
30
31 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
32     {
33         template_name => "admin/curbside_pickup.tt",
34         query         => $input,
35         type          => "intranet",
36         flagsrequired => { parameters => 'manage_curbside_pickups' },
37     }
38 );
39
40 my $libraries = Koha::Libraries->search( {}, { order_by => ['branchname'] } );
41 if ( $op eq 'save' ) {
42     foreach my $library ( $libraries->as_list ) {
43         my $branchcode = $library->branchcode;
44
45         my $params = {
46             branchcode      => $branchcode,
47             enabled         => scalar $input->param("enable-$branchcode") || 0,
48             enable_waiting_holds_only => scalar $input->param("enable-waiting-holds-only-$branchcode") || 0,
49             pickup_interval => scalar $input->param("interval-$branchcode"),
50             patrons_per_interval    => scalar $input->param("max-per-interval-$branchcode"),
51             patron_scheduled_pickup => scalar $input->param("patron-scheduled-$branchcode") || 0,
52         };
53
54         my $policy =
55           Koha::CurbsidePickupPolicies->find_or_create( { branchcode => $branchcode } );
56         $policy->update($params);
57
58         $policy->opening_slots->delete;
59         my @pickup_slots = $input->multi_param("pickup-slot-" . $branchcode);
60         for my $pickup_slot ( @pickup_slots ) {
61             $policy->add_opening_slot($pickup_slot);
62         }
63     }
64     $op = 'list';
65 }
66
67 if ( $op eq 'list' ) {
68     $template->param(
69         policies => {
70             map { $_->branchcode => $_ }
71               Koha::CurbsidePickupPolicies->search->as_list
72         },
73         libraries => $libraries,
74     );
75 }
76
77 $template->param(
78     messages => \@messages,
79     op       => $op,
80 );
81
82 output_html_with_http_headers $input, $cookie, $template->output;