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