Bug 19191: Remove need for bug 19966
[koha.git] / Koha / Virtualshelf.pm
blob3992830d59785afc2dac35fabfe1e91ca7f21b9c
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
8 # version.
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.
18 use Modern::Perl;
20 use Carp;
22 use C4::Auth;
24 use Koha::Patrons;
25 use Koha::Database;
26 use Koha::DateUtils qw( dt_from_string );
27 use Koha::Exceptions;
28 use Koha::Virtualshelfshare;
29 use Koha::Virtualshelfshares;
30 use Koha::Virtualshelfcontent;
31 use Koha::Virtualshelfcontents;
33 use base qw(Koha::Object);
35 =head1 NAME
37 Koha::Virtualshelf - Koha Virtualshelf Object class
39 =head1 API
41 =head2 Class Methods
43 =cut
45 =head3 type
47 =cut
49 our $PRIVATE = 1;
50 our $PUBLIC = 2;
52 sub store {
53 my ( $self ) = @_;
55 unless ( $self->owner ) {
56 Koha::Exceptions::Virtualshelves::UseDbAdminAccount->throw;
59 unless ( $self->is_shelfname_valid ) {
60 Koha::Exceptions::Virtualshelves::DuplicateObject->throw;
63 $self->allow_change_from_owner( 1 )
64 unless defined $self->allow_change_from_owner;
65 $self->allow_change_from_others( 0 )
66 unless defined $self->allow_change_from_others;
68 $self->created_on( dt_from_string )
69 unless defined $self->created_on;
71 return $self->SUPER::store( $self );
74 sub is_public {
75 my ( $self ) = @_;
76 return $self->category == $PUBLIC;
79 sub is_private {
80 my ( $self ) = @_;
81 return $self->category == $PRIVATE;
84 sub is_shelfname_valid {
85 my ( $self ) = @_;
87 my $conditions = {
88 shelfname => $self->shelfname,
89 ( $self->shelfnumber ? ( "me.shelfnumber" => { '!=', $self->shelfnumber } ) : () ),
92 if ( $self->is_private and defined $self->owner ) {
93 $conditions->{-or} = {
94 "virtualshelfshares.borrowernumber" => $self->owner,
95 "me.owner" => $self->owner,
97 $conditions->{category} = $PRIVATE;
99 elsif ( $self->is_private and not defined $self->owner ) {
100 $conditions->{owner} = undef;
101 $conditions->{category} = $PRIVATE;
103 else {
104 $conditions->{category} = $PUBLIC;
107 my $count = Koha::Virtualshelves->search(
108 $conditions,
110 join => 'virtualshelfshares',
112 )->count;
113 return $count ? 0 : 1;
116 sub get_shares {
117 my ( $self ) = @_;
118 my $rs = $self->{_result}->virtualshelfshares;
119 my $shares = Koha::Virtualshelfshares->_new_from_dbic( $rs );
120 return $shares;
123 sub get_contents {
124 my ( $self ) = @_;
125 my $rs = $self->{_result}->virtualshelfcontents;
126 my $contents = Koha::Virtualshelfcontents->_new_from_dbic( $rs );
127 return $contents;
130 sub share {
131 my ( $self, $key ) = @_;
132 unless ( $key ) {
133 Koha::Exceptions::Virtualshelves::InvalidKeyOnSharing->throw;
135 Koha::Virtualshelfshare->new(
137 shelfnumber => $self->shelfnumber,
138 invitekey => $key,
139 sharedate => dt_from_string,
141 )->store;
144 sub is_shared {
145 my ( $self ) = @_;
146 return $self->get_shares->search(
148 borrowernumber => { '!=' => undef },
150 )->count;
153 sub is_shared_with {
154 my ( $self, $borrowernumber ) = @_;
155 return unless $borrowernumber;
156 return $self->get_shares->search(
158 borrowernumber => $borrowernumber,
160 )->count;
163 sub remove_share {
164 my ( $self, $borrowernumber ) = @_;
165 my $shelves = Koha::Virtualshelfshares->search(
167 shelfnumber => $self->shelfnumber,
168 borrowernumber => $borrowernumber,
171 return 0 unless $shelves->count;
173 # Only 1 share with 1 patron can exist
174 return $shelves->next->delete;
177 sub add_biblio {
178 my ( $self, $biblionumber, $borrowernumber ) = @_;
179 return unless $biblionumber;
180 my $already_exists = $self->get_contents->search(
182 biblionumber => $biblionumber,
184 )->count;
185 return if $already_exists;
187 # Check permissions
188 return unless ( $self->owner == $borrowernumber && $self->allow_change_from_owner ) || $self->allow_change_from_others;
190 my $content = Koha::Virtualshelfcontent->new(
192 shelfnumber => $self->shelfnumber,
193 biblionumber => $biblionumber,
194 borrowernumber => $borrowernumber,
196 )->store;
197 $self->lastmodified(dt_from_string);
198 $self->store;
200 return $content;
203 sub remove_biblios {
204 my ( $self, $params ) = @_;
205 my $biblionumbers = $params->{biblionumbers} || [];
206 my $borrowernumber = $params->{borrowernumber};
207 return unless @$biblionumbers;
209 my $number_removed = 0;
210 if( ( $self->owner == $borrowernumber && $self->allow_change_from_owner )
211 || $self->allow_change_from_others ) {
212 $number_removed += $self->get_contents->search({
213 biblionumber => $biblionumbers,
214 })->delete;
216 return $number_removed;
219 sub can_be_viewed {
220 my ( $self, $borrowernumber ) = @_;
221 return 1 if $self->is_public;
222 return 0 unless $borrowernumber;
223 return 1 if $self->owner == $borrowernumber;
224 return $self->get_shares->search(
226 borrowernumber => $borrowernumber,
228 )->count;
231 sub can_be_deleted {
232 my ( $self, $borrowernumber ) = @_;
234 return 0 unless $borrowernumber;
235 return 1 if $self->owner == $borrowernumber;
237 my $patron = Koha::Patrons->find( $borrowernumber );
239 return 1 if $self->is_public and C4::Auth::haspermission( $patron->userid, { lists => 'delete_public_lists' } );
241 return 0;
244 sub can_be_managed {
245 my ( $self, $borrowernumber ) = @_;
246 return 1
247 if $borrowernumber and $self->owner == $borrowernumber;
248 return 0;
251 sub can_biblios_be_added {
252 my ( $self, $borrowernumber ) = @_;
254 return 1
255 if $borrowernumber
256 and ( ( $self->owner == $borrowernumber && $self->allow_change_from_owner ) or $self->allow_change_from_others );
257 return 0;
260 sub can_biblios_be_removed {
261 my ( $self, $borrowernumber ) = @_;
262 return $self->can_biblios_be_added( $borrowernumber );
263 # Same answer since bug 18228
266 sub _type {
267 return 'Virtualshelve';