From 56dd4874e57eb086136f8536cba4f74eda08e025 Mon Sep 17 00:00:00 2001 From: David Bourgault Date: Tue, 26 Sep 2017 10:17:06 -0400 Subject: [PATCH] Bug 17385: Add custom export XSLT Rebase --- C4/XSLT.pm | 34 +++++++++- basket/basket.pl | 4 ++ basket/downloadcart.pl | 6 ++ catalogue/detail.pl | 3 + catalogue/export.pl | 16 +++++ .../prog/en/includes/cat-toolbar.inc | 3 + .../en/includes/virtualshelves-toolbar.inc | 3 + .../en/modules/admin/preferences/opac.pref | 1 + .../prog/en/modules/basket/basket.tt | 3 + .../prog/en/modules/catalogue/detail.tt | 7 ++- .../modules/virtualshelves/downloadshelf.tt | 3 + .../biblioexport/MARC21_simple_export.xsl | 62 +++++++++++++++++++ .../en/includes/opac-detail-sidebar.inc | 50 ++++++++------- .../bootstrap/en/modules/opac-downloadcart.tt | 3 + .../en/modules/opac-downloadshelf.tt | 3 + .../biblioexport/MARC21_simple_export.xsl | 62 +++++++++++++++++++ opac/opac-detail.pl | 3 + opac/opac-downloadcart.pl | 8 +++ opac/opac-downloadshelf.pl | 10 ++- opac/opac-export.pl | 11 ++++ t/XSLT.t | 3 +- t/db_dependent/XSLT.t | 23 ++++++- virtualshelves/downloadshelf.pl | 7 ++- virtualshelves/shelves.pl | 1 + 24 files changed, 297 insertions(+), 32 deletions(-) create mode 100644 koha-tmpl/intranet-tmpl/prog/en/xslt/biblioexport/MARC21_simple_export.xsl create mode 100644 koha-tmpl/opac-tmpl/bootstrap/en/xslt/biblioexport/MARC21_simple_export.xsl diff --git a/C4/XSLT.pm b/C4/XSLT.pm index ca25d0db30..0379c6a3fa 100644 --- a/C4/XSLT.pm +++ b/C4/XSLT.pm @@ -32,6 +32,14 @@ use Koha::XSLT::Base; use Koha::Libraries; use Koha::Recalls; + +use XML::LibXML; + +use Encode; + +use vars qw(@ISA @EXPORT); + + my $engine; #XSLT Handler object my %authval_per_framework; # Cache for tagfield-tagsubfield to decode per framework. @@ -46,6 +54,7 @@ BEGIN { getAuthorisedValues4MARCSubfields buildKohaItemsNamespace XSLTParse4Display + &CustomXSLTExportList ); $engine=Koha::XSLT::Base->new( { do_not_return_source => 1 } ); } @@ -157,6 +166,12 @@ sub _get_best_default_xslt_filename { return $xslfilename; } +=head2 get_xslt_sysprefs + + returns XML xslt sysprefs. + +=cut + sub get_xslt_sysprefs { my $sysxml = "\n"; foreach my $syspref ( qw/ hidelostitems OPACURLOpenInNewWindow @@ -194,7 +209,21 @@ sub get_xsl_filename { my $xslfilename = C4::Context->preference($xslsyspref) || "default"; - if ( $xslfilename =~ /^\s*"?default"?\s*$/i ) { + if ($xslsyspref eq "XSLTCustomExport") { + my $dir; + $dir = C4::Context->config('intrahtdocs') . + '/' . C4::Context->preference("template") . + '/' . C4::Languages::getlanguage() . + '/xslt/biblioexport'; + $xslfilename = $dir . "/" . $xslfilename; + } elsif ($xslsyspref eq "OPACXSLTCustomExport") { + my $dir; + $dir = C4::Context->config('opachtdocs') . + '/' . C4::Context->preference("opacthemes") . + '/' . C4::Languages::getlanguage() . + '/xslt/biblioexport'; + $xslfilename = $dir . "/" . $xslfilename; + } elsif ( $xslfilename =~ /^\s*"?default"?\s*$/i ) { my ( $htdocs, $theme, $xslfile ); @@ -267,8 +296,9 @@ sub XSLTParse4Display { } my $xmlrecord = $record->as_xml(C4::Context->preference('marcflavour')); - $variables ||= {}; my $biblio; + my $variables ||= {}; + if (C4::Context->preference('OPACShowOpenURL')) { my @biblio_itemtypes; $biblio //= Koha::Biblios->find($biblionumber); diff --git a/basket/basket.pl b/basket/basket.pl index f6943d9fb9..be9ad23e9d 100755 --- a/basket/basket.pl +++ b/basket/basket.pl @@ -19,6 +19,7 @@ use Modern::Perl; use CGI qw ( -utf8 ); use C4::Koha; + use C4::Biblio qw( GetMarcBiblio GetMarcSeries @@ -28,6 +29,8 @@ use C4::Biblio qw( use C4::Items qw( GetItemsInfo ); use C4::Auth qw( get_template_and_user ); use C4::Output qw( output_html_with_http_headers ); +use C4::XSLT; + use Koha::AuthorisedValues; use Koha::Biblios; @@ -127,6 +130,7 @@ my $resultsarray = \@results; $template->param( BIBLIO_RESULTS => $resultsarray, csv_profiles => Koha::CsvProfiles->search({ type => 'marc', used_for => 'export_records' }), + xsl_exports => CustomXSLTExportList(), bib_list => $bib_list, ); diff --git a/basket/downloadcart.pl b/basket/downloadcart.pl index f612099c6a..b513f7a903 100755 --- a/basket/downloadcart.pl +++ b/basket/downloadcart.pl @@ -27,6 +27,7 @@ use C4::Biblio qw( GetMarcBiblio ); use C4::Output qw( output_html_with_http_headers ); use C4::Record; use C4::Ris qw( marc2ris ); +use C4::XSLT; use Koha::CsvProfiles; @@ -79,11 +80,15 @@ if ($bib_list && $format) { elsif ($format eq 'bibtex') { $output .= marc2bibtex($record, $biblio); } + elsif ($format =~ /^xsl\.(.*)$/) { + $output .= XSLTParse4Display($biblio, $record, 'XSLTCustomExport', 1, undef, , '', $1, ''); + } } } # If it was a CSV export we change the format after the export so the file extension is fine $format = "csv" if ($format =~ m/^\d+$/); + $format = "html" if ($format =~ /^xsl\./); print $query->header( -type => 'application/octet-stream', @@ -93,6 +98,7 @@ if ($bib_list && $format) { } else { $template->param(csv_profiles => Koha::CsvProfiles->search({ type => 'marc', used_for => 'export_records' })); + $template->param(xsl_exports => CustomXSLTExportList()); $template->param(bib_list => $bib_list); output_html_with_http_headers $query, $cookie, $template->output; } diff --git a/catalogue/detail.pl b/catalogue/detail.pl index 2a635e10ef..22278c2009 100755 --- a/catalogue/detail.pl +++ b/catalogue/detail.pl @@ -82,6 +82,9 @@ if ( C4::Context->config('enable_plugins') ) { ); } +##### ADD CUSTOM XSLT +$template->param(filesOptions => CustomXSLTExportList(0)); + my $biblionumber = $query->param('biblionumber'); $biblionumber = HTML::Entities::encode($biblionumber); my $record = GetMarcBiblio({ biblionumber => $biblionumber }); diff --git a/catalogue/export.pl b/catalogue/export.pl index 3785116581..3393d2fc0d 100755 --- a/catalogue/export.pl +++ b/catalogue/export.pl @@ -7,6 +7,8 @@ use C4::Output; use C4::Biblio qw( GetMarcBiblio GetMarcControlnumber ); use CGI qw ( -utf8 ); use C4::Ris qw( marc2ris ); +use C4::XSLT; + @@ -87,4 +89,18 @@ if ($op eq "export") { -attachment=>"$file_pre$file_id.$format"); print $marc; } +}elsif ($op eq "exportxsl"){ + my $biblionumber = $query->param("bib"); + if ($biblionumber){ + my $xslFile = $query->param("file"); + my $marc = GetMarcBiblio( { biblionumber => $biblionumber, embed_items => 1 } ); + my $format=$query->param("format")||'txt'; + my @hiddenitems; + + $marc = XSLTParse4Display($biblionumber, $marc, 'XSLTCustomExport', 1, \@hiddenitems, '', $xslFile,''); + print $query->header( + -type => 'application/octet-stream', + -attachment=>"bib-$biblionumber.$format"); + print $marc; + } } diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/cat-toolbar.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/cat-toolbar.inc index 1f352ab3ae..fb6266ef39 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/includes/cat-toolbar.inc +++ b/koha-tmpl/intranet-tmpl/prog/en/includes/cat-toolbar.inc @@ -102,6 +102,9 @@
  • MARC (Unicode/UTF-8, Standard)
  • MODS (XML)
  • RIS
  • + [% FOREACH filesOption IN filesOptions %] +
  • XSL - [% filesOption.filename | html %]
  • + [% END %] diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/virtualshelves-toolbar.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/virtualshelves-toolbar.inc index c5a6306a78..c241f343f3 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/includes/virtualshelves-toolbar.inc +++ b/koha-tmpl/intranet-tmpl/prog/en/includes/virtualshelves-toolbar.inc @@ -31,6 +31,9 @@ [% FOREACH csv_profile IN csv_profiles %]
  • CSV - [% csv_profile.profile | html %]
  • [% END %] + [% FOREACH xsl_export IN xsl_exports %] +
  • XSL - [% xsl_export.filename | html %]
  • + [% END %] diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/opac.pref b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/opac.pref index 82ad87630f..b2a0cc3f0d 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/opac.pref +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/opac.pref @@ -280,6 +280,7 @@ OPAC: mods: MODS ris: RIS isbd: ISBD + custom: XSL - Simple Export - - pref: OpacSeparateHoldings choices: diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/basket/basket.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/basket/basket.tt index 5c62dad4f1..dbe5b20697 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/basket/basket.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/basket/basket.tt @@ -56,6 +56,9 @@ [% FOREACH csv_profile IN csv_profiles %]
  • CSV - [% csv_profile.profile | html %]
  • [% END %] + [% FOREACH xsl_export IN xsl_exports %] +
  • XSL - [% xsl_export.filename | html %]
  • + [% END %] Print diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/detail.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/detail.tt index 5eca90dcf2..1d0787c8da 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/detail.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/detail.tt @@ -1000,8 +1000,11 @@ Note that permanent location is a code, and location may be an authval. - - + + [% FOREACH filesOption IN filesOptions %] + + [% END %] + diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/virtualshelves/downloadshelf.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/virtualshelves/downloadshelf.tt index 41deaaf85e..73a2733039 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/virtualshelves/downloadshelf.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/virtualshelves/downloadshelf.tt @@ -36,6 +36,9 @@ [% FOREACH csv_profile IN csv_profiles %] [% END %] + [% FOREACH xsl_export IN xsl_exports %] + + [% END %] diff --git a/koha-tmpl/intranet-tmpl/prog/en/xslt/biblioexport/MARC21_simple_export.xsl b/koha-tmpl/intranet-tmpl/prog/en/xslt/biblioexport/MARC21_simple_export.xsl new file mode 100644 index 0000000000..faad19d521 --- /dev/null +++ b/koha-tmpl/intranet-tmpl/prog/en/xslt/biblioexport/MARC21_simple_export.xsl @@ -0,0 +1,62 @@ + + + + + + + + + +

    + + + + + + + + + + + + + + +
    + + + + + + + + + + + + + +
    + +
    + + + + + + + + + + + + + +
    +

    +
    + +
    diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/includes/opac-detail-sidebar.inc b/koha-tmpl/opac-tmpl/bootstrap/en/includes/opac-detail-sidebar.inc index a76406b254..5d55583297 100644 --- a/koha-tmpl/opac-tmpl/bootstrap/en/includes/opac-detail-sidebar.inc +++ b/koha-tmpl/opac-tmpl/bootstrap/en/includes/opac-detail-sidebar.inc @@ -62,30 +62,36 @@
  • + + +
  • [% END %] diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-downloadcart.tt b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-downloadcart.tt index 090936c72a..ab9d4e7de1 100644 --- a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-downloadcart.tt +++ b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-downloadcart.tt @@ -20,6 +20,9 @@ [% FOREACH csv_profile IN csv_profiles %] [% END %] + [% FOREACH xsl_export IN xsl_exports %] + + [% END %]
    diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-downloadshelf.tt b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-downloadshelf.tt index 076fed519f..fbe38a25c7 100644 --- a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-downloadshelf.tt +++ b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-downloadshelf.tt @@ -70,6 +70,9 @@ [% FOREACH csv_profile IN csv_profiles %] [% END %] + [% FOREACH xsl_export IN xsl_exports %] + + [% END %] Required
    diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/xslt/biblioexport/MARC21_simple_export.xsl b/koha-tmpl/opac-tmpl/bootstrap/en/xslt/biblioexport/MARC21_simple_export.xsl new file mode 100644 index 0000000000..faad19d521 --- /dev/null +++ b/koha-tmpl/opac-tmpl/bootstrap/en/xslt/biblioexport/MARC21_simple_export.xsl @@ -0,0 +1,62 @@ + + + + + + + + + +

    + + + + + + + + + + + + + + +
    + + + + + + + + + + + + + +
    + +
    + + + + + + + + + + + + + +
    +

    +
    + +
    diff --git a/opac/opac-detail.pl b/opac/opac-detail.pl index 00c52eb5aa..3b95a6e522 100755 --- a/opac/opac-detail.pl +++ b/opac/opac-detail.pl @@ -104,6 +104,9 @@ my ( $template, $borrowernumber, $cookie ) = get_template_and_user( } ); +#### ADD CUSTOM XSLT +$template->param(filesOptions => CustomXSLTExportList(1)); + my @all_items = GetItemsInfo($biblionumber); if( $specific_item ) { @all_items = grep { $_->{itemnumber} == $query->param('itemnumber') } @all_items; diff --git a/opac/opac-downloadcart.pl b/opac/opac-downloadcart.pl index f3a1769c0b..605435e0ef 100755 --- a/opac/opac-downloadcart.pl +++ b/opac/opac-downloadcart.pl @@ -27,6 +27,8 @@ use C4::Biblio qw( GetFrameworkCode GetISBDView GetMarcBiblio ); use C4::Output qw( output_html_with_http_headers ); use C4::Record; use C4::Ris qw( marc2ris ); +use C4::XSLT; + use Koha::CsvProfiles; use Koha::RecordProcessor; @@ -115,11 +117,15 @@ if ($bib_list && $format) { $extension = "txt"; $type = "text/plain"; } + elsif ($format =~ /^xsl\.(.*)$/) { + $output .= XSLTParse4Display($biblio, $record, 'OPACXSLTCustomExport', 1, undef, undef, $1, ''); + } } } # If it was a CSV export we change the format after the export so the file extension is fine $format = "csv" if ($format =~ m/^\d+$/); + $format = "html" if ($format =~ /^xsl\./); print $query->header( -type => ($type) ? $type : 'application/octet-stream', @@ -139,5 +145,7 @@ if ($bib_list && $format) { ), bib_list => $bib_list, ); + $template->param(bib_list => $bib_list); + $template->param(xsl_exports => CustomXSLTExportList()); output_html_with_http_headers $query, $cookie, $template->output; } diff --git a/opac/opac-downloadshelf.pl b/opac/opac-downloadshelf.pl index 5647f873b2..89f2c2dd18 100755 --- a/opac/opac-downloadshelf.pl +++ b/opac/opac-downloadshelf.pl @@ -26,6 +26,8 @@ use C4::Biblio qw( GetFrameworkCode GetISBDView GetMarcBiblio ); use C4::Output qw( output_html_with_http_headers ); use C4::Record; use C4::Ris qw( marc2ris ); +use C4::XSLT; + use Koha::CsvProfiles; use Koha::RecordProcessor; use Koha::Virtualshelves; @@ -123,13 +125,15 @@ if ( $shelf and $shelf->can_be_viewed( $borrowernumber ) ) { }); $extension = "txt"; $type = "text/plain"; - } + }elsif ($format =~ /^xsl\.(.*)$/){ + $output .= XSLTParse4Display($biblionumber, $record, 'XSLTCustomExport', 1, undef, undef, $1, '', 1); + } } } # If it was a CSV export we change the format after the export so the file extension is fine $format = "csv" if ($format =~ m/^\d+$/); - + $format = "html" if ($format =~ /^xsl\./); print $query->header( -type => ($type) ? $type : 'application/octet-stream', -'Content-Transfer-Encoding' => 'binary', @@ -155,6 +159,8 @@ if ( $shelf and $shelf->can_be_viewed( $borrowernumber ) ) { ), shelf => $shelf, ); + $template->param(xsl_exports => CustomXSLTExportList()); + $template->param( shelf => $shelf ); output_html_with_http_headers $query, $cookie, $template->output; } diff --git a/opac/opac-export.pl b/opac/opac-export.pl index d816ded995..41bfddceec 100755 --- a/opac/opac-export.pl +++ b/opac/opac-export.pl @@ -31,6 +31,8 @@ use C4::Biblio qw( use CGI qw ( -utf8 ); use C4::Auth; use C4::Ris qw( marc2ris ); +use C4::XSLT; + use Koha::RecordProcessor; my $query = CGI->new; @@ -136,6 +138,15 @@ elsif ( $format =~ /isbd/ ) { }); $format = 'isbd'; } +elsif ($format =~ /html/ ) { + if ($biblionumber){ + my $xslFile = $query->param("file"); + $marc = C4::Biblio::GetMarcBiblio({biblionumber => $biblionumber}); + my @hiddenitems; + + $marc = XSLTParse4Display($biblionumber, $marc, 'OPACXSLTCustomExport', 1, \@hiddenitems, undef, $xslFile, undef); + } +} else { $error= "Format $format is not supported."; } diff --git a/t/XSLT.t b/t/XSLT.t index e3a733251c..e20a7b80e1 100755 --- a/t/XSLT.t +++ b/t/XSLT.t @@ -75,5 +75,4 @@ is(find_and_slurp($dir, 'nope', 'fr-FR'), 'Theme prog, language en', 'Fell ba my $matching_string = q{0}; my $sysprefs_xml = C4::XSLT::get_xslt_sysprefs(); -ok( $sysprefs_xml =~ m/$matching_string/, 'singleBranchMode has a value of 0'); - +ok( $sysprefs_xml =~ m/$matching_string/, 'singleBranchMode has a value of 0'); \ No newline at end of file diff --git a/t/db_dependent/XSLT.t b/t/db_dependent/XSLT.t index 23a7340f72..0ed56f6b82 100755 --- a/t/db_dependent/XSLT.t +++ b/t/db_dependent/XSLT.t @@ -155,7 +155,28 @@ subtest 'buildKohaItemsNamespace status tests' => sub { t::lib::Mocks::mock_preference('UseRecalls', 0); }; - +subtest 'CustomXSLTExportList: Caching' => sub { + plan tests => 1; + t::lib::Mocks::mock_preference('OpacExportOptions', 'custom'); + $cache->clear_from_cache('CustomXSLTExportListOPAC'); + my $list = C4::XSLT::CustomXSLTExportList(1); + push @$list, { nonsense => 1 }; + $cache->set_in_cache('CustomXSLTExportListOPAC', $list ); + my $n = @$list; + $list = C4::XSLT::CustomXSLTExportList(1); + is( @$list, $n, 'This list comes from the cache and that is fine' ); + $cache->clear_from_cache('CustomXSLTExportListOPAC'); +}; +subtest 'CustomXSLTExportList: Check export options' => sub { + plan tests => 2; + t::lib::Mocks::mock_preference('OpacExportOptions', 'custom'); + my $list = C4::XSLT::CustomXSLTExportList(1); + is( $list>0, 1, 'We expect at least one result: simple export' ); + $cache->clear_from_cache('CustomXSLTExportListOPAC'); + t::lib::Mocks::mock_preference('OpacExportOptions', 'dc'); + $list = C4::XSLT::CustomXSLTExportList(1); + is( @$list, 0, 'We expect an empty list now' ); +}; $schema->storage->txn_rollback; subtest 'buildKohaItemsNamespace() including/omitting items tests' => sub { diff --git a/virtualshelves/downloadshelf.pl b/virtualshelves/downloadshelf.pl index e922e9b4e2..4c44bf5661 100755 --- a/virtualshelves/downloadshelf.pl +++ b/virtualshelves/downloadshelf.pl @@ -26,6 +26,8 @@ use C4::Biblio qw( GetMarcBiblio ); use C4::Output qw( output_html_with_http_headers ); use C4::Record; use C4::Ris qw( marc2ris ); +use C4::XSLT; + use Koha::CsvProfiles; use Koha::Virtualshelves; @@ -80,12 +82,15 @@ if ($shelfid && $format) { elsif ($format eq 'bibtex') { $output .= marc2bibtex($record, $biblionumber); } + elsif ($format =~ /^xsl\.(.*)$/){ + $output .= XSLTParse4Display($biblionumber, $record, 'XSLTCustomExport', 1, undef, ,'',$1,''); + } } } # If it was a CSV export we change the format after the export so the file extension is fine $format = "csv" if ($format =~ m/^\d+$/); - + $format = "html" if ($format =~ /^xsl\./); print $query->header( -type => 'application/octet-stream', -'Content-Transfer-Encoding' => 'binary', diff --git a/virtualshelves/shelves.pl b/virtualshelves/shelves.pl index 0ff8d9d332..4e8a4cd5a6 100755 --- a/virtualshelves/shelves.pl +++ b/virtualshelves/shelves.pl @@ -359,6 +359,7 @@ $template->param( public => $public, print => scalar $query->param('print') || 0, csv_profiles => [ Koha::CsvProfiles->search({ type => 'marc', used_for => 'export_records' })->as_list ], + xsl_exports => CustomXSLTExportList() ); output_html_with_http_headers $query, $cookie, $template->output; -- 2.25.1