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 if ( $query->param('category') && (
84 ($query->param('category') == 1) ||
85 ($query->param('category') == 2)
88 $category = $query->param('category');
91 my ( $shelf, $shelfnumber, @messages );
93 if ( $op eq 'add_form' ) {
95 $shelf = { allow_change_from_owner => 1 };
96 } elsif ( $op eq 'edit_form' ) {
97 $shelfnumber = $query->param('shelfnumber');
98 $shelf = Koha::Virtualshelves->find($shelfnumber);
101 $category = $shelf->category;
102 my $patron = Koha::Patrons->find( $shelf->owner );
103 $template->param( owner => $patron, );
104 unless ( $shelf->can_be_managed( $loggedinuser ) ) {
105 push @messages, { type => 'error', code => 'unauthorized_on_update' };
109 push @messages, { type => 'error', code => 'does_not_exist' };
111 } elsif ( $op eq 'add' ) {
112 if ( $loggedinuser ) {
113 my $allow_changes_from = $query->param('allow_changes_from');
115 $shelf = Koha::Virtualshelf->new(
116 { shelfname => scalar $query->param('shelfname'),
117 sortfield => scalar $query->param('sortfield'),
118 category => $category || 1,
119 allow_change_from_owner => $allow_changes_from > 0,
120 allow_change_from_others => $allow_changes_from == ANYONE,
121 owner => scalar $loggedinuser,
125 $shelfnumber = $shelf->shelfnumber;
128 push @messages, { type => 'error', code => ref($@), msg => $@ };
129 } elsif ( not $shelf ) {
130 push @messages, { type => 'error', code => 'error_on_insert' };
132 push @messages, { type => 'message', code => 'success_on_insert' };
136 push @messages, { type => 'error', code => 'unauthorized_on_insert' };
139 } elsif ( $op eq 'edit' ) {
140 $shelfnumber = $query->param('shelfnumber');
141 $shelf = Koha::Virtualshelves->find($shelfnumber);
144 my $sortfield = $query->param('sortfield');
145 $sortfield = 'title' unless grep { $_ eq $sortfield } qw( title author copyrightdate itemcallnumber dateadded );
146 if ( $shelf->can_be_managed( $loggedinuser ) ) {
147 $shelf->shelfname( scalar $query->param('shelfname') );
148 $shelf->sortfield( $sortfield );
149 my $allow_changes_from = $query->param('allow_changes_from');
150 $shelf->allow_change_from_owner( $allow_changes_from > 0 );
151 $shelf->allow_change_from_others( $allow_changes_from == ANYONE );
152 $shelf->category( $category );
153 eval { $shelf->store };
156 push @messages, { type => 'error', code => 'error_on_update' };
159 push @messages, { type => 'message', code => 'success_on_update' };
162 push @messages, { type => 'error', code => 'unauthorized_on_update' };
165 push @messages, { type => 'error', code => 'does_not_exist' };
167 } elsif ( $op eq 'delete' ) {
168 $shelfnumber = $query->param('shelfnumber');
169 $shelf = Koha::Virtualshelves->find($shelfnumber);
171 if ( $shelf->can_be_deleted( $loggedinuser ) ) {
172 eval { $shelf->delete; };
174 push @messages, { type => 'error', code => ref($@), msg => $@ };
176 push @messages, { type => 'message', code => 'success_on_delete' };
179 push @messages, { type => 'error', code => 'unauthorized_on_delete' };
182 push @messages, { type => 'error', code => 'does_not_exist' };
185 } elsif ( $op eq 'remove_share' ) {
186 $shelfnumber = $query->param('shelfnumber');
187 $shelf = Koha::Virtualshelves->find($shelfnumber);
189 my $removed = eval { $shelf->remove_share( $loggedinuser ); };
191 push @messages, { type => 'error', code => ref($@), msg => $@ };
192 } elsif ( $removed ) {
193 push @messages, { type => 'message', code => 'success_on_remove_share' };
195 push @messages, { type => 'error', code => 'error_on_remove_share' };
198 push @messages, { type => 'error', code => 'does_not_exist' };
202 } elsif ( $op eq 'add_biblio' ) {
203 $shelfnumber = $query->param('shelfnumber');
204 $shelf = Koha::Virtualshelves->find($shelfnumber);
206 if( my $barcode = $query->param('barcode') ) {
207 my $item = Koha::Items->find({ barcode => $barcode });
209 if ( $shelf->can_biblios_be_added( $loggedinuser ) ) {
210 my $added = eval { $shelf->add_biblio( $item->biblionumber, $loggedinuser ); };
212 push @messages, { type => 'error', code => ref($@), msg => $@ };
214 push @messages, { type => 'message', code => 'success_on_add_biblio' };
216 push @messages, { type => 'message', code => 'error_on_add_biblio' };
219 push @messages, { type => 'error', code => 'unauthorized_on_add_biblio' };
222 push @messages, { type => 'error', code => 'item_does_not_exist' };
226 push @messages, { type => 'error', code => 'does_not_exist' };
229 } elsif ( $op eq 'remove_biblios' ) {
230 $shelfnumber = $query->param('shelfnumber');
231 $shelf = Koha::Virtualshelves->find($shelfnumber);
232 my @biblionumber = $query->multi_param('biblionumber');
234 if ( $shelf->can_biblios_be_removed( $loggedinuser ) ) {
235 my $number_of_biblios_removed = eval {
236 $shelf->remove_biblios(
238 biblionumbers => \@biblionumber,
239 borrowernumber => $loggedinuser,
244 push @messages, { type => 'error', code => ref($@), msg => $@ };
245 } elsif ( $number_of_biblios_removed ) {
246 push @messages, { type => 'message', code => 'success_on_remove_biblios' };
248 push @messages, { type => 'error', code => 'no_biblio_removed' };
251 push @messages, { type => 'error', code => 'unauthorized_on_remove_biblios' };
254 push @messages, { type => 'error', code => 'does_not_exist' };
259 if ( $op eq 'view' ) {
260 $shelfnumber ||= $query->param('shelfnumber');
261 $shelf = Koha::Virtualshelves->find($shelfnumber);
263 if ( $shelf->can_be_viewed( $loggedinuser ) ) {
264 $category = $shelf->category;
265 my( $sortfield, $direction );
266 if( defined( $query->param('sortfield') ) ){ # Passed in sorting overrides default sorting
267 ( $sortfield, $direction ) = split /:/, $query->param('sortfield');
269 $sortfield = $shelf->sortfield;
272 $sortfield = 'title' unless grep $_ eq $sortfield, qw( title author copyrightdate itemcallnumber dateadded );
273 $direction = 'asc' if $direction ne 'asc' and $direction ne 'desc';
275 unless ( $query->param('print') or $query->param('rss') ) {
276 $rows = C4::Context->preference('OPACnumSearchResults') || 20;
277 $page = ( $query->param('page') ? $query->param('page') : 1 );
279 my $order_by = $sortfield eq 'itemcallnumber' ? 'items.cn_sort' : $sortfield;
280 my $contents = $shelf->get_contents->search(
283 prefetch => [ { 'biblionumber' => { 'biblioitems' => 'items' } } ],
286 order_by => { "-$direction" => $order_by },
290 # get biblionumbers stored in the cart
292 if(my $cart_list = $query->cookie('bib_list')){
293 @cart_list = split(/\//, $cart_list);
296 my $patron = Koha::Patrons->find( $loggedinuser );
298 my $categorycode; # needed for may_article_request
299 if( C4::Context->preference('ArticleRequests') ) {
300 $categorycode = $patron ? $patron->categorycode : undef;
303 # Lists display falls back to search results configuration
304 my $xslfile = C4::Context->preference('OPACXSLTListsDisplay');
305 my $lang = $xslfile ? C4::Languages::getlanguage() : undef;
306 my $sysxml = $xslfile ? C4::XSLT::get_xslt_sysprefs() : undef;
308 my $record_processor = Koha::RecordProcessor->new({ filters => 'ViewPolicy' });
311 if( C4::Context->preference('ArticleRequests') ) {
312 $art_req_itypes = Koha::CirculationRules->guess_article_requestable_itemtypes({ $patron ? ( categorycode => $patron->categorycode ) : () });
316 while ( my $content = $contents->next ) {
317 my $biblionumber = $content->biblionumber;
318 my $this_item = GetBiblioData($biblionumber);
319 my $record = GetMarcBiblio({ biblionumber => $biblionumber });
320 my $framework = GetFrameworkCode( $biblionumber );
321 my $biblio = Koha::Biblios->find( $biblionumber );
322 $record_processor->options({
324 frameworkcode => $framework
326 $record_processor->process($record);
330 anonymous_session => ($loggedinuser) ? 0 : 1
332 $this_item->{XSLTBloc} = XSLTParse4Display(
333 $biblionumber, $record,
334 "OPACXSLTListsDisplay", 1,
341 my $marcflavour = C4::Context->preference("marcflavour");
342 my $itemtype = Koha::Biblioitems->search({ biblionumber => $content->biblionumber })->next->itemtype;
343 $itemtype = Koha::ItemTypes->find( $itemtype );
345 $this_item->{imageurl} = C4::Koha::getitemtypeimagelocation( 'opac', $itemtype->imageurl );
346 $this_item->{description} = $itemtype->description; #FIXME Should not it be translated_description?
347 $this_item->{notforloan} = $itemtype->notforloan;
349 $this_item->{'coins'} = $biblio->get_coins;
350 $this_item->{'normalized_upc'} = GetNormalizedUPC( $record, $marcflavour );
351 $this_item->{'normalized_ean'} = GetNormalizedEAN( $record, $marcflavour );
352 $this_item->{'normalized_oclc'} = GetNormalizedOCLCNumber( $record, $marcflavour );
353 $this_item->{'normalized_isbn'} = GetNormalizedISBN( undef, $record, $marcflavour );
354 # BZ17530: 'Intelligent' guess if result can be article requested
355 $this_item->{artreqpossible} = ( $art_req_itypes->{ $this_item->{itemtype} // q{} } || $art_req_itypes->{ '*' } ) ? 1 : q{};
357 unless ( defined $this_item->{size} ) {
359 #TT has problems with size
360 $this_item->{size} = q||;
363 # Getting items infos for location display
364 my @items_infos = &GetItemsLocationInfo( $biblionumber );
365 $this_item->{'ITEM_RESULTS'} = \@items_infos;
367 if (C4::Context->preference('TagsEnabled') and C4::Context->preference('TagsShowOnList')) {
368 $this_item->{TagLoop} = get_tags({
369 biblionumber => $biblionumber, approved=>1, 'sort'=>'-weight',
370 limit => C4::Context->preference('TagsShowOnList'),
374 my $items = $biblio->items;
375 while ( my $item = $items->next ) {
376 $this_item->{allow_onshelf_holds} = Koha::CirculationRules->get_onshelfholds_policy( { item => $item, patron => $patron } );
377 last if $this_item->{allow_onshelf_holds};
380 if ( grep {$_ eq $biblionumber} @cart_list) {
381 $this_item->{incart} = 1;
384 $this_item->{biblio_object} = $biblio;
385 $this_item->{biblionumber} = $biblionumber;
386 push @items, $this_item;
390 can_manage_shelf => $shelf->can_be_managed($loggedinuser),
391 can_delete_shelf => $shelf->can_be_deleted($loggedinuser),
392 can_remove_biblios => $shelf->can_biblios_be_removed($loggedinuser),
393 can_add_biblios => $shelf->can_biblios_be_added($loggedinuser),
394 itemsloop => \@items,
395 sortfield => $sortfield,
396 direction => $direction,
398 Koha::CsvProfiles->search(
399 { type => 'marc', used_for => 'export_records', staff_only => 0 }
404 my $pager = $contents->pager;
406 pagination_bar => pagination_bar(
407 q||, $pager->last_page - $pager->first_page + 1,
408 $page, "page", { op => 'view', shelfnumber => $shelf->shelfnumber, sortfield => $sortfield, direction => $direction, }
413 push @messages, { type => 'error', code => 'unauthorized_on_view' };
417 push @messages, { type => 'error', code => 'does_not_exist' };
421 if ( $op eq 'list' ) {
423 my ( $page, $rows ) = ( $query->param('page') || 1, 20 );
424 if ( $category == 1 ) {
425 $shelves = Koha::Virtualshelves->get_private_shelves({ page => $page, rows => $rows, borrowernumber => $loggedinuser, });
427 $shelves = Koha::Virtualshelves->get_public_shelves({ page => $page, rows => $rows, });
430 my $pager = $shelves->pager;
433 pagination_bar => pagination_bar(
434 q||, $pager->last_page - $pager->first_page + 1,
435 $page, "page", { op => 'list', category => $category, }
444 messages => \@messages,
445 category => ($category == 1 || $category == 2) ? $category : "",
446 print => scalar $query->param('print') || 0,
450 my $content_type = $query->param('rss')? 'rss' : 'html';
451 output_with_http_headers $query, $cookie, $template->output, $content_type;