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