Bug 29368: Correctly mock Zebra index from tests
[koha.git] / admin / item_circulation_alerts.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
20 use CGI qw ( -utf8 );
21 use JSON qw( encode_json );
22 #use Data::Dump 'pp';
23
24 use C4::Auth qw( get_template_and_user );
25 use C4::Context;
26 use C4::ItemCirculationAlertPreference;
27 use C4::Output qw( output_html_with_http_headers );
28
29 use Koha::ItemTypes;
30 use Koha::Patron::Categories;
31
32 # shortcut for long package name
33 our $preferences = 'C4::ItemCirculationAlertPreference';
34
35 # display item circulation alerts
36 sub show {
37     my ($input) = @_;
38     my $dbh = C4::Context->dbh;
39     my ($template, $user, $cookie) = get_template_and_user(
40         {
41             template_name   => "admin/item_circulation_alerts.tt",
42             query           => $input,
43             type            => "intranet",
44             flagsrequired   => { parameters => 'item_circ_alerts' },
45             debug           => defined($input->param('debug')),
46         }
47     );
48
49     my $branch   = $input->param('branch') || '*';
50     my @categories = Koha::Patron::Categories->search_with_library_limits;
51     my @item_types = Koha::ItemTypes->search;
52     my $grid_checkout = $preferences->grid({ branchcode => $branch, notification => 'CHECKOUT' });
53     my $grid_checkin  = $preferences->grid({ branchcode => $branch, notification => 'CHECKIN' });
54
55     $template->param(branch             => $branch);
56     $template->param(categories         => \@categories);
57     $template->param(item_types         => \@item_types);
58     $template->param(grid_checkout      => $grid_checkout);
59     $template->param(grid_checkin       => $grid_checkin);
60
61     output_html_with_http_headers $input, $cookie, $template->output;
62 }
63
64 # toggle a preference via ajax
65 sub toggle {
66     my ($input) = @_;
67     my $id = $input->param('id');
68     my $branch = $input->param('branch');
69     my ($category, $item_type, $notification) = split('-', $id);
70     $category  =~ s/_/*/;
71     $item_type =~ s/_/*/;
72
73     my $settings = {
74         branchcode   => $branch,
75         categorycode => $category,
76         item_type    => $item_type,
77         notification => $notification,
78     };
79
80     my $restrictions = $preferences;  # all the same thing...
81     my $notifications = $preferences; #
82     if ($notifications->is_enabled_for($settings)) {
83         # toggle by adding a restriction
84         $restrictions->create($settings);
85     } else {
86         # toggle by removing the restriction
87         $restrictions->delete($settings);
88     }
89
90     my $response = { success => 1 };
91     my @reasons  = $notifications->is_disabled_for($settings);
92     if (@reasons == 0) {
93         $response->{classes} = '';
94     } else {
95         my $default_exists   = grep { $_->{branchcode} eq '*' } @reasons;
96         my $non_default_also = grep { $_->{branchcode} ne '*' } @reasons;
97         my @classes;
98         push @classes, 'default'  if $default_exists;
99         push @classes, 'disabled' if $non_default_also;
100         $response->{classes} = join(' ', @classes);
101     }
102     print $input->header;
103     print encode_json($response);
104 }
105
106 # dispatch to various actions based on CGI parameter 'action'
107 sub dispatch {
108     my %handler = (
109         show   => \&show,
110         toggle => \&toggle,
111     );
112     my $input  = CGI->new;
113     my $action = $input->param('action') || 'show';
114     if (not exists $handler{$action}) {
115         my $status = 400;
116         print $input->header(-status => $status);
117         print $input->div(
118             $input->h1($status),
119             $input->p("$action is not supported.")
120         );
121     } else {
122         $handler{$action}->($input);
123     }
124 }
125
126 # main
127 dispatch if $ENV{REQUEST_URI};
128 1;
129
130
131 =head1 NAME
132
133 admin/item_circulation_alerts.pl - per-branch configuration for messaging
134
135 =head1 SYNOPSIS
136
137 L<http://intranet.mydomain.com:8080/cgi-bin/koha/admin/item_circulation_alerts.pl>
138
139 =head1 DESCRIPTION
140
141 This CGI script drives an interface for configuring item circulation alerts.
142 If you want to prevent alerts from going out for any combination of branch,
143 patron category, and item type, this is where that policy would be set.
144
145 =head2 URLs
146
147
148 =head3 ?action=show
149
150 Display a branches item circulation alert preferences.
151
152 Parameters:
153
154 =over 2
155
156 =item branch
157
158 What branch are we looking at.  If none is specified, the virtual default
159 branch '*' is used.
160
161 =back
162
163
164
165 =head3 ?action=toggle
166
167 Toggle a preference via AJAX
168
169 Parameters:
170
171 =over 2
172
173 =item id
174
175 The id should be string that can be split on "-" which contains:
176 "$categorycode-$item_type-$notification".
177
178 =item branch
179
180 Branch code to apply this preference to
181
182 =back
183
184 =cut