Bug 14544: Get rid of GetShelfContent
authorJonathan Druart <jonathan.druart@bugs.koha-community.org>
Mon, 17 Aug 2015 17:46:42 +0000 (18:46 +0100)
committerTomas Cohen Arazi <tomascohen@theke.io>
Thu, 5 Nov 2015 12:58:01 +0000 (09:58 -0300)
Signed-off-by: Alex Arnaud <alex.arnaud@biblibre.com>

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>

C4/VirtualShelves.pm
opac/opac-addbybiblionumber.pl
opac/opac-downloadshelf.pl
opac/opac-sendshelf.pl
opac/opac-shelves.pl
virtualshelves/addbybiblionumber.pl
virtualshelves/downloadshelf.pl
virtualshelves/sendshelf.pl
virtualshelves/shelves.pl

index 3e8a686..e85f920 100644 (file)
@@ -40,7 +40,6 @@ BEGIN {
     require Exporter;
     @ISA    = qw(Exporter);
     @EXPORT = qw(
-            &GetShelfContents
     );
         @EXPORT_OK = qw(
             &ShelvesMax
@@ -103,65 +102,6 @@ sub GetSomeShelfNames {
     return ( { bartotal => $bar? scalar @$bar: 0, pubtotal => $pub? scalar @$pub: 0}, $pub, $bar);
 }
 
-=head2 GetShelfContents
-
-  $biblist = &GetShelfContents($shelfnumber);
-
-Looks up information about the contents of virtual virtualshelves number
-C<$shelfnumber>.  Sorted by a field in the biblio table.  copyrightdate 
-gives a desc sort.
-
-Returns a reference-to-array, whose elements are references-to-hash,
-as returned by C<C4::Biblio::GetBiblioFromItemNumber>.
-
-Note: the notforloan status comes from the itemtype, and where it equals 0
-it does not ensure that related items.notforloan status is likewise 0. The
-caller has to check any items on their own, possibly with CanBookBeIssued
-from C4::Circulation.
-
-=cut
-
-sub GetShelfContents {
-    my ($shelfnumber, $row_count, $offset, $sortfield, $sort_direction ) = @_;
-    my $dbh=C4::Context->dbh();
-    my $sth1 = $dbh->prepare("SELECT count(*) FROM virtualshelfcontents WHERE shelfnumber = ?");
-    $sth1->execute($shelfnumber);
-    my $total = $sth1->fetchrow;
-    if(!$sortfield) {
-        my $sth2 = $dbh->prepare('SELECT sortfield FROM virtualshelves WHERE shelfnumber=?');
-        $sth2->execute($shelfnumber);
-        ($sortfield) = $sth2->fetchrow_array;
-    }
-    my $query =
-       " SELECT DISTINCT vc.biblionumber, vc.shelfnumber, vc.dateadded, itemtypes.*,
-            biblio.*, biblioitems.itemtype, biblioitems.publicationyear as year, biblioitems.publishercode, biblioitems.place, biblioitems.size, biblioitems.pages
-         FROM   virtualshelfcontents vc
-         JOIN biblio      ON      vc.biblionumber =      biblio.biblionumber
-         LEFT JOIN biblioitems ON  biblio.biblionumber = biblioitems.biblionumber
-         LEFT JOIN items ON items.biblionumber=vc.biblionumber
-         LEFT JOIN itemtypes   ON biblioitems.itemtype = itemtypes.itemtype
-         WHERE  vc.shelfnumber=? ";
-    my @params = ($shelfnumber);
-    if($sortfield) {
-        $query .= " ORDER BY " . $dbh->quote_identifier( $sortfield );
-        $query .= " DESC " if ( $sort_direction eq 'desc' );
-    }
-    if($row_count){
-       $query .= " LIMIT ?, ? ";
-       push (@params, ($offset ? $offset : 0));
-       push (@params, $row_count);
-    }
-    my $sth3 = $dbh->prepare($query);
-    $sth3->execute(@params);
-    return ($sth3->fetchall_arrayref({}), $total);
-    # Like the perldoc says,
-    # returns reference-to-array, where each element is reference-to-hash of the row:
-    #   like [ $sth->fetchrow_hashref(), $sth->fetchrow_hashref() ... ]
-    # Suitable for use in TMPL_LOOP.
-    # See http://search.cpan.org/~timb/DBI-1.601/DBI.pm#fetchall_arrayref
-    # or newer, for your version of DBI.
-}
-
 =head2 ShelvesMax
 
     $howmany= ShelvesMax($context);
index b20702d..9ca2a3a 100755 (executable)
@@ -41,6 +41,11 @@ our $authorized          = 1;
 our $errcode           = 0;
 our @biblios;
 
+
+if (scalar(@biblionumber) == 1) {
+    @biblionumber = (split /\//,$biblionumber[0]);
+}
+
 our ( $template, $loggedinuser, $cookie ) = get_template_and_user(
     {
         template_name   => "opac-addbybiblionumber.tt",
@@ -72,20 +77,6 @@ else {
 }
 #end
 
-sub AddBibliosToShelf {
-    #splits incoming biblionumber(s) to array and adds each to shelf.
-    my ($shelfnumber,@biblionumber)=@_;
-
-    #multiple bibs might come in as '/' delimited string (from where, i don't see), or as array.
-    if (scalar(@biblionumber) == 1) {
-        @biblionumber = (split /\//,$biblionumber[0]);
-    }
-    for my $bib (@biblionumber) {
-        my $shelf = Koha::Virtualshelves->find( $shelfnumber );
-        $shelf->add_biblio( $bib, $loggedinuser );
-    }
-}
-
 sub HandleNewVirtualShelf {
     if ( $loggedinuser > 0 and
         (
@@ -100,14 +91,18 @@ sub HandleNewVirtualShelf {
                     category => $category,
                     owner => $loggedinuser,
                 }
-            );
+            )->store;
         };
         if ( $@ or not $shelf ) {
             $authorized = 0;
             $errcode = 1;
             return;
         }
-        AddBibliosToShelf($shelfnumber, @biblionumber);
+
+        for my $bib (@biblionumber) {
+            $shelf->add_biblio( $bib, $loggedinuser );
+        }
+
         #Reload the page where you came from
         print $query->header;
         print "<html><meta http-equiv=\"refresh\" content=\"0\" /><body onload=\"window.opener.location.reload(true);self.close();\"></body></html>";
@@ -118,7 +113,9 @@ sub HandleShelfNumber {
     my $shelfnumber = $query->param('shelfnumber');
     my $shelf = Koha::Virtualshelves->find( $shelfnumber );
     if ( $shelf->can_biblios_be_added( $loggedinuser ) ) {
-        AddBibliosToShelf($shelfnumber,@biblionumber);
+        for my $bib (@biblionumber) {
+            $shelf->add_biblio( $bib, $loggedinuser );
+        }
         #Close this page and return
         print $query->header;
         print "<html><meta http-equiv=\"refresh\" content=\"0\" /><body onload=\"self.close();\"></body></html>";
@@ -181,17 +178,13 @@ sub HandleSelect {
 }
 
 sub LoadBib {
-    #see comment in AddBibliosToShelf
-    if (scalar(@biblionumber) == 1) {
-        @biblionumber = (split /\//,$biblionumber[0]);
-    }
     for my $bib (@biblionumber) {
         my $data = GetBiblioData( $bib );
-    push(@biblios,
-        { biblionumber => $bib,
-          title        => $data->{'title'},
-          author       => $data->{'author'},
-    } );
+        push(@biblios,
+            { biblionumber => $bib,
+              title        => $data->{'title'},
+              author       => $data->{'author'},
+        } );
     }
     $template->param(
         multiple => (scalar(@biblios) > 1),
index f7e5571..320b161 100755 (executable)
@@ -56,7 +56,8 @@ if ( $shelf and $shelf->can_be_viewed( $borrowernumber ) ) {
 
     if ($shelfnumber && $format) {
 
-        my ($items, $totitems)  = GetShelfContents($shelfnumber);
+
+        my $contents = $shelf->get_contents;
         my $marcflavour         = C4::Context->preference('marcflavour');
         my $output;
         my $extension;
@@ -65,15 +66,14 @@ if ( $shelf and $shelf->can_be_viewed( $borrowernumber ) ) {
        # CSV
         if ($format =~ /^\d+$/) {
             my @biblios;
-            foreach (@$items) {
-                push @biblios, $_->{biblionumber};
+            while ( my $content = $contents->next ) {
+                push @biblios, $content->biblionumber->biblionumber;
             }
             $output = marc2csv(\@biblios, $format);
-                
         # Other formats
         } else {
-            foreach my $biblio (@$items) {
-                my $biblionumber = $biblio->{biblionumber};
+            while ( my $content = $contents->next ) {
+                my $biblionumber = $content->biblionumber->biblionumber;
 
                 my $record = GetMarcBiblio($biblionumber, 1);
                 next unless $record;
index b0215cc..a1b6105 100755 (executable)
@@ -75,14 +75,13 @@ if ( $email ) {
     );
 
     my $shelf = Koha::Virtualshelves->find( $shelfid );
-    my ($items, $totitems)  = GetShelfContents($shelfid);
+    my $contents = $shelf->get_contents;
     my $marcflavour         = C4::Context->preference('marcflavour');
     my $iso2709;
     my @results;
 
-    # retrieve biblios from shelf
-    foreach my $biblio (@$items) {
-        my $biblionumber = $biblio->{biblionumber};
+    while ( my $content = $contents->next ) {
+        my $biblionumber = $content->biblionumber->biblionumber;
         my $fw               = GetFrameworkCode($biblionumber);
         my $dat              = GetBiblioData($biblionumber);
         my $record           = GetMarcBiblio($biblionumber, 1);
index 632177f..c5f0a35 100755 (executable)
@@ -213,13 +213,12 @@ if ( $op eq 'view' ) {
             $category = $shelf->category;
             my $sortfield = $query->param('sortfield') || $shelf->sortfield;    # Passed in sorting overrides default sorting
             my $direction = $query->param('direction') || 'asc';
-            my ( $shelflimit, $shelfoffset, $itemoff );
+            my ( $page, $rows );
             unless ( $query->param('print') or $query->param('rss') ) {
-                $shelflimit = C4::Context->preference('OPACnumSearchResults') || 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('OPACnumSearchResults') || 20;
+                $page = ( $query->param('page') ? $query->param('page') : 1 );
             }
-            my ( $items, $totitems ) = GetShelfContents( $shelfnumber, $shelflimit, $shelfoffset, $sortfield, $direction );
+            my $contents = $shelf->get_contents->search({}, { join => [ 'biblionumber' ], page => $page, rows => $rows, order_by => "$sortfield $direction", });
 
             # get biblionumbers stored in the cart
             my @cart_list;
@@ -229,8 +228,10 @@ if ( $op eq 'view' ) {
 
             my $borrower = GetMember( borrowernumber => $loggedinuser );
 
-            for my $this_item (@$items) {
-                my $biblionumber = $this_item->{biblionumber};
+            my @items;
+            while ( my $content = $contents->next ) {
+                my $this_item;
+                my $biblionumber = $content->biblionumber->biblionumber;
                 my $record       = GetMarcBiblio($biblionumber);
 
                 if ( C4::Context->preference("OPACXSLTResultsDisplay") ) {
@@ -238,9 +239,12 @@ if ( $op eq 'view' ) {
                 }
 
                 my $marcflavour = C4::Context->preference("marcflavour");
-                $this_item->{'imageurl'}        = getitemtypeinfo( $this_item->{'itemtype'}, 'intranet' )->{'imageurl'};
+                my $itemtypeinfo = getitemtypeinfo( $content->biblionumber->biblioitems->first->itemtype, 'intranet' );
+                $this_item->{imageurl}          = $itemtypeinfo->{imageurl};
+                $this_item->{description}       = $itemtypeinfo->{description};
+                $this_item->{notforloan}        = $itemtypeinfo->{notforloan};
                 $this_item->{'coins'}           = GetCOinSBiblio($record);
-                $this_item->{'subtitle'}        = GetRecordValue( 'subtitle', $record, GetFrameworkCode( $this_item->{'biblionumber'} ) );
+                $this_item->{'subtitle'}        = GetRecordValue( 'subtitle', $record, GetFrameworkCode( $biblionumber ) );
                 $this_item->{'normalized_upc'}  = GetNormalizedUPC( $record, $marcflavour );
                 $this_item->{'normalized_ean'}  = GetNormalizedEAN( $record, $marcflavour );
                 $this_item->{'normalized_oclc'} = GetNormalizedOCLCNumber( $record, $marcflavour );
@@ -253,12 +257,12 @@ 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;
 
                 if (C4::Context->preference('TagsEnabled') and C4::Context->preference('TagsShowOnList')) {
                     $this_item->{TagLoop} = get_tags({
-                        biblionumber=>$this_item->{'biblionumber'}, approved=>1, 'sort'=>'-weight',
+                        biblionumber => $biblionumber, approved=>1, 'sort'=>'-weight',
                         limit => C4::Context->preference('TagsShowOnList'),
                     });
                 }
@@ -273,8 +277,9 @@ if ( $op eq 'view' ) {
                 if ( $query->param('rss') ) {
                     $this_item->{title} = $content->biblionumber->title;
                     $this_item->{author} = $content->biblionumber->author;
-                    $this_item->{biblionumber} = $biblionumber;
                 }
+
+                $this_item->{biblionumber} = $biblionumber;
                 push @items, $this_item;
             }
 
@@ -290,15 +295,16 @@ if ( $op eq 'view' ) {
                 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, }
                     ),
                 );
             }
index e6a1630..8690000 100755 (executable)
@@ -124,13 +124,6 @@ sub HandleBiblioPars {
     return @bib;
 }
 
-sub AddBibliosToShelf {
-    my ($shelfnumber, @biblionumber)=@_;
-    for my $bib (@biblionumber){
-        Koha::Virtualshelves->find( $shelfnumber )->add_biblio( $bib, $loggedinuser );
-    }
-}
-
 sub HandleNewVirtualShelf {
     my $shelf = eval {
         Koha::Virtualshelf->new(
@@ -141,14 +134,16 @@ sub HandleNewVirtualShelf {
                 owner => $loggedinuser,
             }
         );
-    };
+    }->store;
     if ( $@ or not $shelf ) {
         $authorized = 0;
         $errcode    = 1;
         return;
     }
 
-    AddBibliosToShelf($shelfnumber, @biblionumber);
+    for my $bib (@biblionumber){
+        $shelf->add_biblio( $bib, $loggedinuser );
+    }
     #Reload the page where you came from
     print $query->header;
     print "<html><meta http-equiv=\"refresh\" content=\"0\" /><body onload=\"window.opener.location.reload(true);self.close();\"></body></html>";
@@ -157,13 +152,15 @@ sub HandleNewVirtualShelf {
 sub HandleShelfNumber {
     my $shelf = Koha::Virtualshelves->find( $shelfnumber );
     if($authorized = $shelf->can_biblios_be_added( $loggedinuser ) ) {
-    AddBibliosToShelf($shelfnumber, @biblionumber);
-    #Close this page and return
-    print $query->header;
-    print "<html><meta http-equiv=\"refresh\" content=\"0\" /><body onload=\"self.close();\"></body></html>";
+        for my $bib (@biblionumber){
+            $shelf->add_biblio( $bib, $loggedinuser );
+        }
+        #Close this page and return
+        print $query->header;
+        print "<html><meta http-equiv=\"refresh\" content=\"0\" /><body onload=\"self.close();\"></body></html>";
     }
     else {
-    $errcode=2; #no perm
+        $errcode=2; #no perm
     }
 }
 
index 156e261..3d1b10d 100755 (executable)
@@ -54,7 +54,6 @@ my @messages;
 
 if ($shelfid && $format) {
 
-    my ($items, $totitems)  = GetShelfContents($shelfid);
     my $marcflavour         = C4::Context->preference('marcflavour');
     my $output='';
 
@@ -62,17 +61,18 @@ if ($shelfid && $format) {
     if ( $shelf ) {
         if ( $shelf->can_be_viewed( $loggedinuser ) ) {
 
+            my $contents = $shelf->get_contents;
             # CSV
             if ($format =~ /^\d+$/) {
                 my @biblios;
-                foreach (@$items) {
-                    push @biblios, $_->{biblionumber};
+                while ( my $content = $contents->next ) {
+                    push @biblios, $content->biblionumber->biblionumber;
                 }
                 $output = marc2csv(\@biblios, $format);
             }
             else { #Other formats
-                foreach my $biblio (@$items) {
-                    my $biblionumber = $biblio->{biblionumber};
+                while ( my $content = $contents->next ) {
+                    my $biblionumber = $content->biblionumber->biblionumber;
                     my $record = GetMarcBiblio($biblionumber, 1);
                     if ($format eq 'iso2709') {
                         $output .= $record->as_usmarc();
index 7f1ddbf..0ef6e1e 100755 (executable)
@@ -72,14 +72,13 @@ if ($email) {
     );
 
     my $shelf = Koha::Virtualshelves->find( $shelfid );
-    my ( $items, $totitems ) = GetShelfContents($shelf->shelfnumber);
+    my $contents = $shelf->get_contents;
     my $marcflavour = C4::Context->preference('marcflavour');
     my $iso2709;
     my @results;
 
-    # retrieve biblios from shelf
-    foreach my $biblio (@$items) {
-        my $biblionumber     = $biblio->{biblionumber};
+    while ( my $content = $contents->next ) {
+        my $biblionumber     = $content->biblionumber->biblionumber;
         my $fw               = GetFrameworkCode($biblionumber);
         my $dat              = GetBiblioData($biblionumber);
         my $record           = GetMarcBiblio($biblionumber, 1);
index 8e7ecc0..53f9c24 100755 (executable)
@@ -196,18 +196,20 @@ if ( $op eq 'view' ) {
         if ( $shelf->can_be_viewed( $loggedinuser ) ) {
             my $sortfield = $query->param('sortfield') || $shelf->sortfield;    # Passed in sorting overrides default sorting
             my $direction = $query->param('direction') || 'asc';
-            my ( $shelflimit, $shelfoffset, $itemoff );
+            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 $contents = $shelf->get_contents->search({}, { join => [ 'biblionumber' ], page => $page, rows => $rows, order_by => "$sortfield $direction", });
 
             my $borrower = GetMember( borrowernumber => $loggedinuser );
 
-            for my $this_item (@$items) {
-                my $biblionumber = $this_item->{biblionumber};
+            my @items;
+            while ( my $content = $contents->next ) {
+                my $this_item;
+                my $biblionumber = $content->biblionumber->biblionumber;
                 my $record       = GetMarcBiblio($biblionumber);
 
                 if ( C4::Context->preference("XSLTResultsDisplay") ) {
@@ -215,9 +217,12 @@ if ( $op eq 'view' ) {
                 }
 
                 my $marcflavour = C4::Context->preference("marcflavour");
-                $this_item->{'imageurl'}        = getitemtypeinfo( $this_item->{'itemtype'}, 'intranet' )->{'imageurl'};
+                my $itemtypeinfo = getitemtypeinfo( $content->biblionumber->biblioitems->first->itemtype, 'intranet' );
+                $this_item->{imageurl}          = $itemtypeinfo->{imageurl};
+                $this_item->{description}       = $itemtypeinfo->{description};
+                $this_item->{notforloan}        = $itemtypeinfo->{notforloan};
                 $this_item->{'coins'}           = GetCOinSBiblio($record);
-                $this_item->{'subtitle'}        = GetRecordValue( 'subtitle', $record, GetFrameworkCode( $this_item->{'biblionumber'} ) );
+                $this_item->{'subtitle'}        = GetRecordValue( 'subtitle', $record, GetFrameworkCode( $biblionumber ) );
                 $this_item->{'normalized_upc'}  = GetNormalizedUPC( $record, $marcflavour );
                 $this_item->{'normalized_ean'}  = GetNormalizedEAN( $record, $marcflavour );
                 $this_item->{'normalized_oclc'} = GetNormalizedOCLCNumber( $record, $marcflavour );
@@ -230,8 +235,10 @@ 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...
@@ -246,15 +253,16 @@ if ( $op eq 'view' ) {
                 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, }
                     ),
                 );
             }