Bug 34029: DBIC schema
[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 haspermission );
23 use C4::Circulation qw( barcodedecode );
24 use C4::Context;
25 use C4::Koha qw(
26     GetNormalizedEAN
27     GetNormalizedISBN
28     GetNormalizedOCLCNumber
29     GetNormalizedUPC
30 );
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 use constant PERMITTED => 4;
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, $allow_transfer );
61
62 # PART1: Perform a few actions
63 if ( $op eq 'add_form' ) {
64     # Only pass default
65     $shelf = { allow_change_from_owner => 1 };
66 } elsif ( $op eq 'edit_form' ) {
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     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                 allow_change_from_staff           => $allow_changes_from == STAFF,
91                 allow_change_from_permitted_staff => $allow_changes_from == PERMITTED,
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->allow_change_from_permitted_staff( $allow_changes_from == PERMITTED );
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     $shelfnumber = $query->param('shelfnumber');
140     $shelf       = Koha::Virtualshelves->find($shelfnumber);
141     if ($shelf) {
142         if ( $shelf->can_be_deleted( $loggedinuser ) ) {
143             eval { $shelf->delete; };
144             if ($@) {
145                 push @messages, { type => 'alert', code => ref($@), msg => $@ };
146             } else {
147                 push @messages, { type => 'message', code => 'success_on_delete' };
148             }
149         } else {
150             push @messages, { type => 'alert', code => 'unauthorized_on_delete' };
151         }
152     } else {
153         push @messages, { type => 'alert', code => 'does_not_exist' };
154     }
155     $op = 'list';
156 } elsif ( $op eq 'add_biblio' ) {
157     $shelfnumber = $query->param('shelfnumber');
158     $shelf = Koha::Virtualshelves->find($shelfnumber);
159     if ($shelf) {
160         if( my $barcodes = $query->param('barcodes') ) {
161             if ( $shelf->can_biblios_be_added( $loggedinuser ) ) {
162                 my @barcodes = split /\n/, $barcodes; # Entries are effectively passed in as a <cr> separated list
163                 foreach my $barcode (@barcodes){
164                     $barcode = barcodedecode( $barcode ) if $barcode;
165                     next if $barcode eq '';
166                     my $item = Koha::Items->find({barcode => $barcode});
167                     if ( $item ) {
168                         my $added = eval { $shelf->add_biblio( $item->biblionumber, $loggedinuser ); };
169                         if ($@) {
170                             push @messages, { item_barcode => $barcode, type => 'alert', code => ref($@), msg => $@ };
171                         } elsif ( $added ) {
172                             push @messages, { item_barcode => $barcode, type => 'message', code => 'success_on_add_biblio' };
173                         } else {
174                             push @messages, { item_barcode => $barcode, type => 'message', code => 'error_on_add_biblio' };
175                         }
176                     } else {
177                         push @messages, { item_barcode => $barcode, type => 'alert', code => 'item_does_not_exist' };
178                     }
179                 }
180             } else {
181                 push @messages, { type => 'alert', code => 'unauthorized_on_add_biblio' };
182             }
183         }
184         if ( my $biblionumbers = $query->param('biblionumbers') ) {
185             if ( $shelf->can_biblios_be_added( $loggedinuser ) ) {
186                 my @biblionumbers = split /\n/, $biblionumbers;
187                 foreach my $biblionumber (@biblionumbers) {
188                     $biblionumber =~ s/\r$//; # strip any naughty return chars
189                     next if $biblionumber eq '';
190                     my $biblio = Koha::Biblios->find($biblionumber);
191                     if (defined $biblio) {
192                         my $added = eval { $shelf->add_biblio( $biblionumber, $loggedinuser ); };
193                         if ($@) {
194                             push @messages, { bibnum => $biblionumber, type => 'alert', code => ref($@), msg => $@ };
195                         } elsif ( $added ) {
196                             push @messages, { bibnum => $biblionumber, type => 'message', code => 'success_on_add_biblio' };
197                         } else {
198                             push @messages, { bibnum => $biblionumber, type => 'message', code => 'error_on_add_biblio' };
199                         }
200                     } else {
201                         push @messages, { bibnum => $biblionumber, type => 'alert', code => 'item_does_not_exist' };
202                     }
203                 }
204             } else {
205                 push @messages, { type => 'alert', code => 'unauthorized_on_add_biblio' };
206             }
207         }
208     } else {
209         push @messages, { type => 'alert', code => 'does_not_exist' };
210     }
211     $op = $referer;
212 } elsif ( $op eq 'remove_biblios' ) {
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 } elsif ( $op eq 'transfer' ) {
241     $shelfnumber = $query->param('shelfnumber');
242     $shelf = Koha::Virtualshelves->find($shelfnumber) if $shelfnumber;
243     my $new_owner = $query->param('new_owner'); # is a borrowernumber
244     my $error_code = $shelf
245         ? $shelf->cannot_be_transferred({ by => $loggedinuser, to => $new_owner, interface => 'intranet' })
246         : 'does_not_exist';
247
248     if( !$new_owner && $error_code eq 'missing_to_parameter' ) {
249         # show form
250     } elsif( $error_code ) {
251         push @messages, { type => 'error', code => $error_code };
252         $op = 'list';
253     } else {
254         $shelf->owner($new_owner)->store;
255         $op = 'list';
256     }
257 }
258
259 # PART2: After a possible action, further prepare form
260 if ( $op eq 'view' ) {
261     $shelfnumber ||= $query->param('shelfnumber');
262     $shelf = Koha::Virtualshelves->find($shelfnumber);
263     if ( $shelf ) {
264         if ( $shelf->can_be_viewed( $loggedinuser ) ) {
265             my $sortfield = $query->param('sortfield') || $shelf->sortfield || 'title';    # Passed in sorting overrides default sorting
266             $sortfield = 'title' unless grep { $_ eq $sortfield } qw( title author copyrightdate itemcallnumber dateadded );
267             my $direction = $query->param('direction') || 'asc';
268             $direction = 'asc' if $direction ne 'asc' and $direction ne 'desc';
269             my ( $rows, $page );
270             unless ( $query->param('print') ) {
271                 $rows = C4::Context->preference('numSearchResults') || 20;
272                 $page = ( $query->param('page') ? $query->param('page') : 1 );
273             }
274
275             my $order_by = $sortfield eq 'itemcallnumber' ? 'items.cn_sort' : $sortfield;
276             my $contents = $shelf->get_contents->search(
277                 {},
278                 {
279                     prefetch => [ { 'biblionumber' => { 'biblioitems' => 'items' } } ],
280                     page     => $page,
281                     rows     => $rows,
282                     order_by => { "-$direction" => $order_by },
283                 }
284             );
285
286             my @items;
287             while ( my $content = $contents->next ) {
288                 my $this_item;
289                 my $biblionumber = $content->biblionumber;
290                 my $biblio       = Koha::Biblios->find($biblionumber);
291                 my $record       = $biblio->metadata->record;
292
293                 $this_item->{XSLTBloc} = XSLTParse4Display(
294                     {
295                         biblionumber => $biblionumber,
296                         record       => $record,
297                         xsl_syspref  => 'XSLTListsDisplay',
298                         fix_amps     => 1,
299                     }
300                 );
301
302                 my $marcflavour = C4::Context->preference("marcflavour");
303                 my $itemtype = Koha::Biblioitems->search({ biblionumber => $content->biblionumber })->next->itemtype;
304                 $itemtype = Koha::ItemTypes->find( $itemtype );
305                 $this_item->{title}             = $biblio->title;
306                 $this_item->{subtitle}          = $biblio->subtitle;
307                 $this_item->{medium}            = $biblio->medium;
308                 $this_item->{part_number}       = $biblio->part_number;
309                 $this_item->{part_name}         = $biblio->part_name;
310                 $this_item->{author}            = $biblio->author;
311                 $this_item->{dateadded}         = $content->dateadded;
312                 $this_item->{imageurl}          = $itemtype ? C4::Koha::getitemtypeimagelocation( 'intranet', $itemtype->imageurl ) : q{};
313                 $this_item->{description}       = $itemtype ? $itemtype->description : q{}; #FIXME Should this be translated_description ?
314                 $this_item->{notforloan}        = $itemtype->notforloan if $itemtype;
315                 $this_item->{'coins'}           = $biblio->get_coins;
316                 $this_item->{'normalized_upc'}  = GetNormalizedUPC( $record, $marcflavour );
317                 $this_item->{'normalized_ean'}  = GetNormalizedEAN( $record, $marcflavour );
318                 $this_item->{'normalized_oclc'} = GetNormalizedOCLCNumber( $record, $marcflavour );
319                 $this_item->{'normalized_isbn'} = GetNormalizedISBN( undef, $record, $marcflavour );
320
321                 unless ( defined $this_item->{size} ) {
322
323                     #TT has problems with size
324                     $this_item->{size} = q||;
325                 }
326
327                 # Getting items infos for location display
328                 my $items = $biblio->items;
329                 $this_item->{'ITEM_RESULTS'} = $items;
330                 $this_item->{biblionumber} = $biblionumber;
331                 push @items, $this_item;
332             }
333
334             my $some_private_shelves = Koha::Virtualshelves->get_some_shelves(
335                 {
336                     borrowernumber => $loggedinuser,
337                     add_allowed    => 1,
338                     public         => 0,
339                 }
340             );
341             my $some_public_shelves = Koha::Virtualshelves->get_some_shelves(
342                 {
343                     borrowernumber => $loggedinuser,
344                     add_allowed    => 1,
345                     public         => 1,
346                 }
347             );
348
349             $template->param(
350                 add_to_some_private_shelves => $some_private_shelves,
351                 add_to_some_public_shelves  => $some_public_shelves,
352                 can_manage_shelf   => $shelf->can_be_managed($loggedinuser),
353                 can_remove_shelf   => $shelf->can_be_deleted($loggedinuser),
354                 can_remove_biblios => $shelf->can_biblios_be_removed($loggedinuser),
355                 can_add_biblios    => $shelf->can_biblios_be_added($loggedinuser),
356                 sortfield          => $sortfield,
357                 itemsloop          => \@items,
358                 sortfield          => $sortfield,
359                 direction          => $direction,
360             );
361             if ( $page ) {
362                 my $pager = $contents->pager;
363                 $template->param(
364                     pagination_bar => pagination_bar(
365                         q||, $pager->last_page - $pager->first_page + 1,
366                         $page, "page", { op => 'view', shelfnumber => $shelf->shelfnumber, sortfield => $sortfield, direction => $direction, }
367                     ),
368                 );
369             }
370         } else {
371             push @messages, { type => 'error', code => 'unauthorized_on_view' };
372             undef $shelf;
373         }
374     } else {
375         push @messages, { type => 'alert', code => 'does_not_exist' };
376     }
377 } elsif( $op eq 'list' ) {
378     $allow_transfer = haspermission( C4::Context->userenv->{id}, { lists => 'edit_public_lists' } ) ? 1 : 0;
379         # this check only serves for button display
380 }
381
382 $template->param(
383     op       => $op,
384     referer  => $referer,
385     shelf    => $shelf,
386     messages => \@messages,
387     public   => $public,
388     print    => scalar $query->param('print') || 0,
389     csv_profiles => [ Koha::CsvProfiles->search({ type => 'marc', used_for => 'export_records' })->as_list ],
390     allow_transfer => $allow_transfer,
391 );
392
393 output_html_with_http_headers $query, $cookie, $template->output;