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;
90 my $page = int( $query->param('page') || 1 );
92 $public = 1 if $query->param('public') && $query->param('public') == 1;
94 my ( $shelf, $shelfnumber, @messages );
96 # PART 1: Perform a few actions
97 if ( $op eq 'add_form' ) {
99 $shelf = { allow_change_from_owner => 1 };
100 } elsif ( $op eq 'edit_form' ) {
101 $shelfnumber = $query->param('shelfnumber');
102 $shelf = Koha::Virtualshelves->find($shelfnumber);
105 $public = $shelf->public;
106 my $patron = Koha::Patrons->find( $shelf->owner );
107 $template->param( owner => $patron, );
108 unless ( $shelf->can_be_managed( $loggedinuser ) ) {
109 push @messages, { type => 'error', code => 'unauthorized_on_update' };
113 push @messages, { type => 'error', code => 'does_not_exist' };
115 } elsif ( $op eq 'cud-add' ) {
116 if ( $loggedinuser ) {
117 my $allow_changes_from = $query->param('allow_changes_from');
119 $shelf = Koha::Virtualshelf->new(
120 { shelfname => scalar $query->param('shelfname'),
121 sortfield => scalar $query->param('sortfield'),
123 allow_change_from_owner => $allow_changes_from > 0,
124 allow_change_from_others => $allow_changes_from == ANYONE,
125 allow_change_from_staff => $allow_changes_from == STAFF,
126 allow_change_from_permitted_staff => $allow_changes_from == PERMITTED,
127 owner => scalar $loggedinuser,
131 $shelfnumber = $shelf->shelfnumber;
134 push @messages, { type => 'error', code => ref($@), msg => $@ };
135 } elsif ( not $shelf ) {
136 push @messages, { type => 'error', code => 'error_on_insert' };
138 push @messages, { type => 'message', code => 'success_on_insert' };
142 push @messages, { type => 'error', code => 'unauthorized_on_insert' };
145 } elsif ( $op eq 'cud-edit' ) {
146 $shelfnumber = $query->param('shelfnumber');
147 $shelf = Koha::Virtualshelves->find($shelfnumber);
150 my $sortfield = $query->param('sortfield');
151 $sortfield = 'title' unless grep { $_ eq $sortfield } qw( title author copyrightdate itemcallnumber dateadded );
152 if ( $shelf->can_be_managed( $loggedinuser ) ) {
153 $shelf->shelfname( scalar $query->param('shelfname') );
154 $shelf->sortfield( $sortfield );
155 my $allow_changes_from = $query->param('allow_changes_from');
156 $shelf->allow_change_from_owner( $allow_changes_from > 0 );
157 $shelf->allow_change_from_others( $allow_changes_from == ANYONE );
158 $shelf->allow_change_from_staff( $allow_changes_from == STAFF );
159 $shelf->allow_change_from_permitted_staff( $allow_changes_from == PERMITTED );
160 $shelf->public( $public );
161 eval { $shelf->store };
164 push @messages, { type => 'error', code => 'error_on_update' };
167 push @messages, { type => 'message', code => 'success_on_update' };
170 push @messages, { type => 'error', code => 'unauthorized_on_update' };
173 push @messages, { type => 'error', code => 'does_not_exist' };
175 } elsif ( $op eq 'cud-delete' ) {
176 $shelfnumber = $query->param('shelfnumber');
177 $shelf = Koha::Virtualshelves->find($shelfnumber);
179 if ( $shelf->can_be_deleted( $loggedinuser ) ) {
180 eval { $shelf->delete; };
182 push @messages, { type => 'error', code => ref($@), msg => $@ };
184 push @messages, { type => 'message', code => 'success_on_delete' };
187 push @messages, { type => 'error', code => 'unauthorized_on_delete' };
190 push @messages, { type => 'error', code => 'does_not_exist' };
193 } elsif ( $op eq 'cud-remove_share' ) {
194 $shelfnumber = $query->param('shelfnumber');
195 $shelf = Koha::Virtualshelves->find($shelfnumber);
197 my $removed = eval { $shelf->remove_share( $loggedinuser ); };
199 push @messages, { type => 'error', code => ref($@), msg => $@ };
200 } elsif ( $removed ) {
201 push @messages, { type => 'message', code => 'success_on_remove_share' };
203 push @messages, { type => 'error', code => 'error_on_remove_share' };
206 push @messages, { type => 'error', code => 'does_not_exist' };
210 } elsif ( $op eq 'cud-add_biblio' ) {
211 $shelfnumber = $query->param('shelfnumber');
212 $shelf = Koha::Virtualshelves->find($shelfnumber);
214 if( my $barcode = $query->param('barcode') ) {
215 my $item = Koha::Items->find({ barcode => $barcode });
217 if ( $shelf->can_biblios_be_added( $loggedinuser ) ) {
218 my $added = eval { $shelf->add_biblio( $item->biblionumber, $loggedinuser ); };
220 push @messages, { type => 'error', code => ref($@), msg => $@ };
222 push @messages, { type => 'message', code => 'success_on_add_biblio' };
224 push @messages, { type => 'message', code => 'error_on_add_biblio' };
227 push @messages, { type => 'error', code => 'unauthorized_on_add_biblio' };
230 push @messages, { type => 'error', code => 'item_does_not_exist' };
234 push @messages, { type => 'error', code => 'does_not_exist' };
237 } elsif ( $op eq 'cud-remove_biblios' ) {
238 $shelfnumber = $query->param('shelfnumber');
239 $shelf = Koha::Virtualshelves->find($shelfnumber);
240 my @biblionumber = $query->multi_param('biblionumber');
242 if ( $shelf->can_biblios_be_removed( $loggedinuser ) ) {
243 my $number_of_biblios_removed = eval {
244 $shelf->remove_biblios(
246 biblionumbers => \@biblionumber,
247 borrowernumber => $loggedinuser,
252 push @messages, { type => 'error', code => ref($@), msg => $@ };
253 } elsif ( $number_of_biblios_removed ) {
254 push @messages, { type => 'message', code => 'success_on_remove_biblios' };
256 push @messages, { type => 'error', code => 'no_biblio_removed' };
259 push @messages, { type => 'error', code => 'unauthorized_on_remove_biblios' };
262 push @messages, { type => 'error', code => 'does_not_exist' };
265 } elsif( $op eq 'cud-transfer' ) {
266 $shelfnumber = $query->param('shelfnumber');
267 $shelf = Koha::Virtualshelves->find($shelfnumber) if $shelfnumber;
268 my $new_owner = $query->param('new_owner'); # borrowernumber or undef
269 my $error_code = $shelf
270 ? $shelf->cannot_be_transferred({ by => $loggedinuser, to => $new_owner, interface => 'opac' })
273 if( !$new_owner && $error_code eq 'missing_to_parameter' ) { # show transfer form
275 my $shares = $shelf->get_shares->search({ borrowernumber => { '!=' => undef } });
276 while( my $share = $shares->next ) {
277 my $email = $share->sharee->notice_email_address;
278 push @$patrons, { email => $email, borrowernumber => $share->get_column('borrowernumber') } if $email;
281 $template->param( shared_users => $patrons );
282 $op = 'cud-transfer';
284 push @messages, { type => 'error', code => 'no_email_found' };
286 } elsif( $error_code ) {
287 push @messages, { type => 'error', code => $error_code };
289 } else { # transfer; remove new_owner from virtualshelfshares, add loggedinuser
290 $shelf->_result->result_source->schema->txn_do( sub {
291 $shelf->get_shares->search({ borrowernumber => $new_owner })->delete;
292 Koha::Virtualshelfshare->new({ shelfnumber => $shelfnumber, borrowernumber => $loggedinuser, sharedate => dt_from_string })->store;
293 $shelf->owner($new_owner)->store;
299 # PART 2: After a possible action, view one list or show a number of lists
300 if ( $op eq 'view' ) {
301 $shelfnumber ||= $query->param('shelfnumber');
302 $shelf = Koha::Virtualshelves->find($shelfnumber);
304 if ( $shelf->can_be_viewed( $loggedinuser ) ) {
305 $public = $shelf->public;
307 # Sortfield param may still include sort order with :asc or :desc, but direction overrides it
308 my( $sortfield, $direction );
309 if( $query->param('sortfield') ){
310 ( $sortfield, $direction ) = split /:/, $query->param('sortfield');
312 $sortfield = $shelf->sortfield;
315 $direction = $query->param('direction') if $query->param('direction');
316 $direction = 'asc' if !$direction or ( $direction ne 'asc' and $direction ne 'desc' );
317 $sortfield = 'title' if !$sortfield or !grep { $_ eq $sortfield } qw( title author copyrightdate itemcallnumber dateadded );
320 unless ( $query->param('print') or $query->param('rss') ) {
321 $rows = C4::Context->preference('OPACnumSearchResults') || 20;
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 ( $rows ) = ( 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;