Bug 26922: Regression tests
[koha.git] / Koha / Template / Plugin / Branches.pm
blob80ef90e83c0fb66e46a6d2e654f97187694d6c86
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>.
21 use Modern::Perl;
23 use Template::Plugin;
24 use base qw( Template::Plugin );
26 use C4::Koha;
27 use C4::Context;
28 use Koha::Libraries;
30 sub GetName {
31 my ( $self, $branchcode ) = @_;
33 my $l = Koha::Libraries->find($branchcode);
34 return $l ? $l->branchname : q{};
37 sub GetLoggedInBranchcode {
38 my ($self) = @_;
40 return C4::Context::mybranch;
43 sub GetLoggedInBranchname {
44 my ($self) = @_;
46 return C4::Context->userenv ? C4::Context->userenv->{'branchname'} : q{};
49 sub GetURL {
50 my ( $self, $branchcode ) = @_;
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};
59 sub all {
60 my ( $self, $params ) = @_;
61 my $selected = $params->{selected};
62 my $selecteds = $params->{selecteds};
63 my $unfiltered = $params->{unfiltered} || 0;
64 my $search_params = $params->{search_params} || {};
66 if ( !$unfiltered ) {
67 $search_params->{only_from_group} = $params->{only_from_group} || 0;
70 my $libraries = $unfiltered
71 ? Koha::Libraries->search( $search_params, { order_by => ['branchname'] } )->unblessed
72 : Koha::Libraries->search_filtered( $search_params, { order_by => ['branchname'] } )->unblessed;
74 if (defined $selecteds) {
75 # For a select multiple, must be a Koha::Libraries
76 my @selected_branchcodes = $selecteds ? $selecteds->get_column( ['branchcode'] ) : ();
77 $libraries = [ map {
78 my $l = $_;
79 $l->{selected} = 1
80 if grep { $_ eq $l->{branchcode} } @selected_branchcodes;
81 $l;
82 } @$libraries ];
84 else {
85 for my $l ( @$libraries ) {
86 if ( defined $selected and $l->{branchcode} eq $selected
87 or not defined $selected and C4::Context->userenv and $l->{branchcode} eq ( C4::Context->userenv->{branch} // q{} )
88 ) {
89 $l->{selected} = 1;
94 return $libraries;
97 sub InIndependentBranchesMode {
98 my ( $self ) = @_;
99 return ( not C4::Context->preference("IndependentBranches") or C4::Context::IsSuperLibrarian );
102 sub pickup_locations {
103 my ( $self, $params ) = @_;
104 my $search_params = $params->{search_params} || {};
105 my $selected = $params->{selected};
106 my @libraries;
108 if(defined $search_params->{item} || defined $search_params->{biblio}) {
109 my $item = $search_params->{'item'};
110 my $biblio = $search_params->{'biblio'};
111 my $patron = $search_params->{'patron'};
113 unless (! defined $patron || ref($patron) eq 'Koha::Patron') {
114 $patron = Koha::Patrons->find($patron);
117 if ($item) {
118 $item = Koha::Items->find($item)
119 unless ref($item) eq 'Koha::Item';
120 @libraries = $item->pickup_locations( { patron => $patron } )
121 if defined $item;
123 elsif ($biblio) {
124 $biblio = Koha::Biblios->find($biblio)
125 unless ref($biblio) eq 'Koha::Biblio';
126 @libraries = $biblio->pickup_locations( { patron => $patron } )
127 if defined $biblio;
131 @libraries = Koha::Libraries->search( { pickup_location => 1 },
132 { order_by => ['branchname'] } )
133 unless @libraries;
135 @libraries = map { $_->unblessed } @libraries;
137 for my $l (@libraries) {
138 if ( defined $selected and $l->{branchcode} eq $selected
139 or not defined $selected
140 and C4::Context->userenv
141 and $l->{branchcode} eq C4::Context->userenv->{branch} )
143 $l->{selected} = 1;
147 return \@libraries;