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