Bug 17896: load BakerTaylor module with use
[koha.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;
26 use C4::External::BakerTaylor qw(&image_url &link_url);
27 use C4::Koha;
28 use C4::Items;
29 use C4::Members;
30 use C4::Output;
31 use C4::Tags qw( get_tags );
32 use C4::XSLT;
33
34 use Koha::Biblios;
35 use Koha::Biblioitems;
36 use Koha::CirculationRules;
37 use Koha::Items;
38 use Koha::ItemTypes;
39 use Koha::Patrons;
40 use Koha::Virtualshelves;
41 use Koha::RecordProcessor;
42
43 use constant ANYONE => 2;
44
45 my $query = new CGI;
46
47 my $template_name = $query->param('rss') ? "opac-shelves-rss.tt" : "opac-shelves.tt";
48
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;
53 }
54
55 my $op = $query->param('op') || 'list';
56 my ( $template, $loggedinuser, $cookie );
57
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         });
72 }
73
74 my $referer  = $query->param('referer')  || $op;
75 my $category = $query->param('category') || 1;
76 my ( $shelf, $shelfnumber, @messages );
77
78 if ( $op eq 'add_form' ) {
79     # Only pass default
80     $shelf = { allow_change_from_owner => 1 };
81 } elsif ( $op eq 'edit_form' ) {
82     $shelfnumber = $query->param('shelfnumber');
83     $shelf       = Koha::Virtualshelves->find($shelfnumber);
84
85     if ( $shelf ) {
86         $category = $shelf->category;
87         my $patron = Koha::Patrons->find( $shelf->owner );
88         $template->param( owner => $patron, );
89         unless ( $shelf->can_be_managed( $loggedinuser ) ) {
90             push @messages, { type => 'error', code => 'unauthorized_on_update' };
91             $op = 'list';
92         }
93     } else {
94         push @messages, { type => 'error', code => 'does_not_exist' };
95     }
96 } elsif ( $op eq 'add' ) {
97     if ( $loggedinuser ) {
98         my $allow_changes_from = $query->param('allow_changes_from');
99         eval {
100             $shelf = Koha::Virtualshelf->new(
101                 {   shelfname          => scalar $query->param('shelfname'),
102                     sortfield          => scalar $query->param('sortfield'),
103                     category           => scalar $query->param('category') || 1,
104                     allow_change_from_owner => $allow_changes_from > 0,
105                     allow_change_from_others => $allow_changes_from == ANYONE,
106                     owner              => scalar $loggedinuser,
107                 }
108             );
109             $shelf->store;
110             $shelfnumber = $shelf->shelfnumber;
111         };
112         if ($@) {
113             push @messages, { type => 'error', code => ref($@), msg => $@ };
114         } elsif ( not $shelf ) {
115             push @messages, { type => 'error', code => 'error_on_insert' };
116         } else {
117             push @messages, { type => 'message', code => 'success_on_insert' };
118             $op = 'view';
119         }
120     } else {
121         push @messages, { type => 'error', code => 'unauthorized_on_insert' };
122         $op = 'list';
123     }
124 } elsif ( $op eq 'edit' ) {
125     $shelfnumber = $query->param('shelfnumber');
126     $shelf       = Koha::Virtualshelves->find($shelfnumber);
127     if ( $shelf ) {
128         $op = $referer;
129         my $sortfield = $query->param('sortfield');
130         $sortfield = 'title' unless grep { $_ eq $sortfield } qw( title author copyrightdate itemcallnumber dateadded );
131         if ( $shelf->can_be_managed( $loggedinuser ) ) {
132             $shelf->shelfname( scalar $query->param('shelfname') );
133             $shelf->sortfield( $sortfield );
134             my $allow_changes_from = $query->param('allow_changes_from');
135             $shelf->allow_change_from_owner( $allow_changes_from > 0 );
136             $shelf->allow_change_from_others( $allow_changes_from == ANYONE );
137             $shelf->category( scalar $query->param('category') );
138             eval { $shelf->store };
139
140             if ($@) {
141                 push @messages, { type => 'error', code => 'error_on_update' };
142                 $op = 'edit_form';
143             } else {
144                 push @messages, { type => 'message', code => 'success_on_update' };
145             }
146         } else {
147             push @messages, { type => 'error', code => 'unauthorized_on_update' };
148         }
149     } else {
150         push @messages, { type => 'error', code => 'does_not_exist' };
151     }
152 } elsif ( $op eq 'delete' ) {
153     $shelfnumber = $query->param('shelfnumber');
154     $shelf       = Koha::Virtualshelves->find($shelfnumber);
155     if ($shelf) {
156         if ( $shelf->can_be_deleted( $loggedinuser ) ) {
157             eval { $shelf->delete; };
158             if ($@) {
159                 push @messages, { type => 'error', code => ref($@), msg => $@ };
160             } else {
161                 push @messages, { type => 'message', code => 'success_on_delete' };
162             }
163         } else {
164             push @messages, { type => 'error', code => 'unauthorized_on_delete' };
165         }
166     } else {
167         push @messages, { type => 'error', code => 'does_not_exist' };
168     }
169     $op = $referer;
170 } elsif ( $op eq 'remove_share' ) {
171     $shelfnumber = $query->param('shelfnumber');
172     $shelf = Koha::Virtualshelves->find($shelfnumber);
173     if ($shelf) {
174         my $removed = eval { $shelf->remove_share( $loggedinuser ); };
175         if ($@) {
176             push @messages, { type => 'error', code => ref($@), msg => $@ };
177         } elsif ( $removed ) {
178             push @messages, { type => 'message', code => 'success_on_remove_share' };
179         } else {
180             push @messages, { type => 'error', code => 'error_on_remove_share' };
181         }
182     } else {
183         push @messages, { type => 'error', code => 'does_not_exist' };
184     }
185     $op = $referer;
186
187 } elsif ( $op eq 'add_biblio' ) {
188     $shelfnumber = $query->param('shelfnumber');
189     $shelf = Koha::Virtualshelves->find($shelfnumber);
190     if ($shelf) {
191         if( my $barcode = $query->param('barcode') ) {
192             my $item = Koha::Items->find({ barcode => $barcode });
193             if ( $item ) {
194                 if ( $shelf->can_biblios_be_added( $loggedinuser ) ) {
195                     my $added = eval { $shelf->add_biblio( $item->biblionumber, $loggedinuser ); };
196                     if ($@) {
197                         push @messages, { type => 'error', code => ref($@), msg => $@ };
198                     } elsif ( $added ) {
199                         push @messages, { type => 'message', code => 'success_on_add_biblio' };
200                     } else {
201                         push @messages, { type => 'message', code => 'error_on_add_biblio' };
202                     }
203                 } else {
204                     push @messages, { type => 'error', code => 'unauthorized_on_add_biblio' };
205                 }
206             } else {
207                 push @messages, { type => 'error', code => 'item_does_not_exist' };
208             }
209         }
210     } else {
211         push @messages, { type => 'error', code => 'does_not_exist' };
212     }
213     $op = $referer;
214 } elsif ( $op eq 'remove_biblios' ) {
215     $shelfnumber = $query->param('shelfnumber');
216     $shelf = Koha::Virtualshelves->find($shelfnumber);
217     my @biblionumber = $query->multi_param('biblionumber');
218     if ($shelf) {
219         if ( $shelf->can_biblios_be_removed( $loggedinuser ) ) {
220             my $number_of_biblios_removed = eval {
221                 $shelf->remove_biblios(
222                     {
223                         biblionumbers => \@biblionumber,
224                         borrowernumber => $loggedinuser,
225                     }
226                 );
227             };
228             if ($@) {
229                 push @messages, { type => 'error', code => ref($@), msg => $@ };
230             } elsif ( $number_of_biblios_removed ) {
231                 push @messages, { type => 'message', code => 'success_on_remove_biblios' };
232             } else {
233                 push @messages, { type => 'error', code => 'no_biblio_removed' };
234             }
235         } else {
236             push @messages, { type => 'error', code => 'unauthorized_on_remove_biblios' };
237         }
238     } else {
239         push @messages, { type => 'error', code => 'does_not_exist' };
240     }
241     $op = 'view';
242 }
243
244 if ( $op eq 'view' ) {
245     $shelfnumber ||= $query->param('shelfnumber');
246     $shelf = Koha::Virtualshelves->find($shelfnumber);
247     if ( $shelf ) {
248         if ( $shelf->can_be_viewed( $loggedinuser ) ) {
249             $category = $shelf->category;
250             my( $sortfield, $direction );
251             if( defined( $query->param('sortfield') ) ){ # Passed in sorting overrides default sorting
252                 ( $sortfield, $direction ) = split /:/, $query->param('sortfield');
253             } else {
254                 $sortfield = $shelf->sortfield;
255                 $direction = 'asc';
256             }
257             $sortfield = 'title' unless grep $_ eq $sortfield, qw( title author copyrightdate itemcallnumber dateadded );
258             $direction = 'asc' if $direction ne 'asc' and $direction ne 'desc';
259             my ( $page, $rows );
260             unless ( $query->param('print') or $query->param('rss') ) {
261                 $rows = C4::Context->preference('OPACnumSearchResults') || 20;
262                 $page = ( $query->param('page') ? $query->param('page') : 1 );
263             }
264             my $order_by = $sortfield eq 'itemcallnumber' ? 'items.cn_sort' : $sortfield;
265             my $contents = $shelf->get_contents->search(
266                 {},
267                 {
268                     prefetch => [ { 'biblionumber' => { 'biblioitems' => 'items' } } ],
269                     page     => $page,
270                     rows     => $rows,
271                     order_by => { "-$direction" => $order_by },
272                 }
273             );
274
275             # get biblionumbers stored in the cart
276             my @cart_list;
277             if(my $cart_list = $query->cookie('bib_list')){
278                 @cart_list = split(/\//, $cart_list);
279             }
280
281             my $patron = Koha::Patrons->find( $loggedinuser );
282
283             my $categorycode; # needed for may_article_request
284             if( C4::Context->preference('ArticleRequests') ) {
285                 $categorycode = $patron ? $patron->categorycode : undef;
286             }
287
288             # Lists display falls back to search results configuration
289             my $xslfile = C4::Context->preference('OPACXSLTListsDisplay');
290             my $lang   = $xslfile ? C4::Languages::getlanguage()  : undef;
291             my $sysxml = $xslfile ? C4::XSLT::get_xslt_sysprefs() : undef;
292
293             my $record_processor = Koha::RecordProcessor->new({ filters => 'ViewPolicy' });
294
295             my $art_req_itypes;
296             if( C4::Context->preference('ArticleRequests') ) {
297                 $art_req_itypes = Koha::CirculationRules->guess_article_requestable_itemtypes({ $patron ? ( categorycode => $patron->categorycode ) : () });
298             }
299
300             my @items;
301             while ( my $content = $contents->next ) {
302                 my $biblionumber = $content->biblionumber;
303                 my $this_item    = GetBiblioData($biblionumber);
304                 my $record = GetMarcBiblio({ biblionumber => $biblionumber });
305                 my $framework = GetFrameworkCode( $biblionumber );
306                 my $biblio = Koha::Biblios->find( $biblionumber );
307                 $record_processor->options({
308                     interface => 'opac',
309                     frameworkcode => $framework
310                 });
311                 $record_processor->process($record);
312
313                 if ( $xslfile ) {
314                     $this_item->{XSLTBloc} = XSLTParse4Display( $biblionumber, $record, "OPACXSLTListsDisplay",
315                                                                 1, undef, $sysxml, $xslfile, $lang);
316                 }
317
318                 my $marcflavour = C4::Context->preference("marcflavour");
319                 my $itemtype = Koha::Biblioitems->search({ biblionumber => $content->biblionumber })->next->itemtype;
320                 $itemtype = Koha::ItemTypes->find( $itemtype );
321                 if( $itemtype ) {
322                     $this_item->{imageurl}          = C4::Koha::getitemtypeimagelocation( 'opac', $itemtype->imageurl );
323                     $this_item->{description}       = $itemtype->description; #FIXME Should not it be translated_description?
324                     $this_item->{notforloan}        = $itemtype->notforloan;
325                 }
326                 $this_item->{'coins'}           = $biblio->get_coins;
327                 $this_item->{'normalized_upc'}  = GetNormalizedUPC( $record, $marcflavour );
328                 $this_item->{'normalized_ean'}  = GetNormalizedEAN( $record, $marcflavour );
329                 $this_item->{'normalized_oclc'} = GetNormalizedOCLCNumber( $record, $marcflavour );
330                 $this_item->{'normalized_isbn'} = GetNormalizedISBN( undef, $record, $marcflavour );
331                 # BZ17530: 'Intelligent' guess if result can be article requested
332                 $this_item->{artreqpossible} = ( $art_req_itypes->{ $this_item->{itemtype} // q{} } || $art_req_itypes->{ '*' } ) ? 1 : q{};
333
334                 unless ( defined $this_item->{size} ) {
335
336                     #TT has problems with size
337                     $this_item->{size} = q||;
338                 }
339
340                 # Getting items infos for location display
341                 my @items_infos = &GetItemsLocationInfo( $biblionumber );
342                 $this_item->{'ITEM_RESULTS'} = \@items_infos;
343
344                 if (C4::Context->preference('TagsEnabled') and C4::Context->preference('TagsShowOnList')) {
345                     $this_item->{TagLoop} = get_tags({
346                         biblionumber => $biblionumber, approved=>1, 'sort'=>'-weight',
347                         limit => C4::Context->preference('TagsShowOnList'),
348                     });
349                 }
350
351                 my $items = $biblio->items;
352                 while ( my $item = $items->next ) {
353                     $this_item->{allow_onshelf_holds} = Koha::CirculationRules->get_onshelfholds_policy( { item => $item, patron => $patron } );
354                     last if $this_item->{allow_onshelf_holds};
355                 }
356
357                 if ( grep {$_ eq $biblionumber} @cart_list) {
358                     $this_item->{incart} = 1;
359                 }
360
361                 $this_item->{biblio_object} = $biblio;
362                 $this_item->{biblionumber}  = $biblionumber;
363                 push @items, $this_item;
364             }
365
366             $template->param(
367                 can_manage_shelf   => $shelf->can_be_managed($loggedinuser),
368                 can_delete_shelf   => $shelf->can_be_deleted($loggedinuser),
369                 can_remove_biblios => $shelf->can_biblios_be_removed($loggedinuser),
370                 can_add_biblios    => $shelf->can_biblios_be_added($loggedinuser),
371                 itemsloop          => \@items,
372                 sortfield          => $sortfield,
373                 direction          => $direction,
374             );
375             if ( $page ) {
376                 my $pager = $contents->pager;
377                 $template->param(
378                     pagination_bar => pagination_bar(
379                         q||, $pager->last_page - $pager->first_page + 1,
380                         $page, "page", { op => 'view', shelfnumber => $shelf->shelfnumber, sortfield => $sortfield, direction => $direction, }
381                     ),
382                 );
383             }
384         } else {
385             push @messages, { type => 'error', code => 'unauthorized_on_view' };
386             undef $shelf;
387         }
388     } else {
389         push @messages, { type => 'error', code => 'does_not_exist' };
390     }
391 }
392
393 if ( $op eq 'list' ) {
394     my $shelves;
395     my ( $page, $rows ) = ( $query->param('page') || 1, 20 );
396     if ( $category == 1 ) {
397         $shelves = Koha::Virtualshelves->get_private_shelves({ page => $page, rows => $rows, borrowernumber => $loggedinuser, });
398     } else {
399         $shelves = Koha::Virtualshelves->get_public_shelves({ page => $page, rows => $rows, });
400     }
401
402     my $pager = $shelves->pager;
403     $template->param(
404         shelves => $shelves,
405         pagination_bar => pagination_bar(
406             q||, $pager->last_page - $pager->first_page + 1,
407             $page, "page", { op => 'list', category => $category, }
408         ),
409     );
410 }
411
412 $template->param(
413     op       => $op,
414     referer  => $referer,
415     shelf    => $shelf,
416     messages => \@messages,
417     category => $category,
418     print    => scalar $query->param('print') || 0,
419     listsview => 1,
420 );
421
422 output_html_with_http_headers $query, $cookie, $template->output;