From e9d4a4708622bd958f258c8ab1e6784ed144abd5 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Mon, 17 Aug 2015 18:46:42 +0100 Subject: [PATCH] Bug 14544: Get rid of GetShelfContent --- C4/VirtualShelves.pm | 60 ----------------------------------------- opac/opac-downloadshelf.pl | 12 ++++----- opac/opac-sendshelf.pl | 7 +++-- opac/opac-shelves.pl | 32 +++++++++++----------- virtualshelves/downloadshelf.pl | 12 ++++----- virtualshelves/sendshelf.pl | 7 +++-- virtualshelves/shelves.pl | 32 ++++++++++++---------- 7 files changed, 53 insertions(+), 109 deletions(-) diff --git a/C4/VirtualShelves.pm b/C4/VirtualShelves.pm index 3e8a686..e85f920 100644 --- a/C4/VirtualShelves.pm +++ b/C4/VirtualShelves.pm @@ -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. - -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); diff --git a/opac/opac-downloadshelf.pl b/opac/opac-downloadshelf.pl index f7e5571..320b161 100755 --- a/opac/opac-downloadshelf.pl +++ b/opac/opac-downloadshelf.pl @@ -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; diff --git a/opac/opac-sendshelf.pl b/opac/opac-sendshelf.pl index b0215cc..a1b6105 100755 --- a/opac/opac-sendshelf.pl +++ b/opac/opac-sendshelf.pl @@ -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); diff --git a/opac/opac-shelves.pl b/opac/opac-shelves.pl index 632177f..1414210 100755 --- a/opac/opac-shelves.pl +++ b/opac/opac-shelves.pl @@ -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,9 @@ if ( $op eq 'view' ) { } my $marcflavour = C4::Context->preference("marcflavour"); - $this_item->{'imageurl'} = getitemtypeinfo( $this_item->{'itemtype'}, 'intranet' )->{'imageurl'}; + $this_item->{'imageurl'} = getitemtypeinfo( $content->biblionumber->biblioitems->first->itemtype, 'intranet' )->{'imageurl'}; $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 +254,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'), }); } @@ -290,15 +291,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, } ), ); } diff --git a/virtualshelves/downloadshelf.pl b/virtualshelves/downloadshelf.pl index e87f804..3d1b10d 100755 --- a/virtualshelves/downloadshelf.pl +++ b/virtualshelves/downloadshelf.pl @@ -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 ) ) { - # CSV + 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(); diff --git a/virtualshelves/sendshelf.pl b/virtualshelves/sendshelf.pl index 7f1ddbf..0ef6e1e 100755 --- a/virtualshelves/sendshelf.pl +++ b/virtualshelves/sendshelf.pl @@ -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); diff --git a/virtualshelves/shelves.pl b/virtualshelves/shelves.pl index 8e7ecc0..b1a5985 100755 --- a/virtualshelves/shelves.pl +++ b/virtualshelves/shelves.pl @@ -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,9 @@ if ( $op eq 'view' ) { } my $marcflavour = C4::Context->preference("marcflavour"); - $this_item->{'imageurl'} = getitemtypeinfo( $this_item->{'itemtype'}, 'intranet' )->{'imageurl'}; + $this_item->{'imageurl'} = getitemtypeinfo( $content->biblionumber->biblioitems->first->itemtype, 'intranet' )->{'imageurl'}; $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 +232,9 @@ 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; + push @items, $this_item; } # Build drop-down list for 'Add To:' menu... @@ -246,15 +249,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, } ), ); } -- 2.1.0