+} elsif ( $op eq 'add' ) {
+ if ( $loggedinuser ) {
+ eval {
+ $shelf = Koha::Virtualshelf->new(
+ { shelfname => scalar $query->param('shelfname'),
+ sortfield => scalar $query->param('sortfield'),
+ category => scalar $query->param('category') || 1,
+ allow_add => scalar $query->param('allow_add'),
+ allow_delete_own => scalar $query->param('allow_delete_own'),
+ allow_delete_other => scalar $query->param('allow_delete_other'),
+ owner => scalar $loggedinuser,
+ }
+ );
+ $shelf->store;
+ $shelfnumber = $shelf->shelfnumber;
+ };
+ if ($@) {
+ push @messages, { type => 'error', code => ref($@), msg => $@ };
+ } elsif ( not $shelf ) {
+ push @messages, { type => 'error', code => 'error_on_insert' };
+ } else {
+ push @messages, { type => 'message', code => 'success_on_insert' };
+ $op = 'view';
+ }
+ } else {
+ push @messages, { type => 'error', code => 'unauthorized_on_insert' };
+ $op = 'list';
+ }
+} elsif ( $op eq 'edit' ) {
+ $shelfnumber = $query->param('shelfnumber');
+ $shelf = Koha::Virtualshelves->find($shelfnumber);
+ if ( $shelf ) {
+ $op = $referer;
+ if ( $shelf->can_be_managed( $loggedinuser ) ) {
+ $shelf->shelfname( $query->param('shelfname') );
+ $shelf->sortfield( $query->param('sortfield') );
+ $shelf->allow_add( $query->param('allow_add') );
+ $shelf->allow_delete_own( $query->param('allow_delete_own') );
+ $shelf->allow_delete_other( $query->param('allow_delete_other') );
+ $shelf->category( $query->param('category') );
+ eval { $shelf->store };
+
+ 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 = GetItem( 0, $barcode);
+ if (defined $item && $item->{itemnumber}) {
+ my $biblio = GetBiblioFromItemNumber( $item->{itemnumber} );
+ if ( $shelf->can_biblios_be_added( $loggedinuser ) ) {
+ my $added = eval { $shelf->add_biblio( $biblio->{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' };
+ }