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>.
29 use C4::Tags qw( get_tags );
33 use Koha::Biblioitems;
34 use Koha::CirculationRules;
38 use Koha::Virtualshelves;
39 use Koha::RecordProcessor;
41 use constant ANYONE => 2;
45 my $template_name = $query->param('rss') ? "opac-shelves-rss.tt" : "opac-shelves.tt";
47 # if virtualshelves is disabled, leave immediately
48 if ( ! C4::Context->preference('virtualshelves') ) {
49 print $query->redirect("/cgi-bin/koha/errors/404.pl");
53 my ( $template, $loggedinuser, $cookie ) = get_template_and_user({
54 template_name => $template_name,
57 authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
60 my $op = $query->param('op') || 'list';
61 my $referer = $query->param('referer') || $op;
62 my $category = $query->param('category') || 1;
63 my ( $shelf, $shelfnumber, @messages );
65 if ( $op eq 'add_form' ) {
67 $shelf = { allow_change_from_owner => 1 };
68 } elsif ( $op eq 'edit_form' ) {
69 $shelfnumber = $query->param('shelfnumber');
70 $shelf = Koha::Virtualshelves->find($shelfnumber);
73 $category = $shelf->category;
74 my $patron = Koha::Patrons->find( $shelf->owner );
75 $template->param( owner => $patron, );
76 unless ( $shelf->can_be_managed( $loggedinuser ) ) {
77 push @messages, { type => 'error', code => 'unauthorized_on_update' };
81 push @messages, { type => 'error', code => 'does_not_exist' };
83 } elsif ( $op eq 'add' ) {
84 if ( $loggedinuser ) {
85 my $allow_changes_from = $query->param('allow_changes_from');
87 $shelf = Koha::Virtualshelf->new(
88 { shelfname => scalar $query->param('shelfname'),
89 sortfield => scalar $query->param('sortfield'),
90 category => scalar $query->param('category') || 1,
91 allow_change_from_owner => $allow_changes_from > 0,
92 allow_change_from_others => $allow_changes_from == ANYONE,
93 owner => scalar $loggedinuser,
97 $shelfnumber = $shelf->shelfnumber;
100 push @messages, { type => 'error', code => ref($@), msg => $@ };
101 } elsif ( not $shelf ) {
102 push @messages, { type => 'error', code => 'error_on_insert' };
104 push @messages, { type => 'message', code => 'success_on_insert' };
108 push @messages, { type => 'error', code => 'unauthorized_on_insert' };
111 } elsif ( $op eq 'edit' ) {
112 $shelfnumber = $query->param('shelfnumber');
113 $shelf = Koha::Virtualshelves->find($shelfnumber);
116 my $sortfield = $query->param('sortfield');
117 $sortfield = 'title' unless grep {/^$sortfield$/}qw( title author copyrightdate itemcallnumber dateadded );
118 if ( $shelf->can_be_managed( $loggedinuser ) ) {
119 $shelf->shelfname( scalar $query->param('shelfname') );
120 $shelf->sortfield( $sortfield );
121 my $allow_changes_from = $query->param('allow_changes_from');
122 $shelf->allow_change_from_owner( $allow_changes_from > 0 );
123 $shelf->allow_change_from_others( $allow_changes_from == ANYONE );
124 $shelf->category( scalar $query->param('category') );
125 eval { $shelf->store };
128 push @messages, { type => 'error', code => 'error_on_update' };
131 push @messages, { type => 'message', code => 'success_on_update' };
134 push @messages, { type => 'error', code => 'unauthorized_on_update' };
137 push @messages, { type => 'error', code => 'does_not_exist' };
139 } elsif ( $op eq 'delete' ) {
140 $shelfnumber = $query->param('shelfnumber');
141 $shelf = Koha::Virtualshelves->find($shelfnumber);
143 if ( $shelf->can_be_deleted( $loggedinuser ) ) {
144 eval { $shelf->delete; };
146 push @messages, { type => 'error', code => ref($@), msg => $@ };
148 push @messages, { type => 'message', code => 'success_on_delete' };
151 push @messages, { type => 'error', code => 'unauthorized_on_delete' };
154 push @messages, { type => 'error', code => 'does_not_exist' };
157 } elsif ( $op eq 'remove_share' ) {
158 $shelfnumber = $query->param('shelfnumber');
159 $shelf = Koha::Virtualshelves->find($shelfnumber);
161 my $removed = eval { $shelf->remove_share( $loggedinuser ); };
163 push @messages, { type => 'error', code => ref($@), msg => $@ };
164 } elsif ( $removed ) {
165 push @messages, { type => 'message', code => 'success_on_remove_share' };
167 push @messages, { type => 'error', code => 'error_on_remove_share' };
170 push @messages, { type => 'error', code => 'does_not_exist' };
174 } elsif ( $op eq 'add_biblio' ) {
175 $shelfnumber = $query->param('shelfnumber');
176 $shelf = Koha::Virtualshelves->find($shelfnumber);
178 if( my $barcode = $query->param('barcode') ) {
179 my $item = Koha::Items->find({ barcode => $barcode });
181 if ( $shelf->can_biblios_be_added( $loggedinuser ) ) {
182 my $added = eval { $shelf->add_biblio( $item->biblionumber, $loggedinuser ); };
184 push @messages, { type => 'error', code => ref($@), msg => $@ };
186 push @messages, { type => 'message', code => 'success_on_add_biblio' };
188 push @messages, { type => 'message', code => 'error_on_add_biblio' };
191 push @messages, { type => 'error', code => 'unauthorized_on_add_biblio' };
194 push @messages, { type => 'error', code => 'item_does_not_exist' };
198 push @messages, { type => 'error', code => 'does_not_exist' };
201 } elsif ( $op eq 'remove_biblios' ) {
202 $shelfnumber = $query->param('shelfnumber');
203 $shelf = Koha::Virtualshelves->find($shelfnumber);
204 my @biblionumber = $query->multi_param('biblionumber');
206 if ( $shelf->can_biblios_be_removed( $loggedinuser ) ) {
207 my $number_of_biblios_removed = eval {
208 $shelf->remove_biblios(
210 biblionumbers => \@biblionumber,
211 borrowernumber => $loggedinuser,
216 push @messages, { type => 'error', code => ref($@), msg => $@ };
217 } elsif ( $number_of_biblios_removed ) {
218 push @messages, { type => 'message', code => 'success_on_remove_biblios' };
220 push @messages, { type => 'error', code => 'no_biblio_removed' };
223 push @messages, { type => 'error', code => 'unauthorized_on_remove_biblios' };
226 push @messages, { type => 'error', code => 'does_not_exist' };
231 if ( $op eq 'view' ) {
232 $shelfnumber ||= $query->param('shelfnumber');
233 $shelf = Koha::Virtualshelves->find($shelfnumber);
235 if ( $shelf->can_be_viewed( $loggedinuser ) ) {
236 $category = $shelf->category;
237 my( $sortfield, $direction );
238 if( defined( $query->param('sortfield') ) ){ # Passed in sorting overrides default sorting
239 ( $sortfield, $direction ) = split /:/, $query->param('sortfield');
241 $sortfield = $shelf->sortfield;
244 $sortfield = 'title' unless grep $_ eq $sortfield, qw( title author copyrightdate itemcallnumber dateadded );
245 $direction = 'asc' if $direction ne 'asc' and $direction ne 'desc';
247 unless ( $query->param('print') or $query->param('rss') ) {
248 $rows = C4::Context->preference('OPACnumSearchResults') || 20;
249 $page = ( $query->param('page') ? $query->param('page') : 1 );
251 my $order_by = $sortfield eq 'itemcallnumber' ? 'items.cn_sort' : $sortfield;
252 my $contents = $shelf->get_contents->search(
255 prefetch => [ { 'biblionumber' => { 'biblioitems' => 'items' } } ],
258 order_by => { "-$direction" => $order_by },
262 # get biblionumbers stored in the cart
264 if(my $cart_list = $query->cookie('bib_list')){
265 @cart_list = split(/\//, $cart_list);
268 my $patron = Koha::Patrons->find( $loggedinuser );
270 my $categorycode; # needed for may_article_request
271 if( C4::Context->preference('ArticleRequests') ) {
272 $categorycode = $patron ? $patron->categorycode : undef;
275 # Lists display falls back to search results configuration
276 my $xslfile = C4::Context->preference('OPACXSLTListsDisplay');
277 my $lang = $xslfile ? C4::Languages::getlanguage() : undef;
278 my $sysxml = $xslfile ? C4::XSLT::get_xslt_sysprefs() : undef;
280 my $record_processor = Koha::RecordProcessor->new({ filters => 'ViewPolicy' });
283 if( C4::Context->preference('ArticleRequests') ) {
284 $art_req_itypes = Koha::IssuingRules->guess_article_requestable_itemtypes({ $patron ? ( categorycode => $patron->categorycode ) : () });
288 while ( my $content = $contents->next ) {
289 my $biblionumber = $content->biblionumber;
290 my $this_item = GetBiblioData($biblionumber);
291 my $record = GetMarcBiblio({ biblionumber => $biblionumber });
292 my $framework = GetFrameworkCode( $biblionumber );
293 my $biblio = Koha::Biblios->find( $biblionumber );
294 $record_processor->options({
296 frameworkcode => $framework
298 $record_processor->process($record);
301 $this_item->{XSLTBloc} = XSLTParse4Display( $biblionumber, $record, "OPACXSLTListsDisplay",
302 1, undef, $sysxml, $xslfile, $lang);
305 my $marcflavour = C4::Context->preference("marcflavour");
306 my $itemtype = Koha::Biblioitems->search({ biblionumber => $content->biblionumber })->next->itemtype;
307 $itemtype = Koha::ItemTypes->find( $itemtype );
309 $this_item->{imageurl} = C4::Koha::getitemtypeimagelocation( 'opac', $itemtype->imageurl );
310 $this_item->{description} = $itemtype->description; #FIXME Should not it be translated_description?
311 $this_item->{notforloan} = $itemtype->notforloan;
313 $this_item->{'coins'} = $biblio->get_coins;
314 $this_item->{'normalized_upc'} = GetNormalizedUPC( $record, $marcflavour );
315 $this_item->{'normalized_ean'} = GetNormalizedEAN( $record, $marcflavour );
316 $this_item->{'normalized_oclc'} = GetNormalizedOCLCNumber( $record, $marcflavour );
317 $this_item->{'normalized_isbn'} = GetNormalizedISBN( undef, $record, $marcflavour );
318 # BZ17530: 'Intelligent' guess if result can be article requested
319 $this_item->{artreqpossible} = ( $art_req_itypes->{ $this_item->{itemtype} // q{} } || $art_req_itypes->{ '*' } ) ? 1 : q{};
321 unless ( defined $this_item->{size} ) {
323 #TT has problems with size
324 $this_item->{size} = q||;
327 # Getting items infos for location display
328 my @items_infos = &GetItemsLocationInfo( $biblionumber );
329 $this_item->{'ITEM_RESULTS'} = \@items_infos;
331 if (C4::Context->preference('TagsEnabled') and C4::Context->preference('TagsShowOnList')) {
332 $this_item->{TagLoop} = get_tags({
333 biblionumber => $biblionumber, approved=>1, 'sort'=>'-weight',
334 limit => C4::Context->preference('TagsShowOnList'),
338 my $items = $biblio->items;
339 while ( my $item = $items->next ) {
340 $this_item->{allow_onshelf_holds} = Koha::CirculationRules->get_onshelfholds_policy( { item => $item, patron => $patron } );
341 last if $this_item->{allow_onshelf_holds};
344 if ( grep {$_ eq $biblionumber} @cart_list) {
345 $this_item->{incart} = 1;
348 $this_item->{biblio_object} = $biblio;
349 $this_item->{biblionumber} = $biblionumber;
350 push @items, $this_item;
354 can_manage_shelf => $shelf->can_be_managed($loggedinuser),
355 can_delete_shelf => $shelf->can_be_deleted($loggedinuser),
356 can_remove_biblios => $shelf->can_biblios_be_removed($loggedinuser),
357 can_add_biblios => $shelf->can_biblios_be_added($loggedinuser),
358 itemsloop => \@items,
359 sortfield => $sortfield,
360 direction => $direction,
363 my $pager = $contents->pager;
365 pagination_bar => pagination_bar(
366 q||, $pager->last_page - $pager->first_page + 1,
367 $page, "page", { op => 'view', shelfnumber => $shelf->shelfnumber, sortfield => $sortfield, direction => $direction, }
372 push @messages, { type => 'error', code => 'unauthorized_on_view' };
376 push @messages, { type => 'error', code => 'does_not_exist' };
380 if ( $op eq 'list' ) {
382 my ( $page, $rows ) = ( $query->param('page') || 1, 20 );
383 if ( $category == 1 ) {
384 $shelves = Koha::Virtualshelves->get_private_shelves({ page => $page, rows => $rows, borrowernumber => $loggedinuser, });
386 $shelves = Koha::Virtualshelves->get_public_shelves({ page => $page, rows => $rows, });
389 my $pager = $shelves->pager;
392 pagination_bar => pagination_bar(
393 q||, $pager->last_page - $pager->first_page + 1,
394 $page, "page", { op => 'list', category => $category, }
403 messages => \@messages,
404 category => $category,
405 print => scalar $query->param('print') || 0,
409 output_html_with_http_headers $query, $cookie, $template->output;