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>.
21 use C4::Auth qw( haspermission );
25 use Koha::DateUtils qw( dt_from_string );
27 use Koha::Virtualshelfshare;
28 use Koha::Virtualshelfshares;
29 use Koha::Virtualshelfcontent;
30 use Koha::Virtualshelfcontents;
32 use base qw(Koha::Object);
36 Koha::Virtualshelf - Koha Virtualshelf Object class
54 unless ( $self->owner ) {
55 Koha::Exceptions::Virtualshelves::UseDbAdminAccount->throw;
58 unless ( $self->is_shelfname_valid ) {
59 Koha::Exceptions::Virtualshelves::DuplicateObject->throw;
62 $self->allow_change_from_owner( 1 )
63 unless defined $self->allow_change_from_owner;
64 $self->allow_change_from_others( 0 )
65 unless defined $self->allow_change_from_others;
67 $self->created_on( dt_from_string )
68 unless defined $self->created_on;
70 return $self->SUPER::store( $self );
75 return $self->category == $PUBLIC;
80 return $self->category == $PRIVATE;
83 sub is_shelfname_valid {
87 shelfname => $self->shelfname,
88 ( $self->shelfnumber ? ( "me.shelfnumber" => { '!=', $self->shelfnumber } ) : () ),
91 if ( $self->is_private and defined $self->owner ) {
92 $conditions->{-or} = {
93 "virtualshelfshares.borrowernumber" => $self->owner,
94 "me.owner" => $self->owner,
96 $conditions->{category} = $PRIVATE;
98 elsif ( $self->is_private and not defined $self->owner ) {
99 $conditions->{owner} = undef;
100 $conditions->{category} = $PRIVATE;
103 $conditions->{category} = $PUBLIC;
106 my $count = Koha::Virtualshelves->search(
109 join => 'virtualshelfshares',
112 return $count ? 0 : 1;
117 my $rs = $self->{_result}->virtualshelfshares;
118 my $shares = Koha::Virtualshelfshares->_new_from_dbic( $rs );
124 my $rs = $self->{_result}->virtualshelfcontents;
125 my $contents = Koha::Virtualshelfcontents->_new_from_dbic( $rs );
130 my ( $self, $key ) = @_;
132 Koha::Exceptions::Virtualshelves::InvalidKeyOnSharing->throw;
134 Koha::Virtualshelfshare->new(
136 shelfnumber => $self->shelfnumber,
138 sharedate => dt_from_string,
145 return $self->get_shares->search(
147 borrowernumber => { '!=' => undef },
153 my ( $self, $borrowernumber ) = @_;
154 return unless $borrowernumber;
155 return $self->get_shares->search(
157 borrowernumber => $borrowernumber,
163 my ( $self, $borrowernumber ) = @_;
164 my $shelves = Koha::Virtualshelfshares->search(
166 shelfnumber => $self->shelfnumber,
167 borrowernumber => $borrowernumber,
170 return 0 unless $shelves->count;
172 # Only 1 share with 1 patron can exist
173 return $shelves->next->delete;
177 my ( $self, $biblionumber, $borrowernumber ) = @_;
178 return unless $biblionumber;
179 my $already_exists = $self->get_contents->search(
181 biblionumber => $biblionumber,
184 return if $already_exists;
187 return unless ( $self->owner == $borrowernumber && $self->allow_change_from_owner ) || $self->allow_change_from_others;
189 my $content = Koha::Virtualshelfcontent->new(
191 shelfnumber => $self->shelfnumber,
192 biblionumber => $biblionumber,
193 borrowernumber => $borrowernumber,
196 $self->lastmodified(dt_from_string);
203 my ( $self, $params ) = @_;
204 my $biblionumbers = $params->{biblionumbers} || [];
205 my $borrowernumber = $params->{borrowernumber};
206 return unless @$biblionumbers;
208 my $number_removed = 0;
209 if( ( $self->owner == $borrowernumber && $self->allow_change_from_owner )
210 || $self->allow_change_from_others ) {
211 $number_removed += $self->get_contents->search({
212 biblionumber => $biblionumbers,
215 return $number_removed;
219 my ( $self, $borrowernumber ) = @_;
220 return 1 if $self->is_public;
221 return 0 unless $borrowernumber;
222 return 1 if $self->owner == $borrowernumber;
223 return $self->get_shares->search(
225 borrowernumber => $borrowernumber,
231 my ( $self, $borrowernumber ) = @_;
233 return 0 unless $borrowernumber;
234 return 1 if $self->owner == $borrowernumber;
236 my $patron = Koha::Patrons->find( $borrowernumber );
238 return 1 if $self->is_public and C4::Auth::haspermission( $patron->userid, { lists => 'delete_public_lists' } );
244 my ( $self, $borrowernumber ) = @_;
246 if $borrowernumber and $self->owner == $borrowernumber;
250 sub can_biblios_be_added {
251 my ( $self, $borrowernumber ) = @_;
255 and ( ( $self->owner == $borrowernumber && $self->allow_change_from_owner ) or $self->allow_change_from_others );
259 sub can_biblios_be_removed {
260 my ( $self, $borrowernumber ) = @_;
261 return $self->can_biblios_be_added( $borrowernumber );
262 # Same answer since bug 18228
266 return 'Virtualshelve';