Bug Fixing : 3334
[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 $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                 @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 # Cache for tagfield-tagsubfield to decode per framework.
97 # Should be preferably be placed in Koha-core...
98 my %authval_per_framework;
99
100 sub getAuthorisedValues4MARCSubfields {
101     my ($frameworkcode) = @_;
102     unless ( $authval_per_framework{ $frameworkcode } ) {
103         my @results;
104         my $dbh = C4::Context->dbh;
105         my $sth = $dbh->prepare("SELECT DISTINCT tagfield, tagsubfield
106                                  FROM marc_subfield_structure
107                                  WHERE authorised_value IS NOT NULL
108                                    AND authorised_value!=''
109                                    AND frameworkcode=?");
110         $sth->execute( $frameworkcode );
111         while ( my $result = $sth->fetchrow_hashref() ) {
112             push ( @results, $result );
113         }
114         $authval_per_framework{ $frameworkcode } = \@results;
115     }
116     return $authval_per_framework{ $frameworkcode };
117 }
118
119 my $stylesheet;
120
121 sub XSLTParse4Display {
122     my ( $biblionumber, $orig_record, $xsl_suffix ) = @_;
123     # grab the XML, run it through our stylesheet, push it out to the browser
124     my $record = transformMARCXML4XSLT($biblionumber, $orig_record);
125     my $itemsxml  = buildKohaItemsNamespace($biblionumber);
126     my $xmlrecord = $record->as_xml();
127     $xmlrecord =~ s/\<\/record\>/$itemsxml\<\/record\>/;
128     my $parser = XML::LibXML->new();
129     # don't die when you find &, >, etc
130     $parser->recover_silently(1);
131     my $source = $parser->parse_string($xmlrecord);
132     unless ( $stylesheet ) {
133         my $xslt = XML::LibXSLT->new();
134         my $xslfile = C4::Context->config('opachtdocs') . 
135                       "/prog/en/xslt/" .
136                       C4::Context->preference('marcflavour') .
137                       "slim2OPAC$xsl_suffix.xsl";
138         my $style_doc = $parser->parse_file($xslfile);
139         $stylesheet = $xslt->parse_stylesheet($style_doc);
140     }
141     my $results = $stylesheet->transform($source);
142     my $newxmlrecord = $stylesheet->output_string($results);
143     return $newxmlrecord;
144 }
145
146 sub buildKohaItemsNamespace {
147     my ($biblionumber) = @_;
148     my @items = C4::Items::GetItemsInfo($biblionumber);
149     my $branches = GetBranches();
150     my $itemtypes = GetItemTypes();
151
152     my $xml;
153     for my $item (@items) {
154         my $status;
155
156         my ( $transfertwhen, $transfertfrom, $transfertto ) = C4::Circulation::GetTransfers($item->{itemnumber});
157
158         if ( $itemtypes->{ $item->{itype} }->{notforloan} == 1 || $item->{notforloan} || $item->{onloan} || $item->{wthdrawn} || $item->{itemlost} || $item->{damaged} ||
159              ($transfertwhen ne '') || $item->{itemnotforloan} ) {
160             if ( $item->{notforloan} < 0) {
161                 $status = "On order";
162             } 
163             if ( $item->{itemnotforloan} > 0 || $item->{notforloan} > 0 || $itemtypes->{ $item->{itype} }->{notforloan} == 1 ) {
164                 $status = "reference";
165             }
166             if ($item->{onloan}) {
167                 $status = "Checked out";
168             }
169             if ( $item->{wthdrawn}) {
170                 $status = "Withdrawn";
171             }
172             if ($item->{itemlost}) {
173                 $status = "Lost";
174             }
175             if ($item->{damaged}) {
176                 $status = "Damaged"; 
177             }
178             if ($transfertwhen ne '') {
179                 $status = 'In transit';
180             }
181         } else {
182             $status = "available";
183         }
184         $xml.="<item><homebranch>".$branches->{$item->{homebranch}}->{'branchname'}."</homebranch>".
185                 "<status>$status</status>".
186                 "<itemcallnumber>".$item->{'itemcallnumber'}."</itemcallnumber></item>";
187
188     }
189     return "<items xmlns='http://www.koha.org/items'>".$xml."</items>";
190 }
191
192
193
194 1;
195 __END__
196
197 =head1 NOTES
198
199 =head1 AUTHOR
200
201 Joshua Ferraro <jmf@liblime.com>
202
203 =cut