0fbfda159baa4d0796a6586ec23403d72384a66d
[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 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;
105     }
106     return \@results;
107 }
108
109 my $stylesheet;
110
111 sub XSLTParse4Display {
112     my ($biblionumber, $orig_record, $xslfile) = @_;
113     # grab the XML, run it through our stylesheet, push it out to the browser
114     my $record = transformMARCXML4XSLT($biblionumber, $orig_record);
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);
126     }
127     my $results = $stylesheet->transform($source);
128     my $newxmlrecord = $stylesheet->output_string($results);
129     return $newxmlrecord;
130 }
131
132 sub buildKohaItemsNamespace {
133     my ($biblionumber) = @_;
134     my @items = C4::Items::GetItemsInfo($biblionumber);
135     my $branches = GetBranches();
136     my $itemtypes = GetItemTypes();
137
138     my $xml;
139     for my $item (@items) {
140         my $status;
141
142         my ( $transfertwhen, $transfertfrom, $transfertto ) = C4::Circulation::GetTransfers($item->{itemnumber});
143
144         if ( $itemtypes->{ $item->{itype} }->{notforloan} == 1 || $item->{notforloan} || $item->{onloan} || $item->{wthdrawn} || $item->{itemlost} || $item->{damaged} ||
145              ($transfertwhen ne '') || $item->{itemnotforloan} ) {
146             if ( $item->{notforloan} < 0) {
147                 $status = "On order";
148             } 
149             if ( $item->{itemnotforloan} > 0 || $item->{notforloan} > 0 || $itemtypes->{ $item->{itype} }->{notforloan} == 1 ) {
150                 $status = "reference";
151             }
152             if ($item->{onloan}) {
153                 $status = "Checked out";
154             }
155             if ( $item->{wthdrawn}) {
156                 $status = "Withdrawn";
157             }
158             if ($item->{itemlost}) {
159                 $status = "Lost";
160             }
161             if ($item->{damaged}) {
162                 $status = "Damaged"; 
163             }
164             if ($transfertwhen ne '') {
165                 $status = 'In transit';
166             }
167         } else {
168             $status = "available";
169         }
170         $xml.="<item><homebranch>".$branches->{$item->{homebranch}}->{'branchname'}."</homebranch>".
171                 "<status>$status</status>".
172                 "<itemcallnumber>".$item->{'itemcallnumber'}."</itemcallnumber></item>";
173
174     }
175     return "<items xmlns='http://www.koha.org/items'>".$xml."</items>";
176 }
177
178
179
180 1;
181 __END__
182
183 =head1 NOTES
184
185 =head1 AUTHOR
186
187 Joshua Ferraro <jmf@liblime.com>
188
189 =cut