Brand New UI for Alert Configuration
[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 URI::Escape 'uri_escape_utf8';
25 use JSON;
26 #use Data::Dump 'pp';
27
28 use C4::Auth;
29 use C4::Context;
30 use C4::Branch;
31 use C4::Category;
32 use C4::ItemType;
33 use C4::ItemCirculationAlertPreference;
34 use C4::Output;
35
36 # shortcut for long package name
37 my $preferences = 'C4::ItemCirculationAlertPreference';
38
39 # utf8 filter
40 sub utf8 {
41     my ($data, @keys) = @_;
42     for (@keys) {
43         $data->{$_} = decode('utf8', $data->{$_});
44     }
45     $data;
46 }
47
48 # prepend "br_" to column name and replace spaces with "<br/>"
49 sub br {
50     my ($data, @keys) = @_;
51     for (@keys) {
52         my $br = $data->{$_};
53         $br =~ s{\s+}{<br/>}g;
54         $data->{'br_'.$_} = $br;
55     }
56     $data;
57 }
58
59 # display item circulation alerts
60 sub show {
61     my ($input) = @_;
62     my $dbh = C4::Context->dbh;
63     my ($template, $user, $cookie) = get_template_and_user(
64         {
65             template_name   => "admin/item_circulation_alerts.tmpl",
66             query           => $input,
67             type            => "intranet",
68             authnotrequired => 0,
69             flagsrequired   => { admin => 1 },
70             debug           => defined($input->param('debug')),
71         }
72     );
73
74     my $br       = GetBranches;
75     my $branch   = $input->param('branch') || '*';
76     my @branches = map { utf8($_, 'branchname') } (
77         {
78             branchcode => '*',
79             branchname => 'Default',
80         },
81         sort { $a->{branchname} cmp $b->{branchname} } values %$br,
82     );
83     for (@branches) {
84         $_->{selected} = "selected" if ($branch eq $_->{branchcode});
85     }
86     my $branch_name = exists($br->{$branch}) && $br->{$branch}->{branchname};
87
88     my @categories = map { utf8($_, 'description') }  (
89         C4::Category->all
90     );
91     my @item_types = map { utf8($_, 'description'); br($_, 'description') }  (
92         C4::ItemType->all
93     );
94     my $grid_checkout = $preferences->grid({ branchcode => $branch, notification => 'CHECKOUT' });
95     my $grid_checkin  = $preferences->grid({ branchcode => $branch, notification => 'CHECKIN' });
96
97     $template->param(branch             => $branch);
98     $template->param(branch_name        => $branch_name || 'Default');
99     $template->param(branches           => \@branches);
100     $template->param(categories         => \@categories);
101     $template->param(item_types         => \@item_types);
102     $template->param(grid_checkout      => $grid_checkout);
103     $template->param(grid_checkin       => $grid_checkin);
104
105     output_html_with_http_headers $input, $cookie, $template->output;
106 }
107
108 # toggle a preference via ajax
109 sub toggle {
110     my ($input) = @_;
111     my $id = $input->param('id');
112     my $branch = $input->param('branch');
113     my ($category, $item_type, $notification) = split('-', $id);
114     $category  =~ s/_/*/;
115     $item_type =~ s/_/*/;
116
117     my $settings = {
118         branchcode   => $branch,
119         categorycode => $category,
120         item_type    => $item_type,
121         notification => $notification,
122     };
123
124     my $restrictions = $preferences;  # all the same thing...
125     my $notifications = $preferences; #
126     if ($notifications->is_enabled_for($settings)) {
127         # toggle by adding a restriction
128         $restrictions->create($settings);
129     } else {
130         # toggle by removing the restriction
131         $restrictions->delete($settings);
132     }
133
134     my $response = { success => 1 };
135     my @reasons  = $notifications->is_disabled_for($settings);
136     if (@reasons == 0) {
137         $response->{class} = '';
138     } else {
139         my $default_exists   = grep { $_->{branchcode} eq '*' } @reasons;
140         my $non_default_also = grep { $_->{branchcode} ne '*' } @reasons;
141         my @classes;
142         push @classes, 'default'  if $default_exists;
143         push @classes, 'disabled' if $non_default_also;
144         $response->{class} = join(' ', @classes);
145     }
146     print $input->header;
147     print encode_json($response);
148 }
149
150 # dispatch to various actions based on CGI parameter 'action'
151 sub dispatch {
152     my %handler = (
153         show   => \&show,
154         toggle => \&toggle,
155     );
156     my $input  = new CGI;
157     my $action = $input->param('action') || 'show';
158     if (not exists $handler{$action}) {
159         my $status = 400;
160         print $input->header(-status => $status);
161         print $input->div(
162             $input->h1($status),
163             $input->p("$action is not supported.")
164         );
165     } else {
166         $handler{$action}->($input);
167     }
168 }
169
170 # main
171 dispatch if $ENV{REQUEST_URI};
172 1;
173
174
175 =head1 NAME
176
177 admin/item_circulation_alerts.pl - per-branch configuration for messaging
178
179 =head1 SYNOPSIS
180
181 L<http://intranet.mydomain.com:8080/cgi-bin/koha/admin/item_circulation_alerts.pl>
182
183 =head1 DESCRIPTION
184
185 This CGI script drives an interface for configuring item circulation alerts.
186 If you want to prevent alerts from going out for any combination of branch,
187 patron category, and item type, this is where that policy would be set.
188
189 =head2 URLs
190
191
192 =head3 ?action=show
193
194 Display a branches item circulation alert preferences.
195
196 Parameters:
197
198 =over 4
199
200 =item branch
201
202 What branch are we looking at.  If none is specified, the virtual default
203 branch '*' is used.
204
205 =back
206
207
208
209 =head3 ?action=toggle
210
211 Toggle a preference via AJAX
212
213 Parameters:
214
215 =over 4
216
217 =item id
218
219 "$categorycode-$item_type-$notification"
220
221 =item branch
222
223 Branch code to apply this preference to
224
225 =back
226
227 =cut
228
229 # Local Variables: ***
230 # mode: cperl ***
231 # indent-tabs-mode: nil ***
232 # cperl-close-paren-offset: -4 ***
233 # cperl-continued-statement-offset: 4 ***
234 # cperl-indent-level: 4 ***
235 # cperl-indent-parens-as-block: t ***
236 # cperl-tab-always-indent: nil ***
237 # End: ***
238 # vim:tabstop=8 softtabstop=4 shiftwidth=4 shiftround expandtab