[Koha-patches] [PATCH] bug_11213: C4::VirtualShelves::Page::shelf_contents()

Srdjan srdjan at catalyst.net.nz
Mon Mar 31 05:55:48 CEST 2014


* Extracted shelf items processing from shelfpage() into a separate sub
  shelf_contents() in order to be able to test it
* Added tests for shelf_contents() witx XSLT
---
 C4/VirtualShelves/Page.pm            | 105 +++++++++++++++++++++--------------
 t/db_dependent/VirtualShelves_Page.t |  35 +++++++++++-
 2 files changed, 98 insertions(+), 42 deletions(-)

diff --git a/C4/VirtualShelves/Page.pm b/C4/VirtualShelves/Page.pm
index 823248f..e211d80 100644
--- a/C4/VirtualShelves/Page.pm
+++ b/C4/VirtualShelves/Page.pm
@@ -73,7 +73,6 @@ sub shelfpage {
     my $itemoff     = ( $query->param('itemoff')  ? $query->param('itemoff')  : 1 );
     my $displaymode = ( $query->param('display')  ? $query->param('display')  : 'publicshelves' );
     my ( $shelflimit, $shelfoffset, $shelveslimit, $shelvesoffset );
-    my $marcflavour = C4::Context->preference("marcflavour");
 
     # get biblionumbers stored in the cart
     my @cart_list;
@@ -250,7 +249,6 @@ sub shelfpage {
             #check that the user can view the shelf
             if ( ShelfPossibleAction( $loggedinuser, $shelfnumber, 'view' ) ) {
                 my $items;
-                my $tag_quantity;
                 my $sortfield = ( $sorton ? $sorton : 'title' );
                 $sortfield = $query->param('sort') || $sortfield; ## Passed in sorting overrides default sorting
                 my $direction = $query->param('direction') || 'asc';
@@ -258,45 +256,15 @@ sub shelfpage {
                     sort      => $sortfield,
                     direction => $direction,
                 );
-                ( $items, $totitems ) = GetShelfContents( $shelfnumber, $shelflimit, $shelfoffset, $sortfield, $direction );
-                for my $this_item (@$items) {
-                    my $biblionumber = $this_item->{'biblionumber'};
-                    # Getting items infos for location display
-                    my @items_infos = &GetItemsInfo( $this_item->{'biblionumber'}, "cn_sort" );
-                    my $record = GetMarcBiblio($biblionumber);
-                    if (C4::Context->preference("OPACXSLTResultsDisplay") && $type eq 'opac') {
-                        $this_item->{XSLTBloc} = XSLTParse4Display($biblionumber, $record, "OPACXSLTResultsDisplay", \@items_infos);
-                    } elsif (C4::Context->preference("XSLTResultsDisplay") && $type eq 'intranet') {
-                        $this_item->{XSLTBloc} = XSLTParse4Display($biblionumber, $record, "XSLTResultsDisplay", \@items_infos);
-                    }
-
-                    # the virtualshelfcontents table does not store these columns nor are they retrieved from the items
-                    # and itemtypes tables, so I'm commenting them out for now to quiet the log -crn
-                    #$this_item->{imageurl} = $imgdir."/".$itemtypes->{ $this_item->{itemtype}  }->{'imageurl'};
-                    #$this_item->{'description'} = $itemtypes->{ $this_item->{itemtype} }->{'description'};
-                    $this_item->{'dateadded'} = format_date( $this_item->{'dateadded'} );
-                    $this_item->{'imageurl'}  = getitemtypeinfo( $this_item->{'itemtype'}, $type )->{'imageurl'};
-                    $this_item->{'coins'}     = GetCOinSBiblio( $record );
-                    $this_item->{'subtitle'} = GetRecordValue('subtitle', $record, GetFrameworkCode($this_item->{'biblionumber'}));
-                    $this_item->{'normalized_upc'}  = GetNormalizedUPC(       $record,$marcflavour);
-                    $this_item->{'normalized_ean'}  = GetNormalizedEAN(       $record,$marcflavour);
-                    $this_item->{'normalized_oclc'} = GetNormalizedOCLCNumber($record,$marcflavour);
-                    $this_item->{'normalized_isbn'} = GetNormalizedISBN(undef,$record,$marcflavour);
-                    if(!defined($this_item->{'size'})) { $this_item->{'size'} = "" }; #TT has problems with size
-                    $this_item->{'itemsissued'} = CountItemsIssued( $this_item->{'biblionumber'} );
-                    $this_item->{'ITEM_RESULTS'} = \@items_infos;
-                    if ( grep {$_ eq $biblionumber} @cart_list) {
-                        $this_item->{'incart'} = 1;
-                    }
-
-                    if (C4::Context->preference('TagsEnabled') and $tag_quantity = C4::Context->preference('TagsShowOnList')) {
-                        $this_item->{'TagLoop'} = get_tags({
-                            biblionumber=>$this_item->{'biblionumber'}, approved=>1, 'sort'=>'-weight',
-                            limit=>$tag_quantity
-                            });
-                    }
-
-                }
+                ( $items, $totitems ) = shelf_contents({
+                    shelfnumber => $shelfnumber,
+                    shelflimit => $shelflimit,
+                    shelfoffset => $shelfoffset,
+                    sortfield => $sortfield,
+                    direction => $direction,
+                    type => $type,
+                    cart_list => \@cart_list,
+                });
                 if($type eq 'intranet'){
                     # Build drop-down list for 'Add To:' menu...
                     my ($totalref, $pubshelves, $barshelves)=
@@ -487,6 +455,61 @@ sub shelfpage {
     output_html_with_http_headers $query, $cookie, $template->output;
 }
 
+sub shelf_contents {
+    my ( $params ) = @_;
+    my $shelfnumber = $params->{shelfnumber};
+    my $shelflimit = $params->{shelflimit};
+    my $shelfoffset = $params->{shelfoffset};
+    my $sortfield = $params->{sortfield};
+    my $direction = $params->{direction};
+    my $type = $params->{type};
+    my $cart_list = $params->{cart_list};
+
+    my $marcflavour = C4::Context->preference("marcflavour");
+    my $tag_quantity = C4::Context->preference('TagsEnabled')
+      ? C4::Context->preference('TagsShowOnList')
+      : undef;
+    my ( $items, $totitems ) = GetShelfContents( $shelfnumber, $shelflimit, $shelfoffset, $sortfield, $direction );
+    for my $this_item (@$items) {
+        my $biblionumber = $this_item->{'biblionumber'};
+        # Getting items infos for location display
+        my @items_infos = &GetItemsInfo( $this_item->{'biblionumber'}, "cn_sort" );
+        my $record = GetMarcBiblio($biblionumber);
+        if (C4::Context->preference("OPACXSLTResultsDisplay") && $type eq 'opac') {
+            $this_item->{XSLTBloc} = XSLTParse4Display($biblionumber, $record, "OPACXSLTResultsDisplay", \@items_infos);
+        } elsif (C4::Context->preference("XSLTResultsDisplay") && $type eq 'intranet') {
+            $this_item->{XSLTBloc} = XSLTParse4Display($biblionumber, $record, "XSLTResultsDisplay", \@items_infos);
+        }
+
+        # the virtualshelfcontents table does not store these columns nor are they retrieved from the items
+        # and itemtypes tables, so I'm commenting them out for now to quiet the log -crn
+        #$this_item->{imageurl} = $imgdir."/".$itemtypes->{ $this_item->{itemtype}  }->{'imageurl'};
+        #$this_item->{'description'} = $itemtypes->{ $this_item->{itemtype} }->{'description'};
+        $this_item->{'dateadded'} = format_date( $this_item->{'dateadded'} );
+        $this_item->{'imageurl'}  = getitemtypeinfo( $this_item->{'itemtype'}, $type )->{'imageurl'};
+        $this_item->{'coins'}     = GetCOinSBiblio( $record );
+        $this_item->{'subtitle'} = GetRecordValue('subtitle', $record, GetFrameworkCode($this_item->{'biblionumber'}));
+        $this_item->{'normalized_upc'}  = GetNormalizedUPC(       $record,$marcflavour);
+        $this_item->{'normalized_ean'}  = GetNormalizedEAN(       $record,$marcflavour);
+        $this_item->{'normalized_oclc'} = GetNormalizedOCLCNumber($record,$marcflavour);
+        $this_item->{'normalized_isbn'} = GetNormalizedISBN(undef,$record,$marcflavour);
+        if(!defined($this_item->{'size'})) { $this_item->{'size'} = "" }; #TT has problems with size
+        $this_item->{'itemsissued'} = CountItemsIssued( $this_item->{'biblionumber'} );
+        $this_item->{'ITEM_RESULTS'} = \@items_infos;
+        if ( grep {$_ eq $biblionumber} @$cart_list) {
+            $this_item->{'incart'} = 1;
+        }
+
+        if ($tag_quantity) {
+            $this_item->{'TagLoop'} = get_tags({
+                biblionumber=>$this_item->{'biblionumber'}, approved=>1, 'sort'=>'-weight',
+                limit=>$tag_quantity
+                });
+        }
+    }
+    return ( $items, $totitems );
+}
+
 1;
 __END__
 
diff --git a/t/db_dependent/VirtualShelves_Page.t b/t/db_dependent/VirtualShelves_Page.t
index 236d147..24c622d 100755
--- a/t/db_dependent/VirtualShelves_Page.t
+++ b/t/db_dependent/VirtualShelves_Page.t
@@ -6,9 +6,42 @@
 use strict;
 use warnings;
 
-use Test::More tests => 1;
+use Test::More tests => 3;
+
+use C4::Context;
 
 BEGIN {
         use_ok('C4::VirtualShelves::Page');
 }
 
+C4::Context->set_preference('XSLTResultsDisplay', 'default');
+C4::Context->set_preference('OPACXSLTResultsDisplay', 'default');
+
+my $dbh = C4::Context->dbh;
+my $query = qq{
+    SELECT vs.shelfnumber, vs.shelfname,vs.owner,
+    bo.surname,bo.firstname,vs.category,vs.sortfield,
+    count(vc.biblionumber) as count
+    FROM virtualshelves vs
+    JOIN borrowers bo ON vs.owner=bo.borrowernumber
+    JOIN virtualshelfcontents vc USING (shelfnumber)
+    GROUP BY vs.shelfnumber
+    LIMIT 1
+};
+my $shelf = $dbh->selectrow_hashref($query);
+
+my %params = (
+    shelfnumber => $shelf->{shelfnumber},
+    shelflimit => 1,
+    shelfoffset => 0,
+    sortfield => $shelf->{sortfield},
+    direction => 'asc',
+    type => 'opac',
+    cart_list => [],
+);
+my ( $items, $totitems ) = C4::VirtualShelves::Page::shelf_contents(\%params);
+ok( $items->[0]{XSLTBloc}, "opac items XSLT");
+
+$params{type} = 'intranet';
+( $items, $totitems ) = C4::VirtualShelves::Page::shelf_contents(\%params);
+ok( $items->[0]{XSLTBloc}, "intranet items XSLT");
-- 
1.8.3.2


More information about the Koha-patches mailing list