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>.
25 use C4::External::BakerTaylor qw( image_url link_url );
30 use C4::Tags qw( get_tags );
34 use Koha::Biblioitems;
35 use Koha::CirculationRules;
36 use Koha::CsvProfiles;
40 use Koha::Virtualshelves;
41 use Koha::RecordProcessor;
43 use constant ANYONE => 2;
47 my $template_name = $query->param('rss') ? "opac-shelves-rss.tt" : "opac-shelves.tt";
49 # if virtualshelves is disabled, leave immediately
50 if ( ! C4::Context->preference('virtualshelves') ) {
51 print $query->redirect("/cgi-bin/koha/errors/404.pl");
55 my $op = $query->param('op') || 'list';
56 my ( $template, $loggedinuser, $cookie );
58 if( $op eq 'view' || $op eq 'list' ){
59 ( $template, $loggedinuser, $cookie ) = get_template_and_user({
60 template_name => $template_name,
63 authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
66 ( $template, $loggedinuser, $cookie ) = get_template_and_user({
67 template_name => $template_name,
74 if (C4::Context->preference("BakerTaylorEnabled")) {
76 BakerTaylorImageURL => &image_url(),
77 BakerTaylorLinkURL => &link_url(),
81 my $referer = $query->param('referer') || $op;
83 $category = 2 if $query->param('category') && $query->param('category') == 2;
85 my ( $shelf, $shelfnumber, @messages );
87 if ( $op eq 'add_form' ) {
89 $shelf = { allow_change_from_owner => 1 };
90 } elsif ( $op eq 'edit_form' ) {
91 $shelfnumber = $query->param('shelfnumber');
92 $shelf = Koha::Virtualshelves->find($shelfnumber);
95 $category = $shelf->category;
96 my $patron = Koha::Patrons->find( $shelf->owner );
97 $template->param( owner => $patron, );
98 unless ( $shelf->can_be_managed( $loggedinuser ) ) {
99 push @messages, { type => 'error', code => 'unauthorized_on_update' };
103 push @messages, { type => 'error', code => 'does_not_exist' };
105 } elsif ( $op eq 'add' ) {
106 if ( $loggedinuser ) {
107 my $allow_changes_from = $query->param('allow_changes_from');
109 $shelf = Koha::Virtualshelf->new(
110 { shelfname => scalar $query->param('shelfname'),
111 sortfield => scalar $query->param('sortfield'),
112 category => $category,
113 allow_change_from_owner => $allow_changes_from > 0,
114 allow_change_from_others => $allow_changes_from == ANYONE,
115 owner => scalar $loggedinuser,
119 $shelfnumber = $shelf->shelfnumber;
122 push @messages, { type => 'error', code => ref($@), msg => $@ };
123 } elsif ( not $shelf ) {
124 push @messages, { type => 'error', code => 'error_on_insert' };
126 push @messages, { type => 'message', code => 'success_on_insert' };
130 push @messages, { type => 'error', code => 'unauthorized_on_insert' };
133 } elsif ( $op eq 'edit' ) {
134 $shelfnumber = $query->param('shelfnumber');
135 $shelf = Koha::Virtualshelves->find($shelfnumber);
138 my $sortfield = $query->param('sortfield');
139 $sortfield = 'title' unless grep { $_ eq $sortfield } qw( title author copyrightdate itemcallnumber dateadded );
140 if ( $shelf->can_be_managed( $loggedinuser ) ) {
141 $shelf->shelfname( scalar $query->param('shelfname') );
142 $shelf->sortfield( $sortfield );
143 my $allow_changes_from = $query->param('allow_changes_from');
144 $shelf->allow_change_from_owner( $allow_changes_from > 0 );
145 $shelf->allow_change_from_others( $allow_changes_from == ANYONE );
146 $shelf->category( $category );
147 eval { $shelf->store };
150 push @messages, { type => 'error', code => 'error_on_update' };
153 push @messages, { type => 'message', code => 'success_on_update' };
156 push @messages, { type => 'error', code => 'unauthorized_on_update' };
159 push @messages, { type => 'error', code => 'does_not_exist' };
161 } elsif ( $op eq 'delete' ) {
162 $shelfnumber = $query->param('shelfnumber');
163 $shelf = Koha::Virtualshelves->find($shelfnumber);
165 if ( $shelf->can_be_deleted( $loggedinuser ) ) {
166 eval { $shelf->delete; };
168 push @messages, { type => 'error', code => ref($@), msg => $@ };
170 push @messages, { type => 'message', code => 'success_on_delete' };
173 push @messages, { type => 'error', code => 'unauthorized_on_delete' };
176 push @messages, { type => 'error', code => 'does_not_exist' };
179 } elsif ( $op eq 'remove_share' ) {
180 $shelfnumber = $query->param('shelfnumber');
181 $shelf = Koha::Virtualshelves->find($shelfnumber);
183 my $removed = eval { $shelf->remove_share( $loggedinuser ); };
185 push @messages, { type => 'error', code => ref($@), msg => $@ };
186 } elsif ( $removed ) {
187 push @messages, { type => 'message', code => 'success_on_remove_share' };
189 push @messages, { type => 'error', code => 'error_on_remove_share' };
192 push @messages, { type => 'error', code => 'does_not_exist' };
196 } elsif ( $op eq 'add_biblio' ) {
197 $shelfnumber = $query->param('shelfnumber');
198 $shelf = Koha::Virtualshelves->find($shelfnumber);
200 if( my $barcode = $query->param('barcode') ) {
201 my $item = Koha::Items->find({ barcode => $barcode });
203 if ( $shelf->can_biblios_be_added( $loggedinuser ) ) {
204 my $added = eval { $shelf->add_biblio( $item->biblionumber, $loggedinuser ); };
206 push @messages, { type => 'error', code => ref($@), msg => $@ };
208 push @messages, { type => 'message', code => 'success_on_add_biblio' };
210 push @messages, { type => 'message', code => 'error_on_add_biblio' };
213 push @messages, { type => 'error', code => 'unauthorized_on_add_biblio' };
216 push @messages, { type => 'error', code => 'item_does_not_exist' };
220 push @messages, { type => 'error', code => 'does_not_exist' };
223 } elsif ( $op eq 'remove_biblios' ) {
224 $shelfnumber = $query->param('shelfnumber');
225 $shelf = Koha::Virtualshelves->find($shelfnumber);
226 my @biblionumber = $query->multi_param('biblionumber');
228 if ( $shelf->can_biblios_be_removed( $loggedinuser ) ) {
229 my $number_of_biblios_removed = eval {
230 $shelf->remove_biblios(
232 biblionumbers => \@biblionumber,
233 borrowernumber => $loggedinuser,
238 push @messages, { type => 'error', code => ref($@), msg => $@ };
239 } elsif ( $number_of_biblios_removed ) {
240 push @messages, { type => 'message', code => 'success_on_remove_biblios' };
242 push @messages, { type => 'error', code => 'no_biblio_removed' };
245 push @messages, { type => 'error', code => 'unauthorized_on_remove_biblios' };
248 push @messages, { type => 'error', code => 'does_not_exist' };
253 if ( $op eq 'view' ) {
254 $shelfnumber ||= $query->param('shelfnumber');
255 $shelf = Koha::Virtualshelves->find($shelfnumber);
257 if ( $shelf->can_be_viewed( $loggedinuser ) ) {
258 $category = $shelf->category;
260 # Sortfield param may still include sort order with :asc or :desc, but direction overrides it
261 my( $sortfield, $direction );
262 if( $query->param('sortfield') ){
263 ( $sortfield, $direction ) = split /:/, $query->param('sortfield');
265 $sortfield = $shelf->sortfield;
268 $direction = $query->param('direction') if $query->param('direction');
269 $direction = 'asc' if !$direction or ( $direction ne 'asc' and $direction ne 'desc' );
270 $sortfield = 'title' if !$sortfield or !grep { $_ eq $sortfield } qw( title author copyrightdate itemcallnumber dateadded );
273 unless ( $query->param('print') or $query->param('rss') ) {
274 $rows = C4::Context->preference('OPACnumSearchResults') || 20;
275 $page = ( $query->param('page') ? $query->param('page') : 1 );
277 my $order_by = $sortfield eq 'itemcallnumber' ? 'items.cn_sort' : $sortfield;
278 my $contents = $shelf->get_contents->search(
281 distinct => 'biblionumber',
282 join => [ { 'biblionumber' => { 'biblioitems' => 'items' } } ],
285 order_by => { "-$direction" => $order_by },
289 # get biblionumbers stored in the cart
291 if(my $cart_list = $query->cookie('bib_list')){
292 @cart_list = split(/\//, $cart_list);
295 my $patron = Koha::Patrons->find( $loggedinuser );
297 my $categorycode; # needed for may_article_request
298 if( C4::Context->preference('ArticleRequests') ) {
299 $categorycode = $patron ? $patron->categorycode : undef;
302 # Lists display falls back to search results configuration
303 my $xslfile = C4::Context->preference('OPACXSLTListsDisplay');
304 my $lang = $xslfile ? C4::Languages::getlanguage() : undef;
305 my $sysxml = $xslfile ? C4::XSLT::get_xslt_sysprefs() : undef;
307 my $record_processor = Koha::RecordProcessor->new({ filters => 'ViewPolicy' });
310 if( C4::Context->preference('ArticleRequests') ) {
311 $art_req_itypes = Koha::CirculationRules->guess_article_requestable_itemtypes({ $patron ? ( categorycode => $patron->categorycode ) : () });
315 while ( my $content = $contents->next ) {
316 my $biblionumber = $content->biblionumber;
317 my $this_item = GetBiblioData($biblionumber);
318 my $record = GetMarcBiblio({ biblionumber => $biblionumber });
319 my $framework = GetFrameworkCode( $biblionumber );
320 my $biblio = Koha::Biblios->find( $biblionumber );
321 $record_processor->options({
323 frameworkcode => $framework
325 $record_processor->process($record);
327 my $marcflavour = C4::Context->preference("marcflavour");
328 my $itemtype = Koha::Biblioitems->search({ biblionumber => $content->biblionumber })->next->itemtype;
329 $itemtype = Koha::ItemTypes->find( $itemtype );
331 $this_item->{imageurl} = C4::Koha::getitemtypeimagelocation( 'opac', $itemtype->imageurl );
332 $this_item->{description} = $itemtype->description; #FIXME Should not it be translated_description?
333 $this_item->{notforloan} = $itemtype->notforloan;
335 $this_item->{'coins'} = $biblio->get_coins;
336 $this_item->{'normalized_upc'} = GetNormalizedUPC( $record, $marcflavour );
337 $this_item->{'normalized_ean'} = GetNormalizedEAN( $record, $marcflavour );
338 $this_item->{'normalized_oclc'} = GetNormalizedOCLCNumber( $record, $marcflavour );
339 $this_item->{'normalized_isbn'} = GetNormalizedISBN( undef, $record, $marcflavour );
340 # BZ17530: 'Intelligent' guess if result can be article requested
341 $this_item->{artreqpossible} = ( $art_req_itypes->{ $this_item->{itemtype} // q{} } || $art_req_itypes->{ '*' } ) ? 1 : q{};
343 unless ( defined $this_item->{size} ) {
345 #TT has problems with size
346 $this_item->{size} = q||;
349 if (C4::Context->preference('TagsEnabled') and C4::Context->preference('TagsShowOnList')) {
350 $this_item->{TagLoop} = get_tags({
351 biblionumber => $biblionumber, approved=>1, 'sort'=>'-weight',
352 limit => C4::Context->preference('TagsShowOnList'),
356 my $items = $biblio->items->filter_by_visible_in_opac({ patron => $patron });
357 my $allow_onshelf_holds;
358 while ( my $item = $items->next ) {
360 # This method must take a Koha::Items rs
361 $allow_onshelf_holds ||= Koha::CirculationRules->get_onshelfholds_policy(
362 { item => $item, patron => $patron } );
366 $this_item->{allow_onshelf_holds} = $allow_onshelf_holds;
367 $this_item->{'ITEM_RESULTS'} = $items;
371 anonymous_session => ($loggedinuser) ? 0 : 1
373 $this_item->{XSLTBloc} = XSLTParse4Display(
374 $biblionumber, $record,
375 "OPACXSLTListsDisplay", 1,
378 $variables, $items->reset
382 if ( grep {$_ eq $biblionumber} @cart_list) {
383 $this_item->{incart} = 1;
386 $this_item->{biblio_object} = $biblio;
387 $this_item->{biblionumber} = $biblionumber;
388 push @items_info, $this_item;
392 can_manage_shelf => $shelf->can_be_managed($loggedinuser),
393 can_delete_shelf => $shelf->can_be_deleted($loggedinuser),
394 can_remove_biblios => $shelf->can_biblios_be_removed($loggedinuser),
395 can_add_biblios => $shelf->can_biblios_be_added($loggedinuser),
396 itemsloop => \@items_info,
397 sortfield => $sortfield,
398 direction => $direction,
400 Koha::CsvProfiles->search(
401 { type => 'marc', used_for => 'export_records', staff_only => 0 }
406 my $pager = $contents->pager;
408 pagination_bar => pagination_bar(
409 q||, $pager->last_page - $pager->first_page + 1,
410 $page, "page", { op => 'view', shelfnumber => $shelf->shelfnumber, sortfield => $sortfield, direction => $direction, }
415 push @messages, { type => 'error', code => 'unauthorized_on_view' };
419 push @messages, { type => 'error', code => 'does_not_exist' };
423 if ( $op eq 'list' ) {
425 my ( $page, $rows ) = ( $query->param('page') || 1, 20 );
426 if ( $category == 1 ) {
427 $shelves = Koha::Virtualshelves->get_private_shelves({ page => $page, rows => $rows, borrowernumber => $loggedinuser, });
429 $shelves = Koha::Virtualshelves->get_public_shelves({ page => $page, rows => $rows, });
432 my $pager = $shelves->pager;
435 pagination_bar => pagination_bar(
436 q||, $pager->last_page - $pager->first_page + 1,
437 $page, "page", { op => 'list', category => $category, }
446 messages => \@messages,
447 category => $category,
448 print => scalar $query->param('print') || 0,
452 my $content_type = $query->param('rss')? 'rss' : 'html';
453 output_with_http_headers $query, $cookie, $template->output, $content_type;