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