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>.
26 use Koha::DateUtils qw( dt_from_string );
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
55 unless ( $self->owner ) {
56 Koha::Exceptions::Virtualshelves::UseDbAdminAccount->throw;
59 unless ( $self->is_shelfname_valid ) {
60 Koha::Exceptions::Virtualshelves::DuplicateObject->throw;
63 $self->allow_change_from_owner( 1 )
64 unless defined $self->allow_change_from_owner;
65 $self->allow_change_from_others( 0 )
66 unless defined $self->allow_change_from_others;
68 $self->created_on( dt_from_string )
69 unless defined $self->created_on;
71 return $self->SUPER::store( $self );
76 return $self->category == $PUBLIC;
81 return $self->category == $PRIVATE;
84 sub is_shelfname_valid {
88 shelfname => $self->shelfname,
89 ( $self->shelfnumber ? ( "me.shelfnumber" => { '!=', $self->shelfnumber } ) : () ),
92 if ( $self->is_private and defined $self->owner ) {
93 $conditions->{-or} = {
94 "virtualshelfshares.borrowernumber" => $self->owner,
95 "me.owner" => $self->owner,
97 $conditions->{category} = $PRIVATE;
99 elsif ( $self->is_private and not defined $self->owner ) {
100 $conditions->{owner} = undef;
101 $conditions->{category} = $PRIVATE;
104 $conditions->{category} = $PUBLIC;
107 my $count = Koha::Virtualshelves->search(
110 join => 'virtualshelfshares',
113 return $count ? 0 : 1;
118 my $rs = $self->{_result}->virtualshelfshares;
119 my $shares = Koha::Virtualshelfshares->_new_from_dbic( $rs );
125 my $rs = $self->{_result}->virtualshelfcontents;
126 my $contents = Koha::Virtualshelfcontents->_new_from_dbic( $rs );
131 my ( $self, $key ) = @_;
133 Koha::Exceptions::Virtualshelves::InvalidKeyOnSharing->throw;
135 Koha::Virtualshelfshare->new(
137 shelfnumber => $self->shelfnumber,
139 sharedate => dt_from_string,
146 return $self->get_shares->search(
148 borrowernumber => { '!=' => undef },
154 my ( $self, $borrowernumber ) = @_;
155 return unless $borrowernumber;
156 return $self->get_shares->search(
158 borrowernumber => $borrowernumber,
164 my ( $self, $borrowernumber ) = @_;
165 my $shelves = Koha::Virtualshelfshares->search(
167 shelfnumber => $self->shelfnumber,
168 borrowernumber => $borrowernumber,
171 return 0 unless $shelves->count;
173 # Only 1 share with 1 patron can exist
174 return $shelves->next->delete;
178 my ( $self, $biblionumber, $borrowernumber ) = @_;
179 return unless $biblionumber;
180 my $already_exists = $self->get_contents->search(
182 biblionumber => $biblionumber,
185 return if $already_exists;
188 return unless ( $self->owner == $borrowernumber && $self->allow_change_from_owner ) || $self->allow_change_from_others;
190 my $content = Koha::Virtualshelfcontent->new(
192 shelfnumber => $self->shelfnumber,
193 biblionumber => $biblionumber,
194 borrowernumber => $borrowernumber,
197 $self->lastmodified(dt_from_string);
204 my ( $self, $params ) = @_;
205 my $biblionumbers = $params->{biblionumbers} || [];
206 my $borrowernumber = $params->{borrowernumber};
207 return unless @$biblionumbers;
209 my $number_removed = 0;
210 if( ( $self->owner == $borrowernumber && $self->allow_change_from_owner )
211 || $self->allow_change_from_others ) {
212 $number_removed += $self->get_contents->search({
213 biblionumber => $biblionumbers,
216 return $number_removed;
220 my ( $self, $borrowernumber ) = @_;
221 return 1 if $self->is_public;
222 return 0 unless $borrowernumber;
223 return 1 if $self->owner == $borrowernumber;
224 return $self->get_shares->search(
226 borrowernumber => $borrowernumber,
232 my ( $self, $borrowernumber ) = @_;
234 return 0 unless $borrowernumber;
235 return 1 if $self->owner == $borrowernumber;
237 my $patron = Koha::Patrons->find( $borrowernumber );
239 return 1 if $self->is_public and C4::Auth::haspermission( $patron->userid, { lists => 'delete_public_lists' } );
245 my ( $self, $borrowernumber ) = @_;
247 if $borrowernumber and $self->owner == $borrowernumber;
251 sub can_biblios_be_added {
252 my ( $self, $borrowernumber ) = @_;
256 and ( ( $self->owner == $borrowernumber && $self->allow_change_from_owner ) or $self->allow_change_from_others );
260 sub can_biblios_be_removed {
261 my ( $self, $borrowernumber ) = @_;
262 return $self->can_biblios_be_added( $borrowernumber );
263 # Same answer since bug 18228
267 return 'Virtualshelve';