Adding XSLTreultsFilename and XSLTDetailFilename
[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 Encode;
27 use XML::LibXML;
28 use XML::LibXSLT;
29 use LWP::Simple;
30
31 use strict;
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         &GetURI
42     );
43 }
44
45 =head1 NAME
46
47 C4::XSLT - Functions for displaying XSLT-generated content
48
49 =head1 FUNCTIONS
50
51 =head1 GetURI
52
53 =head2 GetURI file and returns the xslt as a string
54
55 =cut
56
57 sub GetURI {
58     my ($uri) = @_;
59     my $string;
60     $string = get $uri ; 
61     return $string;
62 }
63
64 =head1 transformMARCXML4XSLT
65
66 =head2 replaces codes with authorized values in a MARC::Record object
67
68 =cut
69
70 sub transformMARCXML4XSLT {
71     my ($biblionumber, $orig_record) = @_;
72     my $record = $orig_record->clone(); # not updating original record; this may be unnecessarily paranoid
73     my $frameworkcode = GetFrameworkCode($biblionumber);
74     my $tagslib = &GetMarcStructure(1,$frameworkcode);
75     my @fields;
76     # FIXME: wish there was a better way to handle exceptions
77     eval {
78         @fields = $record->fields();
79     };
80     if ($@) { warn "PROBLEM WITH RECORD"; return}
81     my $av = getAuthorisedValues4MARCSubfields($frameworkcode);
82     foreach my $tag ( keys %$av ) {
83         foreach my $field ( $record->field( $tag ) ) {
84             if ( $av->{ $tag } ) {
85                 my @new_subfields = ();
86                 for my $subfield ( $field->subfields() ) {
87                     my ( $letter, $value ) = @$subfield;
88                     $value = GetAuthorisedValueDesc( $tag, $letter, $value, '', $tagslib )
89                         if $av->{ $tag }->{ $letter };
90                     push( @new_subfields, $letter, $value );
91                 } 
92                 $field ->replace_with( MARC::Field->new(
93                     $tag,
94                     $field->indicator(1),
95                     $field->indicator(2),
96                     @new_subfields
97                 ) );
98             }
99         }
100     }
101     return $record;
102 }
103
104 =head1 getAuthorisedValues4MARCSubfields
105
106 =head2 returns an ref of hash of ref of hash for tag -> letter controled bu authorised values
107
108 =cut
109
110 # Cache for tagfield-tagsubfield to decode per framework.
111 # Should be preferably be placed in Koha-core...
112 my %authval_per_framework;
113
114 sub getAuthorisedValues4MARCSubfields {
115     my ($frameworkcode) = @_;
116     unless ( $authval_per_framework{ $frameworkcode } ) {
117         my $dbh = C4::Context->dbh;
118         my $sth = $dbh->prepare("SELECT DISTINCT tagfield, tagsubfield
119                                  FROM marc_subfield_structure
120                                  WHERE authorised_value IS NOT NULL
121                                    AND authorised_value!=''
122                                    AND frameworkcode=?");
123         $sth->execute( $frameworkcode );
124         my $av = { };
125         while ( my ( $tag, $letter ) = $sth->fetchrow() ) {
126             $av->{ $tag }->{ $letter } = 1;
127         }
128         $authval_per_framework{ $frameworkcode } = $av;
129     }
130     return $authval_per_framework{ $frameworkcode };
131 }
132
133 my $stylesheet;
134
135 sub XSLTParse4Display {
136     my ( $biblionumber, $orig_record, $type ) = @_;
137     my $xslfilename=C4::Context->preference("XSLT$type"."Filename")||_buildfilename($type);
138     # grab the XML, run it through our stylesheet, push it out to the browser
139     my $record = transformMARCXML4XSLT($biblionumber, $orig_record);
140     #return $record->as_formatted();
141     my $itemsxml  = buildKohaItemsNamespace($biblionumber);
142     my $xmlrecord = $record->as_xml(C4::Context->preference('marcflavour'));
143     my $sysxml = "";
144     foreach my $syspref ( qw/OPACURLOpenInNewWindow DisplayOPACiconsXSLT URLLinkText/ ) {
145         if (C4::Context->preference( $syspref ) ){
146         $sysxml .= "<syspref name=\"$syspref\">" .
147                    C4::Context->preference( $syspref ) .
148                    "</syspref>\n";
149         }
150     }
151     $sysxml = "<sysprefs>\n".$sysxml."</sysprefs>\n" if length($sysxml);
152     $xmlrecord =~ s/\<\/record\>/$itemsxml$sysxml\<\/record\>/;
153     $xmlrecord =~ s/\& /\&amp\; /;
154
155     my $parser = XML::LibXML->new();
156     # don't die when you find &, >, etc
157     $parser->recover_silently(0);
158     my $source = $parser->parse_string($xmlrecord);
159     unless ( $stylesheet->{$xslfilename} ) {
160         my $xslt = XML::LibXSLT->new();
161         my $style_doc;
162         if ($xslfilename=~/http:/){
163             my $xsltstring=GetURI($xslfilename);
164             $style_doc = $parser->parse_string($xsltstring);
165         }
166         else {
167             use Cwd;
168             warn getcwd;
169             $style_doc = $parser->parse_file($xslfilename);
170         }
171         $stylesheet->{$xslfilename} = $xslt->parse_stylesheet($style_doc);
172     }
173     my $results = $stylesheet->{$xslfilename}->transform($source);
174     my $newxmlrecord = $stylesheet->{$xslfilename}->output_string($results);
175     return $newxmlrecord;
176 }
177
178
179 sub _buildfilename{
180     my $type=shift;
181     return C4::Context->config('opachtdocs') .
182                           "/prog/en/xslt/" .
183                           C4::Context->preference('marcflavour') .
184                           "slim2OPAC$type.xsl";
185 }
186
187 sub buildKohaItemsNamespace {
188     my ($biblionumber) = @_;
189     my @items = C4::Items::GetItemsInfo($biblionumber);
190     my $branches = GetBranches();
191     my $itemtypes = GetItemTypes();
192
193     my $xml;
194     for my $item (@items) {
195         my $status;
196
197         my ( $transfertwhen, $transfertfrom, $transfertto ) = C4::Circulation::GetTransfers($item->{itemnumber});
198
199         if ( $itemtypes->{ $item->{itype} }->{notforloan} == 1 || $item->{notforloan} || $item->{onloan} || $item->{wthdrawn} || $item->{itemlost} || $item->{damaged} ||
200              ($transfertwhen ne '') || $item->{itemnotforloan} ) {
201             if ( $item->{notforloan} < 0) {
202                 $status = "On order";
203             } 
204             if ( $item->{itemnotforloan} > 0 || $item->{notforloan} > 0 || $itemtypes->{ $item->{itype} }->{notforloan} == 1 ) {
205                 $status = "reference";
206             }
207             if ($item->{onloan}) {
208                 $status = "Checked out";
209             }
210             if ( $item->{wthdrawn}) {
211                 $status = "Withdrawn";
212             }
213             if ($item->{itemlost}) {
214                 $status = "Lost";
215             }
216             if ($item->{damaged}) {
217                 $status = "Damaged"; 
218             }
219             if ($transfertwhen ne '') {
220                 $status = 'In transit';
221             }
222         } else {
223             $status = "available";
224         }
225         my $homebranch = $branches->{$item->{homebranch}}->{'branchname'};
226         $xml.= "<item><homebranch>$homebranch</homebranch>".
227                 "<status>$status</status>".
228                 "<itemcallnumber>".$item->{'itemcallnumber'}."</itemcallnumber></item>";
229
230     }
231     $xml = "<items xmlns=\"http://www.koha.org/items\">".$xml."</items>";
232     return $xml;
233 }
234
235
236
237 1;
238 __END__
239
240 =head1 NOTES
241
242 =head1 AUTHOR
243
244 Joshua Ferraro <jmf@liblime.com>
245
246 =cut