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 $op = $query->param('op') || 'list';
54 my ( $template, $loggedinuser, $cookie );
56 if( $op eq 'view' || $op eq 'list' ){
57 ( $template, $loggedinuser, $cookie ) = get_template_and_user
({
58 template_name
=> $template_name,
61 authnotrequired
=> ( C4
::Context
->preference("OpacPublic") ?
1 : 0 ),
64 ( $template, $loggedinuser, $cookie ) = get_template_and_user
({
65 template_name
=> $template_name,
72 my $referer = $query->param('referer') || $op;
73 my $category = $query->param('category') || 1;
74 my ( $shelf, $shelfnumber, @messages );
76 if ( $op eq 'add_form' ) {
78 $shelf = { allow_change_from_owner
=> 1 };
79 } elsif ( $op eq 'edit_form' ) {
80 $shelfnumber = $query->param('shelfnumber');
81 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
84 $category = $shelf->category;
85 my $patron = Koha
::Patrons
->find( $shelf->owner );
86 $template->param( owner
=> $patron, );
87 unless ( $shelf->can_be_managed( $loggedinuser ) ) {
88 push @messages, { type
=> 'error', code
=> 'unauthorized_on_update' };
92 push @messages, { type
=> 'error', code
=> 'does_not_exist' };
94 } elsif ( $op eq 'add' ) {
95 if ( $loggedinuser ) {
96 my $allow_changes_from = $query->param('allow_changes_from');
98 $shelf = Koha
::Virtualshelf
->new(
99 { shelfname
=> scalar $query->param('shelfname'),
100 sortfield
=> scalar $query->param('sortfield'),
101 category
=> scalar $query->param('category') || 1,
102 allow_change_from_owner
=> $allow_changes_from > 0,
103 allow_change_from_others
=> $allow_changes_from == ANYONE
,
104 owner
=> scalar $loggedinuser,
108 $shelfnumber = $shelf->shelfnumber;
111 push @messages, { type
=> 'error', code
=> ref($@
), msg
=> $@
};
112 } elsif ( not $shelf ) {
113 push @messages, { type
=> 'error', code
=> 'error_on_insert' };
115 push @messages, { type
=> 'message', code
=> 'success_on_insert' };
119 push @messages, { type
=> 'error', code
=> 'unauthorized_on_insert' };
122 } elsif ( $op eq 'edit' ) {
123 $shelfnumber = $query->param('shelfnumber');
124 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
127 my $sortfield = $query->param('sortfield');
128 $sortfield = 'title' unless grep { $_ eq $sortfield } qw( title author copyrightdate itemcallnumber dateadded );
129 if ( $shelf->can_be_managed( $loggedinuser ) ) {
130 $shelf->shelfname( scalar $query->param('shelfname') );
131 $shelf->sortfield( $sortfield );
132 my $allow_changes_from = $query->param('allow_changes_from');
133 $shelf->allow_change_from_owner( $allow_changes_from > 0 );
134 $shelf->allow_change_from_others( $allow_changes_from == ANYONE
);
135 $shelf->category( scalar $query->param('category') );
136 eval { $shelf->store };
139 push @messages, { type
=> 'error', code
=> 'error_on_update' };
142 push @messages, { type
=> 'message', code
=> 'success_on_update' };
145 push @messages, { type
=> 'error', code
=> 'unauthorized_on_update' };
148 push @messages, { type
=> 'error', code
=> 'does_not_exist' };
150 } elsif ( $op eq 'delete' ) {
151 $shelfnumber = $query->param('shelfnumber');
152 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
154 if ( $shelf->can_be_deleted( $loggedinuser ) ) {
155 eval { $shelf->delete; };
157 push @messages, { type
=> 'error', code
=> ref($@
), msg
=> $@
};
159 push @messages, { type
=> 'message', code
=> 'success_on_delete' };
162 push @messages, { type
=> 'error', code
=> 'unauthorized_on_delete' };
165 push @messages, { type
=> 'error', code
=> 'does_not_exist' };
168 } elsif ( $op eq 'remove_share' ) {
169 $shelfnumber = $query->param('shelfnumber');
170 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
172 my $removed = eval { $shelf->remove_share( $loggedinuser ); };
174 push @messages, { type
=> 'error', code
=> ref($@
), msg
=> $@
};
175 } elsif ( $removed ) {
176 push @messages, { type
=> 'message', code
=> 'success_on_remove_share' };
178 push @messages, { type
=> 'error', code
=> 'error_on_remove_share' };
181 push @messages, { type
=> 'error', code
=> 'does_not_exist' };
185 } elsif ( $op eq 'add_biblio' ) {
186 $shelfnumber = $query->param('shelfnumber');
187 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
189 if( my $barcode = $query->param('barcode') ) {
190 my $item = Koha
::Items
->find({ barcode
=> $barcode });
192 if ( $shelf->can_biblios_be_added( $loggedinuser ) ) {
193 my $added = eval { $shelf->add_biblio( $item->biblionumber, $loggedinuser ); };
195 push @messages, { type
=> 'error', code
=> ref($@
), msg
=> $@
};
197 push @messages, { type
=> 'message', code
=> 'success_on_add_biblio' };
199 push @messages, { type
=> 'message', code
=> 'error_on_add_biblio' };
202 push @messages, { type
=> 'error', code
=> 'unauthorized_on_add_biblio' };
205 push @messages, { type
=> 'error', code
=> 'item_does_not_exist' };
209 push @messages, { type
=> 'error', code
=> 'does_not_exist' };
212 } elsif ( $op eq 'remove_biblios' ) {
213 $shelfnumber = $query->param('shelfnumber');
214 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
215 my @biblionumber = $query->multi_param('biblionumber');
217 if ( $shelf->can_biblios_be_removed( $loggedinuser ) ) {
218 my $number_of_biblios_removed = eval {
219 $shelf->remove_biblios(
221 biblionumbers
=> \
@biblionumber,
222 borrowernumber
=> $loggedinuser,
227 push @messages, { type
=> 'error', code
=> ref($@
), msg
=> $@
};
228 } elsif ( $number_of_biblios_removed ) {
229 push @messages, { type
=> 'message', code
=> 'success_on_remove_biblios' };
231 push @messages, { type
=> 'error', code
=> 'no_biblio_removed' };
234 push @messages, { type
=> 'error', code
=> 'unauthorized_on_remove_biblios' };
237 push @messages, { type
=> 'error', code
=> 'does_not_exist' };
242 if ( $op eq 'view' ) {
243 $shelfnumber ||= $query->param('shelfnumber');
244 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
246 if ( $shelf->can_be_viewed( $loggedinuser ) ) {
247 $category = $shelf->category;
248 my( $sortfield, $direction );
249 if( defined( $query->param('sortfield') ) ){ # Passed in sorting overrides default sorting
250 ( $sortfield, $direction ) = split /:/, $query->param('sortfield');
252 $sortfield = $shelf->sortfield;
255 $sortfield = 'title' unless grep $_ eq $sortfield, qw( title author copyrightdate itemcallnumber dateadded );
256 $direction = 'asc' if $direction ne 'asc' and $direction ne 'desc';
258 unless ( $query->param('print') or $query->param('rss') ) {
259 $rows = C4
::Context
->preference('OPACnumSearchResults') || 20;
260 $page = ( $query->param('page') ?
$query->param('page') : 1 );
262 my $order_by = $sortfield eq 'itemcallnumber' ?
'items.cn_sort' : $sortfield;
263 my $contents = $shelf->get_contents->search(
266 prefetch
=> [ { 'biblionumber' => { 'biblioitems' => 'items' } } ],
269 order_by
=> { "-$direction" => $order_by },
273 # get biblionumbers stored in the cart
275 if(my $cart_list = $query->cookie('bib_list')){
276 @cart_list = split(/\//, $cart_list);
279 my $patron = Koha
::Patrons
->find( $loggedinuser );
281 my $categorycode; # needed for may_article_request
282 if( C4
::Context
->preference('ArticleRequests') ) {
283 $categorycode = $patron ?
$patron->categorycode : undef;
286 # Lists display falls back to search results configuration
287 my $xslfile = C4
::Context
->preference('OPACXSLTListsDisplay');
288 my $lang = $xslfile ? C4
::Languages
::getlanguage
() : undef;
289 my $sysxml = $xslfile ? C4
::XSLT
::get_xslt_sysprefs
() : undef;
291 my $record_processor = Koha
::RecordProcessor
->new({ filters
=> 'ViewPolicy' });
294 if( C4
::Context
->preference('ArticleRequests') ) {
295 $art_req_itypes = Koha
::CirculationRules
->guess_article_requestable_itemtypes({ $patron ?
( categorycode
=> $patron->categorycode ) : () });
299 while ( my $content = $contents->next ) {
300 my $biblionumber = $content->biblionumber;
301 my $this_item = GetBiblioData
($biblionumber);
302 my $record = GetMarcBiblio
({ biblionumber
=> $biblionumber });
303 my $framework = GetFrameworkCode
( $biblionumber );
304 my $biblio = Koha
::Biblios
->find( $biblionumber );
305 $record_processor->options({
307 frameworkcode
=> $framework
309 $record_processor->process($record);
312 $this_item->{XSLTBloc
} = XSLTParse4Display
( $biblionumber, $record, "OPACXSLTListsDisplay",
313 1, undef, $sysxml, $xslfile, $lang);
316 my $marcflavour = C4
::Context
->preference("marcflavour");
317 my $itemtype = Koha
::Biblioitems
->search({ biblionumber
=> $content->biblionumber })->next->itemtype;
318 $itemtype = Koha
::ItemTypes
->find( $itemtype );
320 $this_item->{imageurl
} = C4
::Koha
::getitemtypeimagelocation
( 'opac', $itemtype->imageurl );
321 $this_item->{description
} = $itemtype->description; #FIXME Should not it be translated_description?
322 $this_item->{notforloan
} = $itemtype->notforloan;
324 $this_item->{'coins'} = $biblio->get_coins;
325 $this_item->{'normalized_upc'} = GetNormalizedUPC
( $record, $marcflavour );
326 $this_item->{'normalized_ean'} = GetNormalizedEAN
( $record, $marcflavour );
327 $this_item->{'normalized_oclc'} = GetNormalizedOCLCNumber
( $record, $marcflavour );
328 $this_item->{'normalized_isbn'} = GetNormalizedISBN
( undef, $record, $marcflavour );
329 # BZ17530: 'Intelligent' guess if result can be article requested
330 $this_item->{artreqpossible
} = ( $art_req_itypes->{ $this_item->{itemtype
} // q{} } || $art_req_itypes->{ '*' } ) ?
1 : q{};
332 unless ( defined $this_item->{size
} ) {
334 #TT has problems with size
335 $this_item->{size
} = q
||;
338 # Getting items infos for location display
339 my @items_infos = &GetItemsLocationInfo
( $biblionumber );
340 $this_item->{'ITEM_RESULTS'} = \
@items_infos;
342 if (C4
::Context
->preference('TagsEnabled') and C4
::Context
->preference('TagsShowOnList')) {
343 $this_item->{TagLoop
} = get_tags
({
344 biblionumber
=> $biblionumber, approved
=>1, 'sort'=>'-weight',
345 limit
=> C4
::Context
->preference('TagsShowOnList'),
349 my $items = $biblio->items;
350 while ( my $item = $items->next ) {
351 $this_item->{allow_onshelf_holds
} = Koha
::CirculationRules
->get_onshelfholds_policy( { item
=> $item, patron
=> $patron } );
352 last if $this_item->{allow_onshelf_holds
};
355 if ( grep {$_ eq $biblionumber} @cart_list) {
356 $this_item->{incart
} = 1;
359 $this_item->{biblio_object
} = $biblio;
360 $this_item->{biblionumber
} = $biblionumber;
361 push @items, $this_item;
365 can_manage_shelf
=> $shelf->can_be_managed($loggedinuser),
366 can_delete_shelf
=> $shelf->can_be_deleted($loggedinuser),
367 can_remove_biblios
=> $shelf->can_biblios_be_removed($loggedinuser),
368 can_add_biblios
=> $shelf->can_biblios_be_added($loggedinuser),
369 itemsloop
=> \
@items,
370 sortfield
=> $sortfield,
371 direction
=> $direction,
374 my $pager = $contents->pager;
376 pagination_bar
=> pagination_bar
(
377 q
||, $pager->last_page - $pager->first_page + 1,
378 $page, "page", { op
=> 'view', shelfnumber
=> $shelf->shelfnumber, sortfield
=> $sortfield, direction
=> $direction, }
383 push @messages, { type
=> 'error', code
=> 'unauthorized_on_view' };
387 push @messages, { type
=> 'error', code
=> 'does_not_exist' };
391 if ( $op eq 'list' ) {
393 my ( $page, $rows ) = ( $query->param('page') || 1, 20 );
394 if ( $category == 1 ) {
395 $shelves = Koha
::Virtualshelves
->get_private_shelves({ page
=> $page, rows
=> $rows, borrowernumber
=> $loggedinuser, });
397 $shelves = Koha
::Virtualshelves
->get_public_shelves({ page
=> $page, rows
=> $rows, });
400 my $pager = $shelves->pager;
403 pagination_bar
=> pagination_bar
(
404 q
||, $pager->last_page - $pager->first_page + 1,
405 $page, "page", { op
=> 'list', category
=> $category, }
414 messages
=> \
@messages,
415 category
=> $category,
416 print => scalar $query->param('print') || 0,
420 output_html_with_http_headers
$query, $cookie, $template->output;