avoid crashes if record in result set is corrupted
[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 XML::LibXML;
26 use XML::LibXSLT;
27
28 use strict;
29
30 use vars qw($VERSION @ISA @EXPORT);
31
32 BEGIN {
33     require Exporter;
34     $VERSION = 0.03;
35     @ISA = qw(Exporter);
36     @EXPORT = qw(
37         &XSLTParse4Display
38     );
39 }
40
41 =head1 NAME
42
43 C4::XSLT - Functions for displaying XSLT-generated content
44
45 =head1 FUNCTIONS
46
47 =head1 transformMARCXML4XSLT
48
49 =head2 replaces codes with authorized values in a MARCXML record
50
51 =cut
52
53 sub transformMARCXML4XSLT {
54     my ($biblionumber) = @_;
55     my $record = GetMarcBiblio($biblionumber);
56     my $biblio = GetBiblioData($biblionumber);
57     my $frameworkcode = GetFrameworkCode($biblionumber);
58     my $tagslib = &GetMarcStructure(1,$frameworkcode);
59     my @fields;
60     # FIXME: wish there was a better way to handle exceptions
61     eval {
62         @fields = $record->fields();
63     };
64     if ($@) { warn "PROBLEM WITH RECORD"; next; }
65     my $list_of_authvalues = getAuthorisedValues4MARCSubfields($frameworkcode);
66     for my $authvalue (@$list_of_authvalues) {
67         for my $field ( $record->field($authvalue->{tagfield}) ) {
68             my @newSubfields = ();
69             for my $subfield ( $field->subfields() ) {
70                 my ($code,$data) = @$subfield;
71                 unless ($code eq $authvalue->{tagsubfield}) {
72                     push ( @newSubfields, $code, $data );
73                 } else {
74                     my $newvalue = GetAuthorisedValueDesc( $authvalue->{tagfield}, $code, $data, '', $tagslib );
75                     push ( @newSubfields, $code, $newvalue );
76                 }
77             }
78             my $newField = MARC::Field->new(
79                 $authvalue->{tagfield},
80                 $field->indicator(1),
81                 $field->indicator(2),
82                 $authvalue->{tagsubfield} => @newSubfields
83             );
84             $field->replace_with($newField);
85         }
86     }
87     return $record;
88 }
89
90 =head1 getAuthorisedValues4MARCSubfields
91
92 =head2 returns an array of hash refs for authorised value tag/subfield combos for a given framework
93
94 =cut
95
96 sub getAuthorisedValues4MARCSubfields {
97     my ($frameworkcode) = @_;
98     my @results;
99     my $dbh = C4::Context->dbh;
100     my $sth = $dbh->prepare("SELECT DISTINCT tagfield,tagsubfield FROM marc_subfield_structure WHERE authorised_value IS NOT NULL AND authorised_value!='' AND frameworkcode=?");
101     $sth->execute($frameworkcode);
102     while (my $result = $sth->fetchrow_hashref()) {
103         push @results, $result;
104     }
105     return \@results;
106 }
107
108 sub XSLTParse4Display {
109     my ($biblionumber,$xslfile) = @_;
110     # grab the XML, run it through our stylesheet, push it out to the browser
111     my $record = transformMARCXML4XSLT($biblionumber);
112     my $itemsxml  = buildKohaItemsNamespace($biblionumber);
113     my $xmlrecord = $record->as_xml();
114     $xmlrecord =~ s/\<\/record\>/$itemsxml\<\/record\>/;
115     my $parser = XML::LibXML->new();
116     # don't die when you find &, >, etc
117     $parser->recover_silently(1);
118     my $xslt = XML::LibXSLT->new();
119     my $source = $parser->parse_string($xmlrecord);
120     my $style_doc = $parser->parse_file($xslfile);
121     my $stylesheet = $xslt->parse_stylesheet($style_doc);
122     my $results = $stylesheet->transform($source);
123     my $newxmlrecord = $stylesheet->output_string($results);
124     return $newxmlrecord;
125 }
126
127 sub buildKohaItemsNamespace {
128     my ($biblionumber) = @_;
129     my @items = C4::Items::GetItemsInfo($biblionumber);
130     my $branches = GetBranches();
131     my $itemtypes = GetItemTypes();
132     my $xml;
133     for my $item (@items) {
134         my $status;
135         if ( $item->{notforloan} == -1 || $item->{onloan} || $item->{wthdrawn} || $item->{itemlost} || $item->{damaged}) {
136             if ( $item->{notforloan} == -1) {
137                 $status = "On order";
138             }
139             if ($item->{onloan}) {
140                 $status = "On loan";
141             }
142             if ( $item->{wthdrawn}) {
143                 $status = "Withdrawn";
144             }
145             if ($item->{itemlost}) {
146                 $status = "Lost";
147             }
148             if ($item->{damaged}) {
149                 $status = "Damaged"; 
150             }
151         } else {
152             $status = "available";
153         }
154         $xml.="<item><homebranch>".$branches->{$item->{homebranch}}->{'branchname'}."</homebranch>"."<status>$status</status></item>";
155     }
156     return "<items xmlns='http://www.koha.org/items'>".$xml."</items>";
157 }
158
159
160
161 1;
162 __END__
163
164 =head1 NOTES
165
166 =head1 AUTHOR
167
168 Joshua Ferraro <jmf@liblime.com>
169
170 =cut