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.
23 use Koha::DateUtils qw( dt_from_string );
25 use Koha::Virtualshelfshare;
26 use Koha::Virtualshelfshares;
27 use Koha::Virtualshelfcontent;
29 use base qw(Koha::Object);
33 Koha::Virtualshelf - Koha Virtualshelf Object class
51 unless ( $self->is_shelfname_valid ) {
52 Koha::Exceptions::Virtualshelves::DuplicateObject->throw;
56 unless defined $self->allow_add;
57 $self->allow_delete_own( 1 )
58 unless defined $self->allow_delete_own;
59 $self->allow_delete_other( 0 )
60 unless defined $self->allow_delete_other;
62 $self->created_on( dt_from_string );
64 return $self->SUPER::store( $self );
67 sub is_shelfname_valid {
71 shelfname => $self->shelfname,
72 ( $self->shelfnumber ? ( "me.shelfnumber" => { '!=', $self->shelfnumber } ) : () ),
75 if ( $self->category == $PRIVATE and defined $self->owner ) {
76 $conditions->{-or} = {
77 "virtualshelfshares.borrowernumber" => $self->owner,
78 "me.owner" => $self->owner,
80 $conditions->{category} = $PRIVATE;
82 elsif ( $self->category == $PRIVATE and not defined $self->owner ) {
83 $conditions->{owner} = undef;
84 $conditions->{category} = $PRIVATE;
87 $conditions->{category} = $PUBLIC;
90 my $count = Koha::Virtualshelves->search(
93 join => 'virtualshelfshares',
96 return $count ? 0 : 1;
101 return $self->{_result}->virtualshelfshares;
106 return $self->{_result}->virtualshelfcontents;
110 my ( $self, $key ) = @_;
112 Koha::Exceptions::Virtualshelves::InvalidKeyOnSharing->throw;
114 Koha::Virtualshelfshare->new(
116 shelfnumber => $self->shelfnumber,
118 sharedate => dt_from_string,
125 return $self->get_shares->search(
127 borrowernumber => { '!=' => undef },
133 my ( $self, $borrowernumber ) = @_;
134 my $shelves = Koha::Virtualshelfshares->search(
136 shelfnumber => $self->shelfnumber,
137 borrowernumber => $borrowernumber,
140 return 0 unless $shelves->count;
142 # Only 1 share with 1 patron can exist
143 return $shelves->next->delete;
147 my ( $self, $biblionumber, $borrowernumber ) = @_;
148 return unless $biblionumber;
149 my $already_exists = $self->get_contents->search(
151 biblionumber => $biblionumber,
154 return if $already_exists;
155 my $content = Koha::Virtualshelfcontent->new(
157 shelfnumber => $self->shelfnumber,
158 biblionumber => $biblionumber,
159 borrowernumber => $borrowernumber,
162 $self->lastmodified(dt_from_string);
169 my ( $self, $params ) = @_;
170 my $biblionumbers = $params->{biblionumbers} || [];
171 my $borrowernumber = $params->{borrowernumber};
172 return unless @$biblionumbers;
174 my $number_removed = 0;
175 for my $biblionumber ( @$biblionumbers ) {
176 if ( $self->owner == $borrowernumber or $self->allow_delete_own ) {
177 $number_removed += $self->get_contents->search(
179 biblionumber => $biblionumber,
180 borrowernumber => $borrowernumber,
184 if ( $self->allow_delete_other ) {
185 $number_removed += $self->get_contents->search(
187 biblionumber => $biblionumber,
189 # This does not make sense, but it's has been backported from DelFromShelf.
190 # Why shouldn't we allow to remove his own contribution if allow_delete_other is on?
193 '!=' => $borrowernumber,
201 return $number_removed;
205 return 'Virtualshelve';