1 package Koha::Virtualshelf;
3 # This file is part of Koha.
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.
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.
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>.
25 use Koha::DateUtils qw( dt_from_string );
27 use Koha::Exceptions::Virtualshelf;
28 use Koha::Virtualshelfshare;
29 use Koha::Virtualshelfshares;
30 use Koha::Virtualshelfcontent;
31 use Koha::Virtualshelfcontents;
33 use base qw(Koha::Object);
37 Koha::Virtualshelf - Koha Virtualshelf Object class
48 unless ( $self->owner ) {
49 Koha::Exceptions::Virtualshelf::UseDbAdminAccount->throw;
52 unless ( $self->is_shelfname_valid ) {
53 Koha::Exceptions::Virtualshelf::DuplicateObject->throw;
56 $self->allow_change_from_owner( 1 )
57 unless defined $self->allow_change_from_owner;
58 $self->allow_change_from_others( 0 )
59 unless defined $self->allow_change_from_others;
60 $self->allow_change_from_staff( 0 )
61 unless defined $self->allow_change_from_staff;
62 $self->allow_change_from_permitted_staff( 0 )
63 unless defined $self->allow_change_from_permitted_staff;
65 $self->created_on( dt_from_string )
66 unless defined $self->created_on;
68 return $self->SUPER::store( $self );
78 return !$self->public;
81 sub is_shelfname_valid {
85 shelfname => $self->shelfname,
86 ( $self->shelfnumber ? ( "me.shelfnumber" => { '!=', $self->shelfnumber } ) : () ),
89 if ( $self->is_private and defined $self->owner ) {
90 $conditions->{-or} = {
91 "virtualshelfshares.borrowernumber" => $self->owner,
92 "me.owner" => $self->owner,
94 $conditions->{public} = 0;
96 elsif ( $self->is_private and not defined $self->owner ) {
97 $conditions->{owner} = undef;
98 $conditions->{public} = 0;
101 $conditions->{public} = 1;
104 my $count = Koha::Virtualshelves->search(
107 join => 'virtualshelfshares',
110 return $count ? 0 : 1;
115 my $rs = $self->_result->virtualshelfshares;
116 my $shares = Koha::Virtualshelfshares->_new_from_dbic( $rs );
122 my $rs = $self->_result->virtualshelfcontents;
123 my $contents = Koha::Virtualshelfcontents->_new_from_dbic( $rs );
128 my ( $self, $key ) = @_;
130 Koha::Exceptions::Virtualshelf::InvalidKeyOnSharing->throw;
132 Koha::Virtualshelfshare->new(
134 shelfnumber => $self->shelfnumber,
136 sharedate => dt_from_string,
143 return $self->get_shares->search(
145 borrowernumber => { '!=' => undef },
151 my ( $self, $borrowernumber ) = @_;
152 return unless $borrowernumber;
153 return $self->get_shares->search(
155 borrowernumber => $borrowernumber,
161 my ( $self, $borrowernumber ) = @_;
162 my $shelves = Koha::Virtualshelfshares->search(
164 shelfnumber => $self->shelfnumber,
165 borrowernumber => $borrowernumber,
168 return 0 unless $shelves->count;
170 # Only 1 share with 1 patron can exist
171 return $shelves->next->delete;
175 my ( $self, $biblionumber, $borrowernumber ) = @_;
176 return unless $biblionumber;
177 my $already_exists = $self->get_contents->search(
179 biblionumber => $biblionumber,
182 return if $already_exists;
185 my $patron = Koha::Patrons->find( $borrowernumber ) or return 0;
186 return 0 unless ( $self->owner == $borrowernumber && $self->allow_change_from_owner ) || ( $self->allow_change_from_staff && $patron->can_patron_change_staff_only_lists ) || ( $self->allow_change_from_permitted_staff && $patron->can_patron_change_permitted_staff_lists ) || $self->allow_change_from_others;
188 my $content = Koha::Virtualshelfcontent->new(
190 shelfnumber => $self->shelfnumber,
191 biblionumber => $biblionumber,
192 borrowernumber => $borrowernumber,
195 $self->lastmodified(dt_from_string);
202 my ( $self, $params ) = @_;
203 my $biblionumbers = $params->{biblionumbers} || [];
204 my $borrowernumber = $params->{borrowernumber};
205 return unless @$biblionumbers;
207 my $number_removed = 0;
208 my $patron = Koha::Patrons->find( $borrowernumber ) or return 0;
209 if( ( $self->owner == $borrowernumber && $self->allow_change_from_owner )
210 || ( $self->allow_change_from_staff && $patron->can_patron_change_staff_only_lists )
211 || ( $self->allow_change_from_permitted_staff && $patron->can_patron_change_permitted_staff_lists )
212 || $self->allow_change_from_others ) {
213 $number_removed += $self->get_contents->search({
214 biblionumber => $biblionumbers,
217 return $number_removed;
221 my ( $self, $borrowernumber ) = @_;
222 return 1 if $self->is_public;
223 return 0 unless $borrowernumber;
224 return 1 if $self->owner == $borrowernumber;
225 return $self->get_shares->search(
227 borrowernumber => $borrowernumber,
233 my ( $self, $borrowernumber ) = @_;
235 return 0 unless $borrowernumber;
236 return 1 if $self->owner == $borrowernumber;
238 my $patron = Koha::Patrons->find( $borrowernumber ) or return 0;
240 return 1 if $self->is_public and C4::Auth::haspermission( $patron->userid, { lists => 'delete_public_lists' } );
246 my ( $self, $borrowernumber ) = @_;
248 if $borrowernumber and $self->owner == $borrowernumber;
250 my $patron = Koha::Patrons->find( $borrowernumber ) or return 0;
252 if $self->is_public and C4::Auth::haspermission( $patron->userid, { lists => 'edit_public_lists' } );
256 sub can_biblios_be_added {
257 my ( $self, $borrowernumber ) = @_;
259 my $patron = Koha::Patrons->find( $borrowernumber ) or return 0;
262 and ( ( $self->owner == $borrowernumber && $self->allow_change_from_owner ) or ( $self->allow_change_from_staff && $patron->can_patron_change_staff_only_lists ) or ( $self->allow_change_from_permitted_staff && $patron->can_patron_change_permitted_staff_lists ) or $self->allow_change_from_others );
266 sub can_biblios_be_removed {
267 my ( $self, $borrowernumber ) = @_;
268 return $self->can_biblios_be_added( $borrowernumber );
269 # Same answer since bug 18228
272 =head3 cannot_be_transferred
274 $shelf->cannot_be_transferred({
275 by => $p1, to => $p2, interface => opac|intranet|undef,
276 # p1 and p2 are borrowernumbers
279 This routine has two main goals:
280 [1] Decide if patron may transfer a shared list to another
282 [2] Decide if staff member may transfer a public list.
284 If you pass interface, we'll check if it supports transfer too.
285 NOTE: The explicit passing is still more reliable than via context,
286 since we could switch interface after login in the same session.
288 Returns a true value (read: error_code) when not allowed.
289 The following error codes are possible:
290 unauthorized_transfer, missing_by_parameter, new_owner_not_found,
291 new_owner_has_no_share, missing_to_parameter.
292 Otherwise returns false (zero).
296 sub cannot_be_transferred {
297 my ( $self, $params ) = @_;
298 my $to = $params->{to};
299 my $by = $params->{by};
300 my $interface = $params->{interface};
302 # Check on interface: currently we don't support transfer shared on intranet, transfer public on OPAC
304 return 'unauthorized_transfer'
305 if ( $self->public && $interface eq 'opac' ) or
306 ( $self->is_private && $interface eq 'intranet' );
307 # is_private call is enough here, get_shares tested below
310 my $shares = $self->public ? undef : $self->get_shares->search({ borrowernumber => { '!=' => undef } });
311 return 'unauthorized_transfer' if $self->is_private && !$shares->count;
314 if( $self->public ) {
315 my $by_patron = Koha::Patrons->find($by);
316 return 'unauthorized_transfer'
317 if !$by_patron || !C4::Auth::haspermission( $by_patron->userid, { lists => 'edit_public_lists' });
319 return 'unauthorized_transfer' if !$self->can_be_managed($by);
322 return 'missing_by_parameter';
326 if( !Koha::Patrons->find($to) ) {
327 return 'new_owner_not_found';
329 if( !$self->public && !$shares->search({ borrowernumber => $to })->count ) {
330 return 'new_owner_has_no_share';
333 return 'missing_to_parameter';
335 return 0; # serving as green light
338 =head3 transfer_ownership
340 $list->transfer_ownership( $patron_id );
342 This method transfers the list ownership to the passed I<$patron_id>.
346 sub transfer_ownership {
347 my ( $self, $patron_id ) = @_;
349 Koha::Exceptions::MissingParameter->throw( "Mandatory parameter 'patron' missing" )
352 $self->remove_share( $patron_id ) if $self->is_private;
353 return $self->set({ owner => $patron_id })->store;
356 =head2 Internal methods
363 return 'Virtualshelve';