Bug 18228: Implement the new columns in code
[koha.git] / Koha / Virtualshelf.pm
1 package Koha::Virtualshelf;
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
13 #
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.
17
18 use Modern::Perl;
19
20 use Carp;
21
22 use C4::Auth;
23
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;
32
33 use base qw(Koha::Object);
34
35 =head1 NAME
36
37 Koha::Virtualshelf - Koha Virtualshelf Object class
38
39 =head1 API
40
41 =head2 Class Methods
42
43 =cut
44
45 =head3 type
46
47 =cut
48
49 our $PRIVATE = 1;
50 our $PUBLIC = 2;
51
52 sub store {
53     my ( $self ) = @_;
54
55     unless ( $self->owner ) {
56         Koha::Exceptions::Virtualshelves::UseDbAdminAccount->throw;
57     }
58
59     unless ( $self->is_shelfname_valid ) {
60         Koha::Exceptions::Virtualshelves::DuplicateObject->throw;
61     }
62
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;
67
68     $self->created_on( dt_from_string );
69
70     return $self->SUPER::store( $self );
71 }
72
73 sub is_public {
74     my ( $self ) = @_;
75     return $self->category == $PUBLIC;
76 }
77
78 sub is_private {
79     my ( $self ) = @_;
80     return $self->category == $PRIVATE;
81 }
82
83 sub is_shelfname_valid {
84     my ( $self ) = @_;
85
86     my $conditions = {
87         shelfname => $self->shelfname,
88         ( $self->shelfnumber ? ( "me.shelfnumber" => { '!=', $self->shelfnumber } ) : () ),
89     };
90
91     if ( $self->is_private and defined $self->owner ) {
92         $conditions->{-or} = {
93             "virtualshelfshares.borrowernumber" => $self->owner,
94             "me.owner" => $self->owner,
95         };
96         $conditions->{category} = $PRIVATE;
97     }
98     elsif ( $self->is_private and not defined $self->owner ) {
99         $conditions->{owner} = undef;
100         $conditions->{category} = $PRIVATE;
101     }
102     else {
103         $conditions->{category} = $PUBLIC;
104     }
105
106     my $count = Koha::Virtualshelves->search(
107         $conditions,
108         {
109             join => 'virtualshelfshares',
110         }
111     )->count;
112     return $count ? 0 : 1;
113 }
114
115 sub get_shares {
116     my ( $self ) = @_;
117     my $rs = $self->{_result}->virtualshelfshares;
118     my $shares = Koha::Virtualshelfshares->_new_from_dbic( $rs );
119     return $shares;
120 }
121
122 sub get_contents {
123     my ( $self ) = @_;
124     my $rs = $self->{_result}->virtualshelfcontents;
125     my $contents = Koha::Virtualshelfcontents->_new_from_dbic( $rs );
126     return $contents;
127 }
128
129 sub share {
130     my ( $self, $key ) = @_;
131     unless ( $key ) {
132         Koha::Exceptions::Virtualshelves::InvalidKeyOnSharing->throw;
133     }
134     Koha::Virtualshelfshare->new(
135         {
136             shelfnumber => $self->shelfnumber,
137             invitekey => $key,
138             sharedate => dt_from_string,
139         }
140     )->store;
141 }
142
143 sub is_shared {
144     my ( $self ) = @_;
145     return  $self->get_shares->search(
146         {
147             borrowernumber => { '!=' => undef },
148         }
149     )->count;
150 }
151
152 sub is_shared_with {
153     my ( $self, $borrowernumber ) = @_;
154     return unless $borrowernumber;
155     return  $self->get_shares->search(
156         {
157             borrowernumber => $borrowernumber,
158         }
159     )->count;
160 }
161
162 sub remove_share {
163     my ( $self, $borrowernumber ) = @_;
164     my $shelves = Koha::Virtualshelfshares->search(
165         {
166             shelfnumber => $self->shelfnumber,
167             borrowernumber => $borrowernumber,
168         }
169     );
170     return 0 unless $shelves->count;
171
172     # Only 1 share with 1 patron can exist
173     return $shelves->next->delete;
174 }
175
176 sub add_biblio {
177     my ( $self, $biblionumber, $borrowernumber ) = @_;
178     return unless $biblionumber;
179     my $already_exists = $self->get_contents->search(
180         {
181             biblionumber => $biblionumber,
182         }
183     )->count;
184     return if $already_exists;
185
186     # Check permissions
187     return unless ( $self->owner == $borrowernumber && $self->allow_change_from_owner ) || $self->allow_change_from_others;
188
189     my $content = Koha::Virtualshelfcontent->new(
190         {
191             shelfnumber => $self->shelfnumber,
192             biblionumber => $biblionumber,
193             borrowernumber => $borrowernumber,
194         }
195     )->store;
196     $self->lastmodified(dt_from_string);
197     $self->store;
198
199     return $content;
200 }
201
202 sub remove_biblios {
203     my ( $self, $params ) = @_;
204     my $biblionumbers = $params->{biblionumbers} || [];
205     my $borrowernumber = $params->{borrowernumber};
206     return unless @$biblionumbers;
207
208     my $number_removed = 0;
209     if( ( $self->owner == $borrowernumber && $self->allow_change_from_owner )
210       || $self->allow_change_from_others ) {
211         $number_removed += $self->get_contents->search({
212             biblionumber => $biblionumbers,
213         })->delete;
214     }
215     return $number_removed;
216 }
217
218 sub can_be_viewed {
219     my ( $self, $borrowernumber ) = @_;
220     return 1 if $self->is_public;
221     return 0 unless $borrowernumber;
222     return 1 if $self->owner == $borrowernumber;
223     return $self->get_shares->search(
224         {
225             borrowernumber => $borrowernumber,
226         }
227     )->count;
228 }
229
230 sub can_be_deleted {
231     my ( $self, $borrowernumber ) = @_;
232
233     return 0 unless $borrowernumber;
234     return 1 if $self->owner == $borrowernumber;
235
236     my $patron = Koha::Patrons->find( $borrowernumber );
237
238     return 1 if $self->is_public and C4::Auth::haspermission( $patron->userid, { lists => 'delete_public_lists' } );
239
240     return 0;
241 }
242
243 sub can_be_managed {
244     my ( $self, $borrowernumber ) = @_;
245     return 1
246       if $borrowernumber and $self->owner == $borrowernumber;
247     return 0;
248 }
249
250 sub can_biblios_be_added {
251     my ( $self, $borrowernumber ) = @_;
252
253     return 1
254       if $borrowernumber
255       and ( ( $self->owner == $borrowernumber && $self->allow_change_from_owner ) or $self->allow_change_from_others );
256     return 0;
257 }
258
259 sub can_biblios_be_removed {
260     my ( $self, $borrowernumber ) = @_;
261     return $self->can_biblios_be_added( $borrowernumber );
262     # Same answer since bug 18228
263 }
264
265 sub _type {
266     return 'Virtualshelve';
267 }
268
269 1;