Bug 7663 (QA Followup)
[koha.git] / virtualshelves / shelves.pl
index d58cf7b..a6d04ce 100755 (executable)
@@ -21,12 +21,15 @@ use Modern::Perl;
 use CGI qw ( -utf8 );
 use C4::Auth;
 use C4::Biblio;
-use C4::Csv;
 use C4::Koha;
 use C4::Items;
 use C4::Members;
 use C4::Output;
 use C4::XSLT;
+
+use Koha::Biblios;
+use Koha::Biblioitems;
+use Koha::CsvProfiles;
 use Koha::Virtualshelves;
 
 my $query = new CGI;
@@ -56,11 +59,11 @@ if ( $op eq 'add_form' ) {
         my $patron = GetMember( 'borrowernumber' => $shelf->owner );
         $template->param( owner => $patron, );
         unless ( $shelf->can_be_managed( $loggedinuser ) ) {
-            push @messages, { type => 'error', code => 'unauthorized_on_update' };
+            push @messages, { type => 'alert', code => 'unauthorized_on_update' };
             $op = 'list';
         }
     } else {
-        push @messages, { type => 'error', code => 'does_not_exist' };
+        push @messages, { type => 'alert', code => 'does_not_exist' };
     }
 } elsif ( $op eq 'add' ) {
     eval {
@@ -78,9 +81,10 @@ if ( $op eq 'add_form' ) {
         $shelfnumber = $shelf->shelfnumber;
     };
     if ($@) {
-        push @messages, { type => 'error', code => ref($@), msg => $@ };
+        push @messages, { type => 'alert', code => ref($@), msg => $@ };
     } elsif ( not $shelf ) {
-        push @messages, { type => 'error', code => 'error_on_insert' };
+        push @messages, { type => 'alert', code => 'error_on_insert' };
+
     } else {
         push @messages, { type => 'message', code => 'success_on_insert' };
         $op = 'view';
@@ -91,9 +95,11 @@ if ( $op eq 'add_form' ) {
 
     if ( $shelf ) {
         $op = $referer;
+        my $sortfield = $query->param('sortfield');
+        $sortfield = 'title' unless grep {/^$sortfield$/}qw( title author copyrightdate itemcallnumber );
         if ( $shelf->can_be_managed( $loggedinuser ) ) {
             $shelf->shelfname( scalar $query->param('shelfname') );
-            $shelf->sortfield( scalar $query->param('sortfield') );
+            $shelf->sortfield( $sortfield );
             $shelf->allow_add( scalar $query->param('allow_add') );
             $shelf->allow_delete_own( scalar $query->param('allow_delete_own') );
             $shelf->allow_delete_other( scalar $query->param('allow_delete_other') );
@@ -101,16 +107,16 @@ if ( $op eq 'add_form' ) {
             eval { $shelf->store };
 
             if ($@) {
-                push @messages, { type => 'error', code => 'error_on_update' };
+                push @messages, { type => 'alert', 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' };
+            push @messages, { type => 'alert', code => 'unauthorized_on_update' };
         }
     } else {
-        push @messages, { type => 'error', code => 'does_not_exist' };
+        push @messages, { type => 'alert', code => 'does_not_exist' };
     }
 } elsif ( $op eq 'delete' ) {
     $shelfnumber = $query->param('shelfnumber');
@@ -119,43 +125,48 @@ if ( $op eq 'add_form' ) {
         if ( $shelf->can_be_deleted( $loggedinuser ) ) {
             eval { $shelf->delete; };
             if ($@) {
-                push @messages, { type => 'error', code => ref($@), msg => $@ };
+                push @messages, { type => 'alert', code => ref($@), msg => $@ };
             } else {
                 push @messages, { type => 'message', code => 'success_on_delete' };
             }
         } else {
-            push @messages, { type => 'error', code => 'unauthorized_on_delete' };
+            push @messages, { type => 'alert', code => 'unauthorized_on_delete' };
         }
     } else {
-        push @messages, { type => 'error', code => 'does_not_exist' };
+        push @messages, { type => 'alert', code => 'does_not_exist' };
     }
     $op = 'list';
 } 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' };
+        if( my $barcodes = $query->param('barcodes') ) {
+            if ( $shelf->can_biblios_be_added( $loggedinuser ) ) {
+                my @barcodes = split /\n/, $barcodes; # Entries are effectively passed in as a <cr> separated list
+                foreach my $barcode (@barcodes){
+                    $barcode =~ s/\r$//; # strip any naughty return chars
+                    next if $barcode eq '';
+                    my $item = GetItem( 0, $barcode);
+                    if (defined $item && $item->{itemnumber}) {
+                        my $biblio = GetBiblioFromItemNumber( $item->{itemnumber} );
+                        my $added = eval { $shelf->add_biblio( $biblio->{biblionumber}, $loggedinuser ); };
+                        if ($@) {
+                            push @messages, { item_barcode => $barcode, type => 'error', code => ref($@), msg => $@ };
+                        } elsif ( $added ) {
+                            push @messages, { item_barcode => $barcode, type => 'message', code => 'success_on_add_biblio' };
+                        } else {
+                            push @messages, { item_barcode => $barcode, type => 'message', code => 'error_on_add_biblio' };
+                        }
                     } else {
-                        push @messages, { type => 'message', code => 'error_on_add_biblio' };
+                        push @messages, { item_barcode => $barcode, type => 'error', code => 'item_does_not_exist' };
                     }
-                } else {
-                    push @messages, { type => 'error', code => 'unauthorized_on_add_biblio' };
                 }
             } else {
-                push @messages, { type => 'error', code => 'item_does_not_exist' };
+                push @messages, { type => 'error', code => 'unauthorized_on_add_biblio' };
             }
         }
     } else {
-        push @messages, { type => 'error', code => 'does_not_exist' };
+        push @messages, { type => 'alert', code => 'does_not_exist' };
     }
     $op = $referer;
 } elsif ( $op eq 'remove_biblios' ) {
@@ -173,17 +184,17 @@ if ( $op eq 'add_form' ) {
                 );
             };
             if ($@) {
-                push @messages, { type => 'error', code => ref($@), msg => $@ };
+                push @messages, { type => 'alert', 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' };
+                push @messages, { type => 'alert', code => 'no_biblio_removed' };
             }
         } else {
-            push @messages, { type => 'error', code => 'unauthorized_on_remove_biblios' };
+            push @messages, { type => 'alert', code => 'unauthorized_on_remove_biblios' };
         }
     } else {
-        push @messages, { type => 'error', code => 'does_not_exist' };
+        push @messages, { type => 'alert', code => 'does_not_exist' };
     }
     $op = $referer;
 }
@@ -194,6 +205,7 @@ if ( $op eq 'view' ) {
     if ( $shelf ) {
         if ( $shelf->can_be_viewed( $loggedinuser ) ) {
             my $sortfield = $query->param('sortfield') || $shelf->sortfield || 'title';    # Passed in sorting overrides default sorting
+            $sortfield = 'title' unless grep {/^$sortfield$/}qw( title author copyrightdate itemcallnumber );
             my $direction = $query->param('direction') || 'asc';
             $direction = 'asc' if $direction ne 'asc' and $direction ne 'desc';
             my ( $rows, $page );
@@ -215,14 +227,14 @@ if ( $op eq 'view' ) {
 
             my $borrower = GetMember( borrowernumber => $loggedinuser );
 
-            my $xslfile = C4::Context->preference('XSLTListsDisplay') || 'default';
+            my $xslfile = C4::Context->preference('XSLTListsDisplay');
             my $lang   = $xslfile ? C4::Languages::getlanguage()  : undef;
             my $sysxml = $xslfile ? C4::XSLT::get_xslt_sysprefs() : undef;
 
             my @items;
             while ( my $content = $contents->next ) {
                 my $this_item;
-                my $biblionumber = $content->biblionumber->biblionumber;
+                my $biblionumber = $content->biblionumber;
                 my $record       = GetMarcBiblio($biblionumber);
 
                 if ( $xslfile ) {
@@ -231,9 +243,11 @@ if ( $op eq 'view' ) {
                 }
 
                 my $marcflavour = C4::Context->preference("marcflavour");
-                my $itemtypeinfo = getitemtypeinfo( $content->biblionumber->biblioitems->first->itemtype, 'intranet' );
-                $this_item->{title}             = $content->biblionumber->title;
-                $this_item->{author}            = $content->biblionumber->author;
+                my $itemtype = Koha::Biblioitems->search({ biblionumber => $content->biblionumber })->next->itemtype;
+                my $itemtypeinfo = getitemtypeinfo( $itemtype, 'intranet' );
+                my $biblio = Koha::Biblios->find( $content->biblionumber );
+                $this_item->{title}             = $biblio->title;
+                $this_item->{author}            = $biblio->author;
                 $this_item->{dateadded}         = $content->dateadded;
                 $this_item->{imageurl}          = $itemtypeinfo->{imageurl};
                 $this_item->{description}       = $itemtypeinfo->{description};
@@ -296,9 +310,10 @@ if ( $op eq 'view' ) {
             }
         } else {
             push @messages, { type => 'error', code => 'unauthorized_on_view' };
+            undef $shelf;
         }
     } else {
-        push @messages, { type => 'error', code => 'does_not_exist' };
+        push @messages, { type => 'alert', code => 'does_not_exist' };
     }
 }
 
@@ -309,7 +324,7 @@ $template->param(
     messages => \@messages,
     category => $category,
     print    => scalar $query->param('print') || 0,
-    csv_profiles => GetCsvProfilesLoop('marc'),
+    csv_profiles => [ Koha::CsvProfiles->search({ type => 'marc' }) ],
 );
 
 output_html_with_http_headers $query, $cookie, $template->output;