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