Bug 23271: Fix Template/Plugin/Branches.t
[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 use Koha::Libraries;
29
30 sub GetName {
31     my ( $self, $branchcode ) = @_;
32
33     my $l = Koha::Libraries->find($branchcode);
34     return $l ? $l->branchname : q{};
35 }
36
37 sub GetLoggedInBranchcode {
38     my ($self) = @_;
39
40     return C4::Context::mybranch;
41 }
42
43 sub GetLoggedInBranchname {
44     my ($self) = @_;
45
46     return C4::Context->userenv ? C4::Context->userenv->{'branchname'} : q{};
47 }
48
49 sub GetURL {
50     my ( $self, $branchcode ) = @_;
51
52     my $query = "SELECT branchurl FROM branches WHERE branchcode = ?";
53     my $sth   = C4::Context->dbh->prepare($query);
54     $sth->execute($branchcode);
55     my $b = $sth->fetchrow_hashref();
56     return $b->{branchurl};
57 }
58
59 sub all {
60     my ( $self, $params ) = @_;
61     my $selected = $params->{selected} // ();
62     my $unfiltered = $params->{unfiltered} || 0;
63     my $search_params = $params->{search_params} || {};
64     my $do_not_select_my_library = $params->{do_not_select_my_library} || 0; # By default we select the library of the logged in user if no selected passed
65
66     if ( !$unfiltered ) {
67         $search_params->{only_from_group} = $params->{only_from_group} || 0;
68     }
69
70     my @selected =
71       ref $selected eq 'Koha::Libraries'
72       ? $selected->get_column('branchcode')
73       : ( $selected // () );
74
75     my $libraries = $unfiltered
76       ? Koha::Libraries->search( $search_params, { order_by => ['branchname'] } )->unblessed
77       : Koha::Libraries->search_filtered( $search_params, { order_by => ['branchname'] } )->unblessed;
78
79     for my $l (@$libraries) {
80         if ( grep { $l->{branchcode} eq $_ } @selected
81             or  not @selected
82                 and not $do_not_select_my_library
83                 and C4::Context->userenv
84                 and $l->{branchcode} eq ( C4::Context->userenv->{branch} // q{} ) )
85         {
86              $l->{selected} = 1;
87         }
88     }
89
90
91     return $libraries;
92 }
93
94 sub InIndependentBranchesMode {
95     my ( $self ) = @_;
96     return ( not C4::Context->preference("IndependentBranches") or C4::Context::IsSuperLibrarian );
97 }
98
99 sub pickup_locations {
100     my ( $self, $params ) = @_;
101     my $search_params = $params->{search_params} || {};
102     my $selected      = $params->{selected};
103     my @libraries;
104
105     if(defined $search_params->{item} || defined $search_params->{biblio}) {
106         my $item = $search_params->{'item'};
107         my $biblio = $search_params->{'biblio'};
108         my $patron = $search_params->{'patron'};
109
110         unless (! defined $patron || ref($patron) eq 'Koha::Patron') {
111             $patron = Koha::Patrons->find($patron);
112         }
113
114         if ($item) {
115             $item = Koha::Items->find($item)
116               unless ref($item) eq 'Koha::Item';
117             @libraries = $item->pickup_locations( { patron => $patron } )
118               if defined $item;
119         }
120         elsif ($biblio) {
121             $biblio = Koha::Biblios->find($biblio)
122               unless ref($biblio) eq 'Koha::Biblio';
123             @libraries = $biblio->pickup_locations( { patron => $patron } )
124               if defined $biblio;
125         }
126     }
127
128     @libraries = Koha::Libraries->search( { pickup_location => 1 },
129         { order_by => ['branchname'] } )
130       unless @libraries;
131
132     @libraries = map { $_->unblessed } @libraries;
133
134     for my $l (@libraries) {
135         if ( defined $selected and $l->{branchcode} eq $selected
136             or not defined $selected
137             and C4::Context->userenv
138             and $l->{branchcode} eq C4::Context->userenv->{branch} )
139         {
140             $l->{selected} = 1;
141         }
142     }
143
144     return \@libraries;
145 }
146
147 1;