bug_6410: correct borrowernumber template var name
[koha.git] / C4 / XSLT.pm
blob4dafff382519601dd75d391439185fa9b1d0fc52
1 package C4::XSLT;
2 # Copyright (C) 2006 LibLime
3 # <jmf at liblime dot com>
4 # Parts Copyright Katrin Fischer 2011
5 # Parts Copyright ByWater Solutions 2011
7 # This file is part of Koha.
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
12 # version.
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License along
19 # with Koha; if not, write to the Free Software Foundation, Inc.,
20 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 use strict;
23 use warnings;
25 use C4::Context;
26 use C4::Branch;
27 use C4::Items;
28 use C4::Koha;
29 use C4::Biblio;
30 use C4::Circulation;
31 use C4::Reserves;
32 use C4::Output qw//;
33 use Encode;
34 use XML::LibXML;
35 use XML::LibXSLT;
37 use vars qw($VERSION @ISA @EXPORT);
39 BEGIN {
40 require Exporter;
41 $VERSION = 0.03;
42 @ISA = qw(Exporter);
43 @EXPORT = qw(
44 &XSLTParse4Display
48 =head1 NAME
50 C4::XSLT - Functions for displaying XSLT-generated content
52 =head1 FUNCTIONS
54 =head2 transformMARCXML4XSLT
56 Replaces codes with authorized values in a MARC::Record object
58 =cut
60 sub transformMARCXML4XSLT {
61 my ($biblionumber, $record) = @_;
62 my $frameworkcode = GetFrameworkCode($biblionumber);
63 my $tagslib = &GetMarcStructure(1,$frameworkcode);
64 my @fields;
65 # FIXME: wish there was a better way to handle exceptions
66 eval {
67 @fields = $record->fields();
69 if ($@) { warn "PROBLEM WITH RECORD"; next; }
70 my $av = getAuthorisedValues4MARCSubfields($frameworkcode);
71 foreach my $tag ( keys %$av ) {
72 foreach my $field ( $record->field( $tag ) ) {
73 if ( $av->{ $tag } ) {
74 my @new_subfields = ();
75 for my $subfield ( $field->subfields() ) {
76 my ( $letter, $value ) = @$subfield;
77 $value = GetAuthorisedValueDesc( $tag, $letter, $value, '', $tagslib )
78 if $av->{ $tag }->{ $letter };
79 push( @new_subfields, $letter, $value );
81 $field ->replace_with( MARC::Field->new(
82 $tag,
83 $field->indicator(1),
84 $field->indicator(2),
85 @new_subfields
86 ) );
90 return $record;
93 =head2 getAuthorisedValues4MARCSubfields
95 Returns a ref of hash of ref of hash for tag -> letter controled by authorised values
97 =cut
99 # Cache for tagfield-tagsubfield to decode per framework.
100 # Should be preferably be placed in Koha-core...
101 my %authval_per_framework;
103 sub getAuthorisedValues4MARCSubfields {
104 my ($frameworkcode) = @_;
105 unless ( $authval_per_framework{ $frameworkcode } ) {
106 my $dbh = C4::Context->dbh;
107 my $sth = $dbh->prepare("SELECT DISTINCT tagfield, tagsubfield
108 FROM marc_subfield_structure
109 WHERE authorised_value IS NOT NULL
110 AND authorised_value!=''
111 AND frameworkcode=?");
112 $sth->execute( $frameworkcode );
113 my $av = { };
114 while ( my ( $tag, $letter ) = $sth->fetchrow() ) {
115 $av->{ $tag }->{ $letter } = 1;
117 $authval_per_framework{ $frameworkcode } = $av;
119 return $authval_per_framework{ $frameworkcode };
122 my $stylesheet;
124 sub XSLTParse4Display {
125 my ( $biblionumber, $orig_record, $xsl_suffix, $interface, $fixamps ) = @_;
126 $interface = 'opac' unless $interface;
127 # grab the XML, run it through our stylesheet, push it out to the browser
128 my $record = transformMARCXML4XSLT($biblionumber, $orig_record);
129 #return $record->as_formatted();
130 my $itemsxml = buildKohaItemsNamespace($biblionumber);
131 my $xmlrecord = $record->as_xml(C4::Context->preference('marcflavour'));
132 my $sysxml = "<sysprefs>\n";
133 foreach my $syspref ( qw/ hidelostitems OPACURLOpenInNewWindow
134 DisplayOPACiconsXSLT URLLinkText viewISBD
135 OPACBaseURL TraceCompleteSubfields
136 UseAuthoritiesForTracings TraceSubjectSubdivisions
137 Display856uAsImage OPACDisplay856uAsImage
138 UseControlNumber
139 AlternateHoldingsField AlternateHoldingsSeparator / )
141 my $sp = C4::Context->preference( $syspref );
142 next unless defined($sp);
143 $sysxml .= "<syspref name=\"$syspref\">$sp</syspref>\n";
145 $sysxml .= "</sysprefs>\n";
146 $xmlrecord =~ s/\<\/record\>/$itemsxml$sysxml\<\/record\>/;
147 if ($fixamps) { # We need to correct the ampersand entities that Zebra outputs
148 $xmlrecord =~ s/\&amp;amp;/\&amp;/g;
150 $xmlrecord =~ s/\& /\&amp\; /;
151 $xmlrecord =~ s/\&amp\;amp\; /\&amp\; /;
153 my $parser = XML::LibXML->new();
154 # don't die when you find &, >, etc
155 $parser->recover_silently(0);
156 my $source = $parser->parse_string($xmlrecord);
157 unless ( $stylesheet ) {
158 my $xslt = XML::LibXSLT->new();
159 my $xslfile;
160 if ($interface eq 'intranet') {
161 $xslfile = C4::Context->config('intrahtdocs') .
162 '/' . C4::Context->preference("template") .
163 '/' . C4::Output::_current_language() .
164 '/xslt/' .
165 C4::Context->preference('marcflavour') .
166 "slim2intranet$xsl_suffix.xsl";
167 } else {
168 $xslfile = C4::Context->config('opachtdocs') .
169 '/' . C4::Context->preference("opacthemes") .
170 '/' . C4::Output::_current_language() .
171 '/xslt/' .
172 C4::Context->preference('marcflavour') .
173 "slim2OPAC$xsl_suffix.xsl";
175 my $style_doc = $parser->parse_file($xslfile);
176 $stylesheet = $xslt->parse_stylesheet($style_doc);
178 my $results = $stylesheet->transform($source);
179 my $newxmlrecord = $stylesheet->output_string($results);
180 return $newxmlrecord;
183 sub buildKohaItemsNamespace {
184 my ($biblionumber) = @_;
185 my @items = C4::Items::GetItemsInfo($biblionumber);
186 my $branches = GetBranches();
187 my $itemtypes = GetItemTypes();
188 my $xml = '';
189 for my $item (@items) {
190 my $status;
192 my ( $transfertwhen, $transfertfrom, $transfertto ) = C4::Circulation::GetTransfers($item->{itemnumber});
194 my ( $reservestatus, $reserveitem ) = C4::Reserves::CheckReserves($item->{itemnumber});
196 if ( $itemtypes->{ $item->{itype} }->{notforloan} || $item->{notforloan} || $item->{onloan} || $item->{wthdrawn} || $item->{itemlost} || $item->{damaged} ||
197 (defined $transfertwhen && $transfertwhen ne '') || $item->{itemnotforloan} || (defined $reservestatus && $reservestatus eq "Waiting") ){
198 if ( $item->{notforloan} < 0) {
199 $status = "On order";
201 if ( $item->{itemnotforloan} > 0 || $item->{notforloan} > 0 || $itemtypes->{ $item->{itype} }->{notforloan} == 1 ) {
202 $status = "reference";
204 if ($item->{onloan}) {
205 $status = "Checked out";
207 if ( $item->{wthdrawn}) {
208 $status = "Withdrawn";
210 if ($item->{itemlost}) {
211 $status = "Lost";
213 if ($item->{damaged}) {
214 $status = "Damaged";
216 if (defined $transfertwhen && $transfertwhen ne '') {
217 $status = 'In transit';
219 if (defined $reservestatus && $reservestatus eq "Waiting") {
220 $status = 'Waiting';
222 } else {
223 $status = "available";
225 my $homebranch = $item->{homebranch}? xml_escape($branches->{$item->{homebranch}}->{'branchname'}):'';
226 my $itemcallnumber = xml_escape($item->{itemcallnumber});
227 $xml.= "<item><homebranch>$homebranch</homebranch>".
228 "<status>$status</status>".
229 "<itemcallnumber>".$itemcallnumber."</itemcallnumber>"
230 . "</item>";
233 $xml = "<items xmlns=\"http://www.koha-community.org/items\">".$xml."</items>";
234 return $xml;
240 __END__
242 =head1 NOTES
244 =cut
246 =head1 AUTHOR
248 Joshua Ferraro <jmf@liblime.com>
250 =cut