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 my $shares = $self->{_result}->virtualshelfshares;
110 my $contents = $self->{_result}->virtualshelfcontents;
115 my ( $self, $key ) = @_;
117 Koha::Exceptions::Virtualshelves::InvalidKeyOnSharing->throw;
119 Koha::Virtualshelfshare->new(
121 shelfnumber => $self->shelfnumber,
123 sharedate => dt_from_string,
130 return $self->get_shares->search(
132 borrowernumber => { '!=' => undef },
138 my ( $self, $borrowernumber ) = @_;
139 return unless $borrowernumber;
140 return $self->get_shares->search(
142 borrowernumber => $borrowernumber,
148 my ( $self, $borrowernumber ) = @_;
149 my $shelves = Koha::Virtualshelfshares->search(
151 shelfnumber => $self->shelfnumber,
152 borrowernumber => $borrowernumber,
155 return 0 unless $shelves->count;
157 # Only 1 share with 1 patron can exist
158 return $shelves->next->delete;
162 my ( $self, $biblionumber, $borrowernumber ) = @_;
163 return unless $biblionumber;
164 my $already_exists = $self->get_contents->search(
166 biblionumber => $biblionumber,
169 return if $already_exists;
170 my $content = Koha::Virtualshelfcontent->new(
172 shelfnumber => $self->shelfnumber,
173 biblionumber => $biblionumber,
174 borrowernumber => $borrowernumber,
177 $self->lastmodified(dt_from_string);
184 my ( $self, $params ) = @_;
185 my $biblionumbers = $params->{biblionumbers} || [];
186 my $borrowernumber = $params->{borrowernumber};
187 return unless @$biblionumbers;
189 my $number_removed = 0;
190 for my $biblionumber ( @$biblionumbers ) {
191 if ( $self->owner == $borrowernumber or $self->allow_delete_own ) {
192 $number_removed += $self->get_contents->search(
194 biblionumber => $biblionumber,
195 borrowernumber => $borrowernumber,
199 if ( $self->allow_delete_other ) {
200 $number_removed += $self->get_contents->search(
202 biblionumber => $biblionumber,
204 # This does not make sense, but it's has been backported from DelFromShelf.
205 # Why shouldn't we allow to remove his own contribution if allow_delete_other is on?
208 '!=' => $borrowernumber,
216 return $number_removed;
220 my ( $self, $borrowernumber ) = @_;
221 return 1 if $self->category == $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::Borrowers->find( $borrowernumber );
239 return 1 if $self->category == $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
257 or $self->allow_add );
261 sub can_biblios_be_removed {
262 my ( $self, $borrowernumber ) = @_;
266 and ( $self->owner == $borrowernumber
267 or $self->allow_delete_own
268 or $self->allow_delete_other );
273 return 'Virtualshelve';