Bug 24322: Simplify condition to separate elements
[koha-ffzg.git] / virtualshelves / shelves.pl
index 8e7ecc0..e219e9a 100755 (executable)
 
 use Modern::Perl;
 use CGI qw ( -utf8 );
-use C4::VirtualShelves;
 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::Items;
+use Koha::ItemTypes;
+use Koha::CsvProfiles;
+use Koha::Patrons;
 use Koha::Virtualshelves;
 
+use constant ANYONE => 2;
+
 my $query = new CGI;
 
 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
     {   template_name   => "virtualshelves/shelves.tt",
         query           => $query,
         type            => "intranet",
-        authnotrequired => 0,
         flagsrequired   => { catalogue => 1 },
     }
 );
@@ -47,41 +53,43 @@ my $category = $query->param('category') || 1;
 my ( $shelf, $shelfnumber, @messages );
 
 if ( $op eq 'add_form' ) {
-    # Nothing to do
+    # Only pass default
+    $shelf = { allow_change_from_owner => 1 };
 } elsif ( $op eq 'edit_form' ) {
     $shelfnumber = $query->param('shelfnumber');
     $shelf       = Koha::Virtualshelves->find($shelfnumber);
 
     if ( $shelf ) {
         $category = $shelf->category;
-        my $patron = GetMember( 'borrowernumber' => $shelf->owner );
+        my $patron = Koha::Patrons->find( $shelf->owner )->unblessed;
         $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' ) {
+    my $allow_changes_from = $query->param('allow_changes_from');
     eval {
         $shelf = Koha::Virtualshelf->new(
-            {   shelfname          => $query->param('shelfname'),
-                sortfield          => $query->param('sortfield'),
-                category           => $query->param('category'),
-                allow_add          => $query->param('allow_add'),
-                allow_delete_own   => $query->param('allow_delete_own'),
-                allow_delete_other => $query->param('allow_delete_other'),
-                owner              => $query->param('owner'),
+            {   shelfname          => scalar $query->param('shelfname'),
+                sortfield          => scalar $query->param('sortfield'),
+                category           => scalar $query->param('category'),
+                allow_change_from_owner => $allow_changes_from > 0,
+                allow_change_from_others => $allow_changes_from == ANYONE,
+                owner              => scalar $query->param('owner'),
             }
         );
         $shelf->store;
         $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';
@@ -92,26 +100,28 @@ if ( $op eq 'add_form' ) {
 
     if ( $shelf ) {
         $op = $referer;
+        my $sortfield = $query->param('sortfield');
+        $sortfield = 'title' unless grep { $_ eq $sortfield } qw( title author copyrightdate itemcallnumber dateadded );
         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') );
+            $shelf->shelfname( scalar $query->param('shelfname') );
+            $shelf->sortfield( $sortfield );
+            my $allow_changes_from = $query->param('allow_changes_from');
+            $shelf->allow_change_from_owner( $allow_changes_from > 0 );
+            $shelf->allow_change_from_others( $allow_changes_from == ANYONE );
+            $shelf->category( scalar $query->param('category') );
             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');
@@ -120,49 +130,77 @@ 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 = Koha::Items->find({barcode => $barcode});
+                    if ( $item ) {
+                        my $added = eval { $shelf->add_biblio( $item->biblionumber, $loggedinuser ); };
+                        if ($@) {
+                            push @messages, { item_barcode => $barcode, type => 'alert', 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, { item_barcode => $barcode, type => 'alert', code => 'item_does_not_exist' };
+                    }
+                }
+            } else {
+                push @messages, { type => 'alert', code => 'unauthorized_on_add_biblio' };
+            }
+        }
+        if ( my $biblionumbers = $query->param('biblionumbers') ) {
+            if ( $shelf->can_biblios_be_added( $loggedinuser ) ) {
+                my @biblionumbers = split /\n/, $biblionumbers;
+                foreach my $biblionumber (@biblionumbers) {
+                    $biblionumber =~ s/\r$//; # strip any naughty return chars
+                    next if $biblionumber eq '';
+                    my $biblio = Koha::Biblios->find($biblionumber);
+                    if (defined $biblio) {
+                        my $added = eval { $shelf->add_biblio( $biblionumber, $loggedinuser ); };
+                        if ($@) {
+                            push @messages, { bibnum => $biblionumber, type => 'alert', code => ref($@), msg => $@ };
+                        } elsif ( $added ) {
+                            push @messages, { bibnum => $biblionumber, type => 'message', code => 'success_on_add_biblio' };
+                        } else {
+                            push @messages, { bibnum => $biblionumber, type => 'message', code => 'error_on_add_biblio' };
+                        }
                     } else {
-                        push @messages, { type => 'message', code => 'error_on_add_biblio' };
+                        push @messages, { bibnum => $biblionumber, type => 'alert', 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 => 'alert', 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' ) {
     $shelfnumber = $query->param('shelfnumber');
     $shelf = Koha::Virtualshelves->find($shelfnumber);
-    my @biblionumbers = $query->param('biblionumber');
+    my @biblionumbers = $query->multi_param('biblionumber');
     if ($shelf) {
         if ( $shelf->can_biblios_be_removed( $loggedinuser ) ) {
             my $number_of_biblios_removed = eval {
@@ -174,17 +212,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,30 +232,57 @@ if ( $op eq 'view' ) {
     $shelf = Koha::Virtualshelves->find($shelfnumber);
     if ( $shelf ) {
         if ( $shelf->can_be_viewed( $loggedinuser ) ) {
-            my $sortfield = $query->param('sortfield') || $shelf->sortfield;    # Passed in sorting overrides default sorting
+            my $sortfield = $query->param('sortfield') || $shelf->sortfield || 'title';    # Passed in sorting overrides default sorting
+            $sortfield = 'title' unless grep { $_ eq $sortfield } qw( title author copyrightdate itemcallnumber dateadded );
             my $direction = $query->param('direction') || 'asc';
-            my ( $shelflimit, $shelfoffset, $itemoff );
+            $direction = 'asc' if $direction ne 'asc' and $direction ne 'desc';
+            my ( $rows, $page );
             unless ( $query->param('print') ) {
-                $shelflimit = C4::Context->preference('numSearchResults') || 20;
-                $itemoff = ( $query->param('itemoff') ? $query->param('itemoff') : 1 );
-                $shelfoffset = ( $itemoff - 1 ) * $shelflimit;    # Sets the offset to begin retrieving items at
+                $rows = C4::Context->preference('numSearchResults') || 20;
+                $page = ( $query->param('page') ? $query->param('page') : 1 );
             }
-            my ( $items, $totitems ) = GetShelfContents( $shelfnumber, $shelflimit, $shelfoffset, $sortfield, $direction );
 
-            my $borrower = GetMember( borrowernumber => $loggedinuser );
+            my $order_by = $sortfield eq 'itemcallnumber' ? 'items.cn_sort' : $sortfield;
+            my $contents = $shelf->get_contents->search(
+                {},
+                {
+                    prefetch => [ { 'biblionumber' => { 'biblioitems' => 'items' } } ],
+                    page     => $page,
+                    rows     => $rows,
+                    order_by => { "-$direction" => $order_by },
+                }
+            );
 
-            for my $this_item (@$items) {
-                my $biblionumber = $this_item->{biblionumber};
-                my $record       = GetMarcBiblio($biblionumber);
+            my $xslfile = C4::Context->preference('XSLTListsDisplay');
+            my $lang   = $xslfile ? C4::Languages::getlanguage()  : undef;
+            my $sysxml = $xslfile ? C4::XSLT::get_xslt_sysprefs() : undef;
 
-                if ( C4::Context->preference("XSLTResultsDisplay") ) {
-                    $this_item->{XSLTBloc} = XSLTParse4Display( $biblionumber, $record, "XSLTResultsDisplay" );
+            my @items;
+            while ( my $content = $contents->next ) {
+                my $this_item;
+                my $biblionumber = $content->biblionumber;
+                my $record       = GetMarcBiblio({ biblionumber => $biblionumber });
+
+                if ( $xslfile ) {
+                    $this_item->{XSLTBloc} = XSLTParse4Display( $biblionumber, $record, "XSLTListsDisplay",
+                                                                1, undef, $sysxml, $xslfile, $lang);
                 }
 
                 my $marcflavour = C4::Context->preference("marcflavour");
-                $this_item->{'imageurl'}        = getitemtypeinfo( $this_item->{'itemtype'}, 'intranet' )->{'imageurl'};
-                $this_item->{'coins'}           = GetCOinSBiblio($record);
-                $this_item->{'subtitle'}        = GetRecordValue( 'subtitle', $record, GetFrameworkCode( $this_item->{'biblionumber'} ) );
+                my $itemtype = Koha::Biblioitems->search({ biblionumber => $content->biblionumber })->next->itemtype;
+                $itemtype = Koha::ItemTypes->find( $itemtype );
+                my $biblio = Koha::Biblios->find( $content->biblionumber );
+                $this_item->{title}             = $biblio->title;
+                $this_item->{subtitle}          = $biblio->subtitle;
+                $this_item->{medium}            = $biblio->medium;
+                $this_item->{part_number}       = $biblio->part_number;
+                $this_item->{part_name}         = $biblio->part_name;
+                $this_item->{author}            = $biblio->author;
+                $this_item->{dateadded}         = $content->dateadded;
+                $this_item->{imageurl}          = $itemtype ? C4::Koha::getitemtypeimagelocation( 'intranet', $itemtype->imageurl ) : q{};
+                $this_item->{description}       = $itemtype ? $itemtype->description : q{}; #FIXME Should this be translated_description ?
+                $this_item->{notforloan}        = $itemtype->notforloan if $itemtype;
+                $this_item->{'coins'}           = $biblio->get_coins;
                 $this_item->{'normalized_upc'}  = GetNormalizedUPC( $record, $marcflavour );
                 $this_item->{'normalized_ean'}  = GetNormalizedEAN( $record, $marcflavour );
                 $this_item->{'normalized_oclc'} = GetNormalizedOCLCNumber( $record, $marcflavour );
@@ -230,39 +295,54 @@ if ( $op eq 'view' ) {
                 }
 
                 # Getting items infos for location display
-                my @items_infos = &GetItemsLocationInfo( $this_item->{'biblionumber'} );
+                my @items_infos = &GetItemsLocationInfo( $biblionumber );
                 $this_item->{'ITEM_RESULTS'} = \@items_infos;
+                $this_item->{biblionumber} = $biblionumber;
+                push @items, $this_item;
             }
 
-            # Build drop-down list for 'Add To:' menu...
-            my ( $totalref, $pubshelves, $barshelves ) = C4::VirtualShelves::GetSomeShelfNames( $loggedinuser, 'COMBO', 1 );
+            my $some_private_shelves = Koha::Virtualshelves->get_some_shelves(
+                {
+                    borrowernumber => $loggedinuser,
+                    add_allowed    => 1,
+                    category       => 1,
+                }
+            );
+            my $some_public_shelves = Koha::Virtualshelves->get_some_shelves(
+                {
+                    borrowernumber => $loggedinuser,
+                    add_allowed    => 1,
+                    category       => 2,
+                }
+            );
+
             $template->param(
-                addbarshelves      => $totalref->{bartotal},
-                addbarshelvesloop  => $barshelves,
-                addpubshelves      => $totalref->{pubtotal},
-                addpubshelvesloop  => $pubshelves,
+                add_to_some_private_shelves => $some_private_shelves,
+                add_to_some_public_shelves  => $some_public_shelves,
                 can_manage_shelf   => $shelf->can_be_managed($loggedinuser),
                 can_remove_shelf   => $shelf->can_be_deleted($loggedinuser),
                 can_remove_biblios => $shelf->can_biblios_be_removed($loggedinuser),
                 can_add_biblios    => $shelf->can_biblios_be_added($loggedinuser),
                 sortfield          => $sortfield,
-                itemsloop          => $items,
+                itemsloop          => \@items,
                 sortfield          => $sortfield,
                 direction          => $direction,
             );
-            if ($shelflimit) {
+            if ( $page ) {
+                my $pager = $contents->pager;
                 $template->param(
                     pagination_bar => pagination_bar(
-                        q||, ( int( $totitems / $shelflimit ) ) + ( ( $totitems % $shelflimit ) > 0 ? 1 : 0 ),
-                        $itemoff, "itemoff", { op => 'view', shelfnumber => $shelf->shelfnumber, sortfield => $sortfield, direction => $direction, }
+                        q||, $pager->last_page - $pager->first_page + 1,
+                        $page, "page", { op => 'view', shelfnumber => $shelf->shelfnumber, sortfield => $sortfield, direction => $direction, }
                     ),
                 );
             }
         } 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' };
     }
 }
 
@@ -272,8 +352,8 @@ $template->param(
     shelf    => $shelf,
     messages => \@messages,
     category => $category,
-    print    => $query->param('print') || 0,
-    csv_profiles => GetCsvProfilesLoop('marc'),
+    print    => scalar $query->param('print') || 0,
+    csv_profiles => [ Koha::CsvProfiles->search({ type => 'marc', used_for => 'export_records' }) ],
 );
 
 output_html_with_http_headers $query, $cookie, $template->output;