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;
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;
64 unless defined $self->allow_add;
65 $self->allow_delete_own( 1 )
66 unless defined $self->allow_delete_own;
67 $self->allow_delete_other( 0 )
68 unless defined $self->allow_delete_other;
70 $self->created_on( dt_from_string );
72 return $self->SUPER::store( $self );
75 sub is_shelfname_valid {
79 shelfname => $self->shelfname,
80 ( $self->shelfnumber ? ( "me.shelfnumber" => { '!=', $self->shelfnumber } ) : () ),
83 if ( $self->category == $PRIVATE and defined $self->owner ) {
84 $conditions->{-or} = {
85 "virtualshelfshares.borrowernumber" => $self->owner,
86 "me.owner" => $self->owner,
88 $conditions->{category} = $PRIVATE;
90 elsif ( $self->category == $PRIVATE and not defined $self->owner ) {
91 $conditions->{owner} = undef;
92 $conditions->{category} = $PRIVATE;
95 $conditions->{category} = $PUBLIC;
98 my $count = Koha::Virtualshelves->search(
101 join => 'virtualshelfshares',
104 return $count ? 0 : 1;
109 my $rs = $self->{_result}->virtualshelfshares;
110 my $shares = Koha::Virtualshelfshares->_new_from_dbic( $rs );
116 my $rs = $self->{_result}->virtualshelfcontents;
117 my $contents = Koha::Virtualshelfcontents->_new_from_dbic( $rs );
122 my ( $self, $key ) = @_;
124 Koha::Exceptions::Virtualshelves::InvalidKeyOnSharing->throw;
126 Koha::Virtualshelfshare->new(
128 shelfnumber => $self->shelfnumber,
130 sharedate => dt_from_string,
137 return $self->get_shares->search(
139 borrowernumber => { '!=' => undef },
145 my ( $self, $borrowernumber ) = @_;
146 return unless $borrowernumber;
147 return $self->get_shares->search(
149 borrowernumber => $borrowernumber,
155 my ( $self, $borrowernumber ) = @_;
156 my $shelves = Koha::Virtualshelfshares->search(
158 shelfnumber => $self->shelfnumber,
159 borrowernumber => $borrowernumber,
162 return 0 unless $shelves->count;
164 # Only 1 share with 1 patron can exist
165 return $shelves->next->delete;
169 my ( $self, $biblionumber, $borrowernumber ) = @_;
170 return unless $biblionumber;
171 my $already_exists = $self->get_contents->search(
173 biblionumber => $biblionumber,
176 return if $already_exists;
177 my $content = Koha::Virtualshelfcontent->new(
179 shelfnumber => $self->shelfnumber,
180 biblionumber => $biblionumber,
181 borrowernumber => $borrowernumber,
184 $self->lastmodified(dt_from_string);
191 my ( $self, $params ) = @_;
192 my $biblionumbers = $params->{biblionumbers} || [];
193 my $borrowernumber = $params->{borrowernumber};
194 return unless @$biblionumbers;
196 my $number_removed = 0;
197 for my $biblionumber ( @$biblionumbers ) {
198 if ( $self->owner == $borrowernumber or $self->allow_delete_own ) {
199 $number_removed += $self->get_contents->search(
201 biblionumber => $biblionumber,
202 borrowernumber => $borrowernumber,
206 if ( $self->allow_delete_other ) {
207 $number_removed += $self->get_contents->search(
209 biblionumber => $biblionumber,
211 # This does not make sense, but it's has been backported from DelFromShelf.
212 # Why shouldn't we allow to remove his own contribution if allow_delete_other is on?
215 '!=' => $borrowernumber,
223 return $number_removed;
227 my ( $self, $borrowernumber ) = @_;
228 return 1 if $self->category == $PUBLIC;
229 return 0 unless $borrowernumber;
230 return 1 if $self->owner == $borrowernumber;
231 return $self->get_shares->search(
233 borrowernumber => $borrowernumber,
239 my ( $self, $borrowernumber ) = @_;
241 return 0 unless $borrowernumber;
242 return 1 if $self->owner == $borrowernumber;
244 my $patron = Koha::Patrons->find( $borrowernumber );
246 return 1 if $self->category == $PUBLIC and C4::Auth::haspermission( $patron->userid, { lists => 'delete_public_lists' } );
252 my ( $self, $borrowernumber ) = @_;
254 if $borrowernumber and $self->owner == $borrowernumber;
258 sub can_biblios_be_added {
259 my ( $self, $borrowernumber ) = @_;
263 and ( $self->owner == $borrowernumber
264 or $self->allow_add );
268 sub can_biblios_be_removed {
269 my ( $self, $borrowernumber ) = @_;
273 and ( $self->owner == $borrowernumber
274 or $self->allow_delete_own
275 or $self->allow_delete_other );
280 return 'Virtualshelve';