Bug 34650: Remove unnecessary CSRF check on edit_form
[koha.git] / virtualshelves / 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 qw( get_template_and_user haspermission );
23 use C4::Circulation qw( barcodedecode );
24 use C4::Context;
25 use C4::Koha qw(
26     GetNormalizedEAN
27     GetNormalizedISBN
28     GetNormalizedOCLCNumber
29     GetNormalizedUPC
30 );
31 use C4::Members;
32 use C4::Output qw( pagination_bar output_html_with_http_headers output_and_exit_if_error );
33 use C4::XSLT qw( XSLTParse4Display );
34
35 use Koha::Biblios;
36 use Koha::Biblioitems;
37 use Koha::Items;
38 use Koha::ItemTypes;
39 use Koha::CsvProfiles;
40 use Koha::Patrons;
41 use Koha::Virtualshelves;
42
43 use constant ANYONE => 2;
44 use constant STAFF  => 3;
45
46 my $query = CGI->new;
47
48 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
49     {   template_name   => "virtualshelves/shelves.tt",
50         query           => $query,
51         type            => "intranet",
52         flagsrequired   => { catalogue => 1 },
53     }
54 );
55
56 my $op       = $query->param('op')      || 'list';
57 my $referer  = $query->param('referer') || $op;
58 my $public   = $query->param('public') ? 1 : 0;
59 my ( $shelf, $shelfnumber, @messages, $allow_transfer );
60
61 # PART1: Perform a few actions
62 if ( $op eq 'add_form' ) {
63     # Only pass default
64     $shelf = { allow_change_from_owner => 1 };
65 } elsif ( $op eq 'edit_form' ) {
66     $shelfnumber = $query->param('shelfnumber');
67     $shelf       = Koha::Virtualshelves->find($shelfnumber);
68
69     if ( $shelf ) {
70         $public = $shelf->public;
71         my $patron = Koha::Patrons->find( $shelf->owner )->unblessed;
72         $template->param( owner => $patron, );
73         unless ( $shelf->can_be_managed( $loggedinuser ) ) {
74             push @messages, { type => 'alert', code => 'unauthorized_on_update' };
75             $op = 'list';
76         }
77     } else {
78         push @messages, { type => 'alert', code => 'does_not_exist' };
79     }
80 } elsif ( $op eq 'add' ) {
81     output_and_exit_if_error($query, $cookie, $template, { check => 'csrf_token' });
82     my $allow_changes_from = $query->param('allow_changes_from');
83     eval {
84         $shelf = Koha::Virtualshelf->new(
85             {   shelfname          => scalar $query->param('shelfname'),
86                 sortfield          => scalar $query->param('sortfield'),
87                 public             => $public,
88                 allow_change_from_owner => $allow_changes_from > 0,
89                 allow_change_from_others => $allow_changes_from == ANYONE,
90                 allow_change_from_staff => $allow_changes_from == STAFF,
91                 owner              => scalar $query->param('owner'),
92             }
93         );
94         $shelf->store;
95         $shelfnumber = $shelf->shelfnumber;
96     };
97     if ($@) {
98         push @messages, { type => 'alert', code => ref($@), msg => $@ };
99     } elsif ( not $shelf ) {
100         push @messages, { type => 'alert', code => 'error_on_insert' };
101
102     } else {
103         push @messages, { type => 'message', code => 'success_on_insert' };
104         $op = 'view';
105     }
106 } elsif ( $op eq 'edit' ) {
107     output_and_exit_if_error($query, $cookie, $template, { check => 'csrf_token' });
108     $shelfnumber = $query->param('shelfnumber');
109     $shelf       = Koha::Virtualshelves->find($shelfnumber);
110
111     if ( $shelf ) {
112         $op = $referer;
113         my $sortfield = $query->param('sortfield');
114         $sortfield = 'title' unless grep { $_ eq $sortfield } qw( title author copyrightdate itemcallnumber dateadded );
115         if ( $shelf->can_be_managed( $loggedinuser ) ) {
116             $shelf->shelfname( scalar $query->param('shelfname') );
117             $shelf->sortfield( $sortfield );
118             my $allow_changes_from = $query->param('allow_changes_from');
119             $shelf->allow_change_from_owner( $allow_changes_from > 0 );
120             $shelf->allow_change_from_others( $allow_changes_from == ANYONE );
121             $shelf->allow_change_from_staff( $allow_changes_from == STAFF );
122             $shelf->public( scalar $query->param('public') );
123             eval { $shelf->store };
124
125             if ($@) {
126                 push @messages, { type => 'alert', code => 'error_on_update' };
127                 $op = 'edit_form';
128             } else {
129                 push @messages, { type => 'message', code => 'success_on_update' };
130             }
131         } else {
132             push @messages, { type => 'alert', code => 'unauthorized_on_update' };
133         }
134     } else {
135         push @messages, { type => 'alert', code => 'does_not_exist' };
136     }
137 } elsif ( $op eq 'delete' ) {
138     output_and_exit_if_error($query, $cookie, $template, { check => 'csrf_token' });
139     $shelfnumber = $query->param('shelfnumber');
140     $shelf       = Koha::Virtualshelves->find($shelfnumber);
141     if ($shelf) {
142         if ( $shelf->can_be_deleted( $loggedinuser ) ) {
143             eval { $shelf->delete; };
144             if ($@) {
145                 push @messages, { type => 'alert', code => ref($@), msg => $@ };
146             } else {
147                 push @messages, { type => 'message', code => 'success_on_delete' };
148             }
149         } else {
150             push @messages, { type => 'alert', code => 'unauthorized_on_delete' };
151         }
152     } else {
153         push @messages, { type => 'alert', code => 'does_not_exist' };
154     }
155     $op = 'list';
156 } elsif ( $op eq 'add_biblio' ) {
157     output_and_exit_if_error($query, $cookie, $template, { check => 'csrf_token' });
158     $shelfnumber = $query->param('shelfnumber');
159     $shelf = Koha::Virtualshelves->find($shelfnumber);
160     if ($shelf) {
161         if( my $barcodes = $query->param('barcodes') ) {
162             if ( $shelf->can_biblios_be_added( $loggedinuser ) ) {
163                 my @barcodes = split /\n/, $barcodes; # Entries are effectively passed in as a <cr> separated list
164                 foreach my $barcode (@barcodes){
165                     $barcode = barcodedecode( $barcode ) if $barcode;
166                     next if $barcode eq '';
167                     my $item = Koha::Items->find({barcode => $barcode});
168                     if ( $item ) {
169                         my $added = eval { $shelf->add_biblio( $item->biblionumber, $loggedinuser ); };
170                         if ($@) {
171                             push @messages, { item_barcode => $barcode, type => 'alert', code => ref($@), msg => $@ };
172                         } elsif ( $added ) {
173                             push @messages, { item_barcode => $barcode, type => 'message', code => 'success_on_add_biblio' };
174                         } else {
175                             push @messages, { item_barcode => $barcode, type => 'message', code => 'error_on_add_biblio' };
176                         }
177                     } else {
178                         push @messages, { item_barcode => $barcode, type => 'alert', code => 'item_does_not_exist' };
179                     }
180                 }
181             } else {
182                 push @messages, { type => 'alert', code => 'unauthorized_on_add_biblio' };
183             }
184         }
185         if ( my $biblionumbers = $query->param('biblionumbers') ) {
186             if ( $shelf->can_biblios_be_added( $loggedinuser ) ) {
187                 my @biblionumbers = split /\n/, $biblionumbers;
188                 foreach my $biblionumber (@biblionumbers) {
189                     $biblionumber =~ s/\r$//; # strip any naughty return chars
190                     next if $biblionumber eq '';
191                     my $biblio = Koha::Biblios->find($biblionumber);
192                     if (defined $biblio) {
193                         my $added = eval { $shelf->add_biblio( $biblionumber, $loggedinuser ); };
194                         if ($@) {
195                             push @messages, { bibnum => $biblionumber, type => 'alert', code => ref($@), msg => $@ };
196                         } elsif ( $added ) {
197                             push @messages, { bibnum => $biblionumber, type => 'message', code => 'success_on_add_biblio' };
198                         } else {
199                             push @messages, { bibnum => $biblionumber, type => 'message', code => 'error_on_add_biblio' };
200                         }
201                     } else {
202                         push @messages, { bibnum => $biblionumber, type => 'alert', code => 'item_does_not_exist' };
203                     }
204                 }
205             } else {
206                 push @messages, { type => 'alert', code => 'unauthorized_on_add_biblio' };
207             }
208         }
209     } else {
210         push @messages, { type => 'alert', code => 'does_not_exist' };
211     }
212     $op = $referer;
213 } elsif ( $op eq 'remove_biblios' ) {
214     output_and_exit_if_error($query, $cookie, $template, { check => 'csrf_token' });
215     $shelfnumber = $query->param('shelfnumber');
216     $shelf = Koha::Virtualshelves->find($shelfnumber);
217     my @biblionumbers = $query->multi_param('biblionumber');
218     if ($shelf) {
219         if ( $shelf->can_biblios_be_removed( $loggedinuser ) ) {
220             my $number_of_biblios_removed = eval {
221                 $shelf->remove_biblios(
222                     {
223                         biblionumbers => \@biblionumbers,
224                         borrowernumber => $loggedinuser,
225                     }
226                 );
227             };
228             if ($@) {
229                 push @messages, { type => 'alert', code => ref($@), msg => $@ };
230             } elsif ( $number_of_biblios_removed ) {
231                 push @messages, { type => 'message', code => 'success_on_remove_biblios' };
232             } else {
233                 push @messages, { type => 'alert', code => 'no_biblio_removed' };
234             }
235         } else {
236             push @messages, { type => 'alert', code => 'unauthorized_on_remove_biblios' };
237         }
238     } else {
239         push @messages, { type => 'alert', code => 'does_not_exist' };
240     }
241     $op = $referer;
242 } elsif ( $op eq 'transfer' ) {
243     $shelfnumber = $query->param('shelfnumber');
244     $shelf = Koha::Virtualshelves->find($shelfnumber) if $shelfnumber;
245     my $new_owner = $query->param('new_owner'); # is a borrowernumber
246     my $error_code = $shelf
247         ? $shelf->cannot_be_transferred({ by => $loggedinuser, to => $new_owner, interface => 'intranet' })
248         : 'does_not_exist';
249
250     if( !$new_owner && $error_code eq 'missing_to_parameter' ) {
251         # show form
252     } elsif( $error_code ) {
253         push @messages, { type => 'error', code => $error_code };
254         $op = 'list';
255     } else {
256         $shelf->owner($new_owner)->store;
257         $op = 'list';
258     }
259 }
260
261 # PART2: After a possible action, further prepare form
262 if ( $op eq 'view' ) {
263     $shelfnumber ||= $query->param('shelfnumber');
264     $shelf = Koha::Virtualshelves->find($shelfnumber);
265     if ( $shelf ) {
266         if ( $shelf->can_be_viewed( $loggedinuser ) ) {
267             my $sortfield = $query->param('sortfield') || $shelf->sortfield || 'title';    # Passed in sorting overrides default sorting
268             $sortfield = 'title' unless grep { $_ eq $sortfield } qw( title author copyrightdate itemcallnumber dateadded );
269             my $direction = $query->param('direction') || 'asc';
270             $direction = 'asc' if $direction ne 'asc' and $direction ne 'desc';
271             my ( $rows, $page );
272             unless ( $query->param('print') ) {
273                 $rows = C4::Context->preference('numSearchResults') || 20;
274                 $page = ( $query->param('page') ? $query->param('page') : 1 );
275             }
276
277             my $order_by = $sortfield eq 'itemcallnumber' ? 'items.cn_sort' : $sortfield;
278             my $contents = $shelf->get_contents->search(
279                 {},
280                 {
281                     prefetch => [ { 'biblionumber' => { 'biblioitems' => 'items' } } ],
282                     page     => $page,
283                     rows     => $rows,
284                     order_by => { "-$direction" => $order_by },
285                 }
286             );
287
288             my @items;
289             while ( my $content = $contents->next ) {
290                 my $this_item;
291                 my $biblionumber = $content->biblionumber;
292                 my $biblio       = Koha::Biblios->find($biblionumber);
293                 my $record       = $biblio->metadata->record;
294
295                 $this_item->{XSLTBloc} = XSLTParse4Display(
296                     {
297                         biblionumber => $biblionumber,
298                         record       => $record,
299                         xsl_syspref  => 'XSLTListsDisplay',
300                         fix_amps     => 1,
301                     }
302                 );
303
304                 my $marcflavour = C4::Context->preference("marcflavour");
305                 my $itemtype = Koha::Biblioitems->search({ biblionumber => $content->biblionumber })->next->itemtype;
306                 $itemtype = Koha::ItemTypes->find( $itemtype );
307                 $this_item->{title}             = $biblio->title;
308                 $this_item->{subtitle}          = $biblio->subtitle;
309                 $this_item->{medium}            = $biblio->medium;
310                 $this_item->{part_number}       = $biblio->part_number;
311                 $this_item->{part_name}         = $biblio->part_name;
312                 $this_item->{author}            = $biblio->author;
313                 $this_item->{dateadded}         = $content->dateadded;
314                 $this_item->{imageurl}          = $itemtype ? C4::Koha::getitemtypeimagelocation( 'intranet', $itemtype->imageurl ) : q{};
315                 $this_item->{description}       = $itemtype ? $itemtype->description : q{}; #FIXME Should this be translated_description ?
316                 $this_item->{notforloan}        = $itemtype->notforloan if $itemtype;
317                 $this_item->{'coins'}           = $biblio->get_coins;
318                 $this_item->{'normalized_upc'}  = GetNormalizedUPC( $record, $marcflavour );
319                 $this_item->{'normalized_ean'}  = GetNormalizedEAN( $record, $marcflavour );
320                 $this_item->{'normalized_oclc'} = GetNormalizedOCLCNumber( $record, $marcflavour );
321                 $this_item->{'normalized_isbn'} = GetNormalizedISBN( undef, $record, $marcflavour );
322
323                 unless ( defined $this_item->{size} ) {
324
325                     #TT has problems with size
326                     $this_item->{size} = q||;
327                 }
328
329                 # Getting items infos for location display
330                 my $items = $biblio->items;
331                 $this_item->{'ITEM_RESULTS'} = $items;
332                 $this_item->{biblionumber} = $biblionumber;
333                 push @items, $this_item;
334             }
335
336             my $some_private_shelves = Koha::Virtualshelves->get_some_shelves(
337                 {
338                     borrowernumber => $loggedinuser,
339                     add_allowed    => 1,
340                     public         => 0,
341                 }
342             );
343             my $some_public_shelves = Koha::Virtualshelves->get_some_shelves(
344                 {
345                     borrowernumber => $loggedinuser,
346                     add_allowed    => 1,
347                     public         => 1,
348                 }
349             );
350
351             $template->param(
352                 add_to_some_private_shelves => $some_private_shelves,
353                 add_to_some_public_shelves  => $some_public_shelves,
354                 can_manage_shelf   => $shelf->can_be_managed($loggedinuser),
355                 can_remove_shelf   => $shelf->can_be_deleted($loggedinuser),
356                 can_remove_biblios => $shelf->can_biblios_be_removed($loggedinuser),
357                 can_add_biblios    => $shelf->can_biblios_be_added($loggedinuser),
358                 sortfield          => $sortfield,
359                 itemsloop          => \@items,
360                 sortfield          => $sortfield,
361                 direction          => $direction,
362             );
363             if ( $page ) {
364                 my $pager = $contents->pager;
365                 $template->param(
366                     pagination_bar => pagination_bar(
367                         q||, $pager->last_page - $pager->first_page + 1,
368                         $page, "page", { op => 'view', shelfnumber => $shelf->shelfnumber, sortfield => $sortfield, direction => $direction, }
369                     ),
370                 );
371             }
372         } else {
373             push @messages, { type => 'error', code => 'unauthorized_on_view' };
374             undef $shelf;
375         }
376     } else {
377         push @messages, { type => 'alert', code => 'does_not_exist' };
378     }
379 } elsif( $op eq 'list' ) {
380     $allow_transfer = haspermission( C4::Context->userenv->{id}, { lists => 'edit_public_lists' } ) ? 1 : 0;
381         # this check only serves for button display
382 }
383
384 $template->param(
385     op       => $op,
386     referer  => $referer,
387     shelf    => $shelf,
388     messages => \@messages,
389     public   => $public,
390     print    => scalar $query->param('print') || 0,
391     csv_profiles => [ Koha::CsvProfiles->search({ type => 'marc', used_for => 'export_records' })->as_list ],
392     allow_transfer => $allow_transfer,
393 );
394
395 output_html_with_http_headers $query, $cookie, $template->output;