Bug 15070: Update MARC21 it-IT frameworks to Update 21 (September 2015)
[koha.git] / Koha / Virtualshelf.pm
blob7ce2738a55c279a9215c7e6cfda190130f1f38e2
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::Borrowers;
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;
32 use base qw(Koha::Object);
34 =head1 NAME
36 Koha::Virtualshelf - Koha Virtualshelf Object class
38 =head1 API
40 =head2 Class Methods
42 =cut
44 =head3 type
46 =cut
48 our $PRIVATE = 1;
49 our $PUBLIC = 2;
51 sub store {
52 my ( $self ) = @_;
54 unless ( $self->owner ) {
55 Koha::Exceptions::Virtualshelves::UseDbAdminAccount->throw;
58 unless ( $self->is_shelfname_valid ) {
59 Koha::Exceptions::Virtualshelves::DuplicateObject->throw;
62 $self->allow_add( 0 )
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 {
75 my ( $self ) = @_;
77 my $conditions = {
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;
93 else {
94 $conditions->{category} = $PUBLIC;
97 my $count = Koha::Virtualshelves->search(
98 $conditions,
100 join => 'virtualshelfshares',
102 )->count;
103 return $count ? 0 : 1;
106 sub get_shares {
107 my ( $self ) = @_;
108 my $shares = $self->{_result}->virtualshelfshares;
109 return $shares;
112 sub get_contents {
113 my ( $self ) = @_;
114 my $contents = $self->{_result}->virtualshelfcontents;
115 return $contents;
118 sub share {
119 my ( $self, $key ) = @_;
120 unless ( $key ) {
121 Koha::Exceptions::Virtualshelves::InvalidKeyOnSharing->throw;
123 Koha::Virtualshelfshare->new(
125 shelfnumber => $self->shelfnumber,
126 invitekey => $key,
127 sharedate => dt_from_string,
129 )->store;
132 sub is_shared {
133 my ( $self ) = @_;
134 return $self->get_shares->search(
136 borrowernumber => { '!=' => undef },
138 )->count;
141 sub is_shared_with {
142 my ( $self, $borrowernumber ) = @_;
143 return unless $borrowernumber;
144 return $self->get_shares->search(
146 borrowernumber => $borrowernumber,
148 )->count;
151 sub remove_share {
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;
165 sub add_biblio {
166 my ( $self, $biblionumber, $borrowernumber ) = @_;
167 return unless $biblionumber;
168 my $already_exists = $self->get_contents->search(
170 biblionumber => $biblionumber,
172 )->count;
173 return if $already_exists;
174 my $content = Koha::Virtualshelfcontent->new(
176 shelfnumber => $self->shelfnumber,
177 biblionumber => $biblionumber,
178 borrowernumber => $borrowernumber,
180 )->store;
181 $self->lastmodified(dt_from_string);
182 $self->store;
184 return $content;
187 sub remove_biblios {
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,
201 )->delete;
203 if ( $self->allow_delete_other ) {
204 $number_removed += $self->get_contents->search(
206 biblionumber => $biblionumber,
207 # FIXME
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?
210 borrowernumber => {
211 -or => {
212 '!=' => $borrowernumber,
213 '=' => undef
217 )->delete;
220 return $number_removed;
223 sub can_be_viewed {
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,
232 )->count;
235 sub can_be_deleted {
236 my ( $self, $borrowernumber ) = @_;
238 return 0 unless $borrowernumber;
239 return 1 if $self->owner == $borrowernumber;
241 my $patron = Koha::Borrowers->find( $borrowernumber );
243 return 1 if $self->category == $PUBLIC and C4::Auth::haspermission( $patron->userid, { lists => 'delete_public_lists' } );
245 return 0;
248 sub can_be_managed {
249 my ( $self, $borrowernumber ) = @_;
250 return 1
251 if $borrowernumber and $self->owner == $borrowernumber;
252 return 0;
255 sub can_biblios_be_added {
256 my ( $self, $borrowernumber ) = @_;
258 return 1
259 if $borrowernumber
260 and ( $self->owner == $borrowernumber
261 or $self->allow_add );
262 return 0;
265 sub can_biblios_be_removed {
266 my ( $self, $borrowernumber ) = @_;
268 return 1
269 if $borrowernumber
270 and ( $self->owner == $borrowernumber
271 or $self->allow_delete_own
272 or $self->allow_delete_other );
273 return 0;
276 sub type {
277 return 'Virtualshelve';