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