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;
26 use C4::External::BakerTaylor qw(&image_url &link_url);
31 use C4::Tags qw( get_tags );
35 use Koha::Biblioitems;
36 use Koha::CirculationRules;
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 my $referer = $query->param('referer') || $op;
75 my $category = $query->param('category') || 1;
76 my ( $shelf, $shelfnumber, @messages );
78 if ( $op eq 'add_form' ) {
80 $shelf = { allow_change_from_owner => 1 };
81 } elsif ( $op eq 'edit_form' ) {
82 $shelfnumber = $query->param('shelfnumber');
83 $shelf = Koha::Virtualshelves->find($shelfnumber);
86 $category = $shelf->category;
87 my $patron = Koha::Patrons->find( $shelf->owner );
88 $template->param( owner => $patron, );
89 unless ( $shelf->can_be_managed( $loggedinuser ) ) {
90 push @messages, { type => 'error', code => 'unauthorized_on_update' };
94 push @messages, { type => 'error', code => 'does_not_exist' };
96 } elsif ( $op eq 'add' ) {
97 if ( $loggedinuser ) {
98 my $allow_changes_from = $query->param('allow_changes_from');
100 $shelf = Koha::Virtualshelf->new(
101 { shelfname => scalar $query->param('shelfname'),
102 sortfield => scalar $query->param('sortfield'),
103 category => scalar $query->param('category') || 1,
104 allow_change_from_owner => $allow_changes_from > 0,
105 allow_change_from_others => $allow_changes_from == ANYONE,
106 owner => scalar $loggedinuser,
110 $shelfnumber = $shelf->shelfnumber;
113 push @messages, { type => 'error', code => ref($@), msg => $@ };
114 } elsif ( not $shelf ) {
115 push @messages, { type => 'error', code => 'error_on_insert' };
117 push @messages, { type => 'message', code => 'success_on_insert' };
121 push @messages, { type => 'error', code => 'unauthorized_on_insert' };
124 } elsif ( $op eq 'edit' ) {
125 $shelfnumber = $query->param('shelfnumber');
126 $shelf = Koha::Virtualshelves->find($shelfnumber);
129 my $sortfield = $query->param('sortfield');
130 $sortfield = 'title' unless grep { $_ eq $sortfield } qw( title author copyrightdate itemcallnumber dateadded );
131 if ( $shelf->can_be_managed( $loggedinuser ) ) {
132 $shelf->shelfname( scalar $query->param('shelfname') );
133 $shelf->sortfield( $sortfield );
134 my $allow_changes_from = $query->param('allow_changes_from');
135 $shelf->allow_change_from_owner( $allow_changes_from > 0 );
136 $shelf->allow_change_from_others( $allow_changes_from == ANYONE );
137 $shelf->category( scalar $query->param('category') );
138 eval { $shelf->store };
141 push @messages, { type => 'error', code => 'error_on_update' };
144 push @messages, { type => 'message', code => 'success_on_update' };
147 push @messages, { type => 'error', code => 'unauthorized_on_update' };
150 push @messages, { type => 'error', code => 'does_not_exist' };
152 } elsif ( $op eq 'delete' ) {
153 $shelfnumber = $query->param('shelfnumber');
154 $shelf = Koha::Virtualshelves->find($shelfnumber);
156 if ( $shelf->can_be_deleted( $loggedinuser ) ) {
157 eval { $shelf->delete; };
159 push @messages, { type => 'error', code => ref($@), msg => $@ };
161 push @messages, { type => 'message', code => 'success_on_delete' };
164 push @messages, { type => 'error', code => 'unauthorized_on_delete' };
167 push @messages, { type => 'error', code => 'does_not_exist' };
170 } elsif ( $op eq 'remove_share' ) {
171 $shelfnumber = $query->param('shelfnumber');
172 $shelf = Koha::Virtualshelves->find($shelfnumber);
174 my $removed = eval { $shelf->remove_share( $loggedinuser ); };
176 push @messages, { type => 'error', code => ref($@), msg => $@ };
177 } elsif ( $removed ) {
178 push @messages, { type => 'message', code => 'success_on_remove_share' };
180 push @messages, { type => 'error', code => 'error_on_remove_share' };
183 push @messages, { type => 'error', code => 'does_not_exist' };
187 } elsif ( $op eq 'add_biblio' ) {
188 $shelfnumber = $query->param('shelfnumber');
189 $shelf = Koha::Virtualshelves->find($shelfnumber);
191 if( my $barcode = $query->param('barcode') ) {
192 my $item = Koha::Items->find({ barcode => $barcode });
194 if ( $shelf->can_biblios_be_added( $loggedinuser ) ) {
195 my $added = eval { $shelf->add_biblio( $item->biblionumber, $loggedinuser ); };
197 push @messages, { type => 'error', code => ref($@), msg => $@ };
199 push @messages, { type => 'message', code => 'success_on_add_biblio' };
201 push @messages, { type => 'message', code => 'error_on_add_biblio' };
204 push @messages, { type => 'error', code => 'unauthorized_on_add_biblio' };
207 push @messages, { type => 'error', code => 'item_does_not_exist' };
211 push @messages, { type => 'error', code => 'does_not_exist' };
214 } elsif ( $op eq 'remove_biblios' ) {
215 $shelfnumber = $query->param('shelfnumber');
216 $shelf = Koha::Virtualshelves->find($shelfnumber);
217 my @biblionumber = $query->multi_param('biblionumber');
219 if ( $shelf->can_biblios_be_removed( $loggedinuser ) ) {
220 my $number_of_biblios_removed = eval {
221 $shelf->remove_biblios(
223 biblionumbers => \@biblionumber,
224 borrowernumber => $loggedinuser,
229 push @messages, { type => 'error', code => ref($@), msg => $@ };
230 } elsif ( $number_of_biblios_removed ) {
231 push @messages, { type => 'message', code => 'success_on_remove_biblios' };
233 push @messages, { type => 'error', code => 'no_biblio_removed' };
236 push @messages, { type => 'error', code => 'unauthorized_on_remove_biblios' };
239 push @messages, { type => 'error', code => 'does_not_exist' };
244 if ( $op eq 'view' ) {
245 $shelfnumber ||= $query->param('shelfnumber');
246 $shelf = Koha::Virtualshelves->find($shelfnumber);
248 if ( $shelf->can_be_viewed( $loggedinuser ) ) {
249 $category = $shelf->category;
250 my( $sortfield, $direction );
251 if( defined( $query->param('sortfield') ) ){ # Passed in sorting overrides default sorting
252 ( $sortfield, $direction ) = split /:/, $query->param('sortfield');
254 $sortfield = $shelf->sortfield;
257 $sortfield = 'title' unless grep $_ eq $sortfield, qw( title author copyrightdate itemcallnumber dateadded );
258 $direction = 'asc' if $direction ne 'asc' and $direction ne 'desc';
260 unless ( $query->param('print') or $query->param('rss') ) {
261 $rows = C4::Context->preference('OPACnumSearchResults') || 20;
262 $page = ( $query->param('page') ? $query->param('page') : 1 );
264 my $order_by = $sortfield eq 'itemcallnumber' ? 'items.cn_sort' : $sortfield;
265 my $contents = $shelf->get_contents->search(
268 prefetch => [ { 'biblionumber' => { 'biblioitems' => 'items' } } ],
271 order_by => { "-$direction" => $order_by },
275 # get biblionumbers stored in the cart
277 if(my $cart_list = $query->cookie('bib_list')){
278 @cart_list = split(/\//, $cart_list);
281 my $patron = Koha::Patrons->find( $loggedinuser );
283 my $categorycode; # needed for may_article_request
284 if( C4::Context->preference('ArticleRequests') ) {
285 $categorycode = $patron ? $patron->categorycode : undef;
288 # Lists display falls back to search results configuration
289 my $xslfile = C4::Context->preference('OPACXSLTListsDisplay');
290 my $lang = $xslfile ? C4::Languages::getlanguage() : undef;
291 my $sysxml = $xslfile ? C4::XSLT::get_xslt_sysprefs() : undef;
293 my $record_processor = Koha::RecordProcessor->new({ filters => 'ViewPolicy' });
296 if( C4::Context->preference('ArticleRequests') ) {
297 $art_req_itypes = Koha::CirculationRules->guess_article_requestable_itemtypes({ $patron ? ( categorycode => $patron->categorycode ) : () });
301 while ( my $content = $contents->next ) {
302 my $biblionumber = $content->biblionumber;
303 my $this_item = GetBiblioData($biblionumber);
304 my $record = GetMarcBiblio({ biblionumber => $biblionumber });
305 my $framework = GetFrameworkCode( $biblionumber );
306 my $biblio = Koha::Biblios->find( $biblionumber );
307 $record_processor->options({
309 frameworkcode => $framework
311 $record_processor->process($record);
314 $this_item->{XSLTBloc} = XSLTParse4Display( $biblionumber, $record, "OPACXSLTListsDisplay",
315 1, undef, $sysxml, $xslfile, $lang);
318 my $marcflavour = C4::Context->preference("marcflavour");
319 my $itemtype = Koha::Biblioitems->search({ biblionumber => $content->biblionumber })->next->itemtype;
320 $itemtype = Koha::ItemTypes->find( $itemtype );
322 $this_item->{imageurl} = C4::Koha::getitemtypeimagelocation( 'opac', $itemtype->imageurl );
323 $this_item->{description} = $itemtype->description; #FIXME Should not it be translated_description?
324 $this_item->{notforloan} = $itemtype->notforloan;
326 $this_item->{'coins'} = $biblio->get_coins;
327 $this_item->{'normalized_upc'} = GetNormalizedUPC( $record, $marcflavour );
328 $this_item->{'normalized_ean'} = GetNormalizedEAN( $record, $marcflavour );
329 $this_item->{'normalized_oclc'} = GetNormalizedOCLCNumber( $record, $marcflavour );
330 $this_item->{'normalized_isbn'} = GetNormalizedISBN( undef, $record, $marcflavour );
331 # BZ17530: 'Intelligent' guess if result can be article requested
332 $this_item->{artreqpossible} = ( $art_req_itypes->{ $this_item->{itemtype} // q{} } || $art_req_itypes->{ '*' } ) ? 1 : q{};
334 unless ( defined $this_item->{size} ) {
336 #TT has problems with size
337 $this_item->{size} = q||;
340 # Getting items infos for location display
341 my @items_infos = &GetItemsLocationInfo( $biblionumber );
342 $this_item->{'ITEM_RESULTS'} = \@items_infos;
344 if (C4::Context->preference('TagsEnabled') and C4::Context->preference('TagsShowOnList')) {
345 $this_item->{TagLoop} = get_tags({
346 biblionumber => $biblionumber, approved=>1, 'sort'=>'-weight',
347 limit => C4::Context->preference('TagsShowOnList'),
351 my $items = $biblio->items;
352 while ( my $item = $items->next ) {
353 $this_item->{allow_onshelf_holds} = Koha::CirculationRules->get_onshelfholds_policy( { item => $item, patron => $patron } );
354 last if $this_item->{allow_onshelf_holds};
357 if ( grep {$_ eq $biblionumber} @cart_list) {
358 $this_item->{incart} = 1;
361 $this_item->{biblio_object} = $biblio;
362 $this_item->{biblionumber} = $biblionumber;
363 push @items, $this_item;
367 can_manage_shelf => $shelf->can_be_managed($loggedinuser),
368 can_delete_shelf => $shelf->can_be_deleted($loggedinuser),
369 can_remove_biblios => $shelf->can_biblios_be_removed($loggedinuser),
370 can_add_biblios => $shelf->can_biblios_be_added($loggedinuser),
371 itemsloop => \@items,
372 sortfield => $sortfield,
373 direction => $direction,
376 my $pager = $contents->pager;
378 pagination_bar => pagination_bar(
379 q||, $pager->last_page - $pager->first_page + 1,
380 $page, "page", { op => 'view', shelfnumber => $shelf->shelfnumber, sortfield => $sortfield, direction => $direction, }
385 push @messages, { type => 'error', code => 'unauthorized_on_view' };
389 push @messages, { type => 'error', code => 'does_not_exist' };
393 if ( $op eq 'list' ) {
395 my ( $page, $rows ) = ( $query->param('page') || 1, 20 );
396 if ( $category == 1 ) {
397 $shelves = Koha::Virtualshelves->get_private_shelves({ page => $page, rows => $rows, borrowernumber => $loggedinuser, });
399 $shelves = Koha::Virtualshelves->get_public_shelves({ page => $page, rows => $rows, });
402 my $pager = $shelves->pager;
405 pagination_bar => pagination_bar(
406 q||, $pager->last_page - $pager->first_page + 1,
407 $page, "page", { op => 'list', category => $category, }
416 messages => \@messages,
417 category => $category,
418 print => scalar $query->param('print') || 0,
422 output_html_with_http_headers $query, $cookie, $template->output;