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