Bug 29407: Make the pickup locations dropdown JS reusable
[koha.git] / Koha / Clubs.pm
1 package Koha::Clubs;
2
3 # Copyright ByWater Solutions 2014
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21
22
23 use Koha::Database;
24 use Koha::DateUtils qw( dt_from_string );
25
26 use Koha::Club;
27
28 use base qw(Koha::Objects);
29
30 =head1 NAME
31
32 Koha::Clubs - Koha Clubs Object class
33
34 This object represents a collection of clubs a patron may enroll in.
35
36 =head1 API
37
38 =head2 Class Methods
39
40 =cut
41
42 =head3 get_enrollable
43
44 =cut
45
46 sub get_enrollable {
47     my ( $self, $params ) = @_;
48
49     # We need to filter out all the already enrolled in clubs
50     my $borrower = $params->{borrower};
51     if ($borrower) {
52         delete( $params->{borrower} );
53         my @enrollments = $borrower->get_club_enrollments();
54         if (@enrollments) {
55             $params->{'me.id'} = { -not_in => [ map { $_->club()->id() } @enrollments ] };
56         }
57     }
58
59     my $dtf = Koha::Database->new->schema->storage->datetime_parser;
60
61     # Only clubs with no end date or an end date in the future can be enrolled in
62     $params->{'-and'} = [
63         -or => [
64             date_end => { '>=' => $dtf->format_datetime( dt_from_string() ) },
65             date_end => undef,
66         ],
67         -or => [
68             'me.branchcode' => $borrower->branchcode,
69             'me.branchcode' => undef,
70         ]
71     ];
72
73     my $rs = $self->_resultset()->search( $params, { prefetch => 'club_template' } );
74
75     if (wantarray) {
76         my $class = ref($self) ? ref($self) : $self;
77
78         return $class->_wrap( $rs->all() );
79
80     }
81     else {
82         my $class = ref($self) ? ref($self) : $self;
83
84         return $class->_new_from_dbic($rs);
85     }
86 }
87
88 =head3 type
89
90 =cut
91
92 sub _type {
93     return 'Club';
94 }
95
96 sub object_class {
97     return 'Koha::Club';
98 }
99
100 =head1 AUTHOR
101
102 Kyle M Hall <kyle@bywatersolutions.com>
103
104 =cut
105
106 1;