Bug 26265: (QA follow-up) Remove g option from regex, add few dirs
[koha-equinox.git] / opac / opac-shelves.pl
1 #!/usr/bin/perl
2
3 # Copyright 2015 Koha Team
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
16 #
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>.
19
20 use Modern::Perl;
21
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;
32
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;
41
42 use constant ANYONE => 2;
43
44 my $query = new CGI;
45
46 my $template_name = $query->param('rss') ? "opac-shelves-rss.tt" : "opac-shelves.tt";
47
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;
52 }
53
54 my $op = $query->param('op') || 'list';
55 my ( $template, $loggedinuser, $cookie );
56
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         });
71 }
72
73 if (C4::Context->preference("BakerTaylorEnabled")) {
74     $template->param(
75         BakerTaylorImageURL => &image_url(),
76         BakerTaylorLinkURL  => &link_url(),
77     );
78 }
79
80 my $referer  = $query->param('referer')  || $op;
81 my $category = $query->param('category') || 1;
82 my ( $shelf, $shelfnumber, @messages );
83
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);
90
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';
98         }
99     } else {
100         push @messages, { type => 'error', code => 'does_not_exist' };
101     }
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,
113                 }
114             );
115             $shelf->store;
116             $shelfnumber = $shelf->shelfnumber;
117         };
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';
125         }
126     } else {
127         push @messages, { type => 'error', code => 'unauthorized_on_insert' };
128         $op = 'list';
129     }
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 };
145
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' };
151             }
152         } else {
153             push @messages, { type => 'error', code => 'unauthorized_on_update' };
154         }
155     } else {
156         push @messages, { type => 'error', code => 'does_not_exist' };
157     }
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' };
168             }
169         } else {
170             push @messages, { type => 'error', code => 'unauthorized_on_delete' };
171         }
172     } else {
173         push @messages, { type => 'error', code => 'does_not_exist' };
174     }
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' };
187         }
188     } else {
189         push @messages, { type => 'error', code => 'does_not_exist' };
190     }
191     $op = $referer;
192
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' };
208                     }
209                 } else {
210                     push @messages, { type => 'error', code => 'unauthorized_on_add_biblio' };
211                 }
212             } else {
213                 push @messages, { type => 'error', code => 'item_does_not_exist' };
214             }
215         }
216     } else {
217         push @messages, { type => 'error', code => 'does_not_exist' };
218     }
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(
228                     {
229                         biblionumbers => \@biblionumber,
230                         borrowernumber => $loggedinuser,
231                     }
232                 );
233             };
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' };
240             }
241         } else {
242             push @messages, { type => 'error', code => 'unauthorized_on_remove_biblios' };
243         }
244     } else {
245         push @messages, { type => 'error', code => 'does_not_exist' };
246     }
247     $op = 'view';
248 }
249
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';
262             }
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 );
269             }
270             my $order_by = $sortfield eq 'itemcallnumber' ? 'items.cn_sort' : $sortfield;
271             my $contents = $shelf->get_contents->search(
272                 {},
273                 {
274                     prefetch => [ { 'biblionumber' => { 'biblioitems' => 'items' } } ],
275                     page     => $page,
276                     rows     => $rows,
277                     order_by => { "-$direction" => $order_by },
278                 }
279             );
280
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);
285             }
286
287             my $patron = Koha::Patrons->find( $loggedinuser );
288
289             my $categorycode; # needed for may_article_request
290             if( C4::Context->preference('ArticleRequests') ) {
291                 $categorycode = $patron ? $patron->categorycode : undef;
292             }
293
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;
298
299             my $record_processor = Koha::RecordProcessor->new({ filters => 'ViewPolicy' });
300
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 ) : () });
304             }
305
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
316                 });
317                 $record_processor->process($record);
318
319                 if ($xslfile) {
320                     my $variables = {
321                         anonymous_session => ($loggedinuser) ? 0 : 1
322                     };
323                     $this_item->{XSLTBloc} = XSLTParse4Display(
324                         $biblionumber,          $record,
325                         "OPACXSLTListsDisplay", 1,
326                         undef,                  $sysxml,
327                         $xslfile,               $lang,
328                         $variables
329                     );
330                 }
331
332                 my $marcflavour = C4::Context->preference("marcflavour");
333                 my $itemtype = Koha::Biblioitems->search({ biblionumber => $content->biblionumber })->next->itemtype;
334                 $itemtype = Koha::ItemTypes->find( $itemtype );
335                 if( $itemtype ) {
336                     $this_item->{imageurl}          = C4::Koha::getitemtypeimagelocation( 'opac', $itemtype->imageurl );
337                     $this_item->{description}       = $itemtype->description; #FIXME Should not it be translated_description?
338                     $this_item->{notforloan}        = $itemtype->notforloan;
339                 }
340                 $this_item->{'coins'}           = $biblio->get_coins;
341                 $this_item->{'normalized_upc'}  = GetNormalizedUPC( $record, $marcflavour );
342                 $this_item->{'normalized_ean'}  = GetNormalizedEAN( $record, $marcflavour );
343                 $this_item->{'normalized_oclc'} = GetNormalizedOCLCNumber( $record, $marcflavour );
344                 $this_item->{'normalized_isbn'} = GetNormalizedISBN( undef, $record, $marcflavour );
345                 # BZ17530: 'Intelligent' guess if result can be article requested
346                 $this_item->{artreqpossible} = ( $art_req_itypes->{ $this_item->{itemtype} // q{} } || $art_req_itypes->{ '*' } ) ? 1 : q{};
347
348                 unless ( defined $this_item->{size} ) {
349
350                     #TT has problems with size
351                     $this_item->{size} = q||;
352                 }
353
354                 # Getting items infos for location display
355                 my @items_infos = &GetItemsLocationInfo( $biblionumber );
356                 $this_item->{'ITEM_RESULTS'} = \@items_infos;
357
358                 if (C4::Context->preference('TagsEnabled') and C4::Context->preference('TagsShowOnList')) {
359                     $this_item->{TagLoop} = get_tags({
360                         biblionumber => $biblionumber, approved=>1, 'sort'=>'-weight',
361                         limit => C4::Context->preference('TagsShowOnList'),
362                     });
363                 }
364
365                 my $items = $biblio->items;
366                 while ( my $item = $items->next ) {
367                     $this_item->{allow_onshelf_holds} = Koha::CirculationRules->get_onshelfholds_policy( { item => $item, patron => $patron } );
368                     last if $this_item->{allow_onshelf_holds};
369                 }
370
371                 if ( grep {$_ eq $biblionumber} @cart_list) {
372                     $this_item->{incart} = 1;
373                 }
374
375                 $this_item->{biblio_object} = $biblio;
376                 $this_item->{biblionumber}  = $biblionumber;
377                 push @items, $this_item;
378             }
379
380             $template->param(
381                 can_manage_shelf   => $shelf->can_be_managed($loggedinuser),
382                 can_delete_shelf   => $shelf->can_be_deleted($loggedinuser),
383                 can_remove_biblios => $shelf->can_biblios_be_removed($loggedinuser),
384                 can_add_biblios    => $shelf->can_biblios_be_added($loggedinuser),
385                 itemsloop          => \@items,
386                 sortfield          => $sortfield,
387                 direction          => $direction,
388             );
389             if ( $page ) {
390                 my $pager = $contents->pager;
391                 $template->param(
392                     pagination_bar => pagination_bar(
393                         q||, $pager->last_page - $pager->first_page + 1,
394                         $page, "page", { op => 'view', shelfnumber => $shelf->shelfnumber, sortfield => $sortfield, direction => $direction, }
395                     ),
396                 );
397             }
398         } else {
399             push @messages, { type => 'error', code => 'unauthorized_on_view' };
400             undef $shelf;
401         }
402     } else {
403         push @messages, { type => 'error', code => 'does_not_exist' };
404     }
405 }
406
407 if ( $op eq 'list' ) {
408     my $shelves;
409     my ( $page, $rows ) = ( $query->param('page') || 1, 20 );
410     if ( $category == 1 ) {
411         $shelves = Koha::Virtualshelves->get_private_shelves({ page => $page, rows => $rows, borrowernumber => $loggedinuser, });
412     } else {
413         $shelves = Koha::Virtualshelves->get_public_shelves({ page => $page, rows => $rows, });
414     }
415
416     my $pager = $shelves->pager;
417     $template->param(
418         shelves => $shelves,
419         pagination_bar => pagination_bar(
420             q||, $pager->last_page - $pager->first_page + 1,
421             $page, "page", { op => 'list', category => $category, }
422         ),
423     );
424 }
425
426 $template->param(
427     op       => $op,
428     referer  => $referer,
429     shelf    => $shelf,
430     messages => \@messages,
431     category => $category,
432     print    => scalar $query->param('print') || 0,
433     listsview => 1,
434 );
435
436 my $content_type = $query->param('rss')? 'rss' : 'html';
437 output_with_http_headers $query, $cookie, $template->output, $content_type;