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