Bug 24734: Fix paths in LangInstaller.pm for JS files
[koha.git] / opac / opac-shelves.pl
blobc0140c7a75d5d029725d6a0508aa542cb36ba95a
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::Items;
37 use Koha::ItemTypes;
38 use Koha::Patrons;
39 use Koha::Virtualshelves;
40 use Koha::RecordProcessor;
42 use constant ANYONE => 2;
44 my $query = new CGI;
46 my $template_name = $query->param('rss') ? "opac-shelves-rss.tt" : "opac-shelves.tt";
48 # if virtualshelves is disabled, leave immediately
49 if ( ! C4::Context->preference('virtualshelves') ) {
50 print $query->redirect("/cgi-bin/koha/errors/404.pl");
51 exit;
54 my $op = $query->param('op') || 'list';
55 my ( $template, $loggedinuser, $cookie );
57 if( $op eq 'view' || $op eq 'list' ){
58 ( $template, $loggedinuser, $cookie ) = get_template_and_user({
59 template_name => $template_name,
60 query => $query,
61 type => "opac",
62 authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
63 });
64 } else {
65 ( $template, $loggedinuser, $cookie ) = get_template_and_user({
66 template_name => $template_name,
67 query => $query,
68 type => "opac",
69 authnotrequired => 0,
70 });
73 if (C4::Context->preference("BakerTaylorEnabled")) {
74 $template->param(
75 BakerTaylorImageURL => &image_url(),
76 BakerTaylorLinkURL => &link_url(),
80 my $referer = $query->param('referer') || $op;
81 my $category = $query->param('category') || 1;
82 my ( $shelf, $shelfnumber, @messages );
84 if ( $op eq 'add_form' ) {
85 # Only pass default
86 $shelf = { allow_change_from_owner => 1 };
87 } elsif ( $op eq 'edit_form' ) {
88 $shelfnumber = $query->param('shelfnumber');
89 $shelf = Koha::Virtualshelves->find($shelfnumber);
91 if ( $shelf ) {
92 $category = $shelf->category;
93 my $patron = Koha::Patrons->find( $shelf->owner );
94 $template->param( owner => $patron, );
95 unless ( $shelf->can_be_managed( $loggedinuser ) ) {
96 push @messages, { type => 'error', code => 'unauthorized_on_update' };
97 $op = 'list';
99 } else {
100 push @messages, { type => 'error', code => 'does_not_exist' };
102 } elsif ( $op eq 'add' ) {
103 if ( $loggedinuser ) {
104 my $allow_changes_from = $query->param('allow_changes_from');
105 eval {
106 $shelf = Koha::Virtualshelf->new(
107 { shelfname => scalar $query->param('shelfname'),
108 sortfield => scalar $query->param('sortfield'),
109 category => scalar $query->param('category') || 1,
110 allow_change_from_owner => $allow_changes_from > 0,
111 allow_change_from_others => $allow_changes_from == ANYONE,
112 owner => scalar $loggedinuser,
115 $shelf->store;
116 $shelfnumber = $shelf->shelfnumber;
118 if ($@) {
119 push @messages, { type => 'error', code => ref($@), msg => $@ };
120 } elsif ( not $shelf ) {
121 push @messages, { type => 'error', code => 'error_on_insert' };
122 } else {
123 push @messages, { type => 'message', code => 'success_on_insert' };
124 $op = 'view';
126 } else {
127 push @messages, { type => 'error', code => 'unauthorized_on_insert' };
128 $op = 'list';
130 } elsif ( $op eq 'edit' ) {
131 $shelfnumber = $query->param('shelfnumber');
132 $shelf = Koha::Virtualshelves->find($shelfnumber);
133 if ( $shelf ) {
134 $op = $referer;
135 my $sortfield = $query->param('sortfield');
136 $sortfield = 'title' unless grep { $_ eq $sortfield } qw( title author copyrightdate itemcallnumber dateadded );
137 if ( $shelf->can_be_managed( $loggedinuser ) ) {
138 $shelf->shelfname( scalar $query->param('shelfname') );
139 $shelf->sortfield( $sortfield );
140 my $allow_changes_from = $query->param('allow_changes_from');
141 $shelf->allow_change_from_owner( $allow_changes_from > 0 );
142 $shelf->allow_change_from_others( $allow_changes_from == ANYONE );
143 $shelf->category( scalar $query->param('category') );
144 eval { $shelf->store };
146 if ($@) {
147 push @messages, { type => 'error', code => 'error_on_update' };
148 $op = 'edit_form';
149 } else {
150 push @messages, { type => 'message', code => 'success_on_update' };
152 } else {
153 push @messages, { type => 'error', code => 'unauthorized_on_update' };
155 } else {
156 push @messages, { type => 'error', code => 'does_not_exist' };
158 } elsif ( $op eq 'delete' ) {
159 $shelfnumber = $query->param('shelfnumber');
160 $shelf = Koha::Virtualshelves->find($shelfnumber);
161 if ($shelf) {
162 if ( $shelf->can_be_deleted( $loggedinuser ) ) {
163 eval { $shelf->delete; };
164 if ($@) {
165 push @messages, { type => 'error', code => ref($@), msg => $@ };
166 } else {
167 push @messages, { type => 'message', code => 'success_on_delete' };
169 } else {
170 push @messages, { type => 'error', code => 'unauthorized_on_delete' };
172 } else {
173 push @messages, { type => 'error', code => 'does_not_exist' };
175 $op = $referer;
176 } elsif ( $op eq 'remove_share' ) {
177 $shelfnumber = $query->param('shelfnumber');
178 $shelf = Koha::Virtualshelves->find($shelfnumber);
179 if ($shelf) {
180 my $removed = eval { $shelf->remove_share( $loggedinuser ); };
181 if ($@) {
182 push @messages, { type => 'error', code => ref($@), msg => $@ };
183 } elsif ( $removed ) {
184 push @messages, { type => 'message', code => 'success_on_remove_share' };
185 } else {
186 push @messages, { type => 'error', code => 'error_on_remove_share' };
188 } else {
189 push @messages, { type => 'error', code => 'does_not_exist' };
191 $op = $referer;
193 } elsif ( $op eq 'add_biblio' ) {
194 $shelfnumber = $query->param('shelfnumber');
195 $shelf = Koha::Virtualshelves->find($shelfnumber);
196 if ($shelf) {
197 if( my $barcode = $query->param('barcode') ) {
198 my $item = Koha::Items->find({ barcode => $barcode });
199 if ( $item ) {
200 if ( $shelf->can_biblios_be_added( $loggedinuser ) ) {
201 my $added = eval { $shelf->add_biblio( $item->biblionumber, $loggedinuser ); };
202 if ($@) {
203 push @messages, { type => 'error', code => ref($@), msg => $@ };
204 } elsif ( $added ) {
205 push @messages, { type => 'message', code => 'success_on_add_biblio' };
206 } else {
207 push @messages, { type => 'message', code => 'error_on_add_biblio' };
209 } else {
210 push @messages, { type => 'error', code => 'unauthorized_on_add_biblio' };
212 } else {
213 push @messages, { type => 'error', code => 'item_does_not_exist' };
216 } else {
217 push @messages, { type => 'error', code => 'does_not_exist' };
219 $op = $referer;
220 } elsif ( $op eq 'remove_biblios' ) {
221 $shelfnumber = $query->param('shelfnumber');
222 $shelf = Koha::Virtualshelves->find($shelfnumber);
223 my @biblionumber = $query->multi_param('biblionumber');
224 if ($shelf) {
225 if ( $shelf->can_biblios_be_removed( $loggedinuser ) ) {
226 my $number_of_biblios_removed = eval {
227 $shelf->remove_biblios(
229 biblionumbers => \@biblionumber,
230 borrowernumber => $loggedinuser,
234 if ($@) {
235 push @messages, { type => 'error', code => ref($@), msg => $@ };
236 } elsif ( $number_of_biblios_removed ) {
237 push @messages, { type => 'message', code => 'success_on_remove_biblios' };
238 } else {
239 push @messages, { type => 'error', code => 'no_biblio_removed' };
241 } else {
242 push @messages, { type => 'error', code => 'unauthorized_on_remove_biblios' };
244 } else {
245 push @messages, { type => 'error', code => 'does_not_exist' };
247 $op = 'view';
250 if ( $op eq 'view' ) {
251 $shelfnumber ||= $query->param('shelfnumber');
252 $shelf = Koha::Virtualshelves->find($shelfnumber);
253 if ( $shelf ) {
254 if ( $shelf->can_be_viewed( $loggedinuser ) ) {
255 $category = $shelf->category;
256 my( $sortfield, $direction );
257 if( defined( $query->param('sortfield') ) ){ # Passed in sorting overrides default sorting
258 ( $sortfield, $direction ) = split /:/, $query->param('sortfield');
259 } else {
260 $sortfield = $shelf->sortfield;
261 $direction = 'asc';
263 $sortfield = 'title' unless grep $_ eq $sortfield, qw( title author copyrightdate itemcallnumber dateadded );
264 $direction = 'asc' if $direction ne 'asc' and $direction ne 'desc';
265 my ( $page, $rows );
266 unless ( $query->param('print') or $query->param('rss') ) {
267 $rows = C4::Context->preference('OPACnumSearchResults') || 20;
268 $page = ( $query->param('page') ? $query->param('page') : 1 );
270 my $order_by = $sortfield eq 'itemcallnumber' ? 'items.cn_sort' : $sortfield;
271 my $contents = $shelf->get_contents->search(
274 prefetch => [ { 'biblionumber' => { 'biblioitems' => 'items' } } ],
275 page => $page,
276 rows => $rows,
277 order_by => { "-$direction" => $order_by },
281 # get biblionumbers stored in the cart
282 my @cart_list;
283 if(my $cart_list = $query->cookie('bib_list')){
284 @cart_list = split(/\//, $cart_list);
287 my $patron = Koha::Patrons->find( $loggedinuser );
289 my $categorycode; # needed for may_article_request
290 if( C4::Context->preference('ArticleRequests') ) {
291 $categorycode = $patron ? $patron->categorycode : undef;
294 # Lists display falls back to search results configuration
295 my $xslfile = C4::Context->preference('OPACXSLTListsDisplay');
296 my $lang = $xslfile ? C4::Languages::getlanguage() : undef;
297 my $sysxml = $xslfile ? C4::XSLT::get_xslt_sysprefs() : undef;
299 my $record_processor = Koha::RecordProcessor->new({ filters => 'ViewPolicy' });
301 my $art_req_itypes;
302 if( C4::Context->preference('ArticleRequests') ) {
303 $art_req_itypes = Koha::CirculationRules->guess_article_requestable_itemtypes({ $patron ? ( categorycode => $patron->categorycode ) : () });
306 my @items;
307 while ( my $content = $contents->next ) {
308 my $biblionumber = $content->biblionumber;
309 my $this_item = GetBiblioData($biblionumber);
310 my $record = GetMarcBiblio({ biblionumber => $biblionumber });
311 my $framework = GetFrameworkCode( $biblionumber );
312 my $biblio = Koha::Biblios->find( $biblionumber );
313 $record_processor->options({
314 interface => 'opac',
315 frameworkcode => $framework
317 $record_processor->process($record);
319 if ( $xslfile ) {
320 $this_item->{XSLTBloc} = XSLTParse4Display( $biblionumber, $record, "OPACXSLTListsDisplay",
321 1, undef, $sysxml, $xslfile, $lang);
324 my $marcflavour = C4::Context->preference("marcflavour");
325 my $itemtype = Koha::Biblioitems->search({ biblionumber => $content->biblionumber })->next->itemtype;
326 $itemtype = Koha::ItemTypes->find( $itemtype );
327 if( $itemtype ) {
328 $this_item->{imageurl} = C4::Koha::getitemtypeimagelocation( 'opac', $itemtype->imageurl );
329 $this_item->{description} = $itemtype->description; #FIXME Should not it be translated_description?
330 $this_item->{notforloan} = $itemtype->notforloan;
332 $this_item->{'coins'} = $biblio->get_coins;
333 $this_item->{'normalized_upc'} = GetNormalizedUPC( $record, $marcflavour );
334 $this_item->{'normalized_ean'} = GetNormalizedEAN( $record, $marcflavour );
335 $this_item->{'normalized_oclc'} = GetNormalizedOCLCNumber( $record, $marcflavour );
336 $this_item->{'normalized_isbn'} = GetNormalizedISBN( undef, $record, $marcflavour );
337 # BZ17530: 'Intelligent' guess if result can be article requested
338 $this_item->{artreqpossible} = ( $art_req_itypes->{ $this_item->{itemtype} // q{} } || $art_req_itypes->{ '*' } ) ? 1 : q{};
340 unless ( defined $this_item->{size} ) {
342 #TT has problems with size
343 $this_item->{size} = q||;
346 # Getting items infos for location display
347 my @items_infos = &GetItemsLocationInfo( $biblionumber );
348 $this_item->{'ITEM_RESULTS'} = \@items_infos;
350 if (C4::Context->preference('TagsEnabled') and C4::Context->preference('TagsShowOnList')) {
351 $this_item->{TagLoop} = get_tags({
352 biblionumber => $biblionumber, approved=>1, 'sort'=>'-weight',
353 limit => C4::Context->preference('TagsShowOnList'),
357 my $items = $biblio->items;
358 while ( my $item = $items->next ) {
359 $this_item->{allow_onshelf_holds} = Koha::CirculationRules->get_onshelfholds_policy( { item => $item, patron => $patron } );
360 last if $this_item->{allow_onshelf_holds};
363 if ( grep {$_ eq $biblionumber} @cart_list) {
364 $this_item->{incart} = 1;
367 $this_item->{biblio_object} = $biblio;
368 $this_item->{biblionumber} = $biblionumber;
369 push @items, $this_item;
372 $template->param(
373 can_manage_shelf => $shelf->can_be_managed($loggedinuser),
374 can_delete_shelf => $shelf->can_be_deleted($loggedinuser),
375 can_remove_biblios => $shelf->can_biblios_be_removed($loggedinuser),
376 can_add_biblios => $shelf->can_biblios_be_added($loggedinuser),
377 itemsloop => \@items,
378 sortfield => $sortfield,
379 direction => $direction,
381 if ( $page ) {
382 my $pager = $contents->pager;
383 $template->param(
384 pagination_bar => pagination_bar(
385 q||, $pager->last_page - $pager->first_page + 1,
386 $page, "page", { op => 'view', shelfnumber => $shelf->shelfnumber, sortfield => $sortfield, direction => $direction, }
390 } else {
391 push @messages, { type => 'error', code => 'unauthorized_on_view' };
392 undef $shelf;
394 } else {
395 push @messages, { type => 'error', code => 'does_not_exist' };
399 if ( $op eq 'list' ) {
400 my $shelves;
401 my ( $page, $rows ) = ( $query->param('page') || 1, 20 );
402 if ( $category == 1 ) {
403 $shelves = Koha::Virtualshelves->get_private_shelves({ page => $page, rows => $rows, borrowernumber => $loggedinuser, });
404 } else {
405 $shelves = Koha::Virtualshelves->get_public_shelves({ page => $page, rows => $rows, });
408 my $pager = $shelves->pager;
409 $template->param(
410 shelves => $shelves,
411 pagination_bar => pagination_bar(
412 q||, $pager->last_page - $pager->first_page + 1,
413 $page, "page", { op => 'list', category => $category, }
418 $template->param(
419 op => $op,
420 referer => $referer,
421 shelf => $shelf,
422 messages => \@messages,
423 category => $category,
424 print => scalar $query->param('print') || 0,
425 listsview => 1,
428 output_html_with_http_headers $query, $cookie, $template->output;