From 7c25251789220bdaa85c5c6d9520dfc7ad932743 Mon Sep 17 00:00:00 2001 From: David Cook Date: Mon, 25 Feb 2013 15:42:54 +1100 Subject: [PATCH] Bug 9703 - Add optional parameter "as_string" to GetRecordValue subroutine Currently, the GetRecordValue subroutine returns an array of hash refs, which must be looped through to retrieve results. On the template, you typically 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 propose the addition of an optional parameter to the sub to trigger a condition which returns a string of concatenated values rather than an array of hash refs. The patch also includes a utility sub that should in theory turn any array of hash refs into a string. --- 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 | 37 ++++++++++++++++++++++++++++++++++--- 1 files changed, 34 insertions(+), 3 deletions(-) diff --git a/C4/Biblio.pm b/C4/Biblio.pm index 2c7170c..e3ca32f 100644 --- a/C4/Biblio.pm +++ b/C4/Biblio.pm @@ -674,14 +674,14 @@ 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 = ?'); @@ -701,8 +701,39 @@ sub GetRecordValue { } } } + + if ($as_string) { + my $string = _array_of_hashes_to_string(\@result, ' '); + return $string; + } else { + return \@result; + } +} - return \@result; +=head2 _array_of_hashes_to_string + + my $string = _array_of_hashes_to_string($array, $glue); + + Transform an array ref of hash refs into a string + +=cut + +sub _array_of_hashes_to_string { + my ( $array, $glue ) = @_; + my $string = ''; + + if ($array) { + for my $element (@$array) { + if ($element) { + foreach my $key ( keys %$element ) { + $string .= $element->{$key} . $glue; + } + } + } + } + + $string =~ s/\s+$//; + return $string; } =head2 SetFieldMapping -- 1.7.7.4