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