|
| 1 | +#!/usr/bin/perl |
| 2 | + |
| 3 | +# Copyright 2009 BibLibre |
| 4 | +# |
| 5 | +# This file is part of Koha. |
| 6 | +# |
| 7 | +# Koha is free software; you can redistribute it and/or modify it |
| 8 | +# under the terms of the GNU General Public License as published by |
| 9 | +# the Free Software Foundation; either version 3 of the License, or |
| 10 | +# (at your option) any later version. |
| 11 | +# |
| 12 | +# Koha is distributed in the hope that it will be useful, but |
| 13 | +# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | +# GNU General Public License for more details. |
| 16 | +# |
| 17 | +# You should have received a copy of the GNU General Public License |
| 18 | +# along with Koha; if not, see <http://www.gnu.org/licenses>. |
| 19 | + |
| 20 | +use Modern::Perl; |
| 21 | + |
| 22 | +use CGI qw ( -utf8 ); |
| 23 | +use Encode qw( encode ); |
| 24 | + |
| 25 | +use C4::Auth qw( get_template_and_user ); |
| 26 | +use C4::Biblio qw( GetFrameworkCode GetISBDView ); |
| 27 | +use C4::Output qw( output_html_with_http_headers ); |
| 28 | +use C4::Record; |
| 29 | +use C4::Ris qw( marc2ris ); |
| 30 | +use Koha::Biblios; |
| 31 | +use Koha::CsvProfiles; |
| 32 | +use Koha::RecordProcessor; |
| 33 | + |
| 34 | +use utf8; |
| 35 | +my $query = CGI->new(); |
| 36 | +do '../eds-methods.pl'; |
| 37 | +my ( $template, $borrowernumber, $cookie ) = get_template_and_user ( |
| 38 | + { |
| 39 | + template_name => "opac-downloadcart.tt", |
| 40 | + query => $query, |
| 41 | + type => "opac", |
| 42 | + authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ), |
| 43 | + } |
| 44 | +); |
| 45 | +my $eds_data = ""; |
| 46 | +my $bib_list = $query->param('bib_list'); |
| 47 | +#convert _dot_ to . to properly search for items |
| 48 | +$bib_list =~s/\_dot\_/\./g; |
| 49 | +my $format = $query->param('format'); |
| 50 | +my $dbh = C4::Context->dbh; |
| 51 | +$eds_data = $query->param('eds_data'); #EDS Patch |
| 52 | + |
| 53 | +if ($bib_list && $format) { |
| 54 | + |
| 55 | + my $patron = Koha::Patrons->find($borrowernumber); |
| 56 | + |
| 57 | + my @bibs = split( /\//, $bib_list ); |
| 58 | + |
| 59 | + my $marcflavour = C4::Context->preference('marcflavour'); |
| 60 | + my $output; |
| 61 | + my $extension; |
| 62 | + my $type; |
| 63 | + |
| 64 | + # CSV |
| 65 | + if ($format =~ /^\d+$/) { |
| 66 | + |
| 67 | + my $csv_profile = Koha::CsvProfiles->find($format); |
| 68 | + if ( not $csv_profile or $csv_profile->staff_only ) { |
| 69 | + print $query->redirect('/cgi-bin/koha/errors/404.pl'); |
| 70 | + exit; |
| 71 | + } |
| 72 | + |
| 73 | + $output = marc2csv(\@bibs, $format); |
| 74 | + |
| 75 | + # Other formats |
| 76 | + } else { |
| 77 | + my $record_processor = Koha::RecordProcessor->new({ |
| 78 | + filters => 'ViewPolicy' |
| 79 | + }); |
| 80 | + foreach my $biblionumber (@bibs) { |
| 81 | + my $biblio = ''; |
| 82 | + my $record = ''; |
| 83 | + if($biblionumber =~m/\_\_/){ |
| 84 | + my $dat = ''; |
| 85 | + ($record,$dat)= ProcessEDSCartItems($biblionumber,$eds_data,$record,$dat); #EDS patch |
| 86 | + } else { |
| 87 | + $biblio = Koha::Biblios->find($biblionumber); |
| 88 | + $record = $biblio->metadata->record( |
| 89 | + { |
| 90 | + embed_items => 1, |
| 91 | + opac => 1, |
| 92 | + patron => $patron, |
| 93 | + } |
| 94 | + ); |
| 95 | + } |
| 96 | + my $framework = &GetFrameworkCode( $biblio ); |
| 97 | + $record_processor->options({ |
| 98 | + interface => 'opac', |
| 99 | + frameworkcode => $framework |
| 100 | + }); |
| 101 | + $record_processor->process($record); |
| 102 | + |
| 103 | + next unless $record; |
| 104 | + |
| 105 | + if ($format eq 'iso2709') { |
| 106 | + #NOTE: If we don't explicitly UTF-8 encode the output, |
| 107 | + #the browser will guess the encoding, and it won't always choose UTF-8. |
| 108 | + $output .= encode("UTF-8", $record->as_usmarc()) // q{}; |
| 109 | + } |
| 110 | + elsif ($format eq 'ris') { |
| 111 | + $output .= marc2ris($record); |
| 112 | + } |
| 113 | + elsif ($format eq 'bibtex') { |
| 114 | + $output .= marc2bibtex($record, $biblionumber); |
| 115 | + } |
| 116 | + elsif ( $format eq 'isbd' ) { |
| 117 | + my $framework = GetFrameworkCode( $biblionumber ); |
| 118 | + $output .= GetISBDView({ |
| 119 | + 'record' => $record, |
| 120 | + 'template' => 'opac', |
| 121 | + 'framework' => $framework, |
| 122 | + }); |
| 123 | + $extension = "txt"; |
| 124 | + $type = "text/plain"; |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + # If it was a CSV export we change the format after the export so the file extension is fine |
| 130 | + $format = "csv" if ($format =~ m/^\d+$/); |
| 131 | + |
| 132 | + print $query->header( |
| 133 | + -type => ($type) ? $type : 'application/octet-stream', |
| 134 | + -'Content-Transfer-Encoding' => 'binary', |
| 135 | + -attachment => ($extension) ? "cart.$format.$extension" : "cart.$format"); |
| 136 | + print $output; |
| 137 | + |
| 138 | +} else { |
| 139 | + $template->param( |
| 140 | + csv_profiles => Koha::CsvProfiles->search( |
| 141 | + { |
| 142 | + type => 'marc', |
| 143 | + used_for => 'export_records', |
| 144 | + staff_only => 0 |
| 145 | + } |
| 146 | + ), |
| 147 | + bib_list => $bib_list, |
| 148 | + ); |
| 149 | + output_html_with_http_headers $query, $cookie, $template->output; |
| 150 | +} |
0 commit comments