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->owner ) {
55 Koha::Exceptions::Virtualshelves::UseDbAdminAccount->throw;
58 unless ( $self->is_shelfname_valid ) {
59 Koha::Exceptions::Virtualshelves::DuplicateObject->throw;
63 unless defined $self->allow_add;
64 $self->allow_delete_own( 1 )
65 unless defined $self->allow_delete_own;
66 $self->allow_delete_other( 0 )
67 unless defined $self->allow_delete_other;
69 $self->created_on( dt_from_string );
71 return $self->SUPER::store( $self );
74 sub is_shelfname_valid {
78 shelfname => $self->shelfname,
79 ( $self->shelfnumber ? ( "me.shelfnumber" => { '!=', $self->shelfnumber } ) : () ),
82 if ( $self->category == $PRIVATE and defined $self->owner ) {
83 $conditions->{-or} = {
84 "virtualshelfshares.borrowernumber" => $self->owner,
85 "me.owner" => $self->owner,
87 $conditions->{category} = $PRIVATE;
89 elsif ( $self->category == $PRIVATE and not defined $self->owner ) {
90 $conditions->{owner} = undef;
91 $conditions->{category} = $PRIVATE;
94 $conditions->{category} = $PUBLIC;
97 my $count = Koha::Virtualshelves->search(
100 join => 'virtualshelfshares',
103 return $count ? 0 : 1;
108 my $shares = $self->{_result}->virtualshelfshares;
114 my $contents = $self->{_result}->virtualshelfcontents;
119 my ( $self, $key ) = @_;
121 Koha::Exceptions::Virtualshelves::InvalidKeyOnSharing->throw;
123 Koha::Virtualshelfshare->new(
125 shelfnumber => $self->shelfnumber,
127 sharedate => dt_from_string,
134 return $self->get_shares->search(
136 borrowernumber => { '!=' => undef },
142 my ( $self, $borrowernumber ) = @_;
143 return unless $borrowernumber;
144 return $self->get_shares->search(
146 borrowernumber => $borrowernumber,
152 my ( $self, $borrowernumber ) = @_;
153 my $shelves = Koha::Virtualshelfshares->search(
155 shelfnumber => $self->shelfnumber,
156 borrowernumber => $borrowernumber,
159 return 0 unless $shelves->count;
161 # Only 1 share with 1 patron can exist
162 return $shelves->next->delete;
166 my ( $self, $biblionumber, $borrowernumber ) = @_;
167 return unless $biblionumber;
168 my $already_exists = $self->get_contents->search(
170 biblionumber => $biblionumber,
173 return if $already_exists;
174 my $content = Koha::Virtualshelfcontent->new(
176 shelfnumber => $self->shelfnumber,
177 biblionumber => $biblionumber,
178 borrowernumber => $borrowernumber,
181 $self->lastmodified(dt_from_string);
188 my ( $self, $params ) = @_;
189 my $biblionumbers = $params->{biblionumbers} || [];
190 my $borrowernumber = $params->{borrowernumber};
191 return unless @$biblionumbers;
193 my $number_removed = 0;
194 for my $biblionumber ( @$biblionumbers ) {
195 if ( $self->owner == $borrowernumber or $self->allow_delete_own ) {
196 $number_removed += $self->get_contents->search(
198 biblionumber => $biblionumber,
199 borrowernumber => $borrowernumber,
203 if ( $self->allow_delete_other ) {
204 $number_removed += $self->get_contents->search(
206 biblionumber => $biblionumber,
208 # This does not make sense, but it's has been backported from DelFromShelf.
209 # Why shouldn't we allow to remove his own contribution if allow_delete_other is on?
212 '!=' => $borrowernumber,
220 return $number_removed;
224 my ( $self, $borrowernumber ) = @_;
225 return 1 if $self->category == $PUBLIC;
226 return 0 unless $borrowernumber;
227 return 1 if $self->owner == $borrowernumber;
228 return $self->get_shares->search(
230 borrowernumber => $borrowernumber,
236 my ( $self, $borrowernumber ) = @_;
238 return 0 unless $borrowernumber;
239 return 1 if $self->owner == $borrowernumber;
241 my $patron = Koha::Patrons->find( $borrowernumber );
243 return 1 if $self->category == $PUBLIC and C4::Auth::haspermission( $patron->userid, { lists => 'delete_public_lists' } );
249 my ( $self, $borrowernumber ) = @_;
251 if $borrowernumber and $self->owner == $borrowernumber;
255 sub can_biblios_be_added {
256 my ( $self, $borrowernumber ) = @_;
260 and ( $self->owner == $borrowernumber
261 or $self->allow_add );
265 sub can_biblios_be_removed {
266 my ( $self, $borrowernumber ) = @_;
270 and ( $self->owner == $borrowernumber
271 or $self->allow_delete_own
272 or $self->allow_delete_other );
277 return 'Virtualshelve';