Merge remote-tracking branch 'kc/new/bug_6205' into kcmaster
[koha.git] / admin / item_circulation_alerts.pl
blobbba5d6017e18f01c8a3f6b9de9ee639fce62074c
1 #!/usr/bin/perl
3 # This file is part of Koha.
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.
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.
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
18 use strict;
19 use warnings;
21 use CGI;
22 use File::Basename;
23 use Encode;
24 use JSON;
25 #use Data::Dump 'pp';
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;
35 # shortcut for long package name
36 my $preferences = 'C4::ItemCirculationAlertPreference';
38 # utf8 filter
39 sub utf8 {
40 my ($data, @keys) = @_;
41 for (@keys) {
42 $data->{$_} = decode('utf8', $data->{$_});
44 $data;
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;
55 $data;
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(
64 template_name => "admin/item_circulation_alerts.tmpl",
65 query => $input,
66 type => "intranet",
67 authnotrequired => 0,
68 flagsrequired => { parameters => 1 },
69 debug => defined($input->param('debug')),
73 my $br = GetBranches;
74 my $branch = $input->param('branch') || '*';
75 my @branches = (
77 branchcode => '*',
78 branchname => 'Default',
80 sort { $a->{branchname} cmp $b->{branchname} } values %$br,
82 for (@branches) {
83 $_->{selected} = "selected" if ($branch eq $_->{branchcode});
85 my $branch_name = exists($br->{$branch}) && $br->{$branch}->{branchname};
87 my @categories = (
88 C4::Category->all
90 my @item_types = map { br($_, 'description') } (
91 C4::ItemType->all
93 my $grid_checkout = $preferences->grid({ branchcode => $branch, notification => 'CHECKOUT' });
94 my $grid_checkin = $preferences->grid({ branchcode => $branch, notification => 'CHECKIN' });
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);
104 output_html_with_http_headers $input, $cookie, $template->output;
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/_/*/;
116 my $settings = {
117 branchcode => $branch,
118 categorycode => $category,
119 item_type => $item_type,
120 notification => $notification,
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);
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);
145 print $input->header;
146 print encode_json($response);
149 # dispatch to various actions based on CGI parameter 'action'
150 sub dispatch {
151 my %handler = (
152 show => \&show,
153 toggle => \&toggle,
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.")
164 } else {
165 $handler{$action}->($input);
169 # main
170 dispatch if $ENV{REQUEST_URI};
174 =head1 NAME
176 admin/item_circulation_alerts.pl - per-branch configuration for messaging
178 =head1 SYNOPSIS
180 L<http://intranet.mydomain.com:8080/cgi-bin/koha/admin/item_circulation_alerts.pl>
182 =head1 DESCRIPTION
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.
188 =head2 URLs
191 =head3 ?action=show
193 Display a branches item circulation alert preferences.
195 Parameters:
197 =over 2
199 =item branch
201 What branch are we looking at. If none is specified, the virtual default
202 branch '*' is used.
204 =back
208 =head3 ?action=toggle
210 Toggle a preference via AJAX
212 Parameters:
214 =over 2
216 =item id
218 The id should be string that can be split on "-" which contains:
219 "$categorycode-$item_type-$notification".
221 =item branch
223 Branch code to apply this preference to
225 =back
227 =cut