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 if (C4
::Context
->preference("BakerTaylorEnabled")) {
75 BakerTaylorImageURL
=> &image_url
(),
76 BakerTaylorLinkURL
=> &link_url
(),
80 my $referer = $query->param('referer') || $op;
81 my $category = $query->param('category') || 1;
82 my ( $shelf, $shelfnumber, @messages );
84 if ( $op eq 'add_form' ) {
86 $shelf = { allow_change_from_owner
=> 1 };
87 } elsif ( $op eq 'edit_form' ) {
88 $shelfnumber = $query->param('shelfnumber');
89 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
92 $category = $shelf->category;
93 my $patron = Koha
::Patrons
->find( $shelf->owner );
94 $template->param( owner
=> $patron, );
95 unless ( $shelf->can_be_managed( $loggedinuser ) ) {
96 push @messages, { type
=> 'error', code
=> 'unauthorized_on_update' };
100 push @messages, { type
=> 'error', code
=> 'does_not_exist' };
102 } elsif ( $op eq 'add' ) {
103 if ( $loggedinuser ) {
104 my $allow_changes_from = $query->param('allow_changes_from');
106 $shelf = Koha
::Virtualshelf
->new(
107 { shelfname
=> scalar $query->param('shelfname'),
108 sortfield
=> scalar $query->param('sortfield'),
109 category
=> scalar $query->param('category') || 1,
110 allow_change_from_owner
=> $allow_changes_from > 0,
111 allow_change_from_others
=> $allow_changes_from == ANYONE
,
112 owner
=> scalar $loggedinuser,
116 $shelfnumber = $shelf->shelfnumber;
119 push @messages, { type
=> 'error', code
=> ref($@
), msg
=> $@
};
120 } elsif ( not $shelf ) {
121 push @messages, { type
=> 'error', code
=> 'error_on_insert' };
123 push @messages, { type
=> 'message', code
=> 'success_on_insert' };
127 push @messages, { type
=> 'error', code
=> 'unauthorized_on_insert' };
130 } elsif ( $op eq 'edit' ) {
131 $shelfnumber = $query->param('shelfnumber');
132 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
135 my $sortfield = $query->param('sortfield');
136 $sortfield = 'title' unless grep { $_ eq $sortfield } qw( title author copyrightdate itemcallnumber dateadded );
137 if ( $shelf->can_be_managed( $loggedinuser ) ) {
138 $shelf->shelfname( scalar $query->param('shelfname') );
139 $shelf->sortfield( $sortfield );
140 my $allow_changes_from = $query->param('allow_changes_from');
141 $shelf->allow_change_from_owner( $allow_changes_from > 0 );
142 $shelf->allow_change_from_others( $allow_changes_from == ANYONE
);
143 $shelf->category( scalar $query->param('category') );
144 eval { $shelf->store };
147 push @messages, { type
=> 'error', code
=> 'error_on_update' };
150 push @messages, { type
=> 'message', code
=> 'success_on_update' };
153 push @messages, { type
=> 'error', code
=> 'unauthorized_on_update' };
156 push @messages, { type
=> 'error', code
=> 'does_not_exist' };
158 } elsif ( $op eq 'delete' ) {
159 $shelfnumber = $query->param('shelfnumber');
160 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
162 if ( $shelf->can_be_deleted( $loggedinuser ) ) {
163 eval { $shelf->delete; };
165 push @messages, { type
=> 'error', code
=> ref($@
), msg
=> $@
};
167 push @messages, { type
=> 'message', code
=> 'success_on_delete' };
170 push @messages, { type
=> 'error', code
=> 'unauthorized_on_delete' };
173 push @messages, { type
=> 'error', code
=> 'does_not_exist' };
176 } elsif ( $op eq 'remove_share' ) {
177 $shelfnumber = $query->param('shelfnumber');
178 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
180 my $removed = eval { $shelf->remove_share( $loggedinuser ); };
182 push @messages, { type
=> 'error', code
=> ref($@
), msg
=> $@
};
183 } elsif ( $removed ) {
184 push @messages, { type
=> 'message', code
=> 'success_on_remove_share' };
186 push @messages, { type
=> 'error', code
=> 'error_on_remove_share' };
189 push @messages, { type
=> 'error', code
=> 'does_not_exist' };
193 } elsif ( $op eq 'add_biblio' ) {
194 $shelfnumber = $query->param('shelfnumber');
195 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
197 if( my $barcode = $query->param('barcode') ) {
198 my $item = Koha
::Items
->find({ barcode
=> $barcode });
200 if ( $shelf->can_biblios_be_added( $loggedinuser ) ) {
201 my $added = eval { $shelf->add_biblio( $item->biblionumber, $loggedinuser ); };
203 push @messages, { type
=> 'error', code
=> ref($@
), msg
=> $@
};
205 push @messages, { type
=> 'message', code
=> 'success_on_add_biblio' };
207 push @messages, { type
=> 'message', code
=> 'error_on_add_biblio' };
210 push @messages, { type
=> 'error', code
=> 'unauthorized_on_add_biblio' };
213 push @messages, { type
=> 'error', code
=> 'item_does_not_exist' };
217 push @messages, { type
=> 'error', code
=> 'does_not_exist' };
220 } elsif ( $op eq 'remove_biblios' ) {
221 $shelfnumber = $query->param('shelfnumber');
222 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
223 my @biblionumber = $query->multi_param('biblionumber');
225 if ( $shelf->can_biblios_be_removed( $loggedinuser ) ) {
226 my $number_of_biblios_removed = eval {
227 $shelf->remove_biblios(
229 biblionumbers
=> \
@biblionumber,
230 borrowernumber
=> $loggedinuser,
235 push @messages, { type
=> 'error', code
=> ref($@
), msg
=> $@
};
236 } elsif ( $number_of_biblios_removed ) {
237 push @messages, { type
=> 'message', code
=> 'success_on_remove_biblios' };
239 push @messages, { type
=> 'error', code
=> 'no_biblio_removed' };
242 push @messages, { type
=> 'error', code
=> 'unauthorized_on_remove_biblios' };
245 push @messages, { type
=> 'error', code
=> 'does_not_exist' };
250 if ( $op eq 'view' ) {
251 $shelfnumber ||= $query->param('shelfnumber');
252 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
254 if ( $shelf->can_be_viewed( $loggedinuser ) ) {
255 $category = $shelf->category;
256 my( $sortfield, $direction );
257 if( defined( $query->param('sortfield') ) ){ # Passed in sorting overrides default sorting
258 ( $sortfield, $direction ) = split /:/, $query->param('sortfield');
260 $sortfield = $shelf->sortfield;
263 $sortfield = 'title' unless grep $_ eq $sortfield, qw( title author copyrightdate itemcallnumber dateadded );
264 $direction = 'asc' if $direction ne 'asc' and $direction ne 'desc';
266 unless ( $query->param('print') or $query->param('rss') ) {
267 $rows = C4
::Context
->preference('OPACnumSearchResults') || 20;
268 $page = ( $query->param('page') ?
$query->param('page') : 1 );
270 my $order_by = $sortfield eq 'itemcallnumber' ?
'items.cn_sort' : $sortfield;
271 my $contents = $shelf->get_contents->search(
274 prefetch
=> [ { 'biblionumber' => { 'biblioitems' => 'items' } } ],
277 order_by
=> { "-$direction" => $order_by },
281 # get biblionumbers stored in the cart
283 if(my $cart_list = $query->cookie('bib_list')){
284 @cart_list = split(/\//, $cart_list);
287 my $patron = Koha
::Patrons
->find( $loggedinuser );
289 my $categorycode; # needed for may_article_request
290 if( C4
::Context
->preference('ArticleRequests') ) {
291 $categorycode = $patron ?
$patron->categorycode : undef;
294 # Lists display falls back to search results configuration
295 my $xslfile = C4
::Context
->preference('OPACXSLTListsDisplay');
296 my $lang = $xslfile ? C4
::Languages
::getlanguage
() : undef;
297 my $sysxml = $xslfile ? C4
::XSLT
::get_xslt_sysprefs
() : undef;
299 my $record_processor = Koha
::RecordProcessor
->new({ filters
=> 'ViewPolicy' });
302 if( C4
::Context
->preference('ArticleRequests') ) {
303 $art_req_itypes = Koha
::CirculationRules
->guess_article_requestable_itemtypes({ $patron ?
( categorycode
=> $patron->categorycode ) : () });
307 while ( my $content = $contents->next ) {
308 my $biblionumber = $content->biblionumber;
309 my $this_item = GetBiblioData
($biblionumber);
310 my $record = GetMarcBiblio
({ biblionumber
=> $biblionumber });
311 my $framework = GetFrameworkCode
( $biblionumber );
312 my $biblio = Koha
::Biblios
->find( $biblionumber );
313 $record_processor->options({
315 frameworkcode
=> $framework
317 $record_processor->process($record);
321 anonymous_session
=> ($loggedinuser) ?
0 : 1
323 $this_item->{XSLTBloc
} = XSLTParse4Display
(
324 $biblionumber, $record,
325 "OPACXSLTListsDisplay", 1,
332 my $marcflavour = C4
::Context
->preference("marcflavour");
333 my $itemtype = Koha
::Biblioitems
->search({ biblionumber
=> $content->biblionumber })->next->itemtype;
334 $itemtype = Koha
::ItemTypes
->find( $itemtype );
336 $this_item->{imageurl
} = C4
::Koha
::getitemtypeimagelocation
( 'opac', $itemtype->imageurl );
337 $this_item->{description
} = $itemtype->description; #FIXME Should not it be translated_description?
338 $this_item->{notforloan
} = $itemtype->notforloan;
340 $this_item->{'coins'} = $biblio->get_coins;
341 $this_item->{'normalized_upc'} = GetNormalizedUPC
( $record, $marcflavour );
342 $this_item->{'normalized_ean'} = GetNormalizedEAN
( $record, $marcflavour );
343 $this_item->{'normalized_oclc'} = GetNormalizedOCLCNumber
( $record, $marcflavour );
344 $this_item->{'normalized_isbn'} = GetNormalizedISBN
( undef, $record, $marcflavour );
345 # BZ17530: 'Intelligent' guess if result can be article requested
346 $this_item->{artreqpossible
} = ( $art_req_itypes->{ $this_item->{itemtype
} // q{} } || $art_req_itypes->{ '*' } ) ?
1 : q{};
348 unless ( defined $this_item->{size
} ) {
350 #TT has problems with size
351 $this_item->{size
} = q
||;
354 # Getting items infos for location display
355 my @items_infos = &GetItemsLocationInfo
( $biblionumber );
356 $this_item->{'ITEM_RESULTS'} = \
@items_infos;
358 if (C4
::Context
->preference('TagsEnabled') and C4
::Context
->preference('TagsShowOnList')) {
359 $this_item->{TagLoop
} = get_tags
({
360 biblionumber
=> $biblionumber, approved
=>1, 'sort'=>'-weight',
361 limit
=> C4
::Context
->preference('TagsShowOnList'),
365 my $items = $biblio->items;
366 while ( my $item = $items->next ) {
367 $this_item->{allow_onshelf_holds
} = Koha
::CirculationRules
->get_onshelfholds_policy( { item
=> $item, patron
=> $patron } );
368 last if $this_item->{allow_onshelf_holds
};
371 if ( grep {$_ eq $biblionumber} @cart_list) {
372 $this_item->{incart
} = 1;
375 $this_item->{biblio_object
} = $biblio;
376 $this_item->{biblionumber
} = $biblionumber;
377 push @items, $this_item;
381 can_manage_shelf
=> $shelf->can_be_managed($loggedinuser),
382 can_delete_shelf
=> $shelf->can_be_deleted($loggedinuser),
383 can_remove_biblios
=> $shelf->can_biblios_be_removed($loggedinuser),
384 can_add_biblios
=> $shelf->can_biblios_be_added($loggedinuser),
385 itemsloop
=> \
@items,
386 sortfield
=> $sortfield,
387 direction
=> $direction,
390 my $pager = $contents->pager;
392 pagination_bar
=> pagination_bar
(
393 q
||, $pager->last_page - $pager->first_page + 1,
394 $page, "page", { op
=> 'view', shelfnumber
=> $shelf->shelfnumber, sortfield
=> $sortfield, direction
=> $direction, }
399 push @messages, { type
=> 'error', code
=> 'unauthorized_on_view' };
403 push @messages, { type
=> 'error', code
=> 'does_not_exist' };
407 if ( $op eq 'list' ) {
409 my ( $page, $rows ) = ( $query->param('page') || 1, 20 );
410 if ( $category == 1 ) {
411 $shelves = Koha
::Virtualshelves
->get_private_shelves({ page
=> $page, rows
=> $rows, borrowernumber
=> $loggedinuser, });
413 $shelves = Koha
::Virtualshelves
->get_public_shelves({ page
=> $page, rows
=> $rows, });
416 my $pager = $shelves->pager;
419 pagination_bar
=> pagination_bar
(
420 q
||, $pager->last_page - $pager->first_page + 1,
421 $page, "page", { op
=> 'list', category
=> $category, }
430 messages
=> \
@messages,
431 category
=> $category,
432 print => scalar $query->param('print') || 0,
436 my $content_type = $query->param('rss')?
'rss' : 'html';
437 output_with_http_headers
$query, $cookie, $template->output, $content_type;