1 package Koha::Template::Plugin::Branches;
3 # Copyright ByWater Solutions 2012
4 # Copyright BibLibre 2014
6 # This file is part of Koha.
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.
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.
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>.
24 use base qw( Template::Plugin );
31 my ( $self, $branchcode ) = @_;
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{};
40 sub GetLoggedInBranchcode {
43 return C4::Context->userenv ?
44 C4::Context->userenv->{'branch'} :
49 my ( $self, $branchcode ) = @_;
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};
59 my ( $self, $params ) = @_;
60 my $selected = $params->{selected};
61 my $unfiltered = $params->{unfiltered} || 0;
62 my $search_params = $params->{search_params} || {};
65 $search_params->{only_from_group} = $params->{only_from_group} || 0;
68 my $libraries = $unfiltered
69 ? Koha::Libraries->search( $search_params, { order_by => ['branchname'] } )->unblessed
70 : Koha::Libraries->search_filtered( $search_params, { order_by => ['branchname'] } )->unblessed;
72 for my $l ( @$libraries ) {
73 if ( defined $selected and $l->{branchcode} eq $selected
74 or not defined $selected and C4::Context->userenv and $l->{branchcode} eq ( C4::Context->userenv->{branch} // q{} )
83 sub InIndependentBranchesMode {
85 return ( not C4::Context->preference("IndependentBranches") or C4::Context::IsSuperLibrarian );
88 sub pickup_locations {
89 my ( $self, $params ) = @_;
90 my $search_params = $params->{search_params} || {};
91 my $selected = $params->{selected};
94 if(defined $search_params->{item} || defined $search_params->{biblio}) {
95 my $item = $search_params->{'item'};
96 my $biblio = $search_params->{'biblio'};
97 my $patron = $search_params->{'patron'};
99 unless (! defined $patron || ref($patron) eq 'Koha::Patron') {
100 $patron = Koha::Patrons->find($patron);
104 $item = Koha::Items->find($item) unless ref($item) eq 'Koha::Item';
105 @libraries = map { $_->unblessed } $item->pickup_locations( {patron => $patron} ) if defined $item;
107 $biblio = Koha::Biblios->find($biblio) unless ref($biblio) eq 'Koha::Biblio';
108 @libraries = map { $_->unblessed } $biblio->pickup_locations( {patron => $patron} ) if defined $biblio;
113 @libraries = Koha::Libraries->pickup_locations() unless @libraries;
115 for my $l (@libraries) {
116 if ( defined $selected and $l->{branchcode} eq $selected
117 or not defined $selected
118 and C4::Context->userenv
119 and $l->{branchcode} eq C4::Context->userenv->{branch} )