Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

executable file 229 lines (182 sloc) 6.057 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
#!/usr/bin/env perl

# This file is part of Koha.
#
# Koha is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
# Suite 330, Boston, MA 02111-1307 USA

use strict;
use warnings;

use CGI;
use File::Basename;
use Encode;
use JSON;
#use Data::Dump 'pp';

use C4::Auth;
use Koha;
use C4::Context;
use C4::Branch;
use C4::Category;
use C4::ItemType;
use C4::ItemCirculationAlertPreference;
use C4::Output;

# shortcut for long package name
our $preferences = 'C4::ItemCirculationAlertPreference';

# utf8 filter
sub utf8 {
    my ($data, @keys) = @_;
    for (@keys) {
        $data->{$_} = decode('utf8', $data->{$_});
    }
    $data;
}

# prepend "br_" to column name and replace spaces with "<br/>"
sub br {
    my ($data, @keys) = @_;
    for (@keys) {
        my $br = $data->{$_};
        $br =~ s{\s+}{<br/>}g;
        $data->{'br_'.$_} = $br;
    }
    $data;
}

# display item circulation alerts
sub show {
    my ($input) = @_;
    my $dbh = C4::Context->dbh;
    my ($template, $user, $cookie) = get_template_and_user(
        {
            template_name => "admin/item_circulation_alerts.tmpl",
            query => $input,
            type => "intranet",
            authnotrequired => 0,
            flagsrequired => { parameters => 1 },
            debug => defined($input->param('debug')),
        }
    );

    my $br = GetBranches;
    my $branch = $input->param('branch') || '*';
    my @branches = (
        {
            branchcode => '*',
            branchname => 'Default',
        },
        sort { $a->{branchname} cmp $b->{branchname} } values %$br,
    );
    for (@branches) {
        $_->{selected} = "selected" if ($branch eq $_->{branchcode});
    }
    my $branch_name = exists($br->{$branch}) && $br->{$branch}->{branchname};

    my @categories = (
        C4::Category->all
    );
    my @item_types = map { br($_, 'description') } (
        C4::ItemType->all
    );
    my $grid_checkout = $preferences->grid({ branchcode => $branch, notification => 'CHECKOUT' });
    my $grid_checkin = $preferences->grid({ branchcode => $branch, notification => 'CHECKIN' });

    $template->param(branch => $branch);
    $template->param(branch_name => $branch_name || 'Default');
    $template->param(branches => \@branches);
    $template->param(categories => \@categories);
    $template->param(item_types => \@item_types);
    $template->param(grid_checkout => $grid_checkout);
    $template->param(grid_checkin => $grid_checkin);

    output_html_with_http_headers $input, $cookie, $template->output;
}

# toggle a preference via ajax
sub toggle {
    my ($input) = @_;
    my $id = $input->param('id');
    my $branch = $input->param('branch');
    my ($category, $item_type, $notification) = split('-', $id);
    $category =~ s/_/*/;
    $item_type =~ s/_/*/;

    my $settings = {
        branchcode => $branch,
        categorycode => $category,
        item_type => $item_type,
        notification => $notification,
    };

    my $restrictions = $preferences; # all the same thing...
    my $notifications = $preferences; #
    if ($notifications->is_enabled_for($settings)) {
        # toggle by adding a restriction
        $restrictions->create($settings);
    } else {
        # toggle by removing the restriction
        $restrictions->delete($settings);
    }

    my $response = { success => 1 };
    my @reasons = $notifications->is_disabled_for($settings);
    if (@reasons == 0) {
        $response->{classes} = '';
    } else {
        my $default_exists = grep { $_->{branchcode} eq '*' } @reasons;
        my $non_default_also = grep { $_->{branchcode} ne '*' } @reasons;
        my @classes;
        push @classes, 'default' if $default_exists;
        push @classes, 'disabled' if $non_default_also;
        $response->{classes} = join(' ', @classes);
    }
    print $input->header;
    print encode_json($response);
}

# dispatch to various actions based on CGI parameter 'action'
sub dispatch {
    my %handler = (
        show => \&show,
        toggle => \&toggle,
    );
    my $input = new CGI;
    my $action = $input->param('action') || 'show';
    if (not exists $handler{$action}) {
        my $status = 400;
        print $input->header(-status => $status);
        print $input->div(
            $input->h1($status),
            $input->p("$action is not supported.")
        );
    } else {
        $handler{$action}->($input);
    }
}

# main
dispatch if $ENV{REQUEST_URI};
1;


=head1 NAME

admin/item_circulation_alerts.pl - per-branch configuration for messaging

=head1 SYNOPSIS

L<http://intranet.mydomain.com:8080/cgi-bin/koha/admin/item_circulation_alerts.pl>

=head1 DESCRIPTION

This CGI script drives an interface for configuring item circulation alerts.
If you want to prevent alerts from going out for any combination of branch,
patron category, and item type, this is where that policy would be set.

=head2 URLs


=head3 ?action=show

Display a branches item circulation alert preferences.

Parameters:

=over 2

=item branch

What branch are we looking at. If none is specified, the virtual default
branch '*' is used.

=back



=head3 ?action=toggle

Toggle a preference via AJAX

Parameters:

=over 2

=item id

The id should be string that can be split on "-" which contains:
"$categorycode-$item_type-$notification".

=item branch

Branch code to apply this preference to

=back

=cut
Something went wrong with that request. Please try again.