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