Bug 16573: fix linkid value on marc_subfield_structure
[koha.git] / admin / item_circulation_alerts.pl
blobca253aac3f89c2869b0662ec4e73d32762b8c2a9
1 #!/usr/bin/perl
3 # This file is part of Koha.
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.
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.
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>.
18 use strict;
19 use warnings;
21 use CGI qw ( -utf8 );
22 use File::Basename;
23 use Encode;
24 use JSON;
25 #use Data::Dump 'pp';
27 use C4::Auth;
28 use C4::Context;
29 use C4::Branch;
30 use C4::Category;
31 use C4::ItemCirculationAlertPreference;
32 use C4::Output;
34 use Koha::ItemTypes;
36 # shortcut for long package name
37 our $preferences = 'C4::ItemCirculationAlertPreference';
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(
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')),
54 my $br = GetBranches;
55 my $branch = $input->param('branch') || '*';
56 my @branches = (
58 branchcode => '*',
59 branchname => 'Default',
61 sort { $a->{branchname} cmp $b->{branchname} } values %$br,
63 for (@branches) {
64 $_->{selected} = "selected" if ($branch eq $_->{branchcode});
66 my $branch_name = exists($br->{$branch}) && $br->{$branch}->{branchname};
68 my @categories = (
69 C4::Category->all
71 my @item_types = Koha::ItemTypes->search;
72 my $grid_checkout = $preferences->grid({ branchcode => $branch, notification => 'CHECKOUT' });
73 my $grid_checkin = $preferences->grid({ branchcode => $branch, notification => 'CHECKIN' });
75 $template->param(branch => $branch);
76 $template->param(branch_name => $branch_name || 'Default');
77 $template->param(branches => \@branches);
78 $template->param(categories => \@categories);
79 $template->param(item_types => \@item_types);
80 $template->param(grid_checkout => $grid_checkout);
81 $template->param(grid_checkin => $grid_checkin);
83 output_html_with_http_headers $input, $cookie, $template->output;
86 # toggle a preference via ajax
87 sub toggle {
88 my ($input) = @_;
89 my $id = $input->param('id');
90 my $branch = $input->param('branch');
91 my ($category, $item_type, $notification) = split('-', $id);
92 $category =~ s/_/*/;
93 $item_type =~ s/_/*/;
95 my $settings = {
96 branchcode => $branch,
97 categorycode => $category,
98 item_type => $item_type,
99 notification => $notification,
102 my $restrictions = $preferences; # all the same thing...
103 my $notifications = $preferences; #
104 if ($notifications->is_enabled_for($settings)) {
105 # toggle by adding a restriction
106 $restrictions->create($settings);
107 } else {
108 # toggle by removing the restriction
109 $restrictions->delete($settings);
112 my $response = { success => 1 };
113 my @reasons = $notifications->is_disabled_for($settings);
114 if (@reasons == 0) {
115 $response->{classes} = '';
116 } else {
117 my $default_exists = grep { $_->{branchcode} eq '*' } @reasons;
118 my $non_default_also = grep { $_->{branchcode} ne '*' } @reasons;
119 my @classes;
120 push @classes, 'default' if $default_exists;
121 push @classes, 'disabled' if $non_default_also;
122 $response->{classes} = join(' ', @classes);
124 print $input->header;
125 print encode_json($response);
128 # dispatch to various actions based on CGI parameter 'action'
129 sub dispatch {
130 my %handler = (
131 show => \&show,
132 toggle => \&toggle,
134 my $input = new CGI;
135 my $action = $input->param('action') || 'show';
136 if (not exists $handler{$action}) {
137 my $status = 400;
138 print $input->header(-status => $status);
139 print $input->div(
140 $input->h1($status),
141 $input->p("$action is not supported.")
143 } else {
144 $handler{$action}->($input);
148 # main
149 dispatch if $ENV{REQUEST_URI};
153 =head1 NAME
155 admin/item_circulation_alerts.pl - per-branch configuration for messaging
157 =head1 SYNOPSIS
159 L<http://intranet.mydomain.com:8080/cgi-bin/koha/admin/item_circulation_alerts.pl>
161 =head1 DESCRIPTION
163 This CGI script drives an interface for configuring item circulation alerts.
164 If you want to prevent alerts from going out for any combination of branch,
165 patron category, and item type, this is where that policy would be set.
167 =head2 URLs
170 =head3 ?action=show
172 Display a branches item circulation alert preferences.
174 Parameters:
176 =over 2
178 =item branch
180 What branch are we looking at. If none is specified, the virtual default
181 branch '*' is used.
183 =back
187 =head3 ?action=toggle
189 Toggle a preference via AJAX
191 Parameters:
193 =over 2
195 =item id
197 The id should be string that can be split on "-" which contains:
198 "$categorycode-$item_type-$notification".
200 =item branch
202 Branch code to apply this preference to
204 =back
206 =cut