Fix for ampersand display problem in OPAC search results
[koha.git] / C4 / XSLT.pm
1 package C4::XSLT;
2 # Copyright (C) 2006 LibLime
3 # <jmf at liblime dot com>
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
15 #
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.
19
20 use strict;
21 use warnings;
22
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 Encode;
31 use XML::LibXML;
32 use XML::LibXSLT;
33
34 use vars qw($VERSION @ISA @EXPORT);
35
36 BEGIN {
37     require Exporter;
38     $VERSION = 0.03;
39     @ISA = qw(Exporter);
40     @EXPORT = qw(
41         &XSLTParse4Display
42     );
43 }
44
45 =head1 NAME
46
47 C4::XSLT - Functions for displaying XSLT-generated content
48
49 =head1 FUNCTIONS
50
51 =head1 transformMARCXML4XSLT
52
53 =head2 replaces codes with authorized values in a MARC::Record object
54
55 =cut
56
57 sub transformMARCXML4XSLT {
58     my ($biblionumber, $record) = @_;
59     my $frameworkcode = GetFrameworkCode($biblionumber);
60     my $tagslib = &GetMarcStructure(1,$frameworkcode);
61     my @fields;
62     # FIXME: wish there was a better way to handle exceptions
63     eval {
64         @fields = $record->fields();
65     };
66     if ($@) { warn "PROBLEM WITH RECORD"; next; }
67     my $av = getAuthorisedValues4MARCSubfields($frameworkcode);
68     foreach my $tag ( keys %$av ) {
69         foreach my $field ( $record->field( $tag ) ) {
70             if ( $av->{ $tag } ) {
71                 my @new_subfields = ();
72                 for my $subfield ( $field->subfields() ) {
73                     my ( $letter, $value ) = @$subfield;
74                     $value = GetAuthorisedValueDesc( $tag, $letter, $value, '', $tagslib )
75                         if $av->{ $tag }->{ $letter };
76                     push( @new_subfields, $letter, $value );
77                 } 
78                 $field ->replace_with( MARC::Field->new(
79                     $tag,
80                     $field->indicator(1),
81                     $field->indicator(2),
82                     @new_subfields
83                 ) );
84             }
85         }
86     }
87     return $record;
88 }
89
90 =head1 getAuthorisedValues4MARCSubfields
91
92 =head2 returns an ref of hash of ref of hash for tag -> letter controled bu authorised values
93
94 =cut
95
96 # Cache for tagfield-tagsubfield to decode per framework.
97 # Should be preferably be placed in Koha-core...
98 my %authval_per_framework;
99
100 sub getAuthorisedValues4MARCSubfields {
101     my ($frameworkcode) = @_;
102     unless ( $authval_per_framework{ $frameworkcode } ) {
103         my $dbh = C4::Context->dbh;
104         my $sth = $dbh->prepare("SELECT DISTINCT tagfield, tagsubfield
105                                  FROM marc_subfield_structure
106                                  WHERE authorised_value IS NOT NULL
107                                    AND authorised_value!=''
108                                    AND frameworkcode=?");
109         $sth->execute( $frameworkcode );
110         my $av = { };
111         while ( my ( $tag, $letter ) = $sth->fetchrow() ) {
112             $av->{ $tag }->{ $letter } = 1;
113         }
114         $authval_per_framework{ $frameworkcode } = $av;
115     }
116     return $authval_per_framework{ $frameworkcode };
117 }
118
119 my $stylesheet;
120
121 sub XSLTParse4Display {
122     my ( $biblionumber, $orig_record, $xsl_suffix, $interface ) = @_;
123     $interface = 'opac' unless $interface;
124     # grab the XML, run it through our stylesheet, push it out to the browser
125     my $record = transformMARCXML4XSLT($biblionumber, $orig_record);
126     #return $record->as_formatted();
127     my $itemsxml  = buildKohaItemsNamespace($biblionumber);
128     my $xmlrecord = $record->as_xml(C4::Context->preference('marcflavour'));
129     my $sysxml = "<sysprefs>\n";
130 #    warn $xmlrecord;
131     foreach my $syspref ( qw/OPACURLOpenInNewWindow DisplayOPACiconsXSLT URLLinkText/ ) {
132         $sysxml .= "<syspref name=\"$syspref\">" .
133                    C4::Context->preference( $syspref ) .
134                    "</syspref>\n";
135     }
136     $sysxml .= "</sysprefs>\n";
137     $xmlrecord =~ s/\<\/record\>/$itemsxml$sysxml\<\/record\>/;
138     $xmlrecord =~ s/\& /\&amp\; /;
139     $xmlrecord=~ s/\&amp\;amp\; /\&amp\; /;
140
141     my $parser = XML::LibXML->new();
142     # don't die when you find &, >, etc
143     $parser->recover_silently(0);
144     my $source = $parser->parse_string($xmlrecord);
145     unless ( $stylesheet ) {
146         my $xslt = XML::LibXSLT->new();
147         my $xslfile;
148         if ($interface eq 'intranet') {
149             $xslfile = C4::Context->config('intrahtdocs') . 
150                       "/prog/en/xslt/" .
151                       C4::Context->preference('marcflavour') .
152                       "slim2intranet$xsl_suffix.xsl";
153         } else {
154             $xslfile = C4::Context->config('opachtdocs') . 
155                       "/prog/en/xslt/" .
156                       C4::Context->preference('marcflavour') .
157                       "slim2OPAC$xsl_suffix.xsl";
158         }
159         my $style_doc = $parser->parse_file($xslfile);
160         $stylesheet = $xslt->parse_stylesheet($style_doc);
161     }
162     my $results = $stylesheet->transform($source);
163     my $newxmlrecord = $stylesheet->output_string($results);
164     return $newxmlrecord;
165 }
166
167 sub buildKohaItemsNamespace {
168     my ($biblionumber) = @_;
169     my @items = C4::Items::GetItemsInfo($biblionumber);
170     my $branches = GetBranches();
171     my $itemtypes = GetItemTypes();
172     my $xml = '';
173     for my $item (@items) {
174         my $status;
175
176         my ( $transfertwhen, $transfertfrom, $transfertto ) = C4::Circulation::GetTransfers($item->{itemnumber});
177
178         my ( $reservestatus, $reserveitem ) = C4::Reserves::CheckReserves($item->{itemnumber});
179
180         if ( $itemtypes->{ $item->{itype} }->{notforloan} || $item->{notforloan} || $item->{onloan} || $item->{wthdrawn} || $item->{itemlost} || $item->{damaged} || 
181              (defined $transfertwhen && $transfertwhen ne '') || $item->{itemnotforloan} || (defined $reservestatus && $reservestatus eq "Waiting") ){ 
182             if ( $item->{notforloan} < 0) {
183                 $status = "On order";
184             } 
185             if ( $item->{itemnotforloan} > 0 || $item->{notforloan} > 0 || $itemtypes->{ $item->{itype} }->{notforloan} == 1 ) {
186                 $status = "reference";
187             }
188             if ($item->{onloan}) {
189                 $status = "Checked out";
190             }
191             if ( $item->{wthdrawn}) {
192                 $status = "Withdrawn";
193             }
194             if ($item->{itemlost}) {
195                 $status = "Lost";
196             }
197             if ($item->{damaged}) {
198                 $status = "Damaged"; 
199             }
200             if (defined $transfertwhen && $transfertwhen ne '') {
201                 $status = 'In transit';
202             }
203             if (defined $reservestatus && $reservestatus eq "Waiting") {
204                 $status = 'Waiting';
205             }
206         } else {
207             $status = "available";
208         }
209         my $homebranch = $branches->{$item->{homebranch}}->{'branchname'};
210         $xml.= "<item><homebranch>$homebranch</homebranch>".
211                 "<status>$status</status>".
212                 (defined $item->{'itemcallnumber'} ? "<itemcallnumber>".$item->{'itemcallnumber'}."</itemcallnumber>" 
213                                            : "<itemcallnumber />")
214         . "</item>";
215
216     }
217     $xml = "<items xmlns=\"http://www.koha.org/items\">".$xml."</items>";
218     return $xml;
219 }
220
221
222
223 1;
224 __END__
225
226 =head1 NOTES
227
228 =head1 AUTHOR
229
230 Joshua Ferraro <jmf@liblime.com>
231
232 =cut