-sub viewshelf {
- my $shelfnumber=shift;
- #check that the user can view the shelf
- return unless (ShelfPossibleAction($loggedinuser,$shelfnumber,'view'));
- my ($itemlist) = GetShelfContents($env, $shelfnumber);
- my $item='';
- my $color='';
- my @itemsloop;
- foreach $item (sort {$a->{'barcode'} cmp $b->{'barcode'}} @$itemlist) {
- my %line;
- ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
- $line{'color'}=$color;
- $line{'itemnumber'}=$item->{'itemnumber'};
- $line{'barcode'}=$item->{'barcode'};
- $line{'title'}=$item->{'title'};
- $line{'author'}=$item->{'author'};
- $line{'classification'}=$item->{'classification'};
- $line{'itemtype'}=$item->{'itemtype'};
- $line{biblionumber} = $item->{biblionumber};
- push(@itemsloop, \%line);
- }
- $template->param( itemsloop => \@itemsloop,
- shelfname => $shelflist->{$shelfnumber}->{'shelfname'},
- shelfnumber => $shelfnumber,
- viewshelf => $query->param('viewshelf'),
- manageshelf => &ShelfPossibleAction($loggedinuser,$shelfnumber,'manage'),
- );
+ if ($@) {
+ push @messages, { type => 'error', code => 'error_on_update' };
+ $op = 'edit_form';
+ } else {
+ push @messages, { type => 'message', code => 'success_on_update' };
+ }
+ } else {
+ push @messages, { type => 'error', code => 'unauthorized_on_update' };
+ }
+ } else {
+ push @messages, { type => 'error', code => 'does_not_exist' };
+ }
+} elsif ( $op eq 'delete' ) {
+ $shelfnumber = $query->param('shelfnumber');
+ $shelf = Koha::Virtualshelves->find($shelfnumber);
+ if ($shelf) {
+ if ( $shelf->can_be_deleted( $loggedinuser ) ) {
+ eval { $shelf->delete; };
+ if ($@) {
+ push @messages, { type => 'error', code => ref($@), msg => $@ };
+ } else {
+ push @messages, { type => 'message', code => 'success_on_delete' };
+ }
+ } else {
+ push @messages, { type => 'error', code => 'unauthorized_on_delete' };
+ }
+ } else {
+ push @messages, { type => 'error', code => 'does_not_exist' };
+ }
+ $op = $referer;
+} elsif ( $op eq 'remove_share' ) {
+ $shelfnumber = $query->param('shelfnumber');
+ $shelf = Koha::Virtualshelves->find($shelfnumber);
+ if ($shelf) {
+ my $removed = eval { $shelf->remove_share( $loggedinuser ); };
+ if ($@) {
+ push @messages, { type => 'error', code => ref($@), msg => $@ };
+ } elsif ( $removed ) {
+ push @messages, { type => 'message', code => 'success_on_remove_share' };
+ } else {
+ push @messages, { type => 'error', code => 'error_on_remove_share' };
+ }
+ } else {
+ push @messages, { type => 'error', code => 'does_not_exist' };
+ }
+ $op = $referer;
+
+} elsif ( $op eq 'add_biblio' ) {
+ $shelfnumber = $query->param('shelfnumber');
+ $shelf = Koha::Virtualshelves->find($shelfnumber);
+ if ($shelf) {
+ if( my $barcode = $query->param('barcode') ) {
+ my $item = Koha::Items->find({ barcode => $barcode });
+ if ( $item ) {
+ if ( $shelf->can_biblios_be_added( $loggedinuser ) ) {
+ my $added = eval { $shelf->add_biblio( $item->biblionumber, $loggedinuser ); };
+ if ($@) {
+ push @messages, { type => 'error', code => ref($@), msg => $@ };
+ } elsif ( $added ) {
+ push @messages, { type => 'message', code => 'success_on_add_biblio' };
+ } else {
+ push @messages, { type => 'message', code => 'error_on_add_biblio' };
+ }
+ } else {
+ push @messages, { type => 'error', code => 'unauthorized_on_add_biblio' };
+ }
+ } else {
+ push @messages, { type => 'error', code => 'item_does_not_exist' };
+ }
+ }
+ } else {
+ push @messages, { type => 'error', code => 'does_not_exist' };
+ }
+ $op = $referer;
+} elsif ( $op eq 'remove_biblios' ) {
+ $shelfnumber = $query->param('shelfnumber');
+ $shelf = Koha::Virtualshelves->find($shelfnumber);
+ my @biblionumber = $query->multi_param('biblionumber');
+ if ($shelf) {
+ if ( $shelf->can_biblios_be_removed( $loggedinuser ) ) {
+ my $number_of_biblios_removed = eval {
+ $shelf->remove_biblios(
+ {
+ biblionumbers => \@biblionumber,
+ borrowernumber => $loggedinuser,
+ }
+ );
+ };
+ if ($@) {
+ push @messages, { type => 'error', code => ref($@), msg => $@ };
+ } elsif ( $number_of_biblios_removed ) {
+ push @messages, { type => 'message', code => 'success_on_remove_biblios' };
+ } else {
+ push @messages, { type => 'error', code => 'no_biblio_removed' };
+ }
+ } else {
+ push @messages, { type => 'error', code => 'unauthorized_on_remove_biblios' };
+ }
+ } else {
+ push @messages, { type => 'error', code => 'does_not_exist' };
+ }
+ $op = 'view';