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