Avoid XSLT stylesheet building for each biblio record to transform
[koha.git] / C4 / XSLT.pm
blob1bea9f1aaf63c782818764e5e4cf4ed126840411
1 package C4::XSLT;
2 # Copyright (C) 2006 LibLime
3 # <jmf at liblime dot com>
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA 02111-1307 USA
20 use C4::Context;
21 use C4::Branch;
22 use C4::Items;
23 use C4::Koha;
24 use C4::Biblio;
25 use C4::Circulation;
26 use XML::LibXML;
27 use XML::LibXSLT;
29 use strict;
31 use vars qw($VERSION @ISA @EXPORT);
33 BEGIN {
34 require Exporter;
35 $VERSION = 0.03;
36 @ISA = qw(Exporter);
37 @EXPORT = qw(
38 &XSLTParse4Display
42 =head1 NAME
44 C4::XSLT - Functions for displaying XSLT-generated content
46 =head1 FUNCTIONS
48 =head1 transformMARCXML4XSLT
50 =head2 replaces codes with authorized values in a MARCXML record
52 =cut
54 sub transformMARCXML4XSLT {
55 my ($biblionumber) = @_;
56 my $record = GetMarcBiblio($biblionumber);
57 my $biblio = GetBiblioData($biblionumber);
58 my $frameworkcode = GetFrameworkCode($biblionumber);
59 my $tagslib = &GetMarcStructure(1,$frameworkcode);
60 my @fields;
61 # FIXME: wish there was a better way to handle exceptions
62 eval {
63 @fields = $record->fields();
65 if ($@) { warn "PROBLEM WITH RECORD"; next; }
66 my $list_of_authvalues = getAuthorisedValues4MARCSubfields($frameworkcode);
67 for my $authvalue (@$list_of_authvalues) {
68 for my $field ( $record->field($authvalue->{tagfield}) ) {
69 my @newSubfields = ();
70 for my $subfield ( $field->subfields() ) {
71 my ($code,$data) = @$subfield;
72 unless ($code eq $authvalue->{tagsubfield}) {
73 push ( @newSubfields, $code, $data );
74 } else {
75 my $newvalue = GetAuthorisedValueDesc( $authvalue->{tagfield}, $code, $data, '', $tagslib );
76 push ( @newSubfields, $code, $newvalue );
79 my $newField = MARC::Field->new(
80 $authvalue->{tagfield},
81 $field->indicator(1),
82 $field->indicator(2),
83 $authvalue->{tagsubfield} => @newSubfields
85 $field->replace_with($newField);
88 return $record;
91 =head1 getAuthorisedValues4MARCSubfields
93 =head2 returns an array of hash refs for authorised value tag/subfield combos for a given framework
95 =cut
97 sub getAuthorisedValues4MARCSubfields {
98 my ($frameworkcode) = @_;
99 my @results;
100 my $dbh = C4::Context->dbh;
101 my $sth = $dbh->prepare("SELECT DISTINCT tagfield,tagsubfield FROM marc_subfield_structure WHERE authorised_value IS NOT NULL AND authorised_value!='' AND frameworkcode=?");
102 $sth->execute($frameworkcode);
103 while (my $result = $sth->fetchrow_hashref()) {
104 push @results, $result;
106 return \@results;
109 my $stylesheet;
111 sub XSLTParse4Display {
112 my ($biblionumber,$xslfile) = @_;
113 # grab the XML, run it through our stylesheet, push it out to the browser
114 my $record = transformMARCXML4XSLT($biblionumber);
115 my $itemsxml = buildKohaItemsNamespace($biblionumber);
116 my $xmlrecord = $record->as_xml();
117 $xmlrecord =~ s/\<\/record\>/$itemsxml\<\/record\>/;
118 my $parser = XML::LibXML->new();
119 # don't die when you find &, >, etc
120 $parser->recover_silently(1);
121 my $source = $parser->parse_string($xmlrecord);
122 unless ( $stylesheet ) {
123 my $xslt = XML::LibXSLT->new();
124 my $style_doc = $parser->parse_file($xslfile);
125 $stylesheet = $xslt->parse_stylesheet($style_doc);
127 my $results = $stylesheet->transform($source);
128 my $newxmlrecord = $stylesheet->output_string($results);
129 return $newxmlrecord;
132 sub buildKohaItemsNamespace {
133 my ($biblionumber) = @_;
134 my @items = C4::Items::GetItemsInfo($biblionumber);
135 my $branches = GetBranches();
136 my $itemtypes = GetItemTypes();
137 my $xml;
138 for my $item (@items) {
139 my $status;
140 my ( $transfertwhen, $transfertfrom, $transfertto ) = C4::Circulation::GetTransfers($item->{itemnumber});
141 if ( $item->{notforloan} == -1 || $item->{onloan} || $item->{wthdrawn} || $item->{itemlost} || $item->{damaged} ||
142 ($transfertwhen ne '') || $item->{itemnotforloan} ) {
143 if ( $item->{notforloan} == -1) {
144 $status = "On order";
146 if ( $item->{itemnotforloan} ) {
147 $status = "Not for loan";
149 if ($item->{onloan}) {
150 $status = "Checked out";
152 if ( $item->{wthdrawn}) {
153 $status = "Withdrawn";
155 if ($item->{itemlost}) {
156 $status = "Lost";
158 if ($item->{damaged}) {
159 $status = "Damaged";
161 if ($transfertwhen ne '') {
162 $status = 'In transit';
164 } else {
165 $status = "available";
167 $xml.="<item><homebranch>".$branches->{$item->{homebranch}}->{'branchname'}."</homebranch>".
168 "<status>$status</status>".
169 "<itemcallnumber>".$item->{'itemcallnumber'}."</itemcallnumber></item>";
172 return "<items xmlns='http://www.koha.org/items'>".$xml."</items>";
178 __END__
180 =head1 NOTES
182 =head1 AUTHOR
184 Joshua Ferraro <jmf@liblime.com>
186 =cut