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