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