Bug 6679: (follow-up) fix 9 perlcritic violations in C4/TmplTokenType.pm
[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 strict;
19 use warnings;
20
21 use CGI qw ( -utf8 );
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 # prepend "br_" to column name and replace spaces with "<br/>"
39 sub br {
40     my ($data, @keys) = @_;
41     for (@keys) {
42         my $br = $data->{$_};
43         $br =~ s{\s+}{<br/>}g;
44         $data->{'br_'.$_} = $br;
45     }
46     $data;
47 }
48
49 # display item circulation alerts
50 sub show {
51     my ($input) = @_;
52     my $dbh = C4::Context->dbh;
53     my ($template, $user, $cookie) = get_template_and_user(
54         {
55             template_name   => "admin/item_circulation_alerts.tt",
56             query           => $input,
57             type            => "intranet",
58             authnotrequired => 0,
59             flagsrequired   => { parameters => 'parameters_remaining_permissions' },
60             debug           => defined($input->param('debug')),
61         }
62     );
63
64     my $br       = GetBranches;
65     my $branch   = $input->param('branch') || '*';
66     my @branches = (
67         {
68             branchcode => '*',
69             branchname => 'Default',
70         },
71         sort { $a->{branchname} cmp $b->{branchname} } values %$br,
72     );
73     for (@branches) {
74         $_->{selected} = "selected" if ($branch eq $_->{branchcode});
75     }
76     my $branch_name = exists($br->{$branch}) && $br->{$branch}->{branchname};
77
78     my @categories = (
79         C4::Category->all
80     );
81     my @item_types = map { br($_, 'description') }  (
82         C4::ItemType->all
83     );
84     my $grid_checkout = $preferences->grid({ branchcode => $branch, notification => 'CHECKOUT' });
85     my $grid_checkin  = $preferences->grid({ branchcode => $branch, notification => 'CHECKIN' });
86
87     $template->param(branch             => $branch);
88     $template->param(branch_name        => $branch_name || 'Default');
89     $template->param(branches           => \@branches);
90     $template->param(categories         => \@categories);
91     $template->param(item_types         => \@item_types);
92     $template->param(grid_checkout      => $grid_checkout);
93     $template->param(grid_checkin       => $grid_checkin);
94
95     output_html_with_http_headers $input, $cookie, $template->output;
96 }
97
98 # toggle a preference via ajax
99 sub toggle {
100     my ($input) = @_;
101     my $id = $input->param('id');
102     my $branch = $input->param('branch');
103     my ($category, $item_type, $notification) = split('-', $id);
104     $category  =~ s/_/*/;
105     $item_type =~ s/_/*/;
106
107     my $settings = {
108         branchcode   => $branch,
109         categorycode => $category,
110         item_type    => $item_type,
111         notification => $notification,
112     };
113
114     my $restrictions = $preferences;  # all the same thing...
115     my $notifications = $preferences; #
116     if ($notifications->is_enabled_for($settings)) {
117         # toggle by adding a restriction
118         $restrictions->create($settings);
119     } else {
120         # toggle by removing the restriction
121         $restrictions->delete($settings);
122     }
123
124     my $response = { success => 1 };
125     my @reasons  = $notifications->is_disabled_for($settings);
126     if (@reasons == 0) {
127         $response->{classes} = '';
128     } else {
129         my $default_exists   = grep { $_->{branchcode} eq '*' } @reasons;
130         my $non_default_also = grep { $_->{branchcode} ne '*' } @reasons;
131         my @classes;
132         push @classes, 'default'  if $default_exists;
133         push @classes, 'disabled' if $non_default_also;
134         $response->{classes} = join(' ', @classes);
135     }
136     print $input->header;
137     print encode_json($response);
138 }
139
140 # dispatch to various actions based on CGI parameter 'action'
141 sub dispatch {
142     my %handler = (
143         show   => \&show,
144         toggle => \&toggle,
145     );
146     my $input  = new CGI;
147     my $action = $input->param('action') || 'show';
148     if (not exists $handler{$action}) {
149         my $status = 400;
150         print $input->header(-status => $status);
151         print $input->div(
152             $input->h1($status),
153             $input->p("$action is not supported.")
154         );
155     } else {
156         $handler{$action}->($input);
157     }
158 }
159
160 # main
161 dispatch if $ENV{REQUEST_URI};
162 1;
163
164
165 =head1 NAME
166
167 admin/item_circulation_alerts.pl - per-branch configuration for messaging
168
169 =head1 SYNOPSIS
170
171 L<http://intranet.mydomain.com:8080/cgi-bin/koha/admin/item_circulation_alerts.pl>
172
173 =head1 DESCRIPTION
174
175 This CGI script drives an interface for configuring item circulation alerts.
176 If you want to prevent alerts from going out for any combination of branch,
177 patron category, and item type, this is where that policy would be set.
178
179 =head2 URLs
180
181
182 =head3 ?action=show
183
184 Display a branches item circulation alert preferences.
185
186 Parameters:
187
188 =over 2
189
190 =item branch
191
192 What branch are we looking at.  If none is specified, the virtual default
193 branch '*' is used.
194
195 =back
196
197
198
199 =head3 ?action=toggle
200
201 Toggle a preference via AJAX
202
203 Parameters:
204
205 =over 2
206
207 =item id
208
209 The id should be string that can be split on "-" which contains:
210 "$categorycode-$item_type-$notification".
211
212 =item branch
213
214 Branch code to apply this preference to
215
216 =back
217
218 =cut