MT2621 : Fix simple barcodes display
[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
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 C4::Reserves;
30 use Encode;
31 use XML::LibXML;
32 use XML::LibXSLT;
33
34 use vars qw($VERSION @ISA @EXPORT);
35
36 BEGIN {
37     require Exporter;
38     $VERSION = 0.03;
39     @ISA = qw(Exporter);
40     @EXPORT = qw(
41         &XSLTParse4Display
42     );
43 }
44
45 =head1 NAME
46
47 C4::XSLT - Functions for displaying XSLT-generated content
48
49 =head1 FUNCTIONS
50
51 =head1 transformMARCXML4XSLT
52
53 =head2 replaces codes with authorized values in a MARC::Record object
54
55 =cut
56
57 sub transformMARCXML4XSLT {
58     my ($biblionumber, $record) = @_;
59     my $frameworkcode = GetFrameworkCode($biblionumber);
60     my $tagslib = &GetMarcStructure(1,$frameworkcode);
61     my @fields;
62     # FIXME: wish there was a better way to handle exceptions
63     eval {
64         @fields = $record->fields();
65     };
66     if ($@) { warn "PROBLEM WITH RECORD"; next; }
67     my $av = getAuthorisedValues4MARCSubfields($frameworkcode);
68     foreach my $tag ( keys %$av ) {
69         foreach my $field ( $record->field( $tag ) ) {
70             if ( $av->{ $tag } ) {
71                 my @new_subfields = ();
72                 for my $subfield ( $field->subfields() ) {
73                     my ( $letter, $value ) = @$subfield;
74                     $value = GetAuthorisedValueDesc( $tag, $letter, $value, '', $tagslib )
75                         if $av->{ $tag }->{ $letter };
76                     push( @new_subfields, $letter, $value );
77                 } 
78                 $field ->replace_with( MARC::Field->new(
79                     $tag,
80                     $field->indicator(1),
81                     $field->indicator(2),
82                     @new_subfields
83                 ) );
84             }
85         }
86     }
87     return $record;
88 }
89
90 =head1 getAuthorisedValues4MARCSubfields
91
92 =head2 returns an ref of hash of ref of hash for tag -> letter controled bu authorised values
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 $dbh = C4::Context->dbh;
104         my $sth = $dbh->prepare("SELECT DISTINCT tagfield, tagsubfield
105                                  FROM marc_subfield_structure
106                                  WHERE authorised_value IS NOT NULL
107                                    AND authorised_value!=''
108                                    AND frameworkcode=?");
109         $sth->execute( $frameworkcode );
110         my $av = { };
111         while ( my ( $tag, $letter ) = $sth->fetchrow() ) {
112             $av->{ $tag }->{ $letter } = 1;
113         }
114         $authval_per_framework{ $frameworkcode } = $av;
115     }
116     return $authval_per_framework{ $frameworkcode };
117 }
118
119 my $stylesheet;
120
121 sub XSLTParse4Display {
122     my ( $biblionumber, $orig_record, $xsl_suffix, $interface ) = @_;
123     $interface = 'opac' unless $interface;
124     # grab the XML, run it through our stylesheet, push it out to the browser
125     my $record = transformMARCXML4XSLT($biblionumber, $orig_record);
126     #return $record->as_formatted();
127     my $itemsxml  = buildKohaItemsNamespace($biblionumber);
128     my $xmlrecord = $record->as_xml(C4::Context->preference('marcflavour'));
129     my $sysxml = "<sysprefs>\n";
130     foreach my $syspref ( qw/OPACURLOpenInNewWindow DisplayOPACiconsXSLT URLLinkText viewISBD/ ) {
131         $sysxml .= "<syspref name=\"$syspref\">" .
132                    C4::Context->preference( $syspref ) .
133                    "</syspref>\n";
134     }
135     $sysxml .= "</sysprefs>\n";
136     $xmlrecord =~ s/\<\/record\>/$itemsxml$sysxml\<\/record\>/;
137     $xmlrecord =~ s/\& /\&amp\; /;
138     $xmlrecord=~ s/\&amp\;amp\; /\&amp\; /;
139
140     my $parser = XML::LibXML->new();
141     # don't die when you find &, >, etc
142     $parser->recover_silently(0);
143     my $source = $parser->parse_string($xmlrecord);
144     unless ( $stylesheet ) {
145         my $xslt = XML::LibXSLT->new();
146         my $xslfile;
147         if ($interface eq 'intranet') {
148             $xslfile = C4::Context->config('intrahtdocs') . 
149                       "/prog/en/xslt/" .
150                       C4::Context->preference('marcflavour') .
151                       "slim2intranet$xsl_suffix.xsl";
152         } else {
153             $xslfile = C4::Context->config('opachtdocs') . 
154                       "/prog/en/xslt/" .
155                       C4::Context->preference('marcflavour') .
156                       "slim2OPAC$xsl_suffix.xsl";
157         }
158         my $style_doc = $parser->parse_file($xslfile);
159         $stylesheet = $xslt->parse_stylesheet($style_doc);
160     }
161     my $results = $stylesheet->transform($source);
162     my $newxmlrecord = $stylesheet->output_string($results);
163     return $newxmlrecord;
164 }
165
166 sub buildKohaItemsNamespace {
167     my ($biblionumber) = @_;
168     my @items = C4::Items::GetItemsInfo($biblionumber);
169     my $branches = GetBranches();
170     my $itemtypes = GetItemTypes();
171     my $xml = '';
172     for my $item (@items) {
173         my $status;
174
175         my ( $transfertwhen, $transfertfrom, $transfertto ) = C4::Circulation::GetTransfers($item->{itemnumber});
176
177         my ( $reservestatus, $reserveitem ) = C4::Reserves::CheckReserves($item->{itemnumber});
178
179         if ( $itemtypes->{ $item->{itype} }->{notforloan} || $item->{notforloan} || $item->{onloan} || $item->{wthdrawn} || $item->{itemlost} || $item->{damaged} || 
180              (defined $transfertwhen && $transfertwhen ne '') || $item->{itemnotforloan} || (defined $reservestatus && $reservestatus eq "Waiting") ){ 
181             if ( $item->{notforloan} < 0) {
182                 $status = "On order";
183             } 
184             if ( $item->{itemnotforloan} > 0 || $item->{notforloan} > 0 || $itemtypes->{ $item->{itype} }->{notforloan} == 1 ) {
185                 $status = "reference";
186             }
187             if ($item->{onloan}) {
188                 $status = "Checked out";
189             }
190             if ( $item->{wthdrawn}) {
191                 $status = "Withdrawn";
192             }
193             if ($item->{itemlost}) {
194                 $status = "Lost";
195             }
196             if ($item->{damaged}) {
197                 $status = "Damaged"; 
198             }
199             if (defined $transfertwhen && $transfertwhen ne '') {
200                 $status = 'In transit';
201             }
202             if (defined $reservestatus && $reservestatus eq "Waiting") {
203                 $status = 'Waiting';
204             }
205         } else {
206             $status = "available";
207         }
208         my $homebranch = $branches->{$item->{homebranch}}->{'branchname'};
209         $xml.= "<item><homebranch>$homebranch</homebranch>".
210                 "<status>$status</status>".
211                 (defined $item->{'itemcallnumber'} ? "<itemcallnumber>".$item->{'itemcallnumber'}."</itemcallnumber>" 
212                                            : "<itemcallnumber />")
213         . "</item>";
214
215     }
216     $xml = "<items xmlns=\"http://www.koha.org/items\">".$xml."</items>";
217     return $xml;
218 }
219
220
221
222 1;
223 __END__
224
225 =head1 NOTES
226
227 =head1 AUTHOR
228
229 Joshua Ferraro <jmf@liblime.com>
230
231 =cut