Merge remote-tracking branch 'kc/new/enh/bug_5917' into kcmaster
[koha.git] / C4 / XSLT.pm
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
6 #
7 # This file is part of Koha.
8 #
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.
13 #
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.
17 #
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.
21
22 use strict;
23 use warnings;
24
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;
36
37 use vars qw($VERSION @ISA @EXPORT);
38
39 BEGIN {
40     require Exporter;
41     $VERSION = 0.03;
42     @ISA = qw(Exporter);
43     @EXPORT = qw(
44         &XSLTParse4Display
45     );
46 }
47
48 =head1 NAME
49
50 C4::XSLT - Functions for displaying XSLT-generated content
51
52 =head1 FUNCTIONS
53
54 =head2 transformMARCXML4XSLT
55
56 Replaces codes with authorized values in a MARC::Record object
57
58 =cut
59
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();
68     };
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 );
80                 } 
81                 $field ->replace_with( MARC::Field->new(
82                     $tag,
83                     $field->indicator(1),
84                     $field->indicator(2),
85                     @new_subfields
86                 ) );
87             }
88         }
89     }
90     return $record;
91 }
92
93 =head2 getAuthorisedValues4MARCSubfields
94
95 Returns a ref of hash of ref of hash for tag -> letter controled by authorised values
96
97 =cut
98
99 # Cache for tagfield-tagsubfield to decode per framework.
100 # Should be preferably be placed in Koha-core...
101 my %authval_per_framework;
102
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;
116         }
117         $authval_per_framework{ $frameworkcode } = $av;
118     }
119     return $authval_per_framework{ $frameworkcode };
120 }
121
122 my $stylesheet;
123
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 / )
140     {
141         my $sp = C4::Context->preference( $syspref );
142         next unless defined($sp);
143         $sysxml .= "<syspref name=\"$syspref\">$sp</syspref>\n";
144     }
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;
149     }
150     $xmlrecord =~ s/\& /\&amp\; /;
151     $xmlrecord =~ s/\&amp\;amp\; /\&amp\; /;
152
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";
174         }
175         my $style_doc = $parser->parse_file($xslfile);
176         $stylesheet = $xslt->parse_stylesheet($style_doc);
177     }
178     my $results = $stylesheet->transform($source);
179     my $newxmlrecord = $stylesheet->output_string($results);
180     return $newxmlrecord;
181 }
182
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;
191
192         my ( $transfertwhen, $transfertfrom, $transfertto ) = C4::Circulation::GetTransfers($item->{itemnumber});
193
194         my ( $reservestatus, $reserveitem ) = C4::Reserves::CheckReserves($item->{itemnumber});
195
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";
200             } 
201             if ( $item->{itemnotforloan} > 0 || $item->{notforloan} > 0 || $itemtypes->{ $item->{itype} }->{notforloan} == 1 ) {
202                 $status = "reference";
203             }
204             if ($item->{onloan}) {
205                 $status = "Checked out";
206             }
207             if ( $item->{wthdrawn}) {
208                 $status = "Withdrawn";
209             }
210             if ($item->{itemlost}) {
211                 $status = "Lost";
212             }
213             if ($item->{damaged}) {
214                 $status = "Damaged"; 
215             }
216             if (defined $transfertwhen && $transfertwhen ne '') {
217                 $status = 'In transit';
218             }
219             if (defined $reservestatus && $reservestatus eq "Waiting") {
220                 $status = 'Waiting';
221             }
222         } else {
223             $status = "available";
224         }
225         my $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>";
231
232     }
233     $xml = "<items xmlns=\"http://www.koha-community.org/items\">".$xml."</items>";
234     return $xml;
235 }
236
237
238
239 1;
240 __END__
241
242 =head1 NOTES
243
244 =cut
245
246 =head1 AUTHOR
247
248 Joshua Ferraro <jmf@liblime.com>
249
250 =cut