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