25a3b01c50805d35756092c23db11b8a7852ab6d
[koha.git] / Koha / Virtualshelves.pm
1 package Koha::Virtualshelves;
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20
21 use Koha::Database;
22
23 use Koha::Virtualshelf;
24
25 use base qw(Koha::Objects);
26
27 =head1 NAME
28
29 Koha::Virtualshelf - Koha Virtualshelf Object class
30
31 =head1 API
32
33 =head2 Class Methods
34
35 =cut
36
37 =head3 type
38
39 =cut
40
41 sub get_private_shelves {
42     my ( $self, $params ) = @_;
43     my $page = $params->{page};
44     my $rows = $params->{rows};
45     my $borrowernumber = $params->{borrowernumber} || 0;
46
47     $self->search(
48         {
49             category => 1,
50             -or => {
51                 'virtualshelfshares.borrowernumber' => $borrowernumber,
52                 'me.owner' => $borrowernumber,
53             }
54         },
55         {
56             join => [ 'virtualshelfshares' ],
57             distinct => 'shelfnumber',
58             order_by => 'shelfname',
59             ( ( $page and $rows ) ? ( page => $page, rows => $rows ) : () ),
60         }
61     );
62 }
63
64
65 sub get_public_shelves {
66     my ( $self, $params ) = @_;
67     my $page = $params->{page};
68     my $rows = $params->{rows};
69
70     $self->search(
71         {
72             category => 2,
73         },
74         {
75             distinct => 'shelfnumber',
76             order_by => 'shelfname',
77             ( ( $page and $rows ) ? ( page => $page, rows => $rows ) : () ),
78         }
79     );
80 }
81
82 sub get_some_shelves {
83     my ( $self, $params ) = @_;
84     my $borrowernumber = $params->{borrowernumber} || 0;
85     my $category = $params->{category} || 1;
86     my $add_allowed = $params->{add_allowed};
87
88     my @conditions;
89     if ( $add_allowed ) {
90         push @conditions, {
91             -or =>
92             [
93                 {
94                     "me.owner" => $borrowernumber,
95                     "me.allow_change_from_owner" => 1,
96                 },
97                 "me.allow_change_from_others" => 1,
98             ]
99         };
100     }
101     if ( $category == 1 ) {
102         push @conditions, {
103             -or =>
104             {
105                 "virtualshelfshares.borrowernumber" => $borrowernumber,
106                 "me.owner" => $borrowernumber,
107             }
108         };
109     }
110
111     $self->search(
112         {
113             category => $category,
114             ( @conditions ? ( -and => \@conditions ) : () ),
115         },
116         {
117             join => [ 'virtualshelfshares' ],
118             distinct => 'shelfnumber',
119             order_by => { -desc => 'lastmodified' },
120         }
121     );
122 }
123
124 sub get_shelves_containing_record {
125     my ( $self, $params ) = @_;
126     my $borrowernumber = $params->{borrowernumber};
127     my $biblionumber   = $params->{biblionumber};
128
129     my @conditions = ( 'virtualshelfcontents.biblionumber' => $biblionumber );
130     if ($borrowernumber) {
131         push @conditions,
132           {
133               -or => [
134                 {
135                     category => 1,
136                     -or      => {
137                         'me.owner' => $borrowernumber,
138                         -or        => {
139                             'virtualshelfshares.borrowernumber' => $borrowernumber,
140                         },
141                     }
142                 },
143                 { category => 2 },
144             ]
145           };
146     } else {
147         push @conditions, { category => 2 };
148     }
149
150     return Koha::Virtualshelves->search(
151         {
152             -and => \@conditions
153         },
154         {
155             join     => [ 'virtualshelfcontents', 'virtualshelfshares' ],
156             distinct => 'shelfnumber',
157             order_by => { -asc => 'shelfname' },
158         }
159     );
160 }
161
162 sub _type {
163     return 'Virtualshelve';
164 }
165
166 sub object_class {
167     return 'Koha::Virtualshelf';
168 }
169
170 1;