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;
39 use Koha::Virtualshelves;
40 use Koha::RecordProcessor;
42 use constant ANYONE => 2;
46 my $template_name = $query->param('rss') ? "opac-shelves-rss.tt" : "opac-shelves.tt";
48 # if virtualshelves is disabled, leave immediately
49 if ( ! C4::Context->preference('virtualshelves') ) {
50 print $query->redirect("/cgi-bin/koha/errors/404.pl");
54 my $op = $query->param('op') || 'list';
55 my ( $template, $loggedinuser, $cookie );
57 if( $op eq 'view' || $op eq 'list' ){
58 ( $template, $loggedinuser, $cookie ) = get_template_and_user({
59 template_name => $template_name,
62 authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
65 ( $template, $loggedinuser, $cookie ) = get_template_and_user({
66 template_name => $template_name,
73 my $referer = $query->param('referer') || $op;
74 my $category = $query->param('category') || 1;
75 my ( $shelf, $shelfnumber, @messages );
77 if ( $op eq 'add_form' ) {
79 $shelf = { allow_change_from_owner => 1 };
80 } elsif ( $op eq 'edit_form' ) {
81 $shelfnumber = $query->param('shelfnumber');
82 $shelf = Koha::Virtualshelves->find($shelfnumber);
85 $category = $shelf->category;
86 my $patron = Koha::Patrons->find( $shelf->owner );
87 $template->param( owner => $patron, );
88 unless ( $shelf->can_be_managed( $loggedinuser ) ) {
89 push @messages, { type => 'error', code => 'unauthorized_on_update' };
93 push @messages, { type => 'error', code => 'does_not_exist' };
95 } elsif ( $op eq 'add' ) {
96 if ( $loggedinuser ) {
97 my $allow_changes_from = $query->param('allow_changes_from');
99 $shelf = Koha::Virtualshelf->new(
100 { shelfname => scalar $query->param('shelfname'),
101 sortfield => scalar $query->param('sortfield'),
102 category => scalar $query->param('category') || 1,
103 allow_change_from_owner => $allow_changes_from > 0,
104 allow_change_from_others => $allow_changes_from == ANYONE,
105 owner => scalar $loggedinuser,
109 $shelfnumber = $shelf->shelfnumber;
112 push @messages, { type => 'error', code => ref($@), msg => $@ };
113 } elsif ( not $shelf ) {
114 push @messages, { type => 'error', code => 'error_on_insert' };
116 push @messages, { type => 'message', code => 'success_on_insert' };
120 push @messages, { type => 'error', code => 'unauthorized_on_insert' };
123 } elsif ( $op eq 'edit' ) {
124 $shelfnumber = $query->param('shelfnumber');
125 $shelf = Koha::Virtualshelves->find($shelfnumber);
128 my $sortfield = $query->param('sortfield');
129 $sortfield = 'title' unless grep { $_ eq $sortfield } qw( title author copyrightdate itemcallnumber dateadded );
130 if ( $shelf->can_be_managed( $loggedinuser ) ) {
131 $shelf->shelfname( scalar $query->param('shelfname') );
132 $shelf->sortfield( $sortfield );
133 my $allow_changes_from = $query->param('allow_changes_from');
134 $shelf->allow_change_from_owner( $allow_changes_from > 0 );
135 $shelf->allow_change_from_others( $allow_changes_from == ANYONE );
136 $shelf->category( scalar $query->param('category') );
137 eval { $shelf->store };
140 push @messages, { type => 'error', code => 'error_on_update' };
143 push @messages, { type => 'message', code => 'success_on_update' };
146 push @messages, { type => 'error', code => 'unauthorized_on_update' };
149 push @messages, { type => 'error', code => 'does_not_exist' };
151 } elsif ( $op eq 'delete' ) {
152 $shelfnumber = $query->param('shelfnumber');
153 $shelf = Koha::Virtualshelves->find($shelfnumber);
155 if ( $shelf->can_be_deleted( $loggedinuser ) ) {
156 eval { $shelf->delete; };
158 push @messages, { type => 'error', code => ref($@), msg => $@ };
160 push @messages, { type => 'message', code => 'success_on_delete' };
163 push @messages, { type => 'error', code => 'unauthorized_on_delete' };
166 push @messages, { type => 'error', code => 'does_not_exist' };
169 } elsif ( $op eq 'remove_share' ) {
170 $shelfnumber = $query->param('shelfnumber');
171 $shelf = Koha::Virtualshelves->find($shelfnumber);
173 my $removed = eval { $shelf->remove_share( $loggedinuser ); };
175 push @messages, { type => 'error', code => ref($@), msg => $@ };
176 } elsif ( $removed ) {
177 push @messages, { type => 'message', code => 'success_on_remove_share' };
179 push @messages, { type => 'error', code => 'error_on_remove_share' };
182 push @messages, { type => 'error', code => 'does_not_exist' };
186 } elsif ( $op eq 'add_biblio' ) {
187 $shelfnumber = $query->param('shelfnumber');
188 $shelf = Koha::Virtualshelves->find($shelfnumber);
190 if( my $barcode = $query->param('barcode') ) {
191 my $item = Koha::Items->find({ barcode => $barcode });
193 if ( $shelf->can_biblios_be_added( $loggedinuser ) ) {
194 my $added = eval { $shelf->add_biblio( $item->biblionumber, $loggedinuser ); };
196 push @messages, { type => 'error', code => ref($@), msg => $@ };
198 push @messages, { type => 'message', code => 'success_on_add_biblio' };
200 push @messages, { type => 'message', code => 'error_on_add_biblio' };
203 push @messages, { type => 'error', code => 'unauthorized_on_add_biblio' };
206 push @messages, { type => 'error', code => 'item_does_not_exist' };
210 push @messages, { type => 'error', code => 'does_not_exist' };
213 } elsif ( $op eq 'remove_biblios' ) {
214 $shelfnumber = $query->param('shelfnumber');
215 $shelf = Koha::Virtualshelves->find($shelfnumber);
216 my @biblionumber = $query->multi_param('biblionumber');
218 if ( $shelf->can_biblios_be_removed( $loggedinuser ) ) {
219 my $number_of_biblios_removed = eval {
220 $shelf->remove_biblios(
222 biblionumbers => \@biblionumber,
223 borrowernumber => $loggedinuser,
228 push @messages, { type => 'error', code => ref($@), msg => $@ };
229 } elsif ( $number_of_biblios_removed ) {
230 push @messages, { type => 'message', code => 'success_on_remove_biblios' };
232 push @messages, { type => 'error', code => 'no_biblio_removed' };
235 push @messages, { type => 'error', code => 'unauthorized_on_remove_biblios' };
238 push @messages, { type => 'error', code => 'does_not_exist' };
243 if ( $op eq 'view' ) {
244 $shelfnumber ||= $query->param('shelfnumber');
245 $shelf = Koha::Virtualshelves->find($shelfnumber);
247 if ( $shelf->can_be_viewed( $loggedinuser ) ) {
248 $category = $shelf->category;
249 my( $sortfield, $direction );
250 if( defined( $query->param('sortfield') ) ){ # Passed in sorting overrides default sorting
251 ( $sortfield, $direction ) = split /:/, $query->param('sortfield');
253 $sortfield = $shelf->sortfield;
256 $sortfield = 'title' unless grep $_ eq $sortfield, qw( title author copyrightdate itemcallnumber dateadded );
257 $direction = 'asc' if $direction ne 'asc' and $direction ne 'desc';
259 unless ( $query->param('print') or $query->param('rss') ) {
260 $rows = C4::Context->preference('OPACnumSearchResults') || 20;
261 $page = ( $query->param('page') ? $query->param('page') : 1 );
263 my $order_by = $sortfield eq 'itemcallnumber' ? 'items.cn_sort' : $sortfield;
264 my $contents = $shelf->get_contents->search(
267 prefetch => [ { 'biblionumber' => { 'biblioitems' => 'items' } } ],
270 order_by => { "-$direction" => $order_by },
274 # get biblionumbers stored in the cart
276 if(my $cart_list = $query->cookie('bib_list')){
277 @cart_list = split(/\//, $cart_list);
280 my $patron = Koha::Patrons->find( $loggedinuser );
282 my $categorycode; # needed for may_article_request
283 if( C4::Context->preference('ArticleRequests') ) {
284 $categorycode = $patron ? $patron->categorycode : undef;
287 # Lists display falls back to search results configuration
288 my $xslfile = C4::Context->preference('OPACXSLTListsDisplay');
289 my $lang = $xslfile ? C4::Languages::getlanguage() : undef;
290 my $sysxml = $xslfile ? C4::XSLT::get_xslt_sysprefs() : undef;
292 my $record_processor = Koha::RecordProcessor->new({ filters => 'ViewPolicy' });
295 if( C4::Context->preference('ArticleRequests') ) {
296 $art_req_itypes = Koha::CirculationRules->guess_article_requestable_itemtypes({ $patron ? ( categorycode => $patron->categorycode ) : () });
300 while ( my $content = $contents->next ) {
301 my $biblionumber = $content->biblionumber;
302 my $this_item = GetBiblioData($biblionumber);
303 my $record = GetMarcBiblio({ biblionumber => $biblionumber });
304 my $framework = GetFrameworkCode( $biblionumber );
305 my $biblio = Koha::Biblios->find( $biblionumber );
306 $record_processor->options({
308 frameworkcode => $framework
310 $record_processor->process($record);
313 $this_item->{XSLTBloc} = XSLTParse4Display( $biblionumber, $record, "OPACXSLTListsDisplay",
314 1, undef, $sysxml, $xslfile, $lang);
317 my $marcflavour = C4::Context->preference("marcflavour");
318 my $itemtype = Koha::Biblioitems->search({ biblionumber => $content->biblionumber })->next->itemtype;
319 $itemtype = Koha::ItemTypes->find( $itemtype );
321 $this_item->{imageurl} = C4::Koha::getitemtypeimagelocation( 'opac', $itemtype->imageurl );
322 $this_item->{description} = $itemtype->description; #FIXME Should not it be translated_description?
323 $this_item->{notforloan} = $itemtype->notforloan;
325 $this_item->{'coins'} = $biblio->get_coins;
326 $this_item->{'normalized_upc'} = GetNormalizedUPC( $record, $marcflavour );
327 $this_item->{'normalized_ean'} = GetNormalizedEAN( $record, $marcflavour );
328 $this_item->{'normalized_oclc'} = GetNormalizedOCLCNumber( $record, $marcflavour );
329 $this_item->{'normalized_isbn'} = GetNormalizedISBN( undef, $record, $marcflavour );
330 # BZ17530: 'Intelligent' guess if result can be article requested
331 $this_item->{artreqpossible} = ( $art_req_itypes->{ $this_item->{itemtype} // q{} } || $art_req_itypes->{ '*' } ) ? 1 : q{};
333 unless ( defined $this_item->{size} ) {
335 #TT has problems with size
336 $this_item->{size} = q||;
339 # Getting items infos for location display
340 my @items_infos = &GetItemsLocationInfo( $biblionumber );
341 $this_item->{'ITEM_RESULTS'} = \@items_infos;
343 if (C4::Context->preference('TagsEnabled') and C4::Context->preference('TagsShowOnList')) {
344 $this_item->{TagLoop} = get_tags({
345 biblionumber => $biblionumber, approved=>1, 'sort'=>'-weight',
346 limit => C4::Context->preference('TagsShowOnList'),
350 my $items = $biblio->items;
351 while ( my $item = $items->next ) {
352 $this_item->{allow_onshelf_holds} = Koha::CirculationRules->get_onshelfholds_policy( { item => $item, patron => $patron } );
353 last if $this_item->{allow_onshelf_holds};
356 if ( grep {$_ eq $biblionumber} @cart_list) {
357 $this_item->{incart} = 1;
360 $this_item->{biblio_object} = $biblio;
361 $this_item->{biblionumber} = $biblionumber;
362 push @items, $this_item;
366 can_manage_shelf => $shelf->can_be_managed($loggedinuser),
367 can_delete_shelf => $shelf->can_be_deleted($loggedinuser),
368 can_remove_biblios => $shelf->can_biblios_be_removed($loggedinuser),
369 can_add_biblios => $shelf->can_biblios_be_added($loggedinuser),
370 itemsloop => \@items,
371 sortfield => $sortfield,
372 direction => $direction,
375 my $pager = $contents->pager;
377 pagination_bar => pagination_bar(
378 q||, $pager->last_page - $pager->first_page + 1,
379 $page, "page", { op => 'view', shelfnumber => $shelf->shelfnumber, sortfield => $sortfield, direction => $direction, }
384 push @messages, { type => 'error', code => 'unauthorized_on_view' };
388 push @messages, { type => 'error', code => 'does_not_exist' };
392 if ( $op eq 'list' ) {
394 my ( $page, $rows ) = ( $query->param('page') || 1, 20 );
395 if ( $category == 1 ) {
396 $shelves = Koha::Virtualshelves->get_private_shelves({ page => $page, rows => $rows, borrowernumber => $loggedinuser, });
398 $shelves = Koha::Virtualshelves->get_public_shelves({ page => $page, rows => $rows, });
401 my $pager = $shelves->pager;
404 pagination_bar => pagination_bar(
405 q||, $pager->last_page - $pager->first_page + 1,
406 $page, "page", { op => 'list', category => $category, }
415 messages => \@messages,
416 category => $category,
417 print => scalar $query->param('print') || 0,
421 output_html_with_http_headers $query, $cookie, $template->output;