Bug 20434: Update UNIMARC framework - auth (NP)
[koha.git] / opac / opac-shelves.pl
blob8ae84e414d77aa3c20de47f87ae78825c0a00a45
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::IssuingRules;
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 ( $template, $loggedinuser, $cookie ) = get_template_and_user({
54 template_name => $template_name,
55 query => $query,
56 type => "opac",
57 authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
58 });
60 my $op = $query->param('op') || 'list';
61 my $referer = $query->param('referer') || $op;
62 my $category = $query->param('category') || 1;
63 my ( $shelf, $shelfnumber, @messages );
65 if ( $op eq 'add_form' ) {
66 # Only pass default
67 $shelf = { allow_change_from_owner => 1 };
68 } elsif ( $op eq 'edit_form' ) {
69 $shelfnumber = $query->param('shelfnumber');
70 $shelf = Koha::Virtualshelves->find($shelfnumber);
72 if ( $shelf ) {
73 $category = $shelf->category;
74 my $patron = Koha::Patrons->find( $shelf->owner );
75 $template->param( owner => $patron, );
76 unless ( $shelf->can_be_managed( $loggedinuser ) ) {
77 push @messages, { type => 'error', code => 'unauthorized_on_update' };
78 $op = 'list';
80 } else {
81 push @messages, { type => 'error', code => 'does_not_exist' };
83 } elsif ( $op eq 'add' ) {
84 if ( $loggedinuser ) {
85 my $allow_changes_from = $query->param('allow_changes_from');
86 eval {
87 $shelf = Koha::Virtualshelf->new(
88 { shelfname => scalar $query->param('shelfname'),
89 sortfield => scalar $query->param('sortfield'),
90 category => scalar $query->param('category') || 1,
91 allow_change_from_owner => $allow_changes_from > 0,
92 allow_change_from_others => $allow_changes_from == ANYONE,
93 owner => scalar $loggedinuser,
96 $shelf->store;
97 $shelfnumber = $shelf->shelfnumber;
99 if ($@) {
100 push @messages, { type => 'error', code => ref($@), msg => $@ };
101 } elsif ( not $shelf ) {
102 push @messages, { type => 'error', code => 'error_on_insert' };
103 } else {
104 push @messages, { type => 'message', code => 'success_on_insert' };
105 $op = 'view';
107 } else {
108 push @messages, { type => 'error', code => 'unauthorized_on_insert' };
109 $op = 'list';
111 } elsif ( $op eq 'edit' ) {
112 $shelfnumber = $query->param('shelfnumber');
113 $shelf = Koha::Virtualshelves->find($shelfnumber);
114 if ( $shelf ) {
115 $op = $referer;
116 my $sortfield = $query->param('sortfield');
117 $sortfield = 'title' unless grep {/^$sortfield$/}qw( title author copyrightdate itemcallnumber dateadded );
118 if ( $shelf->can_be_managed( $loggedinuser ) ) {
119 $shelf->shelfname( scalar $query->param('shelfname') );
120 $shelf->sortfield( $sortfield );
121 my $allow_changes_from = $query->param('allow_changes_from');
122 $shelf->allow_change_from_owner( $allow_changes_from > 0 );
123 $shelf->allow_change_from_others( $allow_changes_from == ANYONE );
124 $shelf->category( scalar $query->param('category') );
125 eval { $shelf->store };
127 if ($@) {
128 push @messages, { type => 'error', code => 'error_on_update' };
129 $op = 'edit_form';
130 } else {
131 push @messages, { type => 'message', code => 'success_on_update' };
133 } else {
134 push @messages, { type => 'error', code => 'unauthorized_on_update' };
136 } else {
137 push @messages, { type => 'error', code => 'does_not_exist' };
139 } elsif ( $op eq 'delete' ) {
140 $shelfnumber = $query->param('shelfnumber');
141 $shelf = Koha::Virtualshelves->find($shelfnumber);
142 if ($shelf) {
143 if ( $shelf->can_be_deleted( $loggedinuser ) ) {
144 eval { $shelf->delete; };
145 if ($@) {
146 push @messages, { type => 'error', code => ref($@), msg => $@ };
147 } else {
148 push @messages, { type => 'message', code => 'success_on_delete' };
150 } else {
151 push @messages, { type => 'error', code => 'unauthorized_on_delete' };
153 } else {
154 push @messages, { type => 'error', code => 'does_not_exist' };
156 $op = $referer;
157 } elsif ( $op eq 'remove_share' ) {
158 $shelfnumber = $query->param('shelfnumber');
159 $shelf = Koha::Virtualshelves->find($shelfnumber);
160 if ($shelf) {
161 my $removed = eval { $shelf->remove_share( $loggedinuser ); };
162 if ($@) {
163 push @messages, { type => 'error', code => ref($@), msg => $@ };
164 } elsif ( $removed ) {
165 push @messages, { type => 'message', code => 'success_on_remove_share' };
166 } else {
167 push @messages, { type => 'error', code => 'error_on_remove_share' };
169 } else {
170 push @messages, { type => 'error', code => 'does_not_exist' };
172 $op = $referer;
174 } elsif ( $op eq 'add_biblio' ) {
175 $shelfnumber = $query->param('shelfnumber');
176 $shelf = Koha::Virtualshelves->find($shelfnumber);
177 if ($shelf) {
178 if( my $barcode = $query->param('barcode') ) {
179 my $item = Koha::Items->find({ barcode => $barcode });
180 if ( $item ) {
181 if ( $shelf->can_biblios_be_added( $loggedinuser ) ) {
182 my $added = eval { $shelf->add_biblio( $item->biblionumber, $loggedinuser ); };
183 if ($@) {
184 push @messages, { type => 'error', code => ref($@), msg => $@ };
185 } elsif ( $added ) {
186 push @messages, { type => 'message', code => 'success_on_add_biblio' };
187 } else {
188 push @messages, { type => 'message', code => 'error_on_add_biblio' };
190 } else {
191 push @messages, { type => 'error', code => 'unauthorized_on_add_biblio' };
193 } else {
194 push @messages, { type => 'error', code => 'item_does_not_exist' };
197 } else {
198 push @messages, { type => 'error', code => 'does_not_exist' };
200 $op = $referer;
201 } elsif ( $op eq 'remove_biblios' ) {
202 $shelfnumber = $query->param('shelfnumber');
203 $shelf = Koha::Virtualshelves->find($shelfnumber);
204 my @biblionumber = $query->multi_param('biblionumber');
205 if ($shelf) {
206 if ( $shelf->can_biblios_be_removed( $loggedinuser ) ) {
207 my $number_of_biblios_removed = eval {
208 $shelf->remove_biblios(
210 biblionumbers => \@biblionumber,
211 borrowernumber => $loggedinuser,
215 if ($@) {
216 push @messages, { type => 'error', code => ref($@), msg => $@ };
217 } elsif ( $number_of_biblios_removed ) {
218 push @messages, { type => 'message', code => 'success_on_remove_biblios' };
219 } else {
220 push @messages, { type => 'error', code => 'no_biblio_removed' };
222 } else {
223 push @messages, { type => 'error', code => 'unauthorized_on_remove_biblios' };
225 } else {
226 push @messages, { type => 'error', code => 'does_not_exist' };
228 $op = 'view';
231 if ( $op eq 'view' ) {
232 $shelfnumber ||= $query->param('shelfnumber');
233 $shelf = Koha::Virtualshelves->find($shelfnumber);
234 if ( $shelf ) {
235 if ( $shelf->can_be_viewed( $loggedinuser ) ) {
236 $category = $shelf->category;
237 my $sortfield = $query->param('sortfield') || $shelf->sortfield; # Passed in sorting overrides default sorting
238 $sortfield = 'title' unless grep $_ eq $sortfield, qw( title author copyrightdate itemcallnumber dateadded );
239 my $direction = $query->param('direction') || 'asc';
240 $direction = 'asc' if $direction ne 'asc' and $direction ne 'desc';
241 my ( $page, $rows );
242 unless ( $query->param('print') or $query->param('rss') ) {
243 $rows = C4::Context->preference('OPACnumSearchResults') || 20;
244 $page = ( $query->param('page') ? $query->param('page') : 1 );
246 my $order_by = $sortfield eq 'itemcallnumber' ? 'items.cn_sort' : $sortfield;
247 my $contents = $shelf->get_contents->search(
250 prefetch => [ { 'biblionumber' => { 'biblioitems' => 'items' } } ],
251 page => $page,
252 rows => $rows,
253 order_by => { "-$direction" => $order_by },
257 # get biblionumbers stored in the cart
258 my @cart_list;
259 if(my $cart_list = $query->cookie('bib_list')){
260 @cart_list = split(/\//, $cart_list);
263 my $patron = Koha::Patrons->find( $loggedinuser );
265 # Lists display falls back to search results configuration
266 my $xslfile = C4::Context->preference('OPACXSLTListsDisplay');
267 my $lang = $xslfile ? C4::Languages::getlanguage() : undef;
268 my $sysxml = $xslfile ? C4::XSLT::get_xslt_sysprefs() : undef;
270 my $record_processor = Koha::RecordProcessor->new({ filters => 'ViewPolicy' });
271 my @items;
272 while ( my $content = $contents->next ) {
273 my $biblionumber = $content->biblionumber;
274 my $this_item = GetBiblioData($biblionumber);
275 my $record = GetMarcBiblio({ biblionumber => $biblionumber });
276 my $framework = GetFrameworkCode( $biblionumber );
277 my $biblio = Koha::Biblios->find( $biblionumber );
278 $record_processor->options({
279 interface => 'opac',
280 frameworkcode => $framework
282 $record_processor->process($record);
284 if ( $xslfile ) {
285 $this_item->{XSLTBloc} = XSLTParse4Display( $biblionumber, $record, "OPACXSLTListsDisplay",
286 1, undef, $sysxml, $xslfile, $lang);
289 my $marcflavour = C4::Context->preference("marcflavour");
290 my $itemtype = Koha::Biblioitems->search({ biblionumber => $content->biblionumber })->next->itemtype;
291 $itemtype = Koha::ItemTypes->find( $itemtype );
292 if( $itemtype ) {
293 $this_item->{imageurl} = C4::Koha::getitemtypeimagelocation( 'opac', $itemtype->imageurl );
294 $this_item->{description} = $itemtype->description; #FIXME Should not it be translated_description?
295 $this_item->{notforloan} = $itemtype->notforloan;
297 $this_item->{'coins'} = $biblio->get_coins;
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} = Koha::IssuingRules->get_onshelfholds_policy( { item => $item, patron => $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;