Bug 15008 - Add custom HTML areas to circulation and reports home pages
[koha.git] / C4 / XSLT.pm
blobdd13c501bcc90775ea7d9278b2ea5029cccd532e
1 package C4::XSLT;
3 # Copyright (C) 2006 LibLime
4 # <jmf at liblime dot com>
5 # Parts Copyright Katrin Fischer 2011
6 # Parts Copyright ByWater Solutions 2011
7 # Parts Copyright Biblibre 2012
9 # This file is part of Koha.
11 # Koha is free software; you can redistribute it and/or modify it under the
12 # terms of the GNU General Public License as published by the Free Software
13 # Foundation; either version 3 of the License, or (at your option) any later
14 # version.
16 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
17 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
18 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License along
21 # with Koha; if not, write to the Free Software Foundation, Inc.,
22 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 use strict;
25 use warnings;
27 use C4::Context;
28 use C4::Branch;
29 use C4::Items;
30 use C4::Koha;
31 use C4::Biblio;
32 use C4::Circulation;
33 use C4::Reserves;
34 use Koha::XSLT_Handler;
35 use Koha::Libraries;
37 use Encode;
39 use vars qw(@ISA @EXPORT);
41 my $engine; #XSLT Handler object
42 my %authval_per_framework;
43 # Cache for tagfield-tagsubfield to decode per framework.
44 # Should be preferably be placed in Koha-core...
46 BEGIN {
47 require Exporter;
48 @ISA = qw(Exporter);
49 @EXPORT = qw(
50 &XSLTParse4Display
52 $engine=Koha::XSLT_Handler->new( { do_not_return_source => 1 } );
55 =head1 NAME
57 C4::XSLT - Functions for displaying XSLT-generated content
59 =head1 FUNCTIONS
61 =head2 transformMARCXML4XSLT
63 Replaces codes with authorized values in a MARC::Record object
64 Is only used in this module currently.
66 =cut
68 sub transformMARCXML4XSLT {
69 my ($biblionumber, $record) = @_;
70 my $frameworkcode = GetFrameworkCode($biblionumber) || '';
71 my $tagslib = &GetMarcStructure(1,$frameworkcode);
72 my @fields;
73 # FIXME: wish there was a better way to handle exceptions
74 eval {
75 @fields = $record->fields();
77 if ($@) { warn "PROBLEM WITH RECORD"; next; }
78 my $av = getAuthorisedValues4MARCSubfields($frameworkcode);
79 foreach my $tag ( keys %$av ) {
80 foreach my $field ( $record->field( $tag ) ) {
81 if ( $av->{ $tag } ) {
82 my @new_subfields = ();
83 for my $subfield ( $field->subfields() ) {
84 my ( $letter, $value ) = @$subfield;
85 $value = GetAuthorisedValueDesc( $tag, $letter, $value, '', $tagslib )
86 if $av->{ $tag }->{ $letter };
87 push( @new_subfields, $letter, $value );
89 $field ->replace_with( MARC::Field->new(
90 $tag,
91 $field->indicator(1),
92 $field->indicator(2),
93 @new_subfields
94 ) );
98 return $record;
101 =head2 getAuthorisedValues4MARCSubfields
103 Returns a ref of hash of ref of hash for tag -> letter controlled by authorised values
104 Is only used in this module currently.
106 =cut
108 sub getAuthorisedValues4MARCSubfields {
109 my ($frameworkcode) = @_;
110 unless ( $authval_per_framework{ $frameworkcode } ) {
111 my $dbh = C4::Context->dbh;
112 my $sth = $dbh->prepare("SELECT DISTINCT tagfield, tagsubfield
113 FROM marc_subfield_structure
114 WHERE authorised_value IS NOT NULL
115 AND authorised_value!=''
116 AND frameworkcode=?");
117 $sth->execute( $frameworkcode );
118 my $av = { };
119 while ( my ( $tag, $letter ) = $sth->fetchrow() ) {
120 $av->{ $tag }->{ $letter } = 1;
122 $authval_per_framework{ $frameworkcode } = $av;
124 return $authval_per_framework{ $frameworkcode };
127 =head2 XSLTParse4Display
129 Returns xml for biblionumber and requested XSLT transformation.
130 Returns undef if the transform fails.
132 Used in OPAC results and detail, intranet results and detail, list display.
133 (Depending on the settings of your XSLT preferences.)
135 The helper function _get_best_default_xslt_filename is used in a unit test.
137 =cut
139 sub _get_best_default_xslt_filename {
140 my ($htdocs, $theme, $lang, $base_xslfile) = @_;
142 my @candidates = (
143 "$htdocs/$theme/$lang/xslt/${base_xslfile}", # exact match
144 "$htdocs/$theme/en/xslt/${base_xslfile}", # if not, preferred theme in English
145 "$htdocs/prog/$lang/xslt/${base_xslfile}", # if not, 'prog' theme in preferred language
146 "$htdocs/prog/en/xslt/${base_xslfile}", # otherwise, prog theme in English; should always
147 # exist
149 my $xslfilename;
150 foreach my $filename (@candidates) {
151 $xslfilename = $filename;
152 if (-f $filename) {
153 last; # we have a winner!
156 return $xslfilename;
159 sub XSLTParse4Display {
160 my ( $biblionumber, $orig_record, $xslsyspref, $fixamps, $hidden_items ) = @_;
161 my $xslfilename = C4::Context->preference($xslsyspref);
162 if ( $xslfilename =~ /^\s*"?default"?\s*$/i ) {
163 my $htdocs;
164 my $theme;
165 my $lang = C4::Languages::getlanguage();
166 my $xslfile;
167 if ($xslsyspref eq "XSLTDetailsDisplay") {
168 $htdocs = C4::Context->config('intrahtdocs');
169 $theme = C4::Context->preference("template");
170 $xslfile = C4::Context->preference('marcflavour') .
171 "slim2intranetDetail.xsl";
172 } elsif ($xslsyspref eq "XSLTResultsDisplay") {
173 $htdocs = C4::Context->config('intrahtdocs');
174 $theme = C4::Context->preference("template");
175 $xslfile = C4::Context->preference('marcflavour') .
176 "slim2intranetResults.xsl";
177 } elsif ($xslsyspref eq "OPACXSLTDetailsDisplay") {
178 $htdocs = C4::Context->config('opachtdocs');
179 $theme = C4::Context->preference("opacthemes");
180 $xslfile = C4::Context->preference('marcflavour') .
181 "slim2OPACDetail.xsl";
182 } elsif ($xslsyspref eq "OPACXSLTResultsDisplay") {
183 $htdocs = C4::Context->config('opachtdocs');
184 $theme = C4::Context->preference("opacthemes");
185 $xslfile = C4::Context->preference('marcflavour') .
186 "slim2OPACResults.xsl";
188 $xslfilename = _get_best_default_xslt_filename($htdocs, $theme, $lang, $xslfile);
191 if ( $xslfilename =~ m/\{langcode\}/ ) {
192 my $lang = C4::Languages::getlanguage();
193 $xslfilename =~ s/\{langcode\}/$lang/;
196 # grab the XML, run it through our stylesheet, push it out to the browser
197 my $record = transformMARCXML4XSLT($biblionumber, $orig_record);
198 my $itemsxml = buildKohaItemsNamespace($biblionumber, $hidden_items);
199 my $xmlrecord = $record->as_xml(C4::Context->preference('marcflavour'));
200 my $sysxml = "<sysprefs>\n";
201 foreach my $syspref ( qw/ hidelostitems OPACURLOpenInNewWindow
202 DisplayOPACiconsXSLT URLLinkText viewISBD
203 OPACBaseURL TraceCompleteSubfields UseICU
204 UseAuthoritiesForTracings TraceSubjectSubdivisions
205 Display856uAsImage OPACDisplay856uAsImage
206 UseControlNumber IntranetBiblioDefaultView BiblioDefaultView
207 OPACItemLocation DisplayIconsXSLT
208 AlternateHoldingsField AlternateHoldingsSeparator
209 TrackClicks opacthemes IdRef / )
211 my $sp = C4::Context->preference( $syspref );
212 next unless defined($sp);
213 $sysxml .= "<syspref name=\"$syspref\">$sp</syspref>\n";
216 # singleBranchMode was a system preference, but no longer is
217 # we can retain it here for compatibility
218 my $singleBranchMode = Koha::Libraries->search->count == 1;
219 $sysxml .= "<syspref name=\"singleBranchMode\">$singleBranchMode</syspref>\n";
221 $sysxml .= "</sysprefs>\n";
222 $xmlrecord =~ s/\<\/record\>/$itemsxml$sysxml\<\/record\>/;
223 if ($fixamps) { # We need to correct the HTML entities that Zebra outputs
224 $xmlrecord =~ s/\&amp;amp;/\&amp;/g;
225 $xmlrecord =~ s/\&amp\;lt\;/\&lt\;/g;
226 $xmlrecord =~ s/\&amp\;gt\;/\&gt\;/g;
228 $xmlrecord =~ s/\& /\&amp\; /;
229 $xmlrecord =~ s/\&amp\;amp\; /\&amp\; /;
231 #If the xslt should fail, we will return undef (old behavior was
232 #raw MARC)
233 #Note that we did set do_not_return_source at object construction
234 return $engine->transform($xmlrecord, $xslfilename ); #file or URL
237 =head2 buildKohaItemsNamespace
239 Returns XML for items.
240 Is only used in this module currently.
242 =cut
244 sub buildKohaItemsNamespace {
245 my ($biblionumber, $hidden_items) = @_;
247 my @items = C4::Items::GetItemsInfo($biblionumber);
248 if ($hidden_items && @$hidden_items) {
249 my %hi = map {$_ => 1} @$hidden_items;
250 @items = grep { !$hi{$_->{itemnumber}} } @items;
253 my $shelflocations = GetKohaAuthorisedValues('items.location',GetFrameworkCode($biblionumber), 'opac');
254 my $ccodes = GetKohaAuthorisedValues('items.ccode',GetFrameworkCode($biblionumber), 'opac');
256 my $branches = GetBranches();
257 my $itemtypes = GetItemTypes();
258 my $location = "";
259 my $ccode = "";
260 my $xml = '';
261 for my $item (@items) {
262 my $status;
264 my ( $transfertwhen, $transfertfrom, $transfertto ) = C4::Circulation::GetTransfers($item->{itemnumber});
266 my $reservestatus = C4::Reserves::GetReserveStatus( $item->{itemnumber} );
268 if ( $itemtypes->{ $item->{itype} }->{notforloan} || $item->{notforloan} || $item->{onloan} || $item->{withdrawn} || $item->{itemlost} || $item->{damaged} ||
269 (defined $transfertwhen && $transfertwhen ne '') || $item->{itemnotforloan} || (defined $reservestatus && $reservestatus eq "Waiting") ){
270 if ( $item->{notforloan} < 0) {
271 $status = "On order";
273 if ( $item->{itemnotforloan} > 0 || $item->{notforloan} > 0 || $itemtypes->{ $item->{itype} }->{notforloan} == 1 ) {
274 $status = "reference";
276 if ($item->{onloan}) {
277 $status = "Checked out";
279 if ( $item->{withdrawn}) {
280 $status = "Withdrawn";
282 if ($item->{itemlost}) {
283 $status = "Lost";
285 if ($item->{damaged}) {
286 $status = "Damaged";
288 if (defined $transfertwhen && $transfertwhen ne '') {
289 $status = 'In transit';
291 if (defined $reservestatus && $reservestatus eq "Waiting") {
292 $status = 'Waiting';
294 } else {
295 $status = "available";
297 my $homebranch = $item->{homebranch}? xml_escape($branches->{$item->{homebranch}}->{'branchname'}):'';
298 my $holdingbranch = $item->{holdingbranch}? xml_escape($branches->{$item->{holdingbranch}}->{'branchname'}):'';
299 $location = $item->{location}? xml_escape($shelflocations->{$item->{location}}||$item->{location}):'';
300 $ccode = $item->{ccode}? xml_escape($ccodes->{$item->{ccode}}||$item->{ccode}):'';
301 my $itemcallnumber = xml_escape($item->{itemcallnumber});
302 $xml.= "<item><homebranch>$homebranch</homebranch>".
303 "<holdingbranch>$holdingbranch</holdingbranch>".
304 "<location>$location</location>".
305 "<ccode>$ccode</ccode>".
306 "<status>$status</status>".
307 "<itemcallnumber>".$itemcallnumber."</itemcallnumber>".
308 "</item>";
310 $xml = "<items xmlns=\"http://www.koha-community.org/items\">".$xml."</items>";
311 return $xml;
314 =head2 engine
316 Returns reference to XSLT handler object.
318 =cut
320 sub engine {
321 return $engine;
326 __END__
328 =head1 AUTHOR
330 Joshua Ferraro <jmf@liblime.com>
332 Koha Development Team <http://koha-community.org/>
334 =cut