Bug 22284: Opac pickup_locations
[koha.git] / Koha / Libraries.pm
1 package Koha::Libraries;
2
3 # Copyright 2015 Koha Development team
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 3 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use Modern::Perl;
21
22 use Carp;
23
24 use C4::Context;
25
26 use Koha::Biblios;
27 use Koha::Database;
28 use Koha::Item::Transfer::Limits;
29 use Koha::Items;
30 use Koha::Library;
31
32 use base qw(Koha::Objects);
33
34 =head1 NAME
35
36 Koha::Libraries - Koha Library Object set class
37
38 =head1 API
39
40 =head2 Class Methods
41
42 =cut
43
44 =head3 pickup_locations
45
46 Returns available pickup locations for
47     A. a specific item
48     B. a biblio
49     C. none of the above, simply all libraries with pickup_location => 1
50
51 This method determines the pickup location by two factors:
52     1. is the library configured as pickup location
53     2. can a specific item / at least one of the items of a biblio be transferred
54        into the library
55
56 OPTIONAL PARAMETERS:
57     item   # Koha::Item object / itemnumber, find pickup locations for item
58     biblio # Koha::Biblio object / biblionumber, find pickup locations for biblio
59
60 If no parameters are given, all libraries with pickup_location => 1 are returned.
61
62 =cut
63
64 sub pickup_locations {
65     my ($self, $params) = @_;
66
67     my $item = $params->{'item'};
68     my $biblio = $params->{'biblio'};
69     my $patron = $params->{'patron'};
70
71     if ($biblio && $item) {
72         Koha::Exceptions::BadParameter->throw(
73             error => "Koha::Libraries->pickup_locations takes either 'biblio' or "
74             ." 'item' as parameter, but not both."
75         );
76     }
77     unless (! defined $patron || ref($patron) eq 'Koha::Patron') {
78         $patron = Koha::Items->find($patron);
79     }
80
81     # Select libraries that are configured as pickup locations
82     my $libraries = $self->search({
83         pickup_location => 1
84     }, {
85         order_by => ['branchname']
86     });
87
88     return $libraries->unblessed unless $item or $biblio;
89     if($item) {
90         unless (ref($item) eq 'Koha::Item') {
91             $item = Koha::Items->find($item);
92             return $libraries->unblessed unless $item;
93         }
94         return $item->pickup_locations( {patron => $patron} );
95     } else {
96         unless (ref($biblio) eq 'Koha::Biblio') {
97             $biblio = Koha::Biblios->find($biblio);
98             return $libraries->unblessed unless $biblio;
99         }
100         return $biblio->pickup_locations( {patron => $patron} );
101     }
102 }
103
104 =head3 search_filtered
105
106 =cut
107
108 sub search_filtered {
109     my ( $self, $params, $attributes ) = @_;
110
111     my @branchcodes;
112     my $userenv = C4::Context->userenv;
113     if ( $userenv and $userenv->{number} ) {
114         my $only_from_group = $params->{only_from_group};
115         if ( $only_from_group ) {
116             my $logged_in_user = Koha::Patrons->find( $userenv->{number} );
117             my @branchcodes = $logged_in_user->libraries_where_can_see_patrons;
118             $params->{branchcode} = { -in => \@branchcodes } if @branchcodes;
119         } else {
120             if ( C4::Context::only_my_library ) {
121                 $params->{branchcode} = C4::Context->userenv->{branch};
122             }
123         }
124     }
125     delete $params->{only_from_group};
126     return $self->SUPER::search( $params, $attributes );
127 }
128
129 =head3 type
130
131 =cut
132
133 sub _type {
134     return 'Branch';
135 }
136
137 sub object_class {
138     return 'Koha::Library';
139 }
140
141 1;