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