C4/XSLT Removing unconditionnal comment
[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, $xslfilename ) = @_;
137     # grab the XML, run it through our stylesheet, push it out to the browser
138     my $record = transformMARCXML4XSLT($biblionumber, $orig_record);
139     #return $record->as_formatted();
140     my $itemsxml  = buildKohaItemsNamespace($biblionumber);
141     my $xmlrecord = $record->as_xml(C4::Context->preference('marcflavour'));
142     my $sysxml = "";
143     foreach my $syspref ( qw/OPACURLOpenInNewWindow DisplayOPACiconsXSLT URLLinkText/ ) {
144         if (C4::Context->preference( $syspref ) ){
145         $sysxml .= "<syspref name=\"$syspref\">" .
146                    C4::Context->preference( $syspref ) .
147                    "</syspref>\n";
148         }
149     }
150     $sysxml = "<sysprefs>\n".$sysxml."</sysprefs>\n" if length($sysxml);
151     $xmlrecord =~ s/\<\/record\>/$itemsxml$sysxml\<\/record\>/;
152     $xmlrecord =~ s/\& /\&amp\; /;
153
154     my $parser = XML::LibXML->new();
155     # don't die when you find &, >, etc
156     $parser->recover_silently(0);
157     my $source = $parser->parse_string($xmlrecord);
158     unless ( $stylesheet->{$xslfilename} ) {
159         my $xslt = XML::LibXSLT->new();
160         my $style_doc;
161         if ($xslfilename=~/http:/){
162             my $xsltstring=GetURI($xslfilename);
163             $style_doc = $parser->parse_string($xsltstring);
164         }
165         else {
166             use Cwd;
167             warn getcwd;
168             $style_doc = $parser->parse_file($xslfilename);
169         }
170         $stylesheet->{$xslfilename} = $xslt->parse_stylesheet($style_doc);
171     }
172     my $results = $stylesheet->{$xslfilename}->transform($source);
173     my $newxmlrecord = $stylesheet->{$xslfilename}->output_string($results);
174     return $newxmlrecord;
175 }
176
177 sub buildKohaItemsNamespace {
178     my ($biblionumber) = @_;
179     my @items = C4::Items::GetItemsInfo($biblionumber);
180     my $branches = GetBranches();
181     my $itemtypes = GetItemTypes();
182
183     my $xml;
184     for my $item (@items) {
185         my $status;
186
187         my ( $transfertwhen, $transfertfrom, $transfertto ) = C4::Circulation::GetTransfers($item->{itemnumber});
188
189         if ( $itemtypes->{ $item->{itype} }->{notforloan} == 1 || $item->{notforloan} || $item->{onloan} || $item->{wthdrawn} || $item->{itemlost} || $item->{damaged} ||
190              ($transfertwhen ne '') || $item->{itemnotforloan} ) {
191             if ( $item->{notforloan} < 0) {
192                 $status = "On order";
193             } 
194             if ( $item->{itemnotforloan} > 0 || $item->{notforloan} > 0 || $itemtypes->{ $item->{itype} }->{notforloan} == 1 ) {
195                 $status = "reference";
196             }
197             if ($item->{onloan}) {
198                 $status = "Checked out";
199             }
200             if ( $item->{wthdrawn}) {
201                 $status = "Withdrawn";
202             }
203             if ($item->{itemlost}) {
204                 $status = "Lost";
205             }
206             if ($item->{damaged}) {
207                 $status = "Damaged"; 
208             }
209             if ($transfertwhen ne '') {
210                 $status = 'In transit';
211             }
212         } else {
213             $status = "available";
214         }
215         my $homebranch = $branches->{$item->{homebranch}}->{'branchname'};
216         $xml.= "<item><homebranch>$homebranch</homebranch>".
217                 "<status>$status</status>".
218                 "<itemcallnumber>".$item->{'itemcallnumber'}."</itemcallnumber></item>";
219
220     }
221     $xml = "<items xmlns=\"http://www.koha.org/items\">".$xml."</items>";
222     return $xml;
223 }
224
225
226
227 1;
228 __END__
229
230 =head1 NOTES
231
232 =head1 AUTHOR
233
234 Joshua Ferraro <jmf@liblime.com>
235
236 =cut