Using warn instead of die
[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 strict;
21 use warnings;
22
23 use C4::Context;
24 use C4::Branch;
25 use C4::Items;
26 use C4::Koha;
27 use C4::Biblio;
28 use C4::Circulation;
29 use Encode;
30 use XML::LibXML;
31 use XML::LibXSLT;
32
33 use vars qw($VERSION @ISA @EXPORT);
34
35 BEGIN {
36     require Exporter;
37     $VERSION = 0.03;
38     @ISA = qw(Exporter);
39     @EXPORT = qw(
40         &XSLTParse4Display
41     );
42 }
43
44 =head1 NAME
45
46 C4::XSLT - Functions for displaying XSLT-generated content
47
48 =head1 FUNCTIONS
49
50 =head1 transformMARCXML4XSLT
51
52 =head2 replaces codes with authorized values in a MARC::Record object
53
54 =cut
55
56 sub transformMARCXML4XSLT {
57     my ($biblionumber, $record) = @_;
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 $av = getAuthorisedValues4MARCSubfields($frameworkcode);
67     foreach my $tag ( keys %$av ) {
68         foreach my $field ( $record->field( $tag ) ) {
69             if ( $av->{ $tag } ) {
70                 my @new_subfields = ();
71                 for my $subfield ( $field->subfields() ) {
72                     my ( $letter, $value ) = @$subfield;
73                     $value = GetAuthorisedValueDesc( $tag, $letter, $value, '', $tagslib )
74                         if $av->{ $tag }->{ $letter };
75                     push( @new_subfields, $letter, $value );
76                 } 
77                 $field ->replace_with( MARC::Field->new(
78                     $tag,
79                     $field->indicator(1),
80                     $field->indicator(2),
81                     @new_subfields
82                 ) );
83             }
84         }
85     }
86     return $record;
87 }
88
89 =head1 getAuthorisedValues4MARCSubfields
90
91 =head2 returns an ref of hash of ref of hash for tag -> letter controled bu authorised values
92
93 =cut
94
95 # Cache for tagfield-tagsubfield to decode per framework.
96 # Should be preferably be placed in Koha-core...
97 my %authval_per_framework;
98
99 sub getAuthorisedValues4MARCSubfields {
100     my ($frameworkcode) = @_;
101     unless ( $authval_per_framework{ $frameworkcode } ) {
102         my $dbh = C4::Context->dbh;
103         my $sth = $dbh->prepare("SELECT DISTINCT tagfield, tagsubfield
104                                  FROM marc_subfield_structure
105                                  WHERE authorised_value IS NOT NULL
106                                    AND authorised_value!=''
107                                    AND frameworkcode=?");
108         $sth->execute( $frameworkcode );
109         my $av = { };
110         while ( my ( $tag, $letter ) = $sth->fetchrow() ) {
111             $av->{ $tag }->{ $letter } = 1;
112         }
113         $authval_per_framework{ $frameworkcode } = $av;
114     }
115     return $authval_per_framework{ $frameworkcode };
116 }
117
118 my $stylesheet;
119
120 sub XSLTParse4Display {
121     my ( $biblionumber, $orig_record, $xsl_suffix ) = @_;
122     # grab the XML, run it through our stylesheet, push it out to the browser
123     my $record = transformMARCXML4XSLT($biblionumber, $orig_record);
124     my $itemsxml  = buildKohaItemsNamespace($biblionumber);
125     my $xmlrecord = $record->as_xml();
126     $xmlrecord =~ s/\<\/record\>/$itemsxml\<\/record\>/;
127     my $parser = XML::LibXML->new();
128     # don't die when you find &, >, etc
129     $parser->recover_silently(1);
130     my $source = $parser->parse_string($xmlrecord);
131     unless ( $stylesheet ) {
132         my $xslt = XML::LibXSLT->new();
133         my $xslfile = C4::Context->config('opachtdocs') . 
134                       "/prog/en/xslt/" .
135                       C4::Context->preference('marcflavour') .
136                       "slim2OPAC$xsl_suffix.xsl";
137         my $style_doc = $parser->parse_file($xslfile);
138         $stylesheet = $xslt->parse_stylesheet($style_doc);
139     }
140     my $results = $stylesheet->transform($source);
141     my $newxmlrecord = $stylesheet->output_string($results);
142     return $newxmlrecord;
143 }
144
145 sub buildKohaItemsNamespace {
146     my ($biblionumber) = @_;
147     my @items = C4::Items::GetItemsInfo($biblionumber);
148     my $branches = GetBranches();
149     my $itemtypes = GetItemTypes();
150     my $xml = '';
151     for my $item (@items) {
152         my $status;
153
154         my ( $transfertwhen, $transfertfrom, $transfertto ) = C4::Circulation::GetTransfers($item->{itemnumber});
155
156         if ( $itemtypes->{ $item->{itype} }->{notforloan} || $item->{notforloan} || $item->{onloan} || $item->{wthdrawn} || $item->{itemlost} || $item->{damaged} ||
157              (defined $transfertwhen && $transfertwhen ne '') || $item->{itemnotforloan} ) {
158             if ( $item->{notforloan} < 0) {
159                 $status = "On order";
160             } 
161             if ( $item->{itemnotforloan} > 0 || $item->{notforloan} > 0 || $itemtypes->{ $item->{itype} }->{notforloan} == 1 ) {
162                 $status = "reference";
163             }
164             if ($item->{onloan}) {
165                 $status = "Checked out";
166             }
167             if ( $item->{wthdrawn}) {
168                 $status = "Withdrawn";
169             }
170             if ($item->{itemlost}) {
171                 $status = "Lost";
172             }
173             if ($item->{damaged}) {
174                 $status = "Damaged"; 
175             }
176             if (defined $transfertwhen && $transfertwhen ne '') {
177                 $status = 'In transit';
178             }
179         } else {
180             $status = "available";
181         }
182         my $homebranch = $branches->{$item->{homebranch}}->{'branchname'};
183         $xml.= "<item><homebranch>$homebranch</homebranch>".
184                 "<status>$status</status>".
185                 (defined $item->{'itemcallnumber'} ? "<itemcallnumber>".$item->{'itemcallnumber'}."</itemcallnumber>" 
186                                            : "<itemcallnumber />")
187         . "</item>";
188
189     }
190     $xml = "<items xmlns=\"http://www.koha.org/items\">".$xml."</items>";
191     return $xml;
192 }
193
194
195
196 1;
197 __END__
198
199 =head1 NOTES
200
201 =head1 AUTHOR
202
203 Joshua Ferraro <jmf@liblime.com>
204
205 =cut