From 24ac194e99ab5dd92326b9fefe4f2d24fd9d978b Mon Sep 17 00:00:00 2001 From: Kyle M Hall Date: Tue, 3 Jun 2014 09:40:41 -0400 Subject: [PATCH] Bug 12357 [3]- Add ability to export arbitrary fields for BibTex format Some libraries would like to be able to add arbitrary fields to both the RIS and BibTeX citation formats that a record can be saved as from the staff intranet and public catalog. In addition, they would like to be able to override the default record type and use Koha's itemtype as the record type for those formats as well. Test Plan: 1) Apply this patch 2) Run updatedatabase.pl 3) Add the following to the new syspref BibtexExportAdditionalFields: lccn: 010$a notes: [501$a, 505$g] 4) Find or create a record with an 010$a (lccn) field, a 501$a field and multiple 505$g fields. 5) Locate the record in the catalog, choose "Save" and select BIBTEX 6) Inspect the downloaded file, note the lccn and multiple note fields --- C4/Record.pm | 33 +++++++++++++++++++- installer/data/mysql/sysprefs.sql | 1 + installer/data/mysql/updatedatabase.pl | 5 +++ .../en/modules/admin/preferences/cataloguing.pref | 9 +++++ 4 files changed, 47 insertions(+), 1 deletions(-) diff --git a/C4/Record.pm b/C4/Record.pm index e291486..ff88f93 100644 --- a/C4/Record.pm +++ b/C4/Record.pm @@ -709,7 +709,38 @@ sub marc2bibtex { $tex .= "\@book{"; $tex .= join(",\n", $id, map { $bh{$_} ? qq(\t$_ = {$bh{$_}}) : () } keys %bh); - $tex .= "\n}\n"; + $tex .= "\n"; + + my $syspref = C4::Context->preference('BibtexExportAdditionalFields'); + if ($syspref) { + $syspref = "$syspref\n\n"; + my $yaml = eval { YAML::Load($syspref); }; + if ($@) { + warn "Unable to parse BibtexExportAdditionalFields : $@"; + } + else { + my $r; + foreach my $bibtex_tag ( keys %$yaml ) { + my @fields = + ref( $yaml->{$bibtex_tag} ) eq 'ARRAY' + ? @{ $yaml->{$bibtex_tag} } + : $yaml->{$bibtex_tag}; + for my $tag_subfield (@fields) { + my ( $f, $sf ) = split /\$/, $tag_subfield; + if ( $f && $sf ) { + foreach my $field ( $record->field($f) ) { + my @values = $field->subfield($sf); + foreach my $v (@values) { + $tex .= qq(\t$bibtex_tag = {$v}\n); + } + } + } + } + } + } + } + + $tex .= "}\n"; return $tex; } diff --git a/installer/data/mysql/sysprefs.sql b/installer/data/mysql/sysprefs.sql index 04ddbd0..d21740f 100644 --- a/installer/data/mysql/sysprefs.sql +++ b/installer/data/mysql/sysprefs.sql @@ -64,6 +64,7 @@ INSERT INTO systempreferences ( `variable`, `value`, `options`, `explanation`, ` ('BasketConfirmations','1','always ask for confirmation.|do not ask for confirmation.','When closing or reopening a basket,','Choice'), ('BiblioAddsAuthorities','0',NULL,'If ON, adding a new biblio will check for an existing authority record and create one on the fly if one doesn\'t exist','YesNo'), ('BiblioDefaultView','normal','normal|marc|isbd','Choose the default detail view in the catalog; choose between normal, marc or isbd','Choice'), +('BibtexExportAdditionalFields', '', NULL , 'Define additional BibTex tags to export from MARC records in YAML format as an associative array with either a marc tag/subfield combination as the value, or a list of tag/subfield combinations.', 'textarea'), ('BlockExpiredPatronOpacActions','1',NULL,'Set whether an expired patron can perform opac actions such as placing holds or renew books, can be overridden on a per patron-type basis','YesNo'), ('BlockReturnOfWithdrawnItems','1','0','If enabled, items that are marked as withdrawn cannot be returned.','YesNo'), ('BorrowerMandatoryField','surname|cardnumber',NULL,'Choose the mandatory fields for a patron\'s account','free'), diff --git a/installer/data/mysql/updatedatabase.pl b/installer/data/mysql/updatedatabase.pl index 90f852b..e43c759 100755 --- a/installer/data/mysql/updatedatabase.pl +++ b/installer/data/mysql/updatedatabase.pl @@ -8572,6 +8572,11 @@ if ( CheckVersion($DBversion) ) { ('RisUseItemtypeAsType', '0', NULL , 'Use biblio.itemtype for RIS export type if set', 'YesNo') }); + $dbh->do(q{ + INSERT INTO systempreferences (variable,value,options,explanation,type) + VALUES ('BibtexExportAdditionalFields', '', NULL , 'Define additional BibTex tags to export from MARC records in YAML format as an associative array with either a marc tag/subfield combination as the value, or a list of tag/subfield combinations.', 'textarea') + }); + print "Upgrade to $DBversion done (Bug XXX - Add ability to export arbitrary MARC fields for RIS and BibTex)\n"; SetVersion($DBversion); } diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/cataloguing.pref b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/cataloguing.pref index a946fd8..fc8d27e 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/cataloguing.pref +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/cataloguing.pref @@ -201,6 +201,15 @@ Cataloging: - attempt to match aggressively by trying all variations of the ISBNs in the imported record as a phrase in the ISBN fields of already cataloged records. Note that this preference has no effect if UseQueryParser is on. Exporting: - + - Include following fields when exporting BibTeX, + - pref: BibtexExportAdditionalFields + type: textarea + - "Use one line per tag in the format BT_TAG: TAG$SUBFIELD ( e.g. lccn: 010$a )" + - "
" + - "To specificy multiple marc tags/subfields as targets for a repeating BibTex tag, use the following format: BT_TAG: [TAG2$SUBFIELD1, TAG2$SUBFIELD2] ( e.g. notes: [501$a, 505$g] )" + - "
" + - "All values of repeating tags and subfields will be printed with the given BibTeX tag." + - - Include following fields when exporting RIS, - pref: RisExportAdditionalFields type: textarea -- 1.7.2.5