Bug 29807: Make Branches plugin handle empty pickup locations list
[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::Cache::Memory::Lite;
29 use Koha::Libraries;
30
31 sub GetName {
32     my ( $self, $branchcode ) = @_;
33
34     my $memory_cache = Koha::Cache::Memory::Lite->get_instance;
35     my $cache_key    = "Library_branchname:" . $branchcode;
36     my $cached       = $memory_cache->get_from_cache($cache_key);
37     return $cached if $cached;
38
39     my $l = Koha::Libraries->find($branchcode);
40
41     my $branchname = $l ? $l->branchname : q{};
42     $memory_cache->set_in_cache( $cache_key, $branchname );
43     return $branchname;
44 }
45
46 sub GetLoggedInBranchcode {
47     my ($self) = @_;
48
49     return C4::Context::mybranch;
50 }
51
52 sub GetLoggedInBranchname {
53     my ($self) = @_;
54
55     return C4::Context->userenv ? C4::Context->userenv->{'branchname'} : q{};
56 }
57
58 sub GetURL {
59     my ( $self, $branchcode ) = @_;
60
61     unless (exists $self->{libraries}->{$branchcode} ){
62         my $l = Koha::Libraries->find($branchcode);
63         $self->{libraries}->{$branchcode} = $l if $l;
64     }
65     return $self->{libraries}->{$branchcode} ? $self->{libraries}->{$branchcode}->branchurl : q{};
66 }
67
68 sub all {
69     my ( $self, $params ) = @_;
70     my $selected = $params->{selected} // ();
71     my $unfiltered = $params->{unfiltered} || 0;
72     my $search_params = $params->{search_params} || {};
73     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
74
75     if ( !$unfiltered ) {
76         $search_params->{only_from_group} = $params->{only_from_group} || 0;
77     }
78
79     my @selected =
80       ref $selected eq 'Koha::Libraries'
81       ? $selected->get_column('branchcode')
82       : ( $selected // () );
83
84     my $libraries = $unfiltered
85       ? Koha::Libraries->search( $search_params, { order_by => ['branchname'] } )->unblessed
86       : Koha::Libraries->search_filtered( $search_params, { order_by => ['branchname'] } )->unblessed;
87
88     for my $l (@$libraries) {
89         if ( grep { $l->{branchcode} eq $_ } @selected
90             or  not @selected
91                 and not $do_not_select_my_library
92                 and C4::Context->userenv
93                 and $l->{branchcode} eq ( C4::Context->userenv->{branch} // q{} ) )
94         {
95              $l->{selected} = 1;
96         }
97     }
98
99
100     return $libraries;
101 }
102
103 sub InIndependentBranchesMode {
104     my ( $self ) = @_;
105     return ( not C4::Context->preference("IndependentBranches") or C4::Context::IsSuperLibrarian );
106 }
107
108 sub pickup_locations {
109     my ( $self, $params ) = @_;
110     my $search_params = $params->{search_params} || {};
111     my $selected      = $params->{selected};
112     my @libraries;
113
114     if ( defined $search_params->{item} || defined $search_params->{biblio} ) {
115         my $item   = $search_params->{'item'};
116         my $biblio = $search_params->{'biblio'};
117         my $patron = $search_params->{'patron'};
118
119         unless ( !defined $patron || ref($patron) eq 'Koha::Patron' ) {
120             $patron = Koha::Patrons->find($patron);
121         }
122
123         if ($item) {
124             $item = Koha::Items->find($item)
125               unless ref($item) eq 'Koha::Item';
126             @libraries = $item->pickup_locations( { patron => $patron } )->as_list
127               if defined $item;
128         } elsif ($biblio) {
129             $biblio = Koha::Biblios->find($biblio)
130               unless ref($biblio) eq 'Koha::Biblio';
131             @libraries = $biblio->pickup_locations( { patron => $patron } )->as_list
132               if defined $biblio;
133         }
134     } else {
135         @libraries = Koha::Libraries->search( { pickup_location => 1 }, { order_by => ['branchname'] } )
136           unless @libraries;
137     }
138
139     @libraries = map { $_->unblessed } @libraries;
140
141     for my $l (@libraries) {
142         if ( defined $selected and $l->{branchcode} eq $selected
143             or not defined $selected
144             and C4::Context->userenv
145             and $l->{branchcode} eq C4::Context->userenv->{branch} )
146         {
147             $l->{selected} = 1;
148         }
149     }
150
151     return \@libraries;
152 }
153
154 1;