Bug 29407: Make the pickup locations dropdown JS reusable
[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
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20
21 use C4::Auth qw( haspermission );
22
23 use Koha::Patrons;
24 use Koha::Database;
25 use Koha::DateUtils qw( dt_from_string );
26 use Koha::Exceptions;
27 use Koha::Virtualshelfshare;
28 use Koha::Virtualshelfshares;
29 use Koha::Virtualshelfcontent;
30 use Koha::Virtualshelfcontents;
31
32 use base qw(Koha::Object);
33
34 =head1 NAME
35
36 Koha::Virtualshelf - Koha Virtualshelf Object class
37
38 =head1 API
39
40 =head2 Class methods
41
42 =cut
43
44 sub store {
45     my ( $self ) = @_;
46
47     unless ( $self->owner ) {
48         Koha::Exceptions::Virtualshelves::UseDbAdminAccount->throw;
49     }
50
51     unless ( $self->is_shelfname_valid ) {
52         Koha::Exceptions::Virtualshelves::DuplicateObject->throw;
53     }
54
55     $self->allow_change_from_owner( 1 )
56         unless defined $self->allow_change_from_owner;
57     $self->allow_change_from_others( 0 )
58         unless defined $self->allow_change_from_others;
59
60     $self->created_on( dt_from_string )
61         unless defined $self->created_on;
62
63     return $self->SUPER::store( $self );
64 }
65
66 sub is_public {
67     my ( $self ) = @_;
68     return $self->public;
69 }
70
71 sub is_private {
72     my ( $self ) = @_;
73     return !$self->public;
74 }
75
76 sub is_shelfname_valid {
77     my ( $self ) = @_;
78
79     my $conditions = {
80         shelfname => $self->shelfname,
81         ( $self->shelfnumber ? ( "me.shelfnumber" => { '!=', $self->shelfnumber } ) : () ),
82     };
83
84     if ( $self->is_private and defined $self->owner ) {
85         $conditions->{-or} = {
86             "virtualshelfshares.borrowernumber" => $self->owner,
87             "me.owner" => $self->owner,
88         };
89         $conditions->{public} = 0;
90     }
91     elsif ( $self->is_private and not defined $self->owner ) {
92         $conditions->{owner} = undef;
93         $conditions->{public} = 0;
94     }
95     else {
96         $conditions->{public} = 1;
97     }
98
99     my $count = Koha::Virtualshelves->search(
100         $conditions,
101         {
102             join => 'virtualshelfshares',
103         }
104     )->count;
105     return $count ? 0 : 1;
106 }
107
108 sub get_shares {
109     my ( $self ) = @_;
110     my $rs = $self->{_result}->virtualshelfshares;
111     my $shares = Koha::Virtualshelfshares->_new_from_dbic( $rs );
112     return $shares;
113 }
114
115 sub get_contents {
116     my ( $self ) = @_;
117     my $rs = $self->{_result}->virtualshelfcontents;
118     my $contents = Koha::Virtualshelfcontents->_new_from_dbic( $rs );
119     return $contents;
120 }
121
122 sub share {
123     my ( $self, $key ) = @_;
124     unless ( $key ) {
125         Koha::Exceptions::Virtualshelves::InvalidKeyOnSharing->throw;
126     }
127     Koha::Virtualshelfshare->new(
128         {
129             shelfnumber => $self->shelfnumber,
130             invitekey => $key,
131             sharedate => dt_from_string,
132         }
133     )->store;
134 }
135
136 sub is_shared {
137     my ( $self ) = @_;
138     return  $self->get_shares->search(
139         {
140             borrowernumber => { '!=' => undef },
141         }
142     )->count;
143 }
144
145 sub is_shared_with {
146     my ( $self, $borrowernumber ) = @_;
147     return unless $borrowernumber;
148     return  $self->get_shares->search(
149         {
150             borrowernumber => $borrowernumber,
151         }
152     )->count;
153 }
154
155 sub remove_share {
156     my ( $self, $borrowernumber ) = @_;
157     my $shelves = Koha::Virtualshelfshares->search(
158         {
159             shelfnumber => $self->shelfnumber,
160             borrowernumber => $borrowernumber,
161         }
162     );
163     return 0 unless $shelves->count;
164
165     # Only 1 share with 1 patron can exist
166     return $shelves->next->delete;
167 }
168
169 sub add_biblio {
170     my ( $self, $biblionumber, $borrowernumber ) = @_;
171     return unless $biblionumber;
172     my $already_exists = $self->get_contents->search(
173         {
174             biblionumber => $biblionumber,
175         }
176     )->count;
177     return if $already_exists;
178
179     # Check permissions
180     return unless ( $self->owner == $borrowernumber && $self->allow_change_from_owner ) || $self->allow_change_from_others;
181
182     my $content = Koha::Virtualshelfcontent->new(
183         {
184             shelfnumber => $self->shelfnumber,
185             biblionumber => $biblionumber,
186             borrowernumber => $borrowernumber,
187         }
188     )->store;
189     $self->lastmodified(dt_from_string);
190     $self->store;
191
192     return $content;
193 }
194
195 sub remove_biblios {
196     my ( $self, $params ) = @_;
197     my $biblionumbers = $params->{biblionumbers} || [];
198     my $borrowernumber = $params->{borrowernumber};
199     return unless @$biblionumbers;
200
201     my $number_removed = 0;
202     if( ( $self->owner == $borrowernumber && $self->allow_change_from_owner )
203       || $self->allow_change_from_others ) {
204         $number_removed += $self->get_contents->search({
205             biblionumber => $biblionumbers,
206         })->delete;
207     }
208     return $number_removed;
209 }
210
211 sub can_be_viewed {
212     my ( $self, $borrowernumber ) = @_;
213     return 1 if $self->is_public;
214     return 0 unless $borrowernumber;
215     return 1 if $self->owner == $borrowernumber;
216     return $self->get_shares->search(
217         {
218             borrowernumber => $borrowernumber,
219         }
220     )->count;
221 }
222
223 sub can_be_deleted {
224     my ( $self, $borrowernumber ) = @_;
225
226     return 0 unless $borrowernumber;
227     return 1 if $self->owner == $borrowernumber;
228
229     my $patron = Koha::Patrons->find( $borrowernumber );
230
231     return 1 if $self->is_public and C4::Auth::haspermission( $patron->userid, { lists => 'delete_public_lists' } );
232
233     return 0;
234 }
235
236 sub can_be_managed {
237     my ( $self, $borrowernumber ) = @_;
238     return 1
239       if $borrowernumber and $self->owner == $borrowernumber;
240     return 0;
241 }
242
243 sub can_biblios_be_added {
244     my ( $self, $borrowernumber ) = @_;
245
246     return 1
247       if $borrowernumber
248       and ( ( $self->owner == $borrowernumber && $self->allow_change_from_owner ) or $self->allow_change_from_others );
249     return 0;
250 }
251
252 sub can_biblios_be_removed {
253     my ( $self, $borrowernumber ) = @_;
254     return $self->can_biblios_be_added( $borrowernumber );
255     # Same answer since bug 18228
256 }
257
258 =head2 Internal methods
259
260 =head3 _type
261
262 =cut
263
264 sub _type {
265     return 'Virtualshelve';
266 }
267
268 1;