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