1 package Koha::Virtualshelf;
3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3 of the License, or (at your option) any later
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 use Koha::DateUtils qw( dt_from_string );
28 use Koha::Virtualshelfshare;
29 use Koha::Virtualshelfshares;
30 use Koha::Virtualshelfcontent;
32 use base qw(Koha::Object);
36 Koha::Virtualshelf - Koha Virtualshelf Object class
54 unless ( $self->is_shelfname_valid ) {
55 Koha::Exceptions::Virtualshelves::DuplicateObject->throw;
59 unless defined $self->allow_add;
60 $self->allow_delete_own( 1 )
61 unless defined $self->allow_delete_own;
62 $self->allow_delete_other( 0 )
63 unless defined $self->allow_delete_other;
65 $self->created_on( dt_from_string );
67 return $self->SUPER::store( $self );
70 sub is_shelfname_valid {
74 shelfname => $self->shelfname,
75 ( $self->shelfnumber ? ( "me.shelfnumber" => { '!=', $self->shelfnumber } ) : () ),
78 if ( $self->category == $PRIVATE and defined $self->owner ) {
79 $conditions->{-or} = {
80 "virtualshelfshares.borrowernumber" => $self->owner,
81 "me.owner" => $self->owner,
83 $conditions->{category} = $PRIVATE;
85 elsif ( $self->category == $PRIVATE and not defined $self->owner ) {
86 $conditions->{owner} = undef;
87 $conditions->{category} = $PRIVATE;
90 $conditions->{category} = $PUBLIC;
93 my $count = Koha::Virtualshelves->search(
96 join => 'virtualshelfshares',
99 return $count ? 0 : 1;
104 return $self->{_result}->virtualshelfshares;
109 return $self->{_result}->virtualshelfcontents;
113 my ( $self, $key ) = @_;
115 Koha::Exceptions::Virtualshelves::InvalidKeyOnSharing->throw;
117 Koha::Virtualshelfshare->new(
119 shelfnumber => $self->shelfnumber,
121 sharedate => dt_from_string,
128 return $self->get_shares->search(
130 borrowernumber => { '!=' => undef },
136 my ( $self, $borrowernumber ) = @_;
137 my $shelves = Koha::Virtualshelfshares->search(
139 shelfnumber => $self->shelfnumber,
140 borrowernumber => $borrowernumber,
143 return 0 unless $shelves->count;
145 # Only 1 share with 1 patron can exist
146 return $shelves->next->delete;
150 my ( $self, $biblionumber, $borrowernumber ) = @_;
151 return unless $biblionumber;
152 my $already_exists = $self->get_contents->search(
154 biblionumber => $biblionumber,
157 return if $already_exists;
158 my $content = Koha::Virtualshelfcontent->new(
160 shelfnumber => $self->shelfnumber,
161 biblionumber => $biblionumber,
162 borrowernumber => $borrowernumber,
165 $self->lastmodified(dt_from_string);
172 my ( $self, $params ) = @_;
173 my $biblionumbers = $params->{biblionumbers} || [];
174 my $borrowernumber = $params->{borrowernumber};
175 return unless @$biblionumbers;
177 my $number_removed = 0;
178 for my $biblionumber ( @$biblionumbers ) {
179 if ( $self->owner == $borrowernumber or $self->allow_delete_own ) {
180 $number_removed += $self->get_contents->search(
182 biblionumber => $biblionumber,
183 borrowernumber => $borrowernumber,
187 if ( $self->allow_delete_other ) {
188 $number_removed += $self->get_contents->search(
190 biblionumber => $biblionumber,
192 # This does not make sense, but it's has been backported from DelFromShelf.
193 # Why shouldn't we allow to remove his own contribution if allow_delete_other is on?
196 '!=' => $borrowernumber,
204 return $number_removed;
208 my ( $self, $borrowernumber ) = @_;
209 return 1 if $self->category == $PUBLIC;
210 return 0 unless $borrowernumber;
211 return 1 if $self->owner == $borrowernumber;
212 return $self->get_shares->search(
214 borrowernumber => $borrowernumber,
220 my ( $self, $borrowernumber ) = @_;
222 return 0 unless $borrowernumber;
223 return 1 if $self->owner == $borrowernumber;
225 my $patron = Koha::Borrowers->find( $borrowernumber );
227 return 1 if $self->category == $PUBLIC and C4::Auth::haspermission( $patron->userid, { lists => 'delete_public_lists' } );
233 my ( $self, $borrowernumber ) = @_;
235 if $borrowernumber and $self->owner == $borrowernumber;
239 sub can_biblios_be_added {
240 my ( $self, $borrowernumber ) = @_;
244 and ( $self->owner == $borrowernumber
245 or $self->allow_add );
249 sub can_biblios_be_removed {
250 my ( $self, $borrowernumber ) = @_;
254 and ( $self->owner == $borrowernumber
255 or $self->allow_delete_own
256 or $self->allow_delete_other );
261 return 'Virtualshelve';