Bug 18433: Remember item search results selected rows in session storage
[koha.git] / opac / opac-shelves.pl
blobcd04709af34745d852a0f5c84d2caeb00791573b
1 #!/usr/bin/perl
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>.
20 use Modern::Perl;
22 use CGI qw ( -utf8 );
23 use C4::Auth;
24 use C4::Biblio;
25 use C4::Koha;
26 use C4::Items;
27 use C4::Members;
28 use C4::Output;
29 use C4::Tags qw( get_tags );
30 use C4::XSLT;
32 use Koha::Biblios;
33 use Koha::Biblioitems;
34 use Koha::CirculationRules;
35 use Koha::Items;
36 use Koha::ItemTypes;
37 use Koha::Patrons;
38 use Koha::Virtualshelves;
39 use Koha::RecordProcessor;
41 use constant ANYONE => 2;
43 my $query = new CGI;
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");
50 exit;
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,
59 query => $query,
60 type => "opac",
61 authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
62 });
63 } else {
64 ( $template, $loggedinuser, $cookie ) = get_template_and_user({
65 template_name => $template_name,
66 query => $query,
67 type => "opac",
68 authnotrequired => 0,
69 });
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' ) {
77 # Only pass default
78 $shelf = { allow_change_from_owner => 1 };
79 } elsif ( $op eq 'edit_form' ) {
80 $shelfnumber = $query->param('shelfnumber');
81 $shelf = Koha::Virtualshelves->find($shelfnumber);
83 if ( $shelf ) {
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' };
89 $op = 'list';
91 } else {
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');
97 eval {
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,
107 $shelf->store;
108 $shelfnumber = $shelf->shelfnumber;
110 if ($@) {
111 push @messages, { type => 'error', code => ref($@), msg => $@ };
112 } elsif ( not $shelf ) {
113 push @messages, { type => 'error', code => 'error_on_insert' };
114 } else {
115 push @messages, { type => 'message', code => 'success_on_insert' };
116 $op = 'view';
118 } else {
119 push @messages, { type => 'error', code => 'unauthorized_on_insert' };
120 $op = 'list';
122 } elsif ( $op eq 'edit' ) {
123 $shelfnumber = $query->param('shelfnumber');
124 $shelf = Koha::Virtualshelves->find($shelfnumber);
125 if ( $shelf ) {
126 $op = $referer;
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 };
138 if ($@) {
139 push @messages, { type => 'error', code => 'error_on_update' };
140 $op = 'edit_form';
141 } else {
142 push @messages, { type => 'message', code => 'success_on_update' };
144 } else {
145 push @messages, { type => 'error', code => 'unauthorized_on_update' };
147 } else {
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);
153 if ($shelf) {
154 if ( $shelf->can_be_deleted( $loggedinuser ) ) {
155 eval { $shelf->delete; };
156 if ($@) {
157 push @messages, { type => 'error', code => ref($@), msg => $@ };
158 } else {
159 push @messages, { type => 'message', code => 'success_on_delete' };
161 } else {
162 push @messages, { type => 'error', code => 'unauthorized_on_delete' };
164 } else {
165 push @messages, { type => 'error', code => 'does_not_exist' };
167 $op = $referer;
168 } elsif ( $op eq 'remove_share' ) {
169 $shelfnumber = $query->param('shelfnumber');
170 $shelf = Koha::Virtualshelves->find($shelfnumber);
171 if ($shelf) {
172 my $removed = eval { $shelf->remove_share( $loggedinuser ); };
173 if ($@) {
174 push @messages, { type => 'error', code => ref($@), msg => $@ };
175 } elsif ( $removed ) {
176 push @messages, { type => 'message', code => 'success_on_remove_share' };
177 } else {
178 push @messages, { type => 'error', code => 'error_on_remove_share' };
180 } else {
181 push @messages, { type => 'error', code => 'does_not_exist' };
183 $op = $referer;
185 } elsif ( $op eq 'add_biblio' ) {
186 $shelfnumber = $query->param('shelfnumber');
187 $shelf = Koha::Virtualshelves->find($shelfnumber);
188 if ($shelf) {
189 if( my $barcode = $query->param('barcode') ) {
190 my $item = Koha::Items->find({ barcode => $barcode });
191 if ( $item ) {
192 if ( $shelf->can_biblios_be_added( $loggedinuser ) ) {
193 my $added = eval { $shelf->add_biblio( $item->biblionumber, $loggedinuser ); };
194 if ($@) {
195 push @messages, { type => 'error', code => ref($@), msg => $@ };
196 } elsif ( $added ) {
197 push @messages, { type => 'message', code => 'success_on_add_biblio' };
198 } else {
199 push @messages, { type => 'message', code => 'error_on_add_biblio' };
201 } else {
202 push @messages, { type => 'error', code => 'unauthorized_on_add_biblio' };
204 } else {
205 push @messages, { type => 'error', code => 'item_does_not_exist' };
208 } else {
209 push @messages, { type => 'error', code => 'does_not_exist' };
211 $op = $referer;
212 } elsif ( $op eq 'remove_biblios' ) {
213 $shelfnumber = $query->param('shelfnumber');
214 $shelf = Koha::Virtualshelves->find($shelfnumber);
215 my @biblionumber = $query->multi_param('biblionumber');
216 if ($shelf) {
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,
226 if ($@) {
227 push @messages, { type => 'error', code => ref($@), msg => $@ };
228 } elsif ( $number_of_biblios_removed ) {
229 push @messages, { type => 'message', code => 'success_on_remove_biblios' };
230 } else {
231 push @messages, { type => 'error', code => 'no_biblio_removed' };
233 } else {
234 push @messages, { type => 'error', code => 'unauthorized_on_remove_biblios' };
236 } else {
237 push @messages, { type => 'error', code => 'does_not_exist' };
239 $op = 'view';
242 if ( $op eq 'view' ) {
243 $shelfnumber ||= $query->param('shelfnumber');
244 $shelf = Koha::Virtualshelves->find($shelfnumber);
245 if ( $shelf ) {
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');
251 } else {
252 $sortfield = $shelf->sortfield;
253 $direction = 'asc';
255 $sortfield = 'title' unless grep $_ eq $sortfield, qw( title author copyrightdate itemcallnumber dateadded );
256 $direction = 'asc' if $direction ne 'asc' and $direction ne 'desc';
257 my ( $page, $rows );
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' } } ],
267 page => $page,
268 rows => $rows,
269 order_by => { "-$direction" => $order_by },
273 # get biblionumbers stored in the cart
274 my @cart_list;
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' });
293 my $art_req_itypes;
294 if( C4::Context->preference('ArticleRequests') ) {
295 $art_req_itypes = Koha::CirculationRules->guess_article_requestable_itemtypes({ $patron ? ( categorycode => $patron->categorycode ) : () });
298 my @items;
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({
306 interface => 'opac',
307 frameworkcode => $framework
309 $record_processor->process($record);
311 if ( $xslfile ) {
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 );
319 if( $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;
364 $template->param(
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,
373 if ( $page ) {
374 my $pager = $contents->pager;
375 $template->param(
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, }
382 } else {
383 push @messages, { type => 'error', code => 'unauthorized_on_view' };
384 undef $shelf;
386 } else {
387 push @messages, { type => 'error', code => 'does_not_exist' };
391 if ( $op eq 'list' ) {
392 my $shelves;
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, });
396 } else {
397 $shelves = Koha::Virtualshelves->get_public_shelves({ page => $page, rows => $rows, });
400 my $pager = $shelves->pager;
401 $template->param(
402 shelves => $shelves,
403 pagination_bar => pagination_bar(
404 q||, $pager->last_page - $pager->first_page + 1,
405 $page, "page", { op => 'list', category => $category, }
410 $template->param(
411 op => $op,
412 referer => $referer,
413 shelf => $shelf,
414 messages => \@messages,
415 category => $category,
416 print => scalar $query->param('print') || 0,
417 listsview => 1,
420 output_html_with_http_headers $query, $cookie, $template->output;