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