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