Lines 17-23
package Koha::Virtualshelf;
Link Here
|
17 |
|
17 |
|
18 |
use Modern::Perl; |
18 |
use Modern::Perl; |
19 |
|
19 |
|
20 |
|
|
|
21 |
use C4::Auth; |
20 |
use C4::Auth; |
22 |
|
21 |
|
23 |
use Koha::Patrons; |
22 |
use Koha::Patrons; |
Lines 43-49
Koha::Virtualshelf - Koha Virtualshelf Object class
Link Here
|
43 |
=cut |
42 |
=cut |
44 |
|
43 |
|
45 |
sub store { |
44 |
sub store { |
46 |
my ( $self ) = @_; |
45 |
my ($self) = @_; |
47 |
|
46 |
|
48 |
unless ( $self->owner ) { |
47 |
unless ( $self->owner ) { |
49 |
Koha::Exceptions::Virtualshelf::UseDbAdminAccount->throw; |
48 |
Koha::Exceptions::Virtualshelf::UseDbAdminAccount->throw; |
Lines 53-85
sub store {
Link Here
|
53 |
Koha::Exceptions::Virtualshelf::DuplicateObject->throw; |
52 |
Koha::Exceptions::Virtualshelf::DuplicateObject->throw; |
54 |
} |
53 |
} |
55 |
|
54 |
|
56 |
$self->allow_change_from_owner( 1 ) |
55 |
$self->allow_change_from_owner(1) |
57 |
unless defined $self->allow_change_from_owner; |
56 |
unless defined $self->allow_change_from_owner; |
58 |
$self->allow_change_from_others( 0 ) |
57 |
$self->allow_change_from_others(0) |
59 |
unless defined $self->allow_change_from_others; |
58 |
unless defined $self->allow_change_from_others; |
60 |
$self->allow_change_from_staff( 0 ) |
59 |
$self->allow_change_from_staff(0) |
61 |
unless defined $self->allow_change_from_staff; |
60 |
unless defined $self->allow_change_from_staff; |
62 |
$self->allow_change_from_permitted_staff( 0 ) |
61 |
$self->allow_change_from_permitted_staff(0) |
63 |
unless defined $self->allow_change_from_permitted_staff; |
62 |
unless defined $self->allow_change_from_permitted_staff; |
64 |
|
63 |
|
65 |
$self->created_on( dt_from_string ) |
64 |
$self->created_on(dt_from_string) |
66 |
unless defined $self->created_on; |
65 |
unless defined $self->created_on; |
67 |
|
66 |
|
68 |
return $self->SUPER::store( $self ); |
67 |
return $self->SUPER::store($self); |
69 |
} |
68 |
} |
70 |
|
69 |
|
71 |
sub is_public { |
70 |
sub is_public { |
72 |
my ( $self ) = @_; |
71 |
my ($self) = @_; |
73 |
return $self->public; |
72 |
return $self->public; |
74 |
} |
73 |
} |
75 |
|
74 |
|
76 |
sub is_private { |
75 |
sub is_private { |
77 |
my ( $self ) = @_; |
76 |
my ($self) = @_; |
78 |
return !$self->public; |
77 |
return !$self->public; |
79 |
} |
78 |
} |
80 |
|
79 |
|
81 |
sub is_shelfname_valid { |
80 |
sub is_shelfname_valid { |
82 |
my ( $self ) = @_; |
81 |
my ($self) = @_; |
83 |
|
82 |
|
84 |
my $conditions = { |
83 |
my $conditions = { |
85 |
shelfname => $self->shelfname, |
84 |
shelfname => $self->shelfname, |
Lines 89-103
sub is_shelfname_valid {
Link Here
|
89 |
if ( $self->is_private and defined $self->owner ) { |
88 |
if ( $self->is_private and defined $self->owner ) { |
90 |
$conditions->{-or} = { |
89 |
$conditions->{-or} = { |
91 |
"virtualshelfshares.borrowernumber" => $self->owner, |
90 |
"virtualshelfshares.borrowernumber" => $self->owner, |
92 |
"me.owner" => $self->owner, |
91 |
"me.owner" => $self->owner, |
93 |
}; |
92 |
}; |
94 |
$conditions->{public} = 0; |
93 |
$conditions->{public} = 0; |
95 |
} |
94 |
} elsif ( $self->is_private and not defined $self->owner ) { |
96 |
elsif ( $self->is_private and not defined $self->owner ) { |
95 |
$conditions->{owner} = undef; |
97 |
$conditions->{owner} = undef; |
|
|
98 |
$conditions->{public} = 0; |
96 |
$conditions->{public} = 0; |
99 |
} |
97 |
} else { |
100 |
else { |
|
|
101 |
$conditions->{public} = 1; |
98 |
$conditions->{public} = 1; |
102 |
} |
99 |
} |
103 |
|
100 |
|
Lines 111-146
sub is_shelfname_valid {
Link Here
|
111 |
} |
108 |
} |
112 |
|
109 |
|
113 |
sub get_shares { |
110 |
sub get_shares { |
114 |
my ( $self ) = @_; |
111 |
my ($self) = @_; |
115 |
my $rs = $self->_result->virtualshelfshares; |
112 |
my $rs = $self->_result->virtualshelfshares; |
116 |
my $shares = Koha::Virtualshelfshares->_new_from_dbic( $rs ); |
113 |
my $shares = Koha::Virtualshelfshares->_new_from_dbic($rs); |
117 |
return $shares; |
114 |
return $shares; |
118 |
} |
115 |
} |
119 |
|
116 |
|
120 |
sub get_contents { |
117 |
sub get_contents { |
121 |
my ( $self ) = @_; |
118 |
my ($self) = @_; |
122 |
my $rs = $self->_result->virtualshelfcontents; |
119 |
my $rs = $self->_result->virtualshelfcontents; |
123 |
my $contents = Koha::Virtualshelfcontents->_new_from_dbic( $rs ); |
120 |
my $contents = Koha::Virtualshelfcontents->_new_from_dbic($rs); |
124 |
return $contents; |
121 |
return $contents; |
125 |
} |
122 |
} |
126 |
|
123 |
|
127 |
sub share { |
124 |
sub share { |
128 |
my ( $self, $key ) = @_; |
125 |
my ( $self, $key ) = @_; |
129 |
unless ( $key ) { |
126 |
unless ($key) { |
130 |
Koha::Exceptions::Virtualshelf::InvalidKeyOnSharing->throw; |
127 |
Koha::Exceptions::Virtualshelf::InvalidKeyOnSharing->throw; |
131 |
} |
128 |
} |
132 |
Koha::Virtualshelfshare->new( |
129 |
Koha::Virtualshelfshare->new( |
133 |
{ |
130 |
{ |
134 |
shelfnumber => $self->shelfnumber, |
131 |
shelfnumber => $self->shelfnumber, |
135 |
invitekey => $key, |
132 |
invitekey => $key, |
136 |
sharedate => dt_from_string, |
133 |
sharedate => dt_from_string, |
137 |
} |
134 |
} |
138 |
)->store; |
135 |
)->store; |
139 |
} |
136 |
} |
140 |
|
137 |
|
141 |
sub is_shared { |
138 |
sub is_shared { |
142 |
my ( $self ) = @_; |
139 |
my ($self) = @_; |
143 |
return $self->get_shares->search( |
140 |
return $self->get_shares->search( |
144 |
{ |
141 |
{ |
145 |
borrowernumber => { '!=' => undef }, |
142 |
borrowernumber => { '!=' => undef }, |
146 |
} |
143 |
} |
Lines 150-156
sub is_shared {
Link Here
|
150 |
sub is_shared_with { |
147 |
sub is_shared_with { |
151 |
my ( $self, $borrowernumber ) = @_; |
148 |
my ( $self, $borrowernumber ) = @_; |
152 |
return unless $borrowernumber; |
149 |
return unless $borrowernumber; |
153 |
return $self->get_shares->search( |
150 |
return $self->get_shares->search( |
154 |
{ |
151 |
{ |
155 |
borrowernumber => $borrowernumber, |
152 |
borrowernumber => $borrowernumber, |
156 |
} |
153 |
} |
Lines 161-167
sub remove_share {
Link Here
|
161 |
my ( $self, $borrowernumber ) = @_; |
158 |
my ( $self, $borrowernumber ) = @_; |
162 |
my $shelves = Koha::Virtualshelfshares->search( |
159 |
my $shelves = Koha::Virtualshelfshares->search( |
163 |
{ |
160 |
{ |
164 |
shelfnumber => $self->shelfnumber, |
161 |
shelfnumber => $self->shelfnumber, |
165 |
borrowernumber => $borrowernumber, |
162 |
borrowernumber => $borrowernumber, |
166 |
} |
163 |
} |
167 |
); |
164 |
); |
Lines 182-194
sub add_biblio {
Link Here
|
182 |
return if $already_exists; |
179 |
return if $already_exists; |
183 |
|
180 |
|
184 |
# Check permissions |
181 |
# Check permissions |
185 |
my $patron = Koha::Patrons->find( $borrowernumber ) or return 0; |
182 |
my $patron = Koha::Patrons->find($borrowernumber) or return 0; |
186 |
return 0 unless ( $self->owner == $borrowernumber && $self->allow_change_from_owner ) || ( $self->allow_change_from_staff && $patron->can_patron_change_staff_only_lists ) || ( $self->allow_change_from_permitted_staff && $patron->can_patron_change_permitted_staff_lists ) || $self->allow_change_from_others; |
183 |
return 0 |
|
|
184 |
unless ( $self->owner == $borrowernumber && $self->allow_change_from_owner ) |
185 |
|| ( $self->allow_change_from_staff && $patron->can_patron_change_staff_only_lists ) |
186 |
|| ( $self->allow_change_from_permitted_staff && $patron->can_patron_change_permitted_staff_lists ) |
187 |
|| $self->allow_change_from_others; |
187 |
|
188 |
|
188 |
my $content = Koha::Virtualshelfcontent->new( |
189 |
my $content = Koha::Virtualshelfcontent->new( |
189 |
{ |
190 |
{ |
190 |
shelfnumber => $self->shelfnumber, |
191 |
shelfnumber => $self->shelfnumber, |
191 |
biblionumber => $biblionumber, |
192 |
biblionumber => $biblionumber, |
192 |
borrowernumber => $borrowernumber, |
193 |
borrowernumber => $borrowernumber, |
193 |
} |
194 |
} |
194 |
)->store; |
195 |
)->store; |
Lines 200-218
sub add_biblio {
Link Here
|
200 |
|
201 |
|
201 |
sub remove_biblios { |
202 |
sub remove_biblios { |
202 |
my ( $self, $params ) = @_; |
203 |
my ( $self, $params ) = @_; |
203 |
my $biblionumbers = $params->{biblionumbers} || []; |
204 |
my $biblionumbers = $params->{biblionumbers} || []; |
204 |
my $borrowernumber = $params->{borrowernumber}; |
205 |
my $borrowernumber = $params->{borrowernumber}; |
205 |
return unless @$biblionumbers; |
206 |
return unless @$biblionumbers; |
206 |
|
207 |
|
207 |
my $number_removed = 0; |
208 |
my $number_removed = 0; |
208 |
my $patron = Koha::Patrons->find( $borrowernumber ) or return 0; |
209 |
my $patron = Koha::Patrons->find($borrowernumber) or return 0; |
209 |
if( ( $self->owner == $borrowernumber && $self->allow_change_from_owner ) |
210 |
if ( ( $self->owner == $borrowernumber && $self->allow_change_from_owner ) |
210 |
|| ( $self->allow_change_from_staff && $patron->can_patron_change_staff_only_lists ) |
211 |
|| ( $self->allow_change_from_staff && $patron->can_patron_change_staff_only_lists ) |
211 |
|| ( $self->allow_change_from_permitted_staff && $patron->can_patron_change_permitted_staff_lists ) |
212 |
|| ( $self->allow_change_from_permitted_staff && $patron->can_patron_change_permitted_staff_lists ) |
212 |
|| $self->allow_change_from_others ) { |
213 |
|| $self->allow_change_from_others ) |
213 |
$number_removed += $self->get_contents->search({ |
214 |
{ |
214 |
biblionumber => $biblionumbers, |
215 |
$number_removed += $self->get_contents->search( |
215 |
})->delete; |
216 |
{ |
|
|
217 |
biblionumber => $biblionumbers, |
218 |
} |
219 |
)->delete; |
216 |
} |
220 |
} |
217 |
return $number_removed; |
221 |
return $number_removed; |
218 |
} |
222 |
} |
Lines 235-241
sub can_be_deleted {
Link Here
|
235 |
return 0 unless $borrowernumber; |
239 |
return 0 unless $borrowernumber; |
236 |
return 1 if $self->owner == $borrowernumber; |
240 |
return 1 if $self->owner == $borrowernumber; |
237 |
|
241 |
|
238 |
my $patron = Koha::Patrons->find( $borrowernumber ) or return 0; |
242 |
my $patron = Koha::Patrons->find($borrowernumber) or return 0; |
239 |
|
243 |
|
240 |
return 1 if $self->is_public and C4::Auth::haspermission( $patron->userid, { lists => 'delete_public_lists' } ); |
244 |
return 1 if $self->is_public and C4::Auth::haspermission( $patron->userid, { lists => 'delete_public_lists' } ); |
241 |
|
245 |
|
Lines 245-255
sub can_be_deleted {
Link Here
|
245 |
sub can_be_managed { |
249 |
sub can_be_managed { |
246 |
my ( $self, $borrowernumber ) = @_; |
250 |
my ( $self, $borrowernumber ) = @_; |
247 |
return 1 |
251 |
return 1 |
248 |
if $borrowernumber and $self->owner == $borrowernumber; |
252 |
if $borrowernumber and $self->owner == $borrowernumber; |
249 |
|
253 |
|
250 |
my $patron = Koha::Patrons->find( $borrowernumber ) or return 0; |
254 |
my $patron = Koha::Patrons->find($borrowernumber) or return 0; |
251 |
return 1 |
255 |
return 1 |
252 |
if $self->is_public and C4::Auth::haspermission( $patron->userid, { lists => 'edit_public_lists' } ); |
256 |
if $self->is_public and C4::Auth::haspermission( $patron->userid, { lists => 'edit_public_lists' } ); |
253 |
return 0; |
257 |
return 0; |
254 |
} |
258 |
} |
255 |
|
259 |
|
Lines 271-276
sub can_biblios_be_added {
Link Here
|
271 |
sub can_biblios_be_removed { |
275 |
sub can_biblios_be_removed { |
272 |
my ( $self, $borrowernumber ) = @_; |
276 |
my ( $self, $borrowernumber ) = @_; |
273 |
return $self->can_biblios_be_added($borrowernumber); |
277 |
return $self->can_biblios_be_added($borrowernumber); |
|
|
278 |
|
274 |
# Same answer since bug 18228 |
279 |
# Same answer since bug 18228 |
275 |
} |
280 |
} |
276 |
|
281 |
|
Lines 300-325
sub can_biblios_be_removed {
Link Here
|
300 |
|
305 |
|
301 |
sub cannot_be_transferred { |
306 |
sub cannot_be_transferred { |
302 |
my ( $self, $params ) = @_; |
307 |
my ( $self, $params ) = @_; |
303 |
my $to = $params->{to}; |
308 |
my $to = $params->{to}; |
304 |
my $by = $params->{by}; |
309 |
my $by = $params->{by}; |
305 |
my $interface = $params->{interface}; |
310 |
my $interface = $params->{interface}; |
306 |
|
311 |
|
307 |
# Check on interface: currently we don't support transfer shared on intranet, transfer public on OPAC |
312 |
# Check on interface: currently we don't support transfer shared on intranet, transfer public on OPAC |
308 |
if( $interface ) { |
313 |
if ($interface) { |
309 |
return 'unauthorized_transfer' |
314 |
return 'unauthorized_transfer' |
310 |
if ( $self->public && $interface eq 'opac' ) or |
315 |
if ( $self->public && $interface eq 'opac' ) |
311 |
( $self->is_private && $interface eq 'intranet' ); |
316 |
or ( $self->is_private && $interface eq 'intranet' ); |
312 |
# is_private call is enough here, get_shares tested below |
317 |
|
|
|
318 |
# is_private call is enough here, get_shares tested below |
313 |
} |
319 |
} |
314 |
|
320 |
|
315 |
my $shares = $self->public ? undef : $self->get_shares->search({ borrowernumber => { '!=' => undef } }); |
321 |
my $shares = $self->public ? undef : $self->get_shares->search( { borrowernumber => { '!=' => undef } } ); |
316 |
return 'unauthorized_transfer' if $self->is_private && !$shares->count; |
322 |
return 'unauthorized_transfer' if $self->is_private && !$shares->count; |
317 |
|
323 |
|
318 |
if( $by ) { |
324 |
if ($by) { |
319 |
if( $self->public ) { |
325 |
if ( $self->public ) { |
320 |
my $by_patron = Koha::Patrons->find($by); |
326 |
my $by_patron = Koha::Patrons->find($by); |
321 |
return 'unauthorized_transfer' |
327 |
return 'unauthorized_transfer' |
322 |
if !$by_patron || !C4::Auth::haspermission( $by_patron->userid, { lists => 'edit_public_lists' }); |
328 |
if !$by_patron || !C4::Auth::haspermission( $by_patron->userid, { lists => 'edit_public_lists' } ); |
323 |
} else { |
329 |
} else { |
324 |
return 'unauthorized_transfer' if !$self->can_be_managed($by); |
330 |
return 'unauthorized_transfer' if !$self->can_be_managed($by); |
325 |
} |
331 |
} |
Lines 327-343
sub cannot_be_transferred {
Link Here
|
327 |
return 'missing_by_parameter'; |
333 |
return 'missing_by_parameter'; |
328 |
} |
334 |
} |
329 |
|
335 |
|
330 |
if( $to ) { |
336 |
if ($to) { |
331 |
if( !Koha::Patrons->find($to) ) { |
337 |
if ( !Koha::Patrons->find($to) ) { |
332 |
return 'new_owner_not_found'; |
338 |
return 'new_owner_not_found'; |
333 |
} |
339 |
} |
334 |
if( !$self->public && !$shares->search({ borrowernumber => $to })->count ) { |
340 |
if ( !$self->public && !$shares->search( { borrowernumber => $to } )->count ) { |
335 |
return 'new_owner_has_no_share'; |
341 |
return 'new_owner_has_no_share'; |
336 |
} |
342 |
} |
337 |
} else { |
343 |
} else { |
338 |
return 'missing_to_parameter'; |
344 |
return 'missing_to_parameter'; |
339 |
} |
345 |
} |
340 |
return 0; # serving as green light |
346 |
return 0; # serving as green light |
341 |
} |
347 |
} |
342 |
|
348 |
|
343 |
=head3 transfer_ownership |
349 |
=head3 transfer_ownership |
Lines 351-361
This method transfers the list ownership to the passed I<$patron_id>.
Link Here
|
351 |
sub transfer_ownership { |
357 |
sub transfer_ownership { |
352 |
my ( $self, $patron_id ) = @_; |
358 |
my ( $self, $patron_id ) = @_; |
353 |
|
359 |
|
354 |
Koha::Exceptions::MissingParameter->throw( "Mandatory parameter 'patron' missing" ) |
360 |
Koha::Exceptions::MissingParameter->throw("Mandatory parameter 'patron' missing") |
355 |
unless $patron_id; |
361 |
unless $patron_id; |
356 |
|
362 |
|
357 |
$self->remove_share( $patron_id ) if $self->is_private; |
363 |
$self->remove_share($patron_id) if $self->is_private; |
358 |
return $self->set({ owner => $patron_id })->store; |
364 |
return $self->set( { owner => $patron_id } )->store; |
359 |
} |
365 |
} |
360 |
|
366 |
|
361 |
=head2 Internal methods |
367 |
=head2 Internal methods |
362 |
- |
|
|