More fixes to the shelves test
[koha.git] / C4 / XSLT.pm
blob8a67e04090ded23a249f1483b99c7bd7c108a98f
1 package C4::XSLT;
2 # Copyright (C) 2006 LibLime
3 # <jmf at liblime dot com>
5 # This file is part of Koha.
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.
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.
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
20 use strict;
21 use warnings;
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;
33 use vars qw($VERSION @ISA @EXPORT);
35 BEGIN {
36 require Exporter;
37 $VERSION = 0.03;
38 @ISA = qw(Exporter);
39 @EXPORT = qw(
40 &XSLTParse4Display
44 =head1 NAME
46 C4::XSLT - Functions for displaying XSLT-generated content
48 =head1 FUNCTIONS
50 =head1 transformMARCXML4XSLT
52 =head2 replaces codes with authorized values in a MARC::Record object
54 =cut
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();
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 );
77 $field ->replace_with( MARC::Field->new(
78 $tag,
79 $field->indicator(1),
80 $field->indicator(2),
81 @new_subfields
82 ) );
86 return $record;
89 =head1 getAuthorisedValues4MARCSubfields
91 =head2 returns an ref of hash of ref of hash for tag -> letter controled bu authorised values
93 =cut
95 # Cache for tagfield-tagsubfield to decode per framework.
96 # Should be preferably be placed in Koha-core...
97 my %authval_per_framework;
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;
113 $authval_per_framework{ $frameworkcode } = $av;
115 return $authval_per_framework{ $frameworkcode };
118 my $stylesheet;
120 sub XSLTParse4Display {
121 my ( $biblionumber, $orig_record, $xsl_suffix, $interface ) = @_;
122 $interface = 'opac' unless $interface;
123 # grab the XML, run it through our stylesheet, push it out to the browser
124 my $record = transformMARCXML4XSLT($biblionumber, $orig_record);
125 #return $record->as_formatted();
126 my $itemsxml = buildKohaItemsNamespace($biblionumber);
127 my $xmlrecord = $record->as_xml();
128 my $sysxml = "<sysprefs>\n";
129 foreach my $syspref ( qw/OPACURLOpenInNewWindow DisplayOPACiconsXSLT URLLinkText/ ) {
130 $sysxml .= "<syspref name=\"$syspref\">" .
131 C4::Context->preference( $syspref ) .
132 "</syspref>\n";
134 $sysxml .= "</sysprefs>\n";
135 $xmlrecord =~ s/\<\/record\>/$itemsxml$sysxml\<\/record\>/;
136 $xmlrecord =~ s/\& /\&amp\; /;
138 my $parser = XML::LibXML->new();
139 # don't die when you find &, >, etc
140 $parser->recover_silently(1);
141 my $source = $parser->parse_string($xmlrecord);
142 unless ( $stylesheet ) {
143 my $xslt = XML::LibXSLT->new();
144 my $xslfile;
145 if ($interface eq 'intranet') {
146 $xslfile = C4::Context->config('intrahtdocs') .
147 "/prog/en/xslt/" .
148 C4::Context->preference('marcflavour') .
149 "slim2intranet$xsl_suffix.xsl";
150 } else {
151 $xslfile = C4::Context->config('opachtdocs') .
152 "/prog/en/xslt/" .
153 C4::Context->preference('marcflavour') .
154 "slim2OPAC$xsl_suffix.xsl";
156 my $style_doc = $parser->parse_file($xslfile);
157 $stylesheet = $xslt->parse_stylesheet($style_doc);
159 my $results = $stylesheet->transform($source);
160 my $newxmlrecord = $stylesheet->output_string($results);
161 return $newxmlrecord;
164 sub buildKohaItemsNamespace {
165 my ($biblionumber) = @_;
166 my @items = C4::Items::GetItemsInfo($biblionumber);
167 my $branches = GetBranches();
168 my $itemtypes = GetItemTypes();
169 my $xml = '';
170 for my $item (@items) {
171 my $status;
173 my ( $transfertwhen, $transfertfrom, $transfertto ) = C4::Circulation::GetTransfers($item->{itemnumber});
175 if ( $itemtypes->{ $item->{itype} }->{notforloan} || $item->{notforloan} || $item->{onloan} || $item->{wthdrawn} || $item->{itemlost} || $item->{damaged} ||
176 (defined $transfertwhen && $transfertwhen ne '') || $item->{itemnotforloan} ) {
177 if ( $item->{notforloan} < 0) {
178 $status = "On order";
180 if ( $item->{itemnotforloan} > 0 || $item->{notforloan} > 0 || $itemtypes->{ $item->{itype} }->{notforloan} == 1 ) {
181 $status = "reference";
183 if ($item->{onloan}) {
184 $status = "Checked out";
186 if ( $item->{wthdrawn}) {
187 $status = "Withdrawn";
189 if ($item->{itemlost}) {
190 $status = "Lost";
192 if ($item->{damaged}) {
193 $status = "Damaged";
195 if (defined $transfertwhen && $transfertwhen ne '') {
196 $status = 'In transit';
198 } else {
199 $status = "available";
201 my $homebranch = $branches->{$item->{homebranch}}->{'branchname'};
202 $xml.= "<item><homebranch>$homebranch</homebranch>".
203 "<status>$status</status>".
204 (defined $item->{'itemcallnumber'} ? "<itemcallnumber>".$item->{'itemcallnumber'}."</itemcallnumber>"
205 : "<itemcallnumber />")
206 . "</item>";
209 $xml = "<items xmlns=\"http://www.koha.org/items\">".$xml."</items>";
210 return $xml;
216 __END__
218 =head1 NOTES
220 =head1 AUTHOR
222 Joshua Ferraro <jmf@liblime.com>
224 =cut