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