Bug 15023: Allow patron anonymize/bulk delete tool to be limited by branch
[koha.git] / Koha / Template / Plugin / Branches.pm
1 package Koha::Template::Plugin::Branches;
2
3 # Copyright ByWater Solutions 2012
4 # Copyright BibLibre 2014
5
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20
21 use Modern::Perl;
22
23 use Template::Plugin;
24 use base qw( Template::Plugin );
25
26 use C4::Koha;
27 use C4::Context;
28
29 sub GetName {
30     my ( $self, $branchcode ) = @_;
31
32     my $query = "SELECT branchname FROM branches WHERE branchcode = ?";
33     my $sth   = C4::Context->dbh->prepare($query);
34     $sth->execute($branchcode);
35     my $b = $sth->fetchrow_hashref();
36     return $b ? $b->{'branchname'} : q{};
37 }
38
39 sub GetLoggedInBranchcode {
40     my ($self) = @_;
41
42     return C4::Context->userenv ?
43         C4::Context->userenv->{'branch'} :
44         '';
45 }
46
47 sub GetURL {
48     my ( $self, $branchcode ) = @_;
49
50     my $query = "SELECT branchurl FROM branches WHERE branchcode = ?";
51     my $sth   = C4::Context->dbh->prepare($query);
52     $sth->execute($branchcode);
53     my $b = $sth->fetchrow_hashref();
54     return $b->{branchurl};
55 }
56
57 sub OnlyMine {
58     return C4::Branch::onlymine;
59 }
60
61 sub all {
62     my ( $self, $params ) = @_;
63     my $selected = $params->{selected};
64     my $dbh = C4::Context->dbh;
65     my @params;
66     my $query = q|
67         SELECT branchcode, branchname
68         FROM branches
69     |;
70     if (    C4::Branch::onlymine
71         and C4::Context->userenv
72         and C4::Context->userenv->{branch} )
73     {
74         $query .= q| WHERE branchcode = ? |;
75         push @params, C4::Context->userenv->{branch};
76     }
77     $query .= q| ORDER BY branchname|;
78     my $branches = $dbh->selectall_arrayref( $query, { Slice => {} }, @params );
79
80     if ( $selected ) {
81         for my $branch ( @$branches ) {
82             if ( $branch->{branchcode} eq $selected ) {
83                 $branch->{selected} = 1;
84             }
85         }
86     }
87     return $branches;
88 }
89
90 1;