From f3f116d6e0c357f594c1986a1e482cdd4187ce72 Mon Sep 17 00:00:00 2001 From: David Cook Date: Mon, 25 Feb 2013 12:51:19 +1100 Subject: [PATCH] Bug 9703 - Add optional parameter "as_string" to GetRecordValue subroutine Currently, the GetRecordValue subroutine returns an array of hashes, which must be looped through to retrieve results. On the template, you write the following code [% FOREACH subtitle %][% subfield %][% END %]. The problem with this is when you're trying to use the Keyword to MARC Mapping to over-write a field in the template that is already written as a single variable (e.g. [% author %]). Of course, you can loop through the array of hashes on the Perl side and pass a string to the template (i.e. opac-detail.tt or detail.tt). However, you're going to have to reproduce this iterator code every time you want to use GetRecordValue to produce a string. So, I figured that it made sense to add an optional parameter to the sub to trigger a condition so as to return a string of concatenated values rather than an array of hashes. --- Note: My ideal goal is to make it so that one can use the Keyword to MARC Mapping to specify any (or almost any) field on the detail pages. At the moment, I'm focusing on author, but I figure physical description will be another good one, as the template shows a bare minimum of the 3xx fields/subfields (in the incorrect order last I looked), --- Note, adding this optional parameter does NOT at all change the existing use of the GetRecordValue sub. The idea is hopefully to eliminate boilerplate by centralizing the "as string" code in this sub. ------ Test Plan: 1) Add multiple Keyword to MARC Mappings to your Koha instance using the "author" field name (e.g. Field name: "author", MARC field: "100", MARC subfield: "a", Field name: "author", MARC field: "100", MARC subfield: "c") 2) Make sure that you have a bibliographic record that has a 100$a and 100$c entry (try anything by "Somme theologique: les actes humaines" by Saint Thomas Aquinas), and that you have this catalogued with the same MARC Bibliographic Framework where you made those Keyword to MARC Mappings. 3) View that record in the OPAC (you'll probably just see "by Thomas," 3) Add the following code to your opac-detail.pl page after "my $dat" is called: "my $author = GetRecordValue('author', $record, GetFrameworkCode($biblionumber), 'as_string'); if ($author) { $dat->{author} = $author ; } 4) Refresh your OPAC page 5) Note that you should now have "by Thomas, Aquinas, Saint," showing --- If I were using GetRecordValue without the "as_string" parameter, I would be getting an array of hashes, which wouldn't show up in the template unless the template were re-written to follow the format: [% FOREACH author %][% subfield %][% END %]. However, if the template were re-written and no Keyword to MARC Mapping was supplied, the default author wouldn't show up either, because it's passed as a scalar variable and not an array. --- C4/Biblio.pm | 25 +++++++++++++++++++------ 1 files changed, 19 insertions(+), 6 deletions(-) diff --git a/C4/Biblio.pm b/C4/Biblio.pm index 2c7170c..5382da5 100644 --- a/C4/Biblio.pm +++ b/C4/Biblio.pm @@ -674,35 +674,48 @@ sub _check_valid_auth_link { =head2 GetRecordValue - my $values = GetRecordValue($field, $record, $frameworkcode); + my $values = GetRecordValue($field, $record, $frameworkcode, $as_string); Get MARC fields from a keyword defined in fieldmapping table. =cut sub GetRecordValue { - my ( $field, $record, $frameworkcode ) = @_; + my ( $field, $record, $frameworkcode, $as_string ) = @_; my $dbh = C4::Context->dbh; my $sth = $dbh->prepare('SELECT fieldcode, subfieldcode FROM fieldmapping WHERE frameworkcode = ? AND field = ?'); $sth->execute( $frameworkcode, $field ); my @result = (); + my $result_string = ''; while ( my $row = $sth->fetchrow_hashref ) { foreach my $field ( $record->field( $row->{fieldcode} ) ) { if ( ( $row->{subfieldcode} ne "" && $field->subfield( $row->{subfieldcode} ) ) ) { foreach my $subfield ( $field->subfield( $row->{subfieldcode} ) ) { - push @result, { 'subfield' => $subfield }; + if ($as_string) { + $result_string .= $subfield . ' '; + } else { + push @result, { 'subfield' => $subfield }; + } } } elsif ( $row->{subfieldcode} eq "" ) { - push @result, { 'subfield' => $field->as_string() }; + if ($as_string) { + $result_string = $field->as_string(); + } else { + push @result, { 'subfield' => $field->as_string() }; + } } } } - - return \@result; + if ($as_string) { + $result_string =~ s/\s+$//;; + return $result_string; + } else { + return \@result; + } } =head2 SetFieldMapping -- 1.7.7.4