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
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.
34 use Koha
::XSLT_Handler
;
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...
52 $engine=Koha
::XSLT_Handler
->new( { do_not_return_source
=> 1 } );
57 C4::XSLT - Functions for displaying XSLT-generated content
61 =head2 transformMARCXML4XSLT
63 Replaces codes with authorized values in a MARC::Record object
64 Is only used in this module currently.
68 sub transformMARCXML4XSLT
{
69 my ($biblionumber, $record) = @_;
70 my $frameworkcode = GetFrameworkCode
($biblionumber) || '';
71 my $tagslib = &GetMarcStructure
(1,$frameworkcode);
73 # FIXME: wish there was a better way to handle exceptions
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(
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.
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 );
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.
139 sub _get_best_default_xslt_filename
{
140 my ($htdocs, $theme, $lang, $base_xslfile) = @_;
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
150 foreach my $filename (@candidates) {
151 $xslfilename = $filename;
153 last; # we have a winner!
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 ) {
165 my $lang = C4
::Languages
::getlanguage
();
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;/\&/g;
225 $xmlrecord =~ s/\&\;lt\;/\<\;/g;
226 $xmlrecord =~ s/\&\;gt\;/\>\;/g;
228 $xmlrecord =~ s/\& /\&\; /;
229 $xmlrecord =~ s/\&\;amp\; /\&\; /;
231 #If the xslt should fail, we will return undef (old behavior was
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.
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
();
261 for my $item (@items) {
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
}) {
285 if ($item->{damaged
}) {
288 if (defined $transfertwhen && $transfertwhen ne '') {
289 $status = 'In transit';
291 if (defined $reservestatus && $reservestatus eq "Waiting") {
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>".
310 $xml = "<items xmlns=\"http://www.koha-community.org/items\">".$xml."</items>";
316 Returns reference to XSLT handler object.
330 Joshua Ferraro <jmf@liblime.com>
332 Koha Development Team <http://koha-community.org/>