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