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