Merge branch 'bug_9272' into 3.12-master
[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 under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 2 of the License, or (at your option) any later
8 # version.
9 #
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along with
15 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
16 # Suite 330, Boston, MA  02111-1307 USA
17
18 use strict;
19 use warnings;
20
21 use CGI;
22 use File::Basename;
23 use Encode;
24 use JSON;
25 #use Data::Dump 'pp';
26
27 use C4::Auth;
28 use C4::Context;
29 use C4::Branch;
30 use C4::Category;
31 use C4::ItemType;
32 use C4::ItemCirculationAlertPreference;
33 use C4::Output;
34
35 # shortcut for long package name
36 our $preferences = 'C4::ItemCirculationAlertPreference';
37
38 # utf8 filter
39 sub utf8 {
40     my ($data, @keys) = @_;
41     for (@keys) {
42         $data->{$_} = decode('utf8', $data->{$_});
43     }
44     $data;
45 }
46
47 # prepend "br_" to column name and replace spaces with "<br/>"
48 sub br {
49     my ($data, @keys) = @_;
50     for (@keys) {
51         my $br = $data->{$_};
52         $br =~ s{\s+}{<br/>}g;
53         $data->{'br_'.$_} = $br;
54     }
55     $data;
56 }
57
58 # display item circulation alerts
59 sub show {
60     my ($input) = @_;
61     my $dbh = C4::Context->dbh;
62     my ($template, $user, $cookie) = get_template_and_user(
63         {
64             template_name   => "admin/item_circulation_alerts.tmpl",
65             query           => $input,
66             type            => "intranet",
67             authnotrequired => 0,
68             flagsrequired   => { parameters => 'parameters_remaining_permissions' },
69             debug           => defined($input->param('debug')),
70         }
71     );
72
73     my $br       = GetBranches;
74     my $branch   = $input->param('branch') || '*';
75     my @branches = (
76         {
77             branchcode => '*',
78             branchname => 'Default',
79         },
80         sort { $a->{branchname} cmp $b->{branchname} } values %$br,
81     );
82     for (@branches) {
83         $_->{selected} = "selected" if ($branch eq $_->{branchcode});
84     }
85     my $branch_name = exists($br->{$branch}) && $br->{$branch}->{branchname};
86
87     my @categories = (
88         C4::Category->all
89     );
90     my @item_types = map { br($_, 'description') }  (
91         C4::ItemType->all
92     );
93     my $grid_checkout = $preferences->grid({ branchcode => $branch, notification => 'CHECKOUT' });
94     my $grid_checkin  = $preferences->grid({ branchcode => $branch, notification => 'CHECKIN' });
95
96     $template->param(branch             => $branch);
97     $template->param(branch_name        => $branch_name || 'Default');
98     $template->param(branches           => \@branches);
99     $template->param(categories         => \@categories);
100     $template->param(item_types         => \@item_types);
101     $template->param(grid_checkout      => $grid_checkout);
102     $template->param(grid_checkin       => $grid_checkin);
103
104     output_html_with_http_headers $input, $cookie, $template->output;
105 }
106
107 # toggle a preference via ajax
108 sub toggle {
109     my ($input) = @_;
110     my $id = $input->param('id');
111     my $branch = $input->param('branch');
112     my ($category, $item_type, $notification) = split('-', $id);
113     $category  =~ s/_/*/;
114     $item_type =~ s/_/*/;
115
116     my $settings = {
117         branchcode   => $branch,
118         categorycode => $category,
119         item_type    => $item_type,
120         notification => $notification,
121     };
122
123     my $restrictions = $preferences;  # all the same thing...
124     my $notifications = $preferences; #
125     if ($notifications->is_enabled_for($settings)) {
126         # toggle by adding a restriction
127         $restrictions->create($settings);
128     } else {
129         # toggle by removing the restriction
130         $restrictions->delete($settings);
131     }
132
133     my $response = { success => 1 };
134     my @reasons  = $notifications->is_disabled_for($settings);
135     if (@reasons == 0) {
136         $response->{classes} = '';
137     } else {
138         my $default_exists   = grep { $_->{branchcode} eq '*' } @reasons;
139         my $non_default_also = grep { $_->{branchcode} ne '*' } @reasons;
140         my @classes;
141         push @classes, 'default'  if $default_exists;
142         push @classes, 'disabled' if $non_default_also;
143         $response->{classes} = join(' ', @classes);
144     }
145     print $input->header;
146     print encode_json($response);
147 }
148
149 # dispatch to various actions based on CGI parameter 'action'
150 sub dispatch {
151     my %handler = (
152         show   => \&show,
153         toggle => \&toggle,
154     );
155     my $input  = new CGI;
156     my $action = $input->param('action') || 'show';
157     if (not exists $handler{$action}) {
158         my $status = 400;
159         print $input->header(-status => $status);
160         print $input->div(
161             $input->h1($status),
162             $input->p("$action is not supported.")
163         );
164     } else {
165         $handler{$action}->($input);
166     }
167 }
168
169 # main
170 dispatch if $ENV{REQUEST_URI};
171 1;
172
173
174 =head1 NAME
175
176 admin/item_circulation_alerts.pl - per-branch configuration for messaging
177
178 =head1 SYNOPSIS
179
180 L<http://intranet.mydomain.com:8080/cgi-bin/koha/admin/item_circulation_alerts.pl>
181
182 =head1 DESCRIPTION
183
184 This CGI script drives an interface for configuring item circulation alerts.
185 If you want to prevent alerts from going out for any combination of branch,
186 patron category, and item type, this is where that policy would be set.
187
188 =head2 URLs
189
190
191 =head3 ?action=show
192
193 Display a branches item circulation alert preferences.
194
195 Parameters:
196
197 =over 2
198
199 =item branch
200
201 What branch are we looking at.  If none is specified, the virtual default
202 branch '*' is used.
203
204 =back
205
206
207
208 =head3 ?action=toggle
209
210 Toggle a preference via AJAX
211
212 Parameters:
213
214 =over 2
215
216 =item id
217
218 The id should be string that can be split on "-" which contains:
219 "$categorycode-$item_type-$notification".
220
221 =item branch
222
223 Branch code to apply this preference to
224
225 =back
226
227 =cut