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($VERSION @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...
48 $VERSION = 3.07.00.049;
53 $engine=Koha::XSLT_Handler->new( { do_not_return_source => 1 } );
58 C4::XSLT - Functions for displaying XSLT-generated content
62 =head2 transformMARCXML4XSLT
64 Replaces codes with authorized values in a MARC::Record object
65 Is only used in this module currently.
69 sub transformMARCXML4XSLT {
70 my ($biblionumber, $record) = @_;
71 my $frameworkcode = GetFrameworkCode($biblionumber) || '';
72 my $tagslib = &GetMarcStructure(1,$frameworkcode);
74 # FIXME: wish there was a better way to handle exceptions
76 @fields = $record->fields();
78 if ($@) { warn "PROBLEM WITH RECORD"; next; }
79 my $av = getAuthorisedValues4MARCSubfields($frameworkcode);
80 foreach my $tag ( keys %$av ) {
81 foreach my $field ( $record->field( $tag ) ) {
82 if ( $av->{ $tag } ) {
83 my @new_subfields = ();
84 for my $subfield ( $field->subfields() ) {
85 my ( $letter, $value ) = @$subfield;
86 $value = GetAuthorisedValueDesc( $tag, $letter, $value, '', $tagslib )
87 if $av->{ $tag }->{ $letter };
88 push( @new_subfields, $letter, $value );
90 $field ->replace_with( MARC::Field->new(
102 =head2 getAuthorisedValues4MARCSubfields
104 Returns a ref of hash of ref of hash for tag -> letter controlled by authorised values
105 Is only used in this module currently.
109 sub getAuthorisedValues4MARCSubfields {
110 my ($frameworkcode) = @_;
111 unless ( $authval_per_framework{ $frameworkcode } ) {
112 my $dbh = C4::Context->dbh;
113 my $sth = $dbh->prepare("SELECT DISTINCT tagfield, tagsubfield
114 FROM marc_subfield_structure
115 WHERE authorised_value IS NOT NULL
116 AND authorised_value!=''
117 AND frameworkcode=?");
118 $sth->execute( $frameworkcode );
120 while ( my ( $tag, $letter ) = $sth->fetchrow() ) {
121 $av->{ $tag }->{ $letter } = 1;
123 $authval_per_framework{ $frameworkcode } = $av;
125 return $authval_per_framework{ $frameworkcode };
128 =head2 XSLTParse4Display
130 Returns xml for biblionumber and requested XSLT transformation.
131 Returns undef if the transform fails.
133 Used in OPAC results and detail, intranet results and detail, list display.
134 (Depending on the settings of your XSLT preferences.)
136 The helper function _get_best_default_xslt_filename is used in a unit test.
140 sub _get_best_default_xslt_filename {
141 my ($htdocs, $theme, $lang, $base_xslfile) = @_;
144 "$htdocs/$theme/$lang/xslt/${base_xslfile}", # exact match
145 "$htdocs/$theme/en/xslt/${base_xslfile}", # if not, preferred theme in English
146 "$htdocs/prog/$lang/xslt/${base_xslfile}", # if not, 'prog' theme in preferred language
147 "$htdocs/prog/en/xslt/${base_xslfile}", # otherwise, prog theme in English; should always
151 foreach my $filename (@candidates) {
152 $xslfilename = $filename;
154 last; # we have a winner!
160 sub XSLTParse4Display {
161 my ( $biblionumber, $orig_record, $xslsyspref, $fixamps, $hidden_items ) = @_;
162 my $xslfilename = C4::Context->preference($xslsyspref);
163 if ( $xslfilename =~ /^\s*"?default"?\s*$/i ) {
166 my $lang = C4::Languages::getlanguage();
168 if ($xslsyspref eq "XSLTDetailsDisplay") {
169 $htdocs = C4::Context->config('intrahtdocs');
170 $theme = C4::Context->preference("template");
171 $xslfile = C4::Context->preference('marcflavour') .
172 "slim2intranetDetail.xsl";
173 } elsif ($xslsyspref eq "XSLTResultsDisplay") {
174 $htdocs = C4::Context->config('intrahtdocs');
175 $theme = C4::Context->preference("template");
176 $xslfile = C4::Context->preference('marcflavour') .
177 "slim2intranetResults.xsl";
178 } elsif ($xslsyspref eq "OPACXSLTDetailsDisplay") {
179 $htdocs = C4::Context->config('opachtdocs');
180 $theme = C4::Context->preference("opacthemes");
181 $xslfile = C4::Context->preference('marcflavour') .
182 "slim2OPACDetail.xsl";
183 } elsif ($xslsyspref eq "OPACXSLTResultsDisplay") {
184 $htdocs = C4::Context->config('opachtdocs');
185 $theme = C4::Context->preference("opacthemes");
186 $xslfile = C4::Context->preference('marcflavour') .
187 "slim2OPACResults.xsl";
189 $xslfilename = _get_best_default_xslt_filename($htdocs, $theme, $lang, $xslfile);
192 if ( $xslfilename =~ m/\{langcode\}/ ) {
193 my $lang = C4::Languages::getlanguage();
194 $xslfilename =~ s/\{langcode\}/$lang/;
197 # grab the XML, run it through our stylesheet, push it out to the browser
198 my $record = transformMARCXML4XSLT($biblionumber, $orig_record);
199 my $itemsxml = buildKohaItemsNamespace($biblionumber, $hidden_items);
200 my $xmlrecord = $record->as_xml(C4::Context->preference('marcflavour'));
201 my $sysxml = "<sysprefs>\n";
202 foreach my $syspref ( qw/ hidelostitems OPACURLOpenInNewWindow
203 DisplayOPACiconsXSLT URLLinkText viewISBD
204 OPACBaseURL TraceCompleteSubfields UseICU
205 UseAuthoritiesForTracings TraceSubjectSubdivisions
206 Display856uAsImage OPACDisplay856uAsImage
207 UseControlNumber IntranetBiblioDefaultView BiblioDefaultView
208 OPACItemLocation DisplayIconsXSLT
209 AlternateHoldingsField AlternateHoldingsSeparator
210 TrackClicks opacthemes IdRef / )
212 my $sp = C4::Context->preference( $syspref );
213 next unless defined($sp);
214 $sysxml .= "<syspref name=\"$syspref\">$sp</syspref>\n";
217 # singleBranchMode was a system preference, but no longer is
218 # we can retain it here for compatibility
219 my $singleBranchMode = Koha::Libraries->search->count == 1;
220 $sysxml .= "<syspref name=\"singleBranchMode\">$singleBranchMode</syspref>\n";
222 $sysxml .= "</sysprefs>\n";
223 $xmlrecord =~ s/\<\/record\>/$itemsxml$sysxml\<\/record\>/;
224 if ($fixamps) { # We need to correct the HTML entities that Zebra outputs
225 $xmlrecord =~ s/\&amp;/\&/g;
226 $xmlrecord =~ s/\&\;lt\;/\<\;/g;
227 $xmlrecord =~ s/\&\;gt\;/\>\;/g;
229 $xmlrecord =~ s/\& /\&\; /;
230 $xmlrecord =~ s/\&\;amp\; /\&\; /;
232 #If the xslt should fail, we will return undef (old behavior was
234 #Note that we did set do_not_return_source at object construction
235 return $engine->transform($xmlrecord, $xslfilename ); #file or URL
238 =head2 buildKohaItemsNamespace
240 Returns XML for items.
241 Is only used in this module currently.
245 sub buildKohaItemsNamespace {
246 my ($biblionumber, $hidden_items) = @_;
248 my @items = C4::Items::GetItemsInfo($biblionumber);
249 if ($hidden_items && @$hidden_items) {
250 my %hi = map {$_ => 1} @$hidden_items;
251 @items = grep { !$hi{$_->{itemnumber}} } @items;
254 my $shelflocations = GetKohaAuthorisedValues('items.location',GetFrameworkCode($biblionumber), 'opac');
255 my $ccodes = GetKohaAuthorisedValues('items.ccode',GetFrameworkCode($biblionumber), 'opac');
257 my $branches = GetBranches();
258 my $itemtypes = GetItemTypes();
262 for my $item (@items) {
265 my ( $transfertwhen, $transfertfrom, $transfertto ) = C4::Circulation::GetTransfers($item->{itemnumber});
267 my $reservestatus = C4::Reserves::GetReserveStatus( $item->{itemnumber} );
269 if ( $itemtypes->{ $item->{itype} }->{notforloan} || $item->{notforloan} || $item->{onloan} || $item->{withdrawn} || $item->{itemlost} || $item->{damaged} ||
270 (defined $transfertwhen && $transfertwhen ne '') || $item->{itemnotforloan} || (defined $reservestatus && $reservestatus eq "Waiting") ){
271 if ( $item->{notforloan} < 0) {
272 $status = "On order";
274 if ( $item->{itemnotforloan} > 0 || $item->{notforloan} > 0 || $itemtypes->{ $item->{itype} }->{notforloan} == 1 ) {
275 $status = "reference";
277 if ($item->{onloan}) {
278 $status = "Checked out";
280 if ( $item->{withdrawn}) {
281 $status = "Withdrawn";
283 if ($item->{itemlost}) {
286 if ($item->{damaged}) {
289 if (defined $transfertwhen && $transfertwhen ne '') {
290 $status = 'In transit';
292 if (defined $reservestatus && $reservestatus eq "Waiting") {
296 $status = "available";
298 my $homebranch = $item->{homebranch}? xml_escape($branches->{$item->{homebranch}}->{'branchname'}):'';
299 my $holdingbranch = $item->{holdingbranch}? xml_escape($branches->{$item->{holdingbranch}}->{'branchname'}):'';
300 $location = $item->{location}? xml_escape($shelflocations->{$item->{location}}||$item->{location}):'';
301 $ccode = $item->{ccode}? xml_escape($ccodes->{$item->{ccode}}||$item->{ccode}):'';
302 my $itemcallnumber = xml_escape($item->{itemcallnumber});
303 $xml.= "<item><homebranch>$homebranch</homebranch>".
304 "<holdingbranch>$holdingbranch</holdingbranch>".
305 "<location>$location</location>".
306 "<ccode>$ccode</ccode>".
307 "<status>$status</status>".
308 "<itemcallnumber>".$itemcallnumber."</itemcallnumber>".
311 $xml = "<items xmlns=\"http://www.koha-community.org/items\">".$xml."</items>";
317 Returns reference to XSLT handler object.
331 Joshua Ferraro <jmf@liblime.com>
333 Koha Development Team <http://koha-community.org/>