2b82dfc73243dd5126ca8adf6a62aba0f72c4c04
[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::Patrons;
24 use Koha::Virtualshelf;
25
26
27 use base qw(Koha::Objects);
28
29 =head1 NAME
30
31 Koha::Virtualshelf - Koha Virtualshelf Object class
32
33 =head1 API
34
35 =head2 Class methods
36
37 =head3 disown_or_delete
38
39     $lists->disown_or_delete;
40
41 This method will transfer public/shared lists to the appropriate patron or
42 just delete them if not possible.
43
44 =cut
45
46 sub disown_or_delete {
47     my ($self) = @_;
48
49     $self->_resultset->result_source->schema->txn_do(
50         sub {
51             if ( C4::Context->preference('ListOwnershipUponPatronDeletion') eq 'transfer' ) {
52                 my $new_owner;
53
54                 $new_owner = C4::Context->preference('ListOwnerDesignated')
55                   if C4::Context->preference('ListOwnerDesignated')
56                   and Koha::Patrons->find( C4::Context->preference('ListOwnerDesignated') );
57
58                 if( !$new_owner && C4::Context->userenv ) {
59                     $new_owner = C4::Context->userenv->{number};
60                 }
61
62                 while ( my $list = $self->next ) {
63                     if ( $new_owner && ( $list->is_public or $list->is_shared ) ) {
64                         $list->transfer_ownership($new_owner);
65                     } else {
66                         $list->delete;
67                     }
68                 }
69             } else {    # 'delete'
70                 $_->delete for $self->as_list;
71             }
72         }
73     );
74
75     return $self;
76 }
77
78 =head3 get_private_shelves
79
80 =cut
81
82 sub get_private_shelves {
83     my ( $self, $params ) = @_;
84     my $page = $params->{page};
85     my $rows = $params->{rows};
86     my $borrowernumber = $params->{borrowernumber} || 0;
87
88     $self->search(
89         {
90             public => 0,
91             -or => {
92                 'virtualshelfshares.borrowernumber' => $borrowernumber,
93                 'me.owner' => $borrowernumber,
94             }
95         },
96         {
97             join => [ 'virtualshelfshares' ],
98             distinct => 'shelfnumber',
99             order_by => 'shelfname',
100             ( ( $page and $rows ) ? ( page => $page, rows => $rows ) : () ),
101         }
102     );
103 }
104
105 =head3 get_public_shelves
106
107 =cut
108
109 sub get_public_shelves {
110     my ( $self, $params ) = @_;
111     my $page = $params->{page};
112     my $rows = $params->{rows};
113
114     $self->search(
115         {
116             public => 1,
117         },
118         {
119             distinct => 'shelfnumber',
120             order_by => 'shelfname',
121             ( ( $page and $rows ) ? ( page => $page, rows => $rows ) : () ),
122         }
123     );
124 }
125
126 =head3 get_some_shelves
127
128 =cut
129
130 sub get_some_shelves {
131     my ( $self, $params ) = @_;
132     my $borrowernumber = $params->{borrowernumber} || 0;
133     my $public = $params->{public} || 0;
134     my $add_allowed = $params->{add_allowed};
135
136     my @conditions;
137     my $patron;
138     my $staffuser = 0;
139     if ( $borrowernumber != 0 ) {
140         $patron = Koha::Patrons->find( $borrowernumber );
141         $staffuser = $patron->can_patron_change_staff_only_lists;
142     }
143     if ( $add_allowed ) {
144         if ( $staffuser ) {
145             push @conditions, {
146                 -or =>
147                 [
148                     {
149                         "me.owner" => $borrowernumber,
150                         "me.allow_change_from_owner" => 1,
151                     },
152                     "me.allow_change_from_others" => 1,
153                     "me.allow_change_from_staff"  => 1
154                 ]
155             };
156         } else {
157             push @conditions, {
158                 -or =>
159                 [
160                     {
161                         "me.owner" => $borrowernumber,
162                         "me.allow_change_from_owner" => 1,
163                     },
164                     "me.allow_change_from_others" => 1,
165                 ]
166             };
167         }
168     }
169     if ( !$public ) {
170         push @conditions, {
171             -or =>
172             {
173                 "virtualshelfshares.borrowernumber" => $borrowernumber,
174                 "me.owner" => $borrowernumber,
175             }
176         };
177     }
178
179     $self->search(
180         {
181             public => $public,
182             ( @conditions ? ( -and => \@conditions ) : () ),
183         },
184         {
185             join => [ 'virtualshelfshares' ],
186             distinct => 'shelfnumber',
187             order_by => { -desc => 'lastmodified' },
188         }
189     );
190 }
191
192 =head3 get_shelves_containing_record
193
194 =cut
195
196 sub get_shelves_containing_record {
197     my ( $self, $params ) = @_;
198     my $borrowernumber = $params->{borrowernumber};
199     my $biblionumber   = $params->{biblionumber};
200
201     my @conditions = ( 'virtualshelfcontents.biblionumber' => $biblionumber );
202     if ($borrowernumber) {
203         push @conditions,
204           {
205               -or => [
206                 {
207                     public => 0,
208                     -or      => {
209                         'me.owner' => $borrowernumber,
210                         -or        => {
211                             'virtualshelfshares.borrowernumber' => $borrowernumber,
212                         },
213                     }
214                 },
215                 { public => 1 },
216             ]
217           };
218     } else {
219         push @conditions, { public => 1 };
220     }
221
222     return Koha::Virtualshelves->search(
223         {
224             -and => \@conditions
225         },
226         {
227             join     => [ 'virtualshelfcontents', 'virtualshelfshares' ],
228             distinct => 'shelfnumber',
229             order_by => { -asc => 'shelfname' },
230         }
231     );
232 }
233
234 =head3 _type
235
236 =cut
237
238 sub _type {
239     return 'Virtualshelve';
240 }
241
242 =head3 object_class
243
244 =cut
245
246 sub object_class {
247     return 'Koha::Virtualshelf';
248 }
249
250 1;