2 # Copyright (C) 2006 LibLime
3 # <jmf at liblime dot com>
4 # Parts Copyright Katrin Fischer 2011
5 # Parts Copyright ByWater Solutions 2011
6 # Parts Copyright Biblibre 2012
8 # This file is part of Koha.
10 # Koha is free software; you can redistribute it and/or modify it under the
11 # terms of the GNU General Public License as published by the Free Software
12 # Foundation; either version 2 of the License, or (at your option) any later
15 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
16 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License along
20 # with Koha; if not, write to the Free Software Foundation, Inc.,
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
38 use vars qw($VERSION @ISA @EXPORT);
52 C4::XSLT - Functions for displaying XSLT-generated content
58 GetURI file and returns the xslt as a string
69 =head2 transformMARCXML4XSLT
71 Replaces codes with authorized values in a MARC::Record object
75 sub transformMARCXML4XSLT {
76 my ($biblionumber, $record) = @_;
77 my $frameworkcode = GetFrameworkCode($biblionumber) || '';
78 my $tagslib = &GetMarcStructure(1,$frameworkcode);
80 # FIXME: wish there was a better way to handle exceptions
82 @fields = $record->fields();
84 if ($@) { warn "PROBLEM WITH RECORD"; next; }
85 my $av = getAuthorisedValues4MARCSubfields($frameworkcode);
86 foreach my $tag ( keys %$av ) {
87 foreach my $field ( $record->field( $tag ) ) {
88 if ( $av->{ $tag } ) {
89 my @new_subfields = ();
90 for my $subfield ( $field->subfields() ) {
91 my ( $letter, $value ) = @$subfield;
92 $value = GetAuthorisedValueDesc( $tag, $letter, $value, '', $tagslib )
93 if $av->{ $tag }->{ $letter };
94 push( @new_subfields, $letter, $value );
96 $field ->replace_with( MARC::Field->new(
108 =head2 getAuthorisedValues4MARCSubfields
110 Returns a ref of hash of ref of hash for tag -> letter controled by authorised values
114 # Cache for tagfield-tagsubfield to decode per framework.
115 # Should be preferably be placed in Koha-core...
116 my %authval_per_framework;
118 sub getAuthorisedValues4MARCSubfields {
119 my ($frameworkcode) = @_;
120 unless ( $authval_per_framework{ $frameworkcode } ) {
121 my $dbh = C4::Context->dbh;
122 my $sth = $dbh->prepare("SELECT DISTINCT tagfield, tagsubfield
123 FROM marc_subfield_structure
124 WHERE authorised_value IS NOT NULL
125 AND authorised_value!=''
126 AND frameworkcode=?");
127 $sth->execute( $frameworkcode );
129 while ( my ( $tag, $letter ) = $sth->fetchrow() ) {
130 $av->{ $tag }->{ $letter } = 1;
132 $authval_per_framework{ $frameworkcode } = $av;
134 return $authval_per_framework{ $frameworkcode };
139 sub XSLTParse4Display {
140 my ( $biblionumber, $orig_record, $xslsyspref, $fixamps, $hidden_items ) = @_;
141 my $xslfilename = C4::Context->preference($xslsyspref);
142 if ( $xslfilename =~ /^\s*"?default"?\s*$/i ) {
143 if ($xslsyspref eq "XSLTDetailsDisplay") {
144 $xslfilename = C4::Context->config('intrahtdocs') .
145 '/' . C4::Context->preference("template") .
146 '/' . C4::Templates::_current_language() .
148 C4::Context->preference('marcflavour') .
149 "slim2intranetDetail.xsl";
150 } elsif ($xslsyspref eq "XSLTResultsDisplay") {
151 $xslfilename = C4::Context->config('intrahtdocs') .
152 '/' . C4::Context->preference("template") .
153 '/' . C4::Templates::_current_language() .
155 C4::Context->preference('marcflavour') .
156 "slim2intranetResults.xsl";
157 } elsif ($xslsyspref eq "OPACXSLTDetailsDisplay") {
158 $xslfilename = C4::Context->config('opachtdocs') .
159 '/' . C4::Context->preference("opacthemes") .
160 '/' . C4::Templates::_current_language() .
162 C4::Context->preference('marcflavour') .
163 "slim2OPACDetail.xsl";
164 } elsif ($xslsyspref eq "OPACXSLTResultsDisplay") {
165 $xslfilename = C4::Context->config('opachtdocs') .
166 '/' . C4::Context->preference("opacthemes") .
167 '/' . C4::Templates::_current_language() .
169 C4::Context->preference('marcflavour') .
170 "slim2OPACResults.xsl";
174 if ( $xslfilename =~ m/\{langcode\}/ ) {
175 my $lang = C4::Templates::_current_language;
176 $xslfilename =~ s/\{langcode\}/$lang/;
179 # grab the XML, run it through our stylesheet, push it out to the browser
180 my $record = transformMARCXML4XSLT($biblionumber, $orig_record);
181 #return $record->as_formatted();
182 my $itemsxml = buildKohaItemsNamespace($biblionumber, $hidden_items);
183 my $xmlrecord = $record->as_xml(C4::Context->preference('marcflavour'));
184 my $sysxml = "<sysprefs>\n";
185 foreach my $syspref ( qw/ hidelostitems OPACURLOpenInNewWindow
186 DisplayOPACiconsXSLT URLLinkText viewISBD
187 OPACBaseURL TraceCompleteSubfields UseICU
188 UseAuthoritiesForTracings TraceSubjectSubdivisions
189 Display856uAsImage OPACDisplay856uAsImage
191 AlternateHoldingsField AlternateHoldingsSeparator / )
193 my $sp = C4::Context->preference( $syspref );
194 next unless defined($sp);
195 $sysxml .= "<syspref name=\"$syspref\">$sp</syspref>\n";
197 $sysxml .= "</sysprefs>\n";
198 $xmlrecord =~ s/\<\/record\>/$itemsxml$sysxml\<\/record\>/;
199 if ($fixamps) { # We need to correct the ampersand entities that Zebra outputs
200 $xmlrecord =~ s/\&amp;/\&/g;
202 $xmlrecord =~ s/\& /\&\; /;
203 $xmlrecord =~ s/\&\;amp\; /\&\; /;
205 my $parser = XML::LibXML->new();
206 # don't die when you find &, >, etc
207 $parser->recover_silently(0);
208 my $source = $parser->parse_string($xmlrecord);
209 unless ( $stylesheet->{$xslfilename} ) {
210 my $xslt = XML::LibXSLT->new();
212 if ( $xslfilename =~ /^https?:\/\// ) {
213 my $xsltstring = GetURI($xslfilename);
214 $style_doc = $parser->parse_string($xsltstring);
217 $style_doc = $parser->parse_file($xslfilename);
219 $stylesheet->{$xslfilename} = $xslt->parse_stylesheet($style_doc);
221 my $results = $stylesheet->{$xslfilename}->transform($source);
222 my $newxmlrecord = $stylesheet->{$xslfilename}->output_string($results);
223 return $newxmlrecord;
226 sub buildKohaItemsNamespace {
227 my ($biblionumber, $hidden_items) = @_;
229 my @items = C4::Items::GetItemsInfo($biblionumber);
230 if ($hidden_items && @$hidden_items) {
231 my %hi = map {$_ => 1} @$hidden_items;
232 @items = grep { !$hi{$_->{itemnumber}} } @items;
234 my $branches = GetBranches();
235 my $itemtypes = GetItemTypes();
237 for my $item (@items) {
240 my ( $transfertwhen, $transfertfrom, $transfertto ) = C4::Circulation::GetTransfers($item->{itemnumber});
242 my ( $reservestatus, $reserveitem, undef ) = C4::Reserves::CheckReserves($item->{itemnumber});
244 if ( $itemtypes->{ $item->{itype} }->{notforloan} || $item->{notforloan} || $item->{onloan} || $item->{wthdrawn} || $item->{itemlost} || $item->{damaged} ||
245 (defined $transfertwhen && $transfertwhen ne '') || $item->{itemnotforloan} || (defined $reservestatus && $reservestatus eq "Waiting") ){
246 if ( $item->{notforloan} < 0) {
247 $status = "On order";
249 if ( $item->{itemnotforloan} > 0 || $item->{notforloan} > 0 || $itemtypes->{ $item->{itype} }->{notforloan} == 1 ) {
250 $status = "reference";
252 if ($item->{onloan}) {
253 $status = "Checked out";
255 if ( $item->{wthdrawn}) {
256 $status = "Withdrawn";
258 if ($item->{itemlost}) {
261 if ($item->{damaged}) {
264 if (defined $transfertwhen && $transfertwhen ne '') {
265 $status = 'In transit';
267 if (defined $reservestatus && $reservestatus eq "Waiting") {
271 $status = "available";
273 my $homebranch = $item->{homebranch}? xml_escape($branches->{$item->{homebranch}}->{'branchname'}):'';
274 my $itemcallnumber = xml_escape($item->{itemcallnumber});
275 $xml.= "<item><homebranch>$homebranch</homebranch>".
276 "<status>$status</status>".
277 "<itemcallnumber>".$itemcallnumber."</itemcallnumber>"
281 $xml = "<items xmlns=\"http://www.koha-community.org/items\">".$xml."</items>";
296 Joshua Ferraro <jmf@liblime.com>