Optimize XSLT.pm
[koha.git] / C4 / XSLT.pm
1 package C4::XSLT;
2 # Copyright (C) 2006 LibLime
3 # <jmf at liblime dot com>
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
15 #
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
19
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;
28
29 use strict;
30
31 use vars qw($VERSION @ISA @EXPORT);
32
33 BEGIN {
34     require Exporter;
35     $VERSION = 0.03;
36     @ISA = qw(Exporter);
37     @EXPORT = qw(
38         &XSLTParse4Display
39     );
40 }
41
42 =head1 NAME
43
44 C4::XSLT - Functions for displaying XSLT-generated content
45
46 =head1 FUNCTIONS
47
48 =head1 transformMARCXML4XSLT
49
50 =head2 replaces codes with authorized values in a MARC::Record object
51
52 =cut
53
54 sub transformMARCXML4XSLT {
55     my ($biblionumber, $orig_record) = @_;
56     my $record = $orig_record->clone(); # not updating original record; this may be unnecessarily paranoid
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();
64     };
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 );
77                 }
78             }
79             my $newField = MARC::Field->new(
80                 $authvalue->{tagfield},
81                 $field->indicator(1),
82                 $field->indicator(2),
83                 $authvalue->{tagsubfield} => @newSubfields
84             );
85             $field->replace_with($newField);
86         }
87     }
88     return $record;
89 }
90
91 =head1 getAuthorisedValues4MARCSubfields
92
93 =head2 returns an array of hash refs for authorised value tag/subfield combos for a given framework
94
95 =cut
96
97 # Cache for tagfield-tagsubfield to decode per framework.
98 # Should be preferably be placed in Koha-core...
99 my %authval_per_framework;
100
101 sub getAuthorisedValues4MARCSubfields {
102     my ($frameworkcode) = @_;
103     unless ( $authval_per_framework{ $frameworkcode } ) {
104         my @results;
105         my $dbh = C4::Context->dbh;
106         my $sth = $dbh->prepare("SELECT DISTINCT tagfield, tagsubfield
107                                  FROM marc_subfield_structure
108                                  WHERE authorised_value IS NOT NULL
109                                    AND authorised_value!=''
110                                    AND frameworkcode=?");
111         $sth->execute( $frameworkcode );
112         while ( my $result = $sth->fetchrow_hashref() ) {
113             push ( @results, $result );
114         }
115         $authval_per_framework{ $frameworkcode } = \@results;
116     }
117     return $authval_per_framework{ $frameworkcode };
118 }
119
120 my $stylesheet;
121
122 sub XSLTParse4Display {
123     my ($biblionumber, $orig_record, $xslfile) = @_;
124     # grab the XML, run it through our stylesheet, push it out to the browser
125     my $record = transformMARCXML4XSLT($biblionumber, $orig_record);
126     my $itemsxml  = buildKohaItemsNamespace($biblionumber);
127     my $xmlrecord = $record->as_xml();
128     $xmlrecord =~ s/\<\/record\>/$itemsxml\<\/record\>/;
129     my $parser = XML::LibXML->new();
130     # don't die when you find &, >, etc
131     $parser->recover_silently(1);
132     my $source = $parser->parse_string($xmlrecord);
133     unless ( $stylesheet ) {
134         my $xslt = XML::LibXSLT->new();
135         my $style_doc = $parser->parse_file($xslfile);
136         $stylesheet = $xslt->parse_stylesheet($style_doc);
137     }
138     my $results = $stylesheet->transform($source);
139     my $newxmlrecord = $stylesheet->output_string($results);
140     return $newxmlrecord;
141 }
142
143 sub buildKohaItemsNamespace {
144     my ($biblionumber) = @_;
145     my @items = C4::Items::GetItemsInfo($biblionumber);
146     my $branches = GetBranches();
147     my $itemtypes = GetItemTypes();
148
149     my $xml;
150     for my $item (@items) {
151         my $status;
152
153         my ( $transfertwhen, $transfertfrom, $transfertto ) = C4::Circulation::GetTransfers($item->{itemnumber});
154
155         if ( $itemtypes->{ $item->{itype} }->{notforloan} == 1 || $item->{notforloan} || $item->{onloan} || $item->{wthdrawn} || $item->{itemlost} || $item->{damaged} ||
156              ($transfertwhen ne '') || $item->{itemnotforloan} ) {
157             if ( $item->{notforloan} < 0) {
158                 $status = "On order";
159             } 
160             if ( $item->{itemnotforloan} > 0 || $item->{notforloan} > 0 || $itemtypes->{ $item->{itype} }->{notforloan} == 1 ) {
161                 $status = "reference";
162             }
163             if ($item->{onloan}) {
164                 $status = "Checked out";
165             }
166             if ( $item->{wthdrawn}) {
167                 $status = "Withdrawn";
168             }
169             if ($item->{itemlost}) {
170                 $status = "Lost";
171             }
172             if ($item->{damaged}) {
173                 $status = "Damaged"; 
174             }
175             if ($transfertwhen ne '') {
176                 $status = 'In transit';
177             }
178         } else {
179             $status = "available";
180         }
181         $xml.="<item><homebranch>".$branches->{$item->{homebranch}}->{'branchname'}."</homebranch>".
182                 "<status>$status</status>".
183                 "<itemcallnumber>".$item->{'itemcallnumber'}."</itemcallnumber></item>";
184
185     }
186     return "<items xmlns='http://www.koha.org/items'>".$xml."</items>";
187 }
188
189
190
191 1;
192 __END__
193
194 =head1 NOTES
195
196 =head1 AUTHOR
197
198 Joshua Ferraro <jmf@liblime.com>
199
200 =cut