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
;
36 use Koha
::CsvProfiles
;
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 if (C4
::Context
->preference("BakerTaylorEnabled")) {
76 BakerTaylorImageURL
=> &image_url
(),
77 BakerTaylorLinkURL
=> &link_url
(),
81 my $referer = $query->param('referer') || $op;
82 my $category = $query->param('category') || 1;
83 my ( $shelf, $shelfnumber, @messages );
85 if ( $op eq 'add_form' ) {
87 $shelf = { allow_change_from_owner
=> 1 };
88 } elsif ( $op eq 'edit_form' ) {
89 $shelfnumber = $query->param('shelfnumber');
90 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
93 $category = $shelf->category;
94 my $patron = Koha
::Patrons
->find( $shelf->owner );
95 $template->param( owner
=> $patron, );
96 unless ( $shelf->can_be_managed( $loggedinuser ) ) {
97 push @messages, { type
=> 'error', code
=> 'unauthorized_on_update' };
101 push @messages, { type
=> 'error', code
=> 'does_not_exist' };
103 } elsif ( $op eq 'add' ) {
104 if ( $loggedinuser ) {
105 my $allow_changes_from = $query->param('allow_changes_from');
107 $shelf = Koha
::Virtualshelf
->new(
108 { shelfname
=> scalar $query->param('shelfname'),
109 sortfield
=> scalar $query->param('sortfield'),
110 category
=> scalar $query->param('category') || 1,
111 allow_change_from_owner
=> $allow_changes_from > 0,
112 allow_change_from_others
=> $allow_changes_from == ANYONE
,
113 owner
=> scalar $loggedinuser,
117 $shelfnumber = $shelf->shelfnumber;
120 push @messages, { type
=> 'error', code
=> ref($@
), msg
=> $@
};
121 } elsif ( not $shelf ) {
122 push @messages, { type
=> 'error', code
=> 'error_on_insert' };
124 push @messages, { type
=> 'message', code
=> 'success_on_insert' };
128 push @messages, { type
=> 'error', code
=> 'unauthorized_on_insert' };
131 } elsif ( $op eq 'edit' ) {
132 $shelfnumber = $query->param('shelfnumber');
133 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
136 my $sortfield = $query->param('sortfield');
137 $sortfield = 'title' unless grep { $_ eq $sortfield } qw( title author copyrightdate itemcallnumber dateadded );
138 if ( $shelf->can_be_managed( $loggedinuser ) ) {
139 $shelf->shelfname( scalar $query->param('shelfname') );
140 $shelf->sortfield( $sortfield );
141 my $allow_changes_from = $query->param('allow_changes_from');
142 $shelf->allow_change_from_owner( $allow_changes_from > 0 );
143 $shelf->allow_change_from_others( $allow_changes_from == ANYONE
);
144 $shelf->category( scalar $query->param('category') );
145 eval { $shelf->store };
148 push @messages, { type
=> 'error', code
=> 'error_on_update' };
151 push @messages, { type
=> 'message', code
=> 'success_on_update' };
154 push @messages, { type
=> 'error', code
=> 'unauthorized_on_update' };
157 push @messages, { type
=> 'error', code
=> 'does_not_exist' };
159 } elsif ( $op eq 'delete' ) {
160 $shelfnumber = $query->param('shelfnumber');
161 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
163 if ( $shelf->can_be_deleted( $loggedinuser ) ) {
164 eval { $shelf->delete; };
166 push @messages, { type
=> 'error', code
=> ref($@
), msg
=> $@
};
168 push @messages, { type
=> 'message', code
=> 'success_on_delete' };
171 push @messages, { type
=> 'error', code
=> 'unauthorized_on_delete' };
174 push @messages, { type
=> 'error', code
=> 'does_not_exist' };
177 } elsif ( $op eq 'remove_share' ) {
178 $shelfnumber = $query->param('shelfnumber');
179 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
181 my $removed = eval { $shelf->remove_share( $loggedinuser ); };
183 push @messages, { type
=> 'error', code
=> ref($@
), msg
=> $@
};
184 } elsif ( $removed ) {
185 push @messages, { type
=> 'message', code
=> 'success_on_remove_share' };
187 push @messages, { type
=> 'error', code
=> 'error_on_remove_share' };
190 push @messages, { type
=> 'error', code
=> 'does_not_exist' };
194 } elsif ( $op eq 'add_biblio' ) {
195 $shelfnumber = $query->param('shelfnumber');
196 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
198 if( my $barcode = $query->param('barcode') ) {
199 my $item = Koha
::Items
->find({ barcode
=> $barcode });
201 if ( $shelf->can_biblios_be_added( $loggedinuser ) ) {
202 my $added = eval { $shelf->add_biblio( $item->biblionumber, $loggedinuser ); };
204 push @messages, { type
=> 'error', code
=> ref($@
), msg
=> $@
};
206 push @messages, { type
=> 'message', code
=> 'success_on_add_biblio' };
208 push @messages, { type
=> 'message', code
=> 'error_on_add_biblio' };
211 push @messages, { type
=> 'error', code
=> 'unauthorized_on_add_biblio' };
214 push @messages, { type
=> 'error', code
=> 'item_does_not_exist' };
218 push @messages, { type
=> 'error', code
=> 'does_not_exist' };
221 } elsif ( $op eq 'remove_biblios' ) {
222 $shelfnumber = $query->param('shelfnumber');
223 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
224 my @biblionumber = $query->multi_param('biblionumber');
226 if ( $shelf->can_biblios_be_removed( $loggedinuser ) ) {
227 my $number_of_biblios_removed = eval {
228 $shelf->remove_biblios(
230 biblionumbers
=> \
@biblionumber,
231 borrowernumber
=> $loggedinuser,
236 push @messages, { type
=> 'error', code
=> ref($@
), msg
=> $@
};
237 } elsif ( $number_of_biblios_removed ) {
238 push @messages, { type
=> 'message', code
=> 'success_on_remove_biblios' };
240 push @messages, { type
=> 'error', code
=> 'no_biblio_removed' };
243 push @messages, { type
=> 'error', code
=> 'unauthorized_on_remove_biblios' };
246 push @messages, { type
=> 'error', code
=> 'does_not_exist' };
251 if ( $op eq 'view' ) {
252 $shelfnumber ||= $query->param('shelfnumber');
253 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
255 if ( $shelf->can_be_viewed( $loggedinuser ) ) {
256 $category = $shelf->category;
257 my( $sortfield, $direction );
258 if( defined( $query->param('sortfield') ) ){ # Passed in sorting overrides default sorting
259 ( $sortfield, $direction ) = split /:/, $query->param('sortfield');
261 $sortfield = $shelf->sortfield;
264 $sortfield = 'title' unless grep $_ eq $sortfield, qw( title author copyrightdate itemcallnumber dateadded );
265 $direction = 'asc' if $direction ne 'asc' and $direction ne 'desc';
267 unless ( $query->param('print') or $query->param('rss') ) {
268 $rows = C4
::Context
->preference('OPACnumSearchResults') || 20;
269 $page = ( $query->param('page') ?
$query->param('page') : 1 );
271 my $order_by = $sortfield eq 'itemcallnumber' ?
'items.cn_sort' : $sortfield;
272 my $contents = $shelf->get_contents->search(
275 prefetch
=> [ { 'biblionumber' => { 'biblioitems' => 'items' } } ],
278 order_by
=> { "-$direction" => $order_by },
282 # get biblionumbers stored in the cart
284 if(my $cart_list = $query->cookie('bib_list')){
285 @cart_list = split(/\//, $cart_list);
288 my $patron = Koha
::Patrons
->find( $loggedinuser );
290 my $categorycode; # needed for may_article_request
291 if( C4
::Context
->preference('ArticleRequests') ) {
292 $categorycode = $patron ?
$patron->categorycode : undef;
295 # Lists display falls back to search results configuration
296 my $xslfile = C4
::Context
->preference('OPACXSLTListsDisplay');
297 my $lang = $xslfile ? C4
::Languages
::getlanguage
() : undef;
298 my $sysxml = $xslfile ? C4
::XSLT
::get_xslt_sysprefs
() : undef;
300 my $record_processor = Koha
::RecordProcessor
->new({ filters
=> 'ViewPolicy' });
303 if( C4
::Context
->preference('ArticleRequests') ) {
304 $art_req_itypes = Koha
::CirculationRules
->guess_article_requestable_itemtypes({ $patron ?
( categorycode
=> $patron->categorycode ) : () });
308 while ( my $content = $contents->next ) {
309 my $biblionumber = $content->biblionumber;
310 my $this_item = GetBiblioData
($biblionumber);
311 my $record = GetMarcBiblio
({ biblionumber
=> $biblionumber });
312 my $framework = GetFrameworkCode
( $biblionumber );
313 my $biblio = Koha
::Biblios
->find( $biblionumber );
314 $record_processor->options({
316 frameworkcode
=> $framework
318 $record_processor->process($record);
322 anonymous_session
=> ($loggedinuser) ?
0 : 1
324 $this_item->{XSLTBloc
} = XSLTParse4Display
(
325 $biblionumber, $record,
326 "OPACXSLTListsDisplay", 1,
333 my $marcflavour = C4
::Context
->preference("marcflavour");
334 my $itemtype = Koha
::Biblioitems
->search({ biblionumber
=> $content->biblionumber })->next->itemtype;
335 $itemtype = Koha
::ItemTypes
->find( $itemtype );
337 $this_item->{imageurl
} = C4
::Koha
::getitemtypeimagelocation
( 'opac', $itemtype->imageurl );
338 $this_item->{description
} = $itemtype->description; #FIXME Should not it be translated_description?
339 $this_item->{notforloan
} = $itemtype->notforloan;
341 $this_item->{'coins'} = $biblio->get_coins;
342 $this_item->{'normalized_upc'} = GetNormalizedUPC
( $record, $marcflavour );
343 $this_item->{'normalized_ean'} = GetNormalizedEAN
( $record, $marcflavour );
344 $this_item->{'normalized_oclc'} = GetNormalizedOCLCNumber
( $record, $marcflavour );
345 $this_item->{'normalized_isbn'} = GetNormalizedISBN
( undef, $record, $marcflavour );
346 # BZ17530: 'Intelligent' guess if result can be article requested
347 $this_item->{artreqpossible
} = ( $art_req_itypes->{ $this_item->{itemtype
} // q{} } || $art_req_itypes->{ '*' } ) ?
1 : q{};
349 unless ( defined $this_item->{size
} ) {
351 #TT has problems with size
352 $this_item->{size
} = q
||;
355 # Getting items infos for location display
356 my @items_infos = &GetItemsLocationInfo
( $biblionumber );
357 $this_item->{'ITEM_RESULTS'} = \
@items_infos;
359 if (C4
::Context
->preference('TagsEnabled') and C4
::Context
->preference('TagsShowOnList')) {
360 $this_item->{TagLoop
} = get_tags
({
361 biblionumber
=> $biblionumber, approved
=>1, 'sort'=>'-weight',
362 limit
=> C4
::Context
->preference('TagsShowOnList'),
366 my $items = $biblio->items;
367 while ( my $item = $items->next ) {
368 $this_item->{allow_onshelf_holds
} = Koha
::CirculationRules
->get_onshelfholds_policy( { item
=> $item, patron
=> $patron } );
369 last if $this_item->{allow_onshelf_holds
};
372 if ( grep {$_ eq $biblionumber} @cart_list) {
373 $this_item->{incart
} = 1;
376 $this_item->{biblio_object
} = $biblio;
377 $this_item->{biblionumber
} = $biblionumber;
378 push @items, $this_item;
382 can_manage_shelf
=> $shelf->can_be_managed($loggedinuser),
383 can_delete_shelf
=> $shelf->can_be_deleted($loggedinuser),
384 can_remove_biblios
=> $shelf->can_biblios_be_removed($loggedinuser),
385 can_add_biblios
=> $shelf->can_biblios_be_added($loggedinuser),
386 itemsloop
=> \
@items,
387 sortfield
=> $sortfield,
388 direction
=> $direction,
390 Koha
::CsvProfiles
->search(
391 { type
=> 'marc', used_for
=> 'export_records', staff_only
=> 0 }
396 my $pager = $contents->pager;
398 pagination_bar
=> pagination_bar
(
399 q
||, $pager->last_page - $pager->first_page + 1,
400 $page, "page", { op
=> 'view', shelfnumber
=> $shelf->shelfnumber, sortfield
=> $sortfield, direction
=> $direction, }
405 push @messages, { type
=> 'error', code
=> 'unauthorized_on_view' };
409 push @messages, { type
=> 'error', code
=> 'does_not_exist' };
413 if ( $op eq 'list' ) {
415 my ( $page, $rows ) = ( $query->param('page') || 1, 20 );
416 if ( $category == 1 ) {
417 $shelves = Koha
::Virtualshelves
->get_private_shelves({ page
=> $page, rows
=> $rows, borrowernumber
=> $loggedinuser, });
419 $shelves = Koha
::Virtualshelves
->get_public_shelves({ page
=> $page, rows
=> $rows, });
422 my $pager = $shelves->pager;
425 pagination_bar
=> pagination_bar
(
426 q
||, $pager->last_page - $pager->first_page + 1,
427 $page, "page", { op
=> 'list', category
=> $category, }
436 messages
=> \
@messages,
437 category
=> $category,
438 print => scalar $query->param('print') || 0,
442 my $content_type = $query->param('rss')?
'rss' : 'html';
443 output_with_http_headers
$query, $cookie, $template->output, $content_type;