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;
38 use vars qw($VERSION @ISA @EXPORT);
40 my $engine; #XSLT Handler object
41 my %authval_per_framework;
42 # Cache for tagfield-tagsubfield to decode per framework.
43 # Should be preferably be placed in Koha-core...
47 $VERSION = 3.07.00.049;
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 singleBranchMode OPACItemLocation DisplayIconsXSLT
208 AlternateHoldingsField AlternateHoldingsSeparator
209 TrackClicks opacthemes IdRef
210 OPACResultsLibrary / )
212 my $sp = C4::Context->preference( $syspref );
213 next unless defined($sp);
214 $sysxml .= "<syspref name=\"$syspref\">$sp</syspref>\n";
216 $sysxml .= "</sysprefs>\n";
217 $xmlrecord =~ s/\<\/record\>/$itemsxml$sysxml\<\/record\>/;
218 if ($fixamps) { # We need to correct the HTML entities that Zebra outputs
219 $xmlrecord =~ s/\&amp;/\&/g;
220 $xmlrecord =~ s/\&\;lt\;/\<\;/g;
221 $xmlrecord =~ s/\&\;gt\;/\>\;/g;
223 $xmlrecord =~ s/\& /\&\; /;
224 $xmlrecord =~ s/\&\;amp\; /\&\; /;
226 #If the xslt should fail, we will return undef (old behavior was
228 #Note that we did set do_not_return_source at object construction
229 return $engine->transform($xmlrecord, $xslfilename ); #file or URL
232 =head2 buildKohaItemsNamespace
234 Returns XML for items.
235 Is only used in this module currently.
239 sub buildKohaItemsNamespace {
240 my ($biblionumber, $hidden_items) = @_;
242 my @items = C4::Items::GetItemsInfo($biblionumber);
243 if ($hidden_items && @$hidden_items) {
244 my %hi = map {$_ => 1} @$hidden_items;
245 @items = grep { !$hi{$_->{itemnumber}} } @items;
248 my $shelflocations = GetKohaAuthorisedValues('items.location',GetFrameworkCode($biblionumber), 'opac');
249 my $ccodes = GetKohaAuthorisedValues('items.ccode',GetFrameworkCode($biblionumber), 'opac');
251 my $branches = GetBranches();
252 my $itemtypes = GetItemTypes();
256 for my $item (@items) {
259 my ( $transfertwhen, $transfertfrom, $transfertto ) = C4::Circulation::GetTransfers($item->{itemnumber});
261 my $reservestatus = C4::Reserves::GetReserveStatus( $item->{itemnumber} );
263 if ( $itemtypes->{ $item->{itype} }->{notforloan} || $item->{notforloan} || $item->{onloan} || $item->{withdrawn} || $item->{itemlost} || $item->{damaged} ||
264 (defined $transfertwhen && $transfertwhen ne '') || $item->{itemnotforloan} || (defined $reservestatus && $reservestatus eq "Waiting") ){
265 if ( $item->{notforloan} < 0) {
266 $status = "On order";
268 if ( $item->{itemnotforloan} > 0 || $item->{notforloan} > 0 || $itemtypes->{ $item->{itype} }->{notforloan} == 1 ) {
269 $status = "reference";
271 if ($item->{onloan}) {
272 $status = "Checked out";
274 if ( $item->{withdrawn}) {
275 $status = "Withdrawn";
277 if ($item->{itemlost}) {
280 if ($item->{damaged}) {
283 if (defined $transfertwhen && $transfertwhen ne '') {
284 $status = 'In transit';
286 if (defined $reservestatus && $reservestatus eq "Waiting") {
290 $status = "available";
292 my $homebranch = $item->{homebranch}? xml_escape($branches->{$item->{homebranch}}->{'branchname'}):'';
293 my $holdingbranch = $item->{holdingbranch}? xml_escape($branches->{$item->{holdingbranch}}->{'branchname'}):'';
294 $location = $item->{location}? xml_escape($shelflocations->{$item->{location}}||$item->{location}):'';
295 $ccode = $item->{ccode}? xml_escape($ccodes->{$item->{ccode}}||$item->{ccode}):'';
296 my $itemcallnumber = xml_escape($item->{itemcallnumber});
299 . "<homebranch>$homebranch</homebranch>"
300 . "<holdingbranch>$holdingbranch</holdingbranch>"
301 . "<location>$location</location>"
302 . "<ccode>$ccode</ccode>"
303 . "<status>$status</status>"
304 . "<itemcallnumber>$itemcallnumber</itemcallnumber>"
307 $xml = "<items xmlns=\"http://www.koha-community.org/items\">".$xml."</items>";
313 Returns reference to XSLT handler object.
327 Joshua Ferraro <jmf@liblime.com>
329 Koha Development Team <http://koha-community.org/>