Merge remote-tracking branch 'translate/21.11.23-translate-20230803' into 21.11.x...
[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 );
23 use C4::Biblio qw( GetMarcBiblio );
24 use C4::Circulation qw( barcodedecode );
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 output_and_exit_if_error );
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
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 );
60
61 if ( $op eq 'add_form' ) {
62     # Only pass default
63     $shelf = { allow_change_from_owner => 1 };
64 } elsif ( $op eq 'edit_form' ) {
65     output_and_exit_if_error($query, $cookie, $template, { check => 'csrf_token' });
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                 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     output_and_exit_if_error($query, $cookie, $template, { check => 'csrf_token' });
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->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     output_and_exit_if_error($query, $cookie, $template, { check => 'csrf_token' });
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     output_and_exit_if_error($query, $cookie, $template, { check => 'csrf_token' });
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     output_and_exit_if_error($query, $cookie, $template, { check => 'csrf_token' });
213     $shelfnumber = $query->param('shelfnumber');
214     $shelf = Koha::Virtualshelves->find($shelfnumber);
215     my @biblionumbers = $query->multi_param('biblionumber');
216     if ($shelf) {
217         if ( $shelf->can_biblios_be_removed( $loggedinuser ) ) {
218             my $number_of_biblios_removed = eval {
219                 $shelf->remove_biblios(
220                     {
221                         biblionumbers => \@biblionumbers,
222                         borrowernumber => $loggedinuser,
223                     }
224                 );
225             };
226             if ($@) {
227                 push @messages, { type => 'alert', code => ref($@), msg => $@ };
228             } elsif ( $number_of_biblios_removed ) {
229                 push @messages, { type => 'message', code => 'success_on_remove_biblios' };
230             } else {
231                 push @messages, { type => 'alert', code => 'no_biblio_removed' };
232             }
233         } else {
234             push @messages, { type => 'alert', code => 'unauthorized_on_remove_biblios' };
235         }
236     } else {
237         push @messages, { type => 'alert', code => 'does_not_exist' };
238     }
239     $op = $referer;
240 }
241
242 if ( $op eq 'view' ) {
243     $shelfnumber ||= $query->param('shelfnumber');
244     $shelf = Koha::Virtualshelves->find($shelfnumber);
245     if ( $shelf ) {
246         if ( $shelf->can_be_viewed( $loggedinuser ) ) {
247             my $sortfield = $query->param('sortfield') || $shelf->sortfield || 'title';    # Passed in sorting overrides default sorting
248             $sortfield = 'title' unless grep { $_ eq $sortfield } qw( title author copyrightdate itemcallnumber dateadded );
249             my $direction = $query->param('direction') || 'asc';
250             $direction = 'asc' if $direction ne 'asc' and $direction ne 'desc';
251             my ( $rows, $page );
252             unless ( $query->param('print') ) {
253                 $rows = C4::Context->preference('numSearchResults') || 20;
254                 $page = ( $query->param('page') ? $query->param('page') : 1 );
255             }
256
257             my $order_by = $sortfield eq 'itemcallnumber' ? 'items.cn_sort' : $sortfield;
258             my $contents = $shelf->get_contents->search(
259                 {},
260                 {
261                     prefetch => [ { 'biblionumber' => { 'biblioitems' => 'items' } } ],
262                     page     => $page,
263                     rows     => $rows,
264                     order_by => { "-$direction" => $order_by },
265                 }
266             );
267
268             my @items;
269             while ( my $content = $contents->next ) {
270                 my $this_item;
271                 my $biblionumber = $content->biblionumber;
272                 my $record       = GetMarcBiblio({ biblionumber => $biblionumber });
273
274                 $this_item->{XSLTBloc} = XSLTParse4Display(
275                     {
276                         biblionumber => $biblionumber,
277                         record       => $record,
278                         xsl_syspref  => 'XSLTListsDisplay',
279                         fix_amps     => 1,
280                     }
281                 );
282
283                 my $marcflavour = C4::Context->preference("marcflavour");
284                 my $itemtype = Koha::Biblioitems->search({ biblionumber => $content->biblionumber })->next->itemtype;
285                 $itemtype = Koha::ItemTypes->find( $itemtype );
286                 my $biblio = Koha::Biblios->find( $content->biblionumber );
287                 $this_item->{title}             = $biblio->title;
288                 $this_item->{subtitle}          = $biblio->subtitle;
289                 $this_item->{medium}            = $biblio->medium;
290                 $this_item->{part_number}       = $biblio->part_number;
291                 $this_item->{part_name}         = $biblio->part_name;
292                 $this_item->{author}            = $biblio->author;
293                 $this_item->{dateadded}         = $content->dateadded;
294                 $this_item->{imageurl}          = $itemtype ? C4::Koha::getitemtypeimagelocation( 'intranet', $itemtype->imageurl ) : q{};
295                 $this_item->{description}       = $itemtype ? $itemtype->description : q{}; #FIXME Should this be translated_description ?
296                 $this_item->{notforloan}        = $itemtype->notforloan if $itemtype;
297                 $this_item->{'coins'}           = $biblio->get_coins;
298                 $this_item->{'normalized_upc'}  = GetNormalizedUPC( $record, $marcflavour );
299                 $this_item->{'normalized_ean'}  = GetNormalizedEAN( $record, $marcflavour );
300                 $this_item->{'normalized_oclc'} = GetNormalizedOCLCNumber( $record, $marcflavour );
301                 $this_item->{'normalized_isbn'} = GetNormalizedISBN( undef, $record, $marcflavour );
302
303                 unless ( defined $this_item->{size} ) {
304
305                     #TT has problems with size
306                     $this_item->{size} = q||;
307                 }
308
309                 # Getting items infos for location display
310                 my @items_infos = &GetItemsLocationInfo( $biblionumber );
311                 $this_item->{'ITEM_RESULTS'} = \@items_infos;
312                 $this_item->{biblionumber} = $biblionumber;
313                 push @items, $this_item;
314             }
315
316             my $some_private_shelves = Koha::Virtualshelves->get_some_shelves(
317                 {
318                     borrowernumber => $loggedinuser,
319                     add_allowed    => 1,
320                     public         => 0,
321                 }
322             );
323             my $some_public_shelves = Koha::Virtualshelves->get_some_shelves(
324                 {
325                     borrowernumber => $loggedinuser,
326                     add_allowed    => 1,
327                     public         => 1,
328                 }
329             );
330
331             $template->param(
332                 add_to_some_private_shelves => $some_private_shelves,
333                 add_to_some_public_shelves  => $some_public_shelves,
334                 can_manage_shelf   => $shelf->can_be_managed($loggedinuser),
335                 can_remove_shelf   => $shelf->can_be_deleted($loggedinuser),
336                 can_remove_biblios => $shelf->can_biblios_be_removed($loggedinuser),
337                 can_add_biblios    => $shelf->can_biblios_be_added($loggedinuser),
338                 sortfield          => $sortfield,
339                 itemsloop          => \@items,
340                 sortfield          => $sortfield,
341                 direction          => $direction,
342             );
343             if ( $page ) {
344                 my $pager = $contents->pager;
345                 $template->param(
346                     pagination_bar => pagination_bar(
347                         q||, $pager->last_page - $pager->first_page + 1,
348                         $page, "page", { op => 'view', shelfnumber => $shelf->shelfnumber, sortfield => $sortfield, direction => $direction, }
349                     ),
350                 );
351             }
352         } else {
353             push @messages, { type => 'error', code => 'unauthorized_on_view' };
354             undef $shelf;
355         }
356     } else {
357         push @messages, { type => 'alert', code => 'does_not_exist' };
358     }
359 }
360
361 $template->param(
362     op       => $op,
363     referer  => $referer,
364     shelf    => $shelf,
365     messages => \@messages,
366     public   => $public,
367     print    => scalar $query->param('print') || 0,
368     csv_profiles => [ Koha::CsvProfiles->search({ type => 'marc', used_for => 'export_records' }) ],
369 );
370
371 output_html_with_http_headers $query, $cookie, $template->output;