Bug 19319: Only fetch the record if it exists
[koha.git] / opac / opac-shelves.pl
blob1feaf57caea20f3c77a7c930f092d1c6f4c21c8a
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::Items;
35 use Koha::ItemTypes;
36 use Koha::Patrons;
37 use Koha::Virtualshelves;
38 use Koha::RecordProcessor;
40 use constant ANYONE => 2;
42 my $query = new CGI;
44 my $template_name = $query->param('rss') ? "opac-shelves-rss.tt" : "opac-shelves.tt";
46 # if virtualshelves is disabled, leave immediately
47 if ( ! C4::Context->preference('virtualshelves') ) {
48 print $query->redirect("/cgi-bin/koha/errors/404.pl");
49 exit;
52 my ( $template, $loggedinuser, $cookie ) = get_template_and_user({
53 template_name => $template_name,
54 query => $query,
55 type => "opac",
56 authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
57 });
59 my $op = $query->param('op') || 'list';
60 my $referer = $query->param('referer') || $op;
61 my $category = $query->param('category') || 1;
62 my ( $shelf, $shelfnumber, @messages );
64 if ( $op eq 'add_form' ) {
65 # Only pass default
66 $shelf = { allow_change_from_owner => 1 };
67 } elsif ( $op eq 'edit_form' ) {
68 $shelfnumber = $query->param('shelfnumber');
69 $shelf = Koha::Virtualshelves->find($shelfnumber);
71 if ( $shelf ) {
72 $category = $shelf->category;
73 my $patron = Koha::Patrons->find( $shelf->owner );
74 $template->param( owner => $patron, );
75 unless ( $shelf->can_be_managed( $loggedinuser ) ) {
76 push @messages, { type => 'error', code => 'unauthorized_on_update' };
77 $op = 'list';
79 } else {
80 push @messages, { type => 'error', code => 'does_not_exist' };
82 } elsif ( $op eq 'add' ) {
83 if ( $loggedinuser ) {
84 my $allow_changes_from = $query->param('allow_changes_from');
85 eval {
86 $shelf = Koha::Virtualshelf->new(
87 { shelfname => scalar $query->param('shelfname'),
88 sortfield => scalar $query->param('sortfield'),
89 category => scalar $query->param('category') || 1,
90 allow_change_from_owner => $allow_changes_from > 0,
91 allow_change_from_others => $allow_changes_from == ANYONE,
92 owner => scalar $loggedinuser,
95 $shelf->store;
96 $shelfnumber = $shelf->shelfnumber;
98 if ($@) {
99 push @messages, { type => 'error', code => ref($@), msg => $@ };
100 } elsif ( not $shelf ) {
101 push @messages, { type => 'error', code => 'error_on_insert' };
102 } else {
103 push @messages, { type => 'message', code => 'success_on_insert' };
104 $op = 'view';
106 } else {
107 push @messages, { type => 'error', code => 'unauthorized_on_insert' };
108 $op = 'list';
110 } elsif ( $op eq 'edit' ) {
111 $shelfnumber = $query->param('shelfnumber');
112 $shelf = Koha::Virtualshelves->find($shelfnumber);
113 if ( $shelf ) {
114 $op = $referer;
115 my $sortfield = $query->param('sortfield');
116 $sortfield = 'title' unless grep {/^$sortfield$/}qw( title author copyrightdate itemcallnumber );
117 if ( $shelf->can_be_managed( $loggedinuser ) ) {
118 $shelf->shelfname( scalar $query->param('shelfname') );
119 $shelf->sortfield( $sortfield );
120 my $allow_changes_from = $query->param('allow_changes_from');
121 $shelf->allow_change_from_owner( $allow_changes_from > 0 );
122 $shelf->allow_change_from_others( $allow_changes_from == ANYONE );
123 $shelf->category( scalar $query->param('category') );
124 eval { $shelf->store };
126 if ($@) {
127 push @messages, { type => 'error', code => 'error_on_update' };
128 $op = 'edit_form';
129 } else {
130 push @messages, { type => 'message', code => 'success_on_update' };
132 } else {
133 push @messages, { type => 'error', code => 'unauthorized_on_update' };
135 } else {
136 push @messages, { type => 'error', code => 'does_not_exist' };
138 } elsif ( $op eq 'delete' ) {
139 $shelfnumber = $query->param('shelfnumber');
140 $shelf = Koha::Virtualshelves->find($shelfnumber);
141 if ($shelf) {
142 if ( $shelf->can_be_deleted( $loggedinuser ) ) {
143 eval { $shelf->delete; };
144 if ($@) {
145 push @messages, { type => 'error', code => ref($@), msg => $@ };
146 } else {
147 push @messages, { type => 'message', code => 'success_on_delete' };
149 } else {
150 push @messages, { type => 'error', code => 'unauthorized_on_delete' };
152 } else {
153 push @messages, { type => 'error', code => 'does_not_exist' };
155 $op = $referer;
156 } elsif ( $op eq 'remove_share' ) {
157 $shelfnumber = $query->param('shelfnumber');
158 $shelf = Koha::Virtualshelves->find($shelfnumber);
159 if ($shelf) {
160 my $removed = eval { $shelf->remove_share( $loggedinuser ); };
161 if ($@) {
162 push @messages, { type => 'error', code => ref($@), msg => $@ };
163 } elsif ( $removed ) {
164 push @messages, { type => 'message', code => 'success_on_remove_share' };
165 } else {
166 push @messages, { type => 'error', code => 'error_on_remove_share' };
168 } else {
169 push @messages, { type => 'error', code => 'does_not_exist' };
171 $op = $referer;
173 } elsif ( $op eq 'add_biblio' ) {
174 $shelfnumber = $query->param('shelfnumber');
175 $shelf = Koha::Virtualshelves->find($shelfnumber);
176 if ($shelf) {
177 if( my $barcode = $query->param('barcode') ) {
178 my $item = GetItem( 0, $barcode);
179 if (defined $item && $item->{itemnumber}) {
180 if ( $shelf->can_biblios_be_added( $loggedinuser ) ) {
181 my $added = eval { $shelf->add_biblio( $item->{biblionumber}, $loggedinuser ); };
182 if ($@) {
183 push @messages, { type => 'error', code => ref($@), msg => $@ };
184 } elsif ( $added ) {
185 push @messages, { type => 'message', code => 'success_on_add_biblio' };
186 } else {
187 push @messages, { type => 'message', code => 'error_on_add_biblio' };
189 } else {
190 push @messages, { type => 'error', code => 'unauthorized_on_add_biblio' };
192 } else {
193 push @messages, { type => 'error', code => 'item_does_not_exist' };
196 } else {
197 push @messages, { type => 'error', code => 'does_not_exist' };
199 $op = $referer;
200 } elsif ( $op eq 'remove_biblios' ) {
201 $shelfnumber = $query->param('shelfnumber');
202 $shelf = Koha::Virtualshelves->find($shelfnumber);
203 my @biblionumber = $query->multi_param('biblionumber');
204 if ($shelf) {
205 if ( $shelf->can_biblios_be_removed( $loggedinuser ) ) {
206 my $number_of_biblios_removed = eval {
207 $shelf->remove_biblios(
209 biblionumbers => \@biblionumber,
210 borrowernumber => $loggedinuser,
214 if ($@) {
215 push @messages, { type => 'error', code => ref($@), msg => $@ };
216 } elsif ( $number_of_biblios_removed ) {
217 push @messages, { type => 'message', code => 'success_on_remove_biblios' };
218 } else {
219 push @messages, { type => 'error', code => 'no_biblio_removed' };
221 } else {
222 push @messages, { type => 'error', code => 'unauthorized_on_remove_biblios' };
224 } else {
225 push @messages, { type => 'error', code => 'does_not_exist' };
227 $op = 'view';
230 if ( $op eq 'view' ) {
231 $shelfnumber ||= $query->param('shelfnumber');
232 $shelf = Koha::Virtualshelves->find($shelfnumber);
233 if ( $shelf ) {
234 if ( $shelf->can_be_viewed( $loggedinuser ) ) {
235 $category = $shelf->category;
236 my $sortfield = $query->param('sortfield') || $shelf->sortfield; # Passed in sorting overrides default sorting
237 $sortfield = 'title' unless grep {/^$sortfield$/}qw( title author copyrightdate itemcallnumber );
238 my $direction = $query->param('direction') || 'asc';
239 $direction = 'asc' if $direction ne 'asc' and $direction ne 'desc';
240 my ( $page, $rows );
241 unless ( $query->param('print') or $query->param('rss') ) {
242 $rows = C4::Context->preference('OPACnumSearchResults') || 20;
243 $page = ( $query->param('page') ? $query->param('page') : 1 );
245 my $order_by = $sortfield eq 'itemcallnumber' ? 'items.itemcallnumber' : $sortfield;
246 my $contents = $shelf->get_contents->search(
249 prefetch => [ { 'biblionumber' => { 'biblioitems' => 'items' } } ],
250 page => $page,
251 rows => $rows,
252 order_by => { "-$direction" => $order_by },
256 # get biblionumbers stored in the cart
257 my @cart_list;
258 if(my $cart_list = $query->cookie('bib_list')){
259 @cart_list = split(/\//, $cart_list);
262 my $patron = Koha::Patrons->find( $loggedinuser );
264 # Lists display falls back to search results configuration
265 my $xslfile = C4::Context->preference('OPACXSLTListsDisplay');
266 my $lang = $xslfile ? C4::Languages::getlanguage() : undef;
267 my $sysxml = $xslfile ? C4::XSLT::get_xslt_sysprefs() : undef;
269 my $record_processor = Koha::RecordProcessor->new({ filters => 'ViewPolicy' });
270 my @items;
271 while ( my $content = $contents->next ) {
272 my $biblionumber = $content->biblionumber;
273 my $this_item = GetBiblioData($biblionumber);
274 my $record = GetMarcBiblio({ biblionumber => $biblionumber });
275 my $framework = GetFrameworkCode( $biblionumber );
276 my $biblio = Koha::Biblios->find( $biblionumber );
277 $record_processor->options({
278 interface => 'opac',
279 frameworkcode => $framework
281 $record_processor->process($record);
283 if ( $xslfile ) {
284 $this_item->{XSLTBloc} = XSLTParse4Display( $biblionumber, $record, "OPACXSLTListsDisplay",
285 1, undef, $sysxml, $xslfile, $lang);
288 my $marcflavour = C4::Context->preference("marcflavour");
289 my $itemtype = Koha::Biblioitems->search({ biblionumber => $content->biblionumber })->next->itemtype;
290 $itemtype = Koha::ItemTypes->find( $itemtype );
291 if( $itemtype ) {
292 $this_item->{imageurl} = C4::Koha::getitemtypeimagelocation( 'opac', $itemtype->imageurl );
293 $this_item->{description} = $itemtype->description; #FIXME Should not it be translated_description?
294 $this_item->{notforloan} = $itemtype->notforloan;
296 $this_item->{'coins'} = GetCOinSBiblio($record);
297 $this_item->{'subtitle'} = GetRecordValue( 'subtitle', $record, GetFrameworkCode( $biblionumber ) );
298 $this_item->{'normalized_upc'} = GetNormalizedUPC( $record, $marcflavour );
299 $this_item->{'normalized_ean'} = GetNormalizedEAN( $record, $marcflavour );
300 $this_item->{'normalized_oclc'} = GetNormalizedOCLCNumber( $record, $marcflavour );
301 $this_item->{'normalized_isbn'} = GetNormalizedISBN( undef, $record, $marcflavour );
303 unless ( defined $this_item->{size} ) {
305 #TT has problems with size
306 $this_item->{size} = q||;
309 # Getting items infos for location display
310 my @items_infos = &GetItemsLocationInfo( $biblionumber );
311 $this_item->{'ITEM_RESULTS'} = \@items_infos;
313 if (C4::Context->preference('TagsEnabled') and C4::Context->preference('TagsShowOnList')) {
314 $this_item->{TagLoop} = get_tags({
315 biblionumber => $biblionumber, approved=>1, 'sort'=>'-weight',
316 limit => C4::Context->preference('TagsShowOnList'),
320 my $items = $biblio->items;
321 while ( my $item = $items->next ) {
322 $this_item->{allow_onshelf_holds} = C4::Reserves::OnShelfHoldsAllowed($item->unblessed, $patron);
323 last if $this_item->{allow_onshelf_holds};
326 if ( grep {$_ eq $biblionumber} @cart_list) {
327 $this_item->{incart} = 1;
330 $this_item->{biblionumber} = $biblionumber;
331 push @items, $this_item;
334 $template->param(
335 can_manage_shelf => $shelf->can_be_managed($loggedinuser),
336 can_delete_shelf => $shelf->can_be_deleted($loggedinuser),
337 can_remove_biblios => $shelf->can_biblios_be_removed($loggedinuser),
338 can_add_biblios => $shelf->can_biblios_be_added($loggedinuser),
339 itemsloop => \@items,
340 sortfield => $sortfield,
341 direction => $direction,
343 if ( $page ) {
344 my $pager = $contents->pager;
345 $template->param(
346 pagination_bar => pagination_bar(
347 q||, $pager->last_page - $pager->first_page + 1,
348 $page, "page", { op => 'view', shelfnumber => $shelf->shelfnumber, sortfield => $sortfield, direction => $direction, }
352 } else {
353 push @messages, { type => 'error', code => 'unauthorized_on_view' };
354 undef $shelf;
356 } else {
357 push @messages, { type => 'error', code => 'does_not_exist' };
361 if ( $op eq 'list' ) {
362 my $shelves;
363 my ( $page, $rows ) = ( $query->param('page') || 1, 20 );
364 if ( $category == 1 ) {
365 $shelves = Koha::Virtualshelves->get_private_shelves({ page => $page, rows => $rows, borrowernumber => $loggedinuser, });
366 } else {
367 $shelves = Koha::Virtualshelves->get_public_shelves({ page => $page, rows => $rows, });
370 my $pager = $shelves->pager;
371 $template->param(
372 shelves => $shelves,
373 pagination_bar => pagination_bar(
374 q||, $pager->last_page - $pager->first_page + 1,
375 $page, "page", { op => 'list', category => $category, }
380 $template->param(
381 op => $op,
382 referer => $referer,
383 shelf => $shelf,
384 messages => \@messages,
385 category => $category,
386 print => scalar $query->param('print') || 0,
387 listsview => 1,
390 output_html_with_http_headers $query, $cookie, $template->output;