Bug 26846: Fix regressions - Preselect expected and late serials
[koha.git] / opac / opac-shelves.pl
blobb94b4126ea5413a52217ce29f7aa2f87c4e83f78
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::External::BakerTaylor qw( image_url link_url );
26 use C4::Koha;
27 use C4::Items;
28 use C4::Members;
29 use C4::Output;
30 use C4::Tags qw( get_tags );
31 use C4::XSLT;
33 use Koha::Biblios;
34 use Koha::Biblioitems;
35 use Koha::CirculationRules;
36 use Koha::CsvProfiles;
37 use Koha::Items;
38 use Koha::ItemTypes;
39 use Koha::Patrons;
40 use Koha::Virtualshelves;
41 use Koha::RecordProcessor;
43 use constant ANYONE => 2;
45 my $query = CGI->new;
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");
52 exit;
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,
61 query => $query,
62 type => "opac",
63 authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
64 });
65 } else {
66 ( $template, $loggedinuser, $cookie ) = get_template_and_user({
67 template_name => $template_name,
68 query => $query,
69 type => "opac",
70 authnotrequired => 0,
71 });
74 if (C4::Context->preference("BakerTaylorEnabled")) {
75 $template->param(
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' ) {
86 # Only pass default
87 $shelf = { allow_change_from_owner => 1 };
88 } elsif ( $op eq 'edit_form' ) {
89 $shelfnumber = $query->param('shelfnumber');
90 $shelf = Koha::Virtualshelves->find($shelfnumber);
92 if ( $shelf ) {
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' };
98 $op = 'list';
100 } else {
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');
106 eval {
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,
116 $shelf->store;
117 $shelfnumber = $shelf->shelfnumber;
119 if ($@) {
120 push @messages, { type => 'error', code => ref($@), msg => $@ };
121 } elsif ( not $shelf ) {
122 push @messages, { type => 'error', code => 'error_on_insert' };
123 } else {
124 push @messages, { type => 'message', code => 'success_on_insert' };
125 $op = 'view';
127 } else {
128 push @messages, { type => 'error', code => 'unauthorized_on_insert' };
129 $op = 'list';
131 } elsif ( $op eq 'edit' ) {
132 $shelfnumber = $query->param('shelfnumber');
133 $shelf = Koha::Virtualshelves->find($shelfnumber);
134 if ( $shelf ) {
135 $op = $referer;
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 };
147 if ($@) {
148 push @messages, { type => 'error', code => 'error_on_update' };
149 $op = 'edit_form';
150 } else {
151 push @messages, { type => 'message', code => 'success_on_update' };
153 } else {
154 push @messages, { type => 'error', code => 'unauthorized_on_update' };
156 } else {
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);
162 if ($shelf) {
163 if ( $shelf->can_be_deleted( $loggedinuser ) ) {
164 eval { $shelf->delete; };
165 if ($@) {
166 push @messages, { type => 'error', code => ref($@), msg => $@ };
167 } else {
168 push @messages, { type => 'message', code => 'success_on_delete' };
170 } else {
171 push @messages, { type => 'error', code => 'unauthorized_on_delete' };
173 } else {
174 push @messages, { type => 'error', code => 'does_not_exist' };
176 $op = $referer;
177 } elsif ( $op eq 'remove_share' ) {
178 $shelfnumber = $query->param('shelfnumber');
179 $shelf = Koha::Virtualshelves->find($shelfnumber);
180 if ($shelf) {
181 my $removed = eval { $shelf->remove_share( $loggedinuser ); };
182 if ($@) {
183 push @messages, { type => 'error', code => ref($@), msg => $@ };
184 } elsif ( $removed ) {
185 push @messages, { type => 'message', code => 'success_on_remove_share' };
186 } else {
187 push @messages, { type => 'error', code => 'error_on_remove_share' };
189 } else {
190 push @messages, { type => 'error', code => 'does_not_exist' };
192 $op = $referer;
194 } elsif ( $op eq 'add_biblio' ) {
195 $shelfnumber = $query->param('shelfnumber');
196 $shelf = Koha::Virtualshelves->find($shelfnumber);
197 if ($shelf) {
198 if( my $barcode = $query->param('barcode') ) {
199 my $item = Koha::Items->find({ barcode => $barcode });
200 if ( $item ) {
201 if ( $shelf->can_biblios_be_added( $loggedinuser ) ) {
202 my $added = eval { $shelf->add_biblio( $item->biblionumber, $loggedinuser ); };
203 if ($@) {
204 push @messages, { type => 'error', code => ref($@), msg => $@ };
205 } elsif ( $added ) {
206 push @messages, { type => 'message', code => 'success_on_add_biblio' };
207 } else {
208 push @messages, { type => 'message', code => 'error_on_add_biblio' };
210 } else {
211 push @messages, { type => 'error', code => 'unauthorized_on_add_biblio' };
213 } else {
214 push @messages, { type => 'error', code => 'item_does_not_exist' };
217 } else {
218 push @messages, { type => 'error', code => 'does_not_exist' };
220 $op = $referer;
221 } elsif ( $op eq 'remove_biblios' ) {
222 $shelfnumber = $query->param('shelfnumber');
223 $shelf = Koha::Virtualshelves->find($shelfnumber);
224 my @biblionumber = $query->multi_param('biblionumber');
225 if ($shelf) {
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,
235 if ($@) {
236 push @messages, { type => 'error', code => ref($@), msg => $@ };
237 } elsif ( $number_of_biblios_removed ) {
238 push @messages, { type => 'message', code => 'success_on_remove_biblios' };
239 } else {
240 push @messages, { type => 'error', code => 'no_biblio_removed' };
242 } else {
243 push @messages, { type => 'error', code => 'unauthorized_on_remove_biblios' };
245 } else {
246 push @messages, { type => 'error', code => 'does_not_exist' };
248 $op = 'view';
251 if ( $op eq 'view' ) {
252 $shelfnumber ||= $query->param('shelfnumber');
253 $shelf = Koha::Virtualshelves->find($shelfnumber);
254 if ( $shelf ) {
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');
260 } else {
261 $sortfield = $shelf->sortfield;
262 $direction = 'asc';
264 $sortfield = 'title' unless grep $_ eq $sortfield, qw( title author copyrightdate itemcallnumber dateadded );
265 $direction = 'asc' if $direction ne 'asc' and $direction ne 'desc';
266 my ( $page, $rows );
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' } } ],
276 page => $page,
277 rows => $rows,
278 order_by => { "-$direction" => $order_by },
282 # get biblionumbers stored in the cart
283 my @cart_list;
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' });
302 my $art_req_itypes;
303 if( C4::Context->preference('ArticleRequests') ) {
304 $art_req_itypes = Koha::CirculationRules->guess_article_requestable_itemtypes({ $patron ? ( categorycode => $patron->categorycode ) : () });
307 my @items;
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({
315 interface => 'opac',
316 frameworkcode => $framework
318 $record_processor->process($record);
320 if ($xslfile) {
321 my $variables = {
322 anonymous_session => ($loggedinuser) ? 0 : 1
324 $this_item->{XSLTBloc} = XSLTParse4Display(
325 $biblionumber, $record,
326 "OPACXSLTListsDisplay", 1,
327 undef, $sysxml,
328 $xslfile, $lang,
329 $variables
333 my $marcflavour = C4::Context->preference("marcflavour");
334 my $itemtype = Koha::Biblioitems->search({ biblionumber => $content->biblionumber })->next->itemtype;
335 $itemtype = Koha::ItemTypes->find( $itemtype );
336 if( $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;
381 $template->param(
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,
389 csv_profiles => [
390 Koha::CsvProfiles->search(
391 { type => 'marc', used_for => 'export_records', staff_only => 0 }
395 if ( $page ) {
396 my $pager = $contents->pager;
397 $template->param(
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, }
404 } else {
405 push @messages, { type => 'error', code => 'unauthorized_on_view' };
406 undef $shelf;
408 } else {
409 push @messages, { type => 'error', code => 'does_not_exist' };
413 if ( $op eq 'list' ) {
414 my $shelves;
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, });
418 } else {
419 $shelves = Koha::Virtualshelves->get_public_shelves({ page => $page, rows => $rows, });
422 my $pager = $shelves->pager;
423 $template->param(
424 shelves => $shelves,
425 pagination_bar => pagination_bar(
426 q||, $pager->last_page - $pager->first_page + 1,
427 $page, "page", { op => 'list', category => $category, }
432 $template->param(
433 op => $op,
434 referer => $referer,
435 shelf => $shelf,
436 messages => \@messages,
437 category => $category,
438 print => scalar $query->param('print') || 0,
439 listsview => 1,
442 my $content_type = $query->param('rss')? 'rss' : 'html';
443 output_with_http_headers $query, $cookie, $template->output, $content_type;