3 # Copyright 2015 Koha Team
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
23 use C4::Auth qw( get_template_and_user );
24 use C4::Biblio qw( GetBiblioData GetFrameworkCode );
25 use C4::External::BakerTaylor qw( image_url link_url );
29 GetNormalizedOCLCNumber
33 use C4::Output qw( pagination_bar output_with_http_headers );
34 use C4::Tags qw( get_tags );
35 use C4::XSLT qw( XSLTParse4Display );
38 use Koha::Biblioitems;
39 use Koha::CirculationRules;
40 use Koha::CsvProfiles;
41 use Koha::DateUtils qw/dt_from_string/;
45 use Koha::Virtualshelfshares;
46 use Koha::Virtualshelves;
47 use Koha::RecordProcessor;
49 use constant ANYONE => 2;
50 use constant STAFF => 3;
51 use constant PERMITTED => 4;
55 my $template_name = $query->param('rss') ? "opac-shelves-rss.tt" : "opac-shelves.tt";
57 # if virtualshelves is disabled, leave immediately
58 if ( ! C4::Context->preference('virtualshelves') ) {
59 print $query->redirect("/cgi-bin/koha/errors/404.pl");
63 my $op = $query->param('op') || 'list';
64 my ( $template, $loggedinuser, $cookie );
66 if( $op eq 'view' || $op eq 'list' ){
67 ( $template, $loggedinuser, $cookie ) = get_template_and_user({
68 template_name => $template_name,
71 authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
74 ( $template, $loggedinuser, $cookie ) = get_template_and_user({
75 template_name => $template_name,
82 if (C4::Context->preference("BakerTaylorEnabled")) {
84 BakerTaylorImageURL => &image_url(),
85 BakerTaylorLinkURL => &link_url(),
89 my $referer = $query->param('referer') || $op;
91 $public = 1 if $query->param('public') && $query->param('public') == 1;
93 my ( $shelf, $shelfnumber, @messages );
95 # PART 1: Perform a few actions
96 if ( $op eq 'add_form' ) {
98 $shelf = { allow_change_from_owner => 1 };
99 } elsif ( $op eq 'edit_form' ) {
100 $shelfnumber = $query->param('shelfnumber');
101 $shelf = Koha::Virtualshelves->find($shelfnumber);
104 $public = $shelf->public;
105 my $patron = Koha::Patrons->find( $shelf->owner );
106 $template->param( owner => $patron, );
107 unless ( $shelf->can_be_managed( $loggedinuser ) ) {
108 push @messages, { type => 'error', code => 'unauthorized_on_update' };
112 push @messages, { type => 'error', code => 'does_not_exist' };
114 } elsif ( $op eq 'add' ) {
115 if ( $loggedinuser ) {
116 my $allow_changes_from = $query->param('allow_changes_from');
118 $shelf = Koha::Virtualshelf->new(
119 { shelfname => scalar $query->param('shelfname'),
120 sortfield => scalar $query->param('sortfield'),
122 allow_change_from_owner => $allow_changes_from > 0,
123 allow_change_from_others => $allow_changes_from == ANYONE,
124 allow_change_from_staff => $allow_changes_from == STAFF,
125 allow_change_from_permitted_staff => $allow_changes_from == PERMITTED,
126 owner => scalar $loggedinuser,
130 $shelfnumber = $shelf->shelfnumber;
133 push @messages, { type => 'error', code => ref($@), msg => $@ };
134 } elsif ( not $shelf ) {
135 push @messages, { type => 'error', code => 'error_on_insert' };
137 push @messages, { type => 'message', code => 'success_on_insert' };
141 push @messages, { type => 'error', code => 'unauthorized_on_insert' };
144 } elsif ( $op eq 'edit' ) {
145 $shelfnumber = $query->param('shelfnumber');
146 $shelf = Koha::Virtualshelves->find($shelfnumber);
149 my $sortfield = $query->param('sortfield');
150 $sortfield = 'title' unless grep { $_ eq $sortfield } qw( title author copyrightdate itemcallnumber dateadded );
151 if ( $shelf->can_be_managed( $loggedinuser ) ) {
152 $shelf->shelfname( scalar $query->param('shelfname') );
153 $shelf->sortfield( $sortfield );
154 my $allow_changes_from = $query->param('allow_changes_from');
155 $shelf->allow_change_from_owner( $allow_changes_from > 0 );
156 $shelf->allow_change_from_others( $allow_changes_from == ANYONE );
157 $shelf->allow_change_from_staff( $allow_changes_from == STAFF );
158 $shelf->allow_change_from_permitted_staff( $allow_changes_from == PERMITTED );
159 $shelf->public( $public );
160 eval { $shelf->store };
163 push @messages, { type => 'error', code => 'error_on_update' };
166 push @messages, { type => 'message', code => 'success_on_update' };
169 push @messages, { type => 'error', code => 'unauthorized_on_update' };
172 push @messages, { type => 'error', code => 'does_not_exist' };
174 } elsif ( $op eq 'delete' ) {
175 $shelfnumber = $query->param('shelfnumber');
176 $shelf = Koha::Virtualshelves->find($shelfnumber);
178 if ( $shelf->can_be_deleted( $loggedinuser ) ) {
179 eval { $shelf->delete; };
181 push @messages, { type => 'error', code => ref($@), msg => $@ };
183 push @messages, { type => 'message', code => 'success_on_delete' };
186 push @messages, { type => 'error', code => 'unauthorized_on_delete' };
189 push @messages, { type => 'error', code => 'does_not_exist' };
192 } elsif ( $op eq 'remove_share' ) {
193 $shelfnumber = $query->param('shelfnumber');
194 $shelf = Koha::Virtualshelves->find($shelfnumber);
196 my $removed = eval { $shelf->remove_share( $loggedinuser ); };
198 push @messages, { type => 'error', code => ref($@), msg => $@ };
199 } elsif ( $removed ) {
200 push @messages, { type => 'message', code => 'success_on_remove_share' };
202 push @messages, { type => 'error', code => 'error_on_remove_share' };
205 push @messages, { type => 'error', code => 'does_not_exist' };
209 } elsif ( $op eq 'add_biblio' ) {
210 $shelfnumber = $query->param('shelfnumber');
211 $shelf = Koha::Virtualshelves->find($shelfnumber);
213 if( my $barcode = $query->param('barcode') ) {
214 my $item = Koha::Items->find({ barcode => $barcode });
216 if ( $shelf->can_biblios_be_added( $loggedinuser ) ) {
217 my $added = eval { $shelf->add_biblio( $item->biblionumber, $loggedinuser ); };
219 push @messages, { type => 'error', code => ref($@), msg => $@ };
221 push @messages, { type => 'message', code => 'success_on_add_biblio' };
223 push @messages, { type => 'message', code => 'error_on_add_biblio' };
226 push @messages, { type => 'error', code => 'unauthorized_on_add_biblio' };
229 push @messages, { type => 'error', code => 'item_does_not_exist' };
233 push @messages, { type => 'error', code => 'does_not_exist' };
236 } elsif ( $op eq 'remove_biblios' ) {
237 $shelfnumber = $query->param('shelfnumber');
238 $shelf = Koha::Virtualshelves->find($shelfnumber);
239 my @biblionumber = $query->multi_param('biblionumber');
241 if ( $shelf->can_biblios_be_removed( $loggedinuser ) ) {
242 my $number_of_biblios_removed = eval {
243 $shelf->remove_biblios(
245 biblionumbers => \@biblionumber,
246 borrowernumber => $loggedinuser,
251 push @messages, { type => 'error', code => ref($@), msg => $@ };
252 } elsif ( $number_of_biblios_removed ) {
253 push @messages, { type => 'message', code => 'success_on_remove_biblios' };
255 push @messages, { type => 'error', code => 'no_biblio_removed' };
258 push @messages, { type => 'error', code => 'unauthorized_on_remove_biblios' };
261 push @messages, { type => 'error', code => 'does_not_exist' };
264 } elsif( $op eq 'transfer' ) {
265 $shelfnumber = $query->param('shelfnumber');
266 $shelf = Koha::Virtualshelves->find($shelfnumber) if $shelfnumber;
267 my $new_owner = $query->param('new_owner'); # borrowernumber or undef
268 my $error_code = $shelf
269 ? $shelf->cannot_be_transferred({ by => $loggedinuser, to => $new_owner, interface => 'opac' })
272 if( !$new_owner && $error_code eq 'missing_to_parameter' ) { # show transfer form
274 my $shares = $shelf->get_shares->search({ borrowernumber => { '!=' => undef } });
275 while( my $share = $shares->next ) {
276 my $email = $share->sharee->notice_email_address;
277 push @$patrons, { email => $email, borrowernumber => $share->get_column('borrowernumber') } if $email;
280 $template->param( shared_users => $patrons );
283 push @messages, { type => 'error', code => 'no_email_found' };
285 } elsif( $error_code ) {
286 push @messages, { type => 'error', code => $error_code };
288 } else { # transfer; remove new_owner from virtualshelfshares, add loggedinuser
289 $shelf->_result->result_source->schema->txn_do( sub {
290 $shelf->get_shares->search({ borrowernumber => $new_owner })->delete;
291 Koha::Virtualshelfshare->new({ shelfnumber => $shelfnumber, borrowernumber => $loggedinuser, sharedate => dt_from_string })->store;
292 $shelf->owner($new_owner)->store;
298 # PART 2: After a possible action, view one list or show a number of lists
299 if ( $op eq 'view' ) {
300 $shelfnumber ||= $query->param('shelfnumber');
301 $shelf = Koha::Virtualshelves->find($shelfnumber);
303 if ( $shelf->can_be_viewed( $loggedinuser ) ) {
304 $public = $shelf->public;
306 # Sortfield param may still include sort order with :asc or :desc, but direction overrides it
307 my( $sortfield, $direction );
308 if( $query->param('sortfield') ){
309 ( $sortfield, $direction ) = split /:/, $query->param('sortfield');
311 $sortfield = $shelf->sortfield;
314 $direction = $query->param('direction') if $query->param('direction');
315 $direction = 'asc' if !$direction or ( $direction ne 'asc' and $direction ne 'desc' );
316 $sortfield = 'title' if !$sortfield or !grep { $_ eq $sortfield } qw( title author copyrightdate itemcallnumber dateadded );
319 unless ( $query->param('print') or $query->param('rss') ) {
320 $rows = C4::Context->preference('OPACnumSearchResults') || 20;
321 $page = ( $query->param('page') ? $query->param('page') : 1 );
323 my $order_by = $sortfield eq 'itemcallnumber' ? 'items.cn_sort' : $sortfield;
324 my $contents = $shelf->get_contents->search(
327 distinct => 'biblionumber',
328 join => [ { 'biblionumber' => { 'biblioitems' => 'items' } } ],
331 order_by => { "-$direction" => $order_by },
335 # get biblionumbers stored in the cart
337 if(my $cart_list = $query->cookie('bib_list')){
338 @cart_list = split(/\//, $cart_list);
341 my $patron = Koha::Patrons->find( $loggedinuser );
343 my $categorycode; # needed for may_article_request
344 if( C4::Context->preference('ArticleRequests') ) {
345 $categorycode = $patron ? $patron->categorycode : undef;
348 my $record_processor = Koha::RecordProcessor->new({ filters => 'ViewPolicy' });
351 if( C4::Context->preference('ArticleRequests') ) {
352 $art_req_itypes = Koha::CirculationRules->guess_article_requestable_itemtypes({ $patron ? ( categorycode => $patron->categorycode ) : () });
356 while ( my $content = $contents->next ) {
357 my $biblionumber = $content->biblionumber;
358 my $this_item = GetBiblioData($biblionumber);
359 my $biblio = Koha::Biblios->find($biblionumber);
360 my $record = $biblio->metadata->record;
361 my $framework = GetFrameworkCode($biblionumber);
362 $record_processor->options(
365 frameworkcode => $framework
367 $record_processor->process($record);
369 my $marcflavour = C4::Context->preference("marcflavour");
370 my $itemtype = Koha::Biblioitems->search({ biblionumber => $content->biblionumber })->next->itemtype;
371 $itemtype = Koha::ItemTypes->find( $itemtype );
373 $this_item->{imageurl} = C4::Koha::getitemtypeimagelocation( 'opac', $itemtype->imageurl );
374 $this_item->{description} = $itemtype->description; #FIXME Should not it be translated_description?
375 $this_item->{notforloan} = $itemtype->notforloan;
377 $this_item->{'coins'} = $biblio->get_coins;
378 $this_item->{'normalized_upc'} = GetNormalizedUPC( $record, $marcflavour );
379 $this_item->{'normalized_ean'} = GetNormalizedEAN( $record, $marcflavour );
380 $this_item->{'normalized_oclc'} = GetNormalizedOCLCNumber( $record, $marcflavour );
381 $this_item->{'normalized_isbn'} = GetNormalizedISBN( undef, $record, $marcflavour );
382 # BZ17530: 'Intelligent' guess if result can be article requested
383 $this_item->{artreqpossible} = ( $art_req_itypes->{ $this_item->{itemtype} // q{} } || $art_req_itypes->{ '*' } ) ? 1 : q{};
385 unless ( defined $this_item->{size} ) {
387 #TT has problems with size
388 $this_item->{size} = q||;
391 if (C4::Context->preference('TagsEnabled') and C4::Context->preference('TagsShowOnList')) {
392 $this_item->{TagLoop} = get_tags({
393 biblionumber => $biblionumber, approved=>1, 'sort'=>'-weight',
394 limit => C4::Context->preference('TagsShowOnList'),
398 my $items = $biblio->items->filter_by_visible_in_opac({ patron => $patron });
399 my $allow_onshelf_holds;
400 while ( my $item = $items->next ) {
402 # This method must take a Koha::Items rs
403 $allow_onshelf_holds ||= Koha::CirculationRules->get_onshelfholds_policy(
404 { item => $item, patron => $patron } );
408 $this_item->{allow_onshelf_holds} = $allow_onshelf_holds;
409 $this_item->{'ITEM_RESULTS'} = $items;
412 anonymous_session => ($loggedinuser) ? 0 : 1
414 $this_item->{XSLTBloc} = XSLTParse4Display(
416 biblionumber => $biblionumber,
418 xsl_syspref => "OPACXSLTListsDisplay",
420 xslt_variables => $variables,
421 items_rs => $items->reset,
426 if ( grep {$_ eq $biblionumber} @cart_list) {
427 $this_item->{incart} = 1;
430 $this_item->{biblio_object} = $biblio;
431 $this_item->{biblionumber} = $biblionumber;
432 $this_item->{shelves} =
433 Koha::Virtualshelves->get_shelves_containing_record(
435 biblionumber => $biblionumber,
436 borrowernumber => $loggedinuser,
439 push @items_info, $this_item;
443 can_manage_shelf => $shelf->can_be_managed($loggedinuser),
444 can_delete_shelf => $shelf->can_be_deleted($loggedinuser),
445 can_remove_biblios => $shelf->can_biblios_be_removed($loggedinuser),
446 can_add_biblios => $shelf->can_biblios_be_added($loggedinuser),
447 itemsloop => \@items_info,
448 sortfield => $sortfield,
449 direction => $direction,
450 csv_profiles => Koha::CsvProfiles->search(
453 used_for => 'export_records',
459 my $pager = $contents->pager;
461 pagination_bar => pagination_bar(
462 q||, $pager->last_page - $pager->first_page + 1,
463 $page, "page", { op => 'view', shelfnumber => $shelf->shelfnumber, sortfield => $sortfield, direction => $direction, }
468 push @messages, { type => 'error', code => 'unauthorized_on_view' };
472 push @messages, { type => 'error', code => 'does_not_exist' };
474 } elsif ( $op eq 'list' ) {
476 my ( $page, $rows ) = ( $query->param('page') || 1, 20 );
478 $shelves = Koha::Virtualshelves->get_private_shelves({ page => $page, rows => $rows, borrowernumber => $loggedinuser, });
480 $shelves = Koha::Virtualshelves->get_public_shelves({ page => $page, rows => $rows, });
483 my $pager = $shelves->pager;
486 pagination_bar => pagination_bar(
487 q||, $pager->last_page - $pager->first_page + 1,
488 $page, "page", { op => 'list', public => $public, }
493 my ($staffuser, $permitteduser);
494 $staffuser = Koha::Patrons->find( $loggedinuser )->can_patron_change_staff_only_lists if $loggedinuser;
495 $permitteduser = Koha::Patrons->find( $loggedinuser )->can_patron_change_permitted_staff_lists if $loggedinuser;
501 messages => \@messages,
503 print => scalar $query->param('print') || 0,
505 staffuser => $staffuser,
506 permitteduser => $permitteduser
509 my $content_type = $query->param('rss')? 'rss' : 'html';
510 output_with_http_headers $query, $cookie, $template->output, $content_type;