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);
320 $this_item->{XSLTBloc
} = XSLTParse4Display
( $biblionumber, $record, "OPACXSLTListsDisplay",
321 1, undef, $sysxml, $xslfile, $lang);
324 my $marcflavour = C4
::Context
->preference("marcflavour");
325 my $itemtype = Koha
::Biblioitems
->search({ biblionumber
=> $content->biblionumber })->next->itemtype;
326 $itemtype = Koha
::ItemTypes
->find( $itemtype );
328 $this_item->{imageurl
} = C4
::Koha
::getitemtypeimagelocation
( 'opac', $itemtype->imageurl );
329 $this_item->{description
} = $itemtype->description; #FIXME Should not it be translated_description?
330 $this_item->{notforloan
} = $itemtype->notforloan;
332 $this_item->{'coins'} = $biblio->get_coins;
333 $this_item->{'normalized_upc'} = GetNormalizedUPC
( $record, $marcflavour );
334 $this_item->{'normalized_ean'} = GetNormalizedEAN
( $record, $marcflavour );
335 $this_item->{'normalized_oclc'} = GetNormalizedOCLCNumber
( $record, $marcflavour );
336 $this_item->{'normalized_isbn'} = GetNormalizedISBN
( undef, $record, $marcflavour );
337 # BZ17530: 'Intelligent' guess if result can be article requested
338 $this_item->{artreqpossible
} = ( $art_req_itypes->{ $this_item->{itemtype
} // q{} } || $art_req_itypes->{ '*' } ) ?
1 : q{};
340 unless ( defined $this_item->{size
} ) {
342 #TT has problems with size
343 $this_item->{size
} = q
||;
346 # Getting items infos for location display
347 my @items_infos = &GetItemsLocationInfo
( $biblionumber );
348 $this_item->{'ITEM_RESULTS'} = \
@items_infos;
350 if (C4
::Context
->preference('TagsEnabled') and C4
::Context
->preference('TagsShowOnList')) {
351 $this_item->{TagLoop
} = get_tags
({
352 biblionumber
=> $biblionumber, approved
=>1, 'sort'=>'-weight',
353 limit
=> C4
::Context
->preference('TagsShowOnList'),
357 my $items = $biblio->items;
358 while ( my $item = $items->next ) {
359 $this_item->{allow_onshelf_holds
} = Koha
::CirculationRules
->get_onshelfholds_policy( { item
=> $item, patron
=> $patron } );
360 last if $this_item->{allow_onshelf_holds
};
363 if ( grep {$_ eq $biblionumber} @cart_list) {
364 $this_item->{incart
} = 1;
367 $this_item->{biblio_object
} = $biblio;
368 $this_item->{biblionumber
} = $biblionumber;
369 push @items, $this_item;
373 can_manage_shelf
=> $shelf->can_be_managed($loggedinuser),
374 can_delete_shelf
=> $shelf->can_be_deleted($loggedinuser),
375 can_remove_biblios
=> $shelf->can_biblios_be_removed($loggedinuser),
376 can_add_biblios
=> $shelf->can_biblios_be_added($loggedinuser),
377 itemsloop
=> \
@items,
378 sortfield
=> $sortfield,
379 direction
=> $direction,
382 my $pager = $contents->pager;
384 pagination_bar
=> pagination_bar
(
385 q
||, $pager->last_page - $pager->first_page + 1,
386 $page, "page", { op
=> 'view', shelfnumber
=> $shelf->shelfnumber, sortfield
=> $sortfield, direction
=> $direction, }
391 push @messages, { type
=> 'error', code
=> 'unauthorized_on_view' };
395 push @messages, { type
=> 'error', code
=> 'does_not_exist' };
399 if ( $op eq 'list' ) {
401 my ( $page, $rows ) = ( $query->param('page') || 1, 20 );
402 if ( $category == 1 ) {
403 $shelves = Koha
::Virtualshelves
->get_private_shelves({ page
=> $page, rows
=> $rows, borrowernumber
=> $loggedinuser, });
405 $shelves = Koha
::Virtualshelves
->get_public_shelves({ page
=> $page, rows
=> $rows, });
408 my $pager = $shelves->pager;
411 pagination_bar
=> pagination_bar
(
412 q
||, $pager->last_page - $pager->first_page + 1,
413 $page, "page", { op
=> 'list', category
=> $category, }
422 messages
=> \
@messages,
423 category
=> $category,
424 print => scalar $query->param('print') || 0,
428 output_html_with_http_headers
$query, $cookie, $template->output;