Bug 9628 - Replace YUI buttons and menus on budgets administration pages with Bootstrap
[koha.git] / C4 / XSLT.pm
1 package C4::XSLT;
2 # Copyright (C) 2006 LibLime
3 # <jmf at liblime dot com>
4 # Parts Copyright Katrin Fischer 2011
5 # Parts Copyright ByWater Solutions 2011
6 # Parts Copyright Biblibre 2012
7 #
8 # This file is part of Koha.
9 #
10 # Koha is free software; you can redistribute it and/or modify it under the
11 # terms of the GNU General Public License as published by the Free Software
12 # Foundation; either version 2 of the License, or (at your option) any later
13 # version.
14 #
15 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
16 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License along
20 # with Koha; if not, write to the Free Software Foundation, Inc.,
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22
23 use strict;
24 use warnings;
25
26 use C4::Context;
27 use C4::Branch;
28 use C4::Items;
29 use C4::Koha;
30 use C4::Biblio;
31 use C4::Circulation;
32 use C4::Reserves;
33 use Encode;
34 use XML::LibXML;
35 use XML::LibXSLT;
36 use LWP::Simple;
37
38 use vars qw($VERSION @ISA @EXPORT);
39
40 BEGIN {
41     require Exporter;
42     $VERSION = 3.07.00.049;
43     @ISA = qw(Exporter);
44     @EXPORT = qw(
45         &XSLTParse4Display
46         &GetURI
47     );
48 }
49
50 =head1 NAME
51
52 C4::XSLT - Functions for displaying XSLT-generated content
53
54 =head1 FUNCTIONS
55
56 =head2 GetURI
57
58 GetURI file and returns the xslt as a string
59
60 =cut
61
62 sub GetURI {
63     my ($uri) = @_;
64     my $string;
65     $string = get $uri ;
66     return $string;
67 }
68
69 =head2 transformMARCXML4XSLT
70
71 Replaces codes with authorized values in a MARC::Record object
72
73 =cut
74
75 sub transformMARCXML4XSLT {
76     my ($biblionumber, $record) = @_;
77     my $frameworkcode = GetFrameworkCode($biblionumber) || '';
78     my $tagslib = &GetMarcStructure(1,$frameworkcode);
79     my @fields;
80     # FIXME: wish there was a better way to handle exceptions
81     eval {
82         @fields = $record->fields();
83     };
84     if ($@) { warn "PROBLEM WITH RECORD"; next; }
85     my $av = getAuthorisedValues4MARCSubfields($frameworkcode);
86     foreach my $tag ( keys %$av ) {
87         foreach my $field ( $record->field( $tag ) ) {
88             if ( $av->{ $tag } ) {
89                 my @new_subfields = ();
90                 for my $subfield ( $field->subfields() ) {
91                     my ( $letter, $value ) = @$subfield;
92                     $value = GetAuthorisedValueDesc( $tag, $letter, $value, '', $tagslib )
93                         if $av->{ $tag }->{ $letter };
94                     push( @new_subfields, $letter, $value );
95                 } 
96                 $field ->replace_with( MARC::Field->new(
97                     $tag,
98                     $field->indicator(1),
99                     $field->indicator(2),
100                     @new_subfields
101                 ) );
102             }
103         }
104     }
105     return $record;
106 }
107
108 =head2 getAuthorisedValues4MARCSubfields
109
110 Returns a ref of hash of ref of hash for tag -> letter controled by authorised values
111
112 =cut
113
114 # Cache for tagfield-tagsubfield to decode per framework.
115 # Should be preferably be placed in Koha-core...
116 my %authval_per_framework;
117
118 sub getAuthorisedValues4MARCSubfields {
119     my ($frameworkcode) = @_;
120     unless ( $authval_per_framework{ $frameworkcode } ) {
121         my $dbh = C4::Context->dbh;
122         my $sth = $dbh->prepare("SELECT DISTINCT tagfield, tagsubfield
123                                  FROM marc_subfield_structure
124                                  WHERE authorised_value IS NOT NULL
125                                    AND authorised_value!=''
126                                    AND frameworkcode=?");
127         $sth->execute( $frameworkcode );
128         my $av = { };
129         while ( my ( $tag, $letter ) = $sth->fetchrow() ) {
130             $av->{ $tag }->{ $letter } = 1;
131         }
132         $authval_per_framework{ $frameworkcode } = $av;
133     }
134     return $authval_per_framework{ $frameworkcode };
135 }
136
137 my $stylesheet;
138
139 sub XSLTParse4Display {
140     my ( $biblionumber, $orig_record, $xslsyspref, $fixamps, $hidden_items ) = @_;
141     my $xslfilename = C4::Context->preference($xslsyspref);
142     if ( $xslfilename =~ /^\s*"?default"?\s*$/i ) {
143         my $htdocs;
144         my $theme;
145         my $lang = C4::Templates::_current_language();
146         my $xslfile;
147         if ($xslsyspref eq "XSLTDetailsDisplay") {
148             $htdocs  = C4::Context->config('intrahtdocs');
149             $theme   = C4::Context->preference("template");
150             $xslfile = C4::Context->preference('marcflavour') .
151                        "slim2intranetDetail.xsl";
152         } elsif ($xslsyspref eq "XSLTResultsDisplay") {
153             $htdocs  = C4::Context->config('intrahtdocs');
154             $theme   = C4::Context->preference("template");
155             $xslfile = C4::Context->preference('marcflavour') .
156                         "slim2intranetResults.xsl";
157         } elsif ($xslsyspref eq "OPACXSLTDetailsDisplay") {
158             $htdocs  = C4::Context->config('opachtdocs');
159             $theme   = C4::Context->preference("opacthemes");
160             $xslfile = C4::Context->preference('marcflavour') .
161                        "slim2OPACDetail.xsl";
162         } elsif ($xslsyspref eq "OPACXSLTResultsDisplay") {
163             $htdocs  = C4::Context->config('opachtdocs');
164             $theme   = C4::Context->preference("opacthemes");
165             $xslfile = C4::Context->preference('marcflavour') .
166                        "slim2OPACResults.xsl";
167         }
168         $xslfilename = "$htdocs/$theme/$lang/xslt/$xslfile";
169         $xslfilename = "$htdocs/$theme/en/xslt/$xslfile" unless ( $lang ne 'en' && -f $xslfilename );
170         $xslfilename = "$htdocs/prog/$lang/xslt/$xslfile" unless ( -f $xslfilename );
171         $xslfilename = "$htdocs/prog/en/xslt/$xslfile" unless ( $lang ne 'en' && -f $xslfilename );
172     }
173
174     if ( $xslfilename =~ m/\{langcode\}/ ) {
175         my $lang = C4::Templates::_current_language();
176         $xslfilename =~ s/\{langcode\}/$lang/;
177     }
178
179     # grab the XML, run it through our stylesheet, push it out to the browser
180     my $record = transformMARCXML4XSLT($biblionumber, $orig_record);
181     #return $record->as_formatted();
182     my $itemsxml  = buildKohaItemsNamespace($biblionumber, $hidden_items);
183     my $xmlrecord = $record->as_xml(C4::Context->preference('marcflavour'));
184     my $sysxml = "<sysprefs>\n";
185     foreach my $syspref ( qw/ hidelostitems OPACURLOpenInNewWindow
186                               DisplayOPACiconsXSLT URLLinkText viewISBD
187                               OPACBaseURL TraceCompleteSubfields UseICU
188                               UseAuthoritiesForTracings TraceSubjectSubdivisions
189                               Display856uAsImage OPACDisplay856uAsImage 
190                               UseControlNumber IntranetBiblioDefaultView BiblioDefaultView
191                               singleBranchMode
192                               AlternateHoldingsField AlternateHoldingsSeparator / )
193     {
194         my $sp = C4::Context->preference( $syspref );
195         next unless defined($sp);
196         $sysxml .= "<syspref name=\"$syspref\">$sp</syspref>\n";
197     }
198     $sysxml .= "</sysprefs>\n";
199     $xmlrecord =~ s/\<\/record\>/$itemsxml$sysxml\<\/record\>/;
200     if ($fixamps) { # We need to correct the ampersand entities that Zebra outputs
201         $xmlrecord =~ s/\&amp;amp;/\&amp;/g;
202     }
203     $xmlrecord =~ s/\& /\&amp\; /;
204     $xmlrecord =~ s/\&amp\;amp\; /\&amp\; /;
205
206     my $parser = XML::LibXML->new();
207     # don't die when you find &, >, etc
208     $parser->recover_silently(0);
209     my $source = $parser->parse_string($xmlrecord);
210     unless ( $stylesheet->{$xslfilename} ) {
211         my $xslt = XML::LibXSLT->new();
212         my $style_doc;
213         if ( $xslfilename =~ /^https?:\/\// ) {
214             my $xsltstring = GetURI($xslfilename);
215             $style_doc = $parser->parse_string($xsltstring);
216         } else {
217             use Cwd;
218             $style_doc = $parser->parse_file($xslfilename);
219         }
220         $stylesheet->{$xslfilename} = $xslt->parse_stylesheet($style_doc);
221     }
222     my $results      = $stylesheet->{$xslfilename}->transform($source);
223     my $newxmlrecord = $stylesheet->{$xslfilename}->output_string($results);
224     return $newxmlrecord;
225 }
226
227 sub buildKohaItemsNamespace {
228     my ($biblionumber, $hidden_items) = @_;
229
230     my @items = C4::Items::GetItemsInfo($biblionumber);
231     if ($hidden_items && @$hidden_items) {
232         my %hi = map {$_ => 1} @$hidden_items;
233         @items = grep { !$hi{$_->{itemnumber}} } @items;
234     }
235     my $branches = GetBranches();
236     my $itemtypes = GetItemTypes();
237     my $xml = '';
238     for my $item (@items) {
239         my $status;
240
241         my ( $transfertwhen, $transfertfrom, $transfertto ) = C4::Circulation::GetTransfers($item->{itemnumber});
242
243         my ( $reservestatus, $reserveitem, undef ) = C4::Reserves::CheckReserves($item->{itemnumber});
244
245         if ( $itemtypes->{ $item->{itype} }->{notforloan} || $item->{notforloan} || $item->{onloan} || $item->{wthdrawn} || $item->{itemlost} || $item->{damaged} || 
246              (defined $transfertwhen && $transfertwhen ne '') || $item->{itemnotforloan} || (defined $reservestatus && $reservestatus eq "Waiting") ){ 
247             if ( $item->{notforloan} < 0) {
248                 $status = "On order";
249             } 
250             if ( $item->{itemnotforloan} > 0 || $item->{notforloan} > 0 || $itemtypes->{ $item->{itype} }->{notforloan} == 1 ) {
251                 $status = "reference";
252             }
253             if ($item->{onloan}) {
254                 $status = "Checked out";
255             }
256             if ( $item->{wthdrawn}) {
257                 $status = "Withdrawn";
258             }
259             if ($item->{itemlost}) {
260                 $status = "Lost";
261             }
262             if ($item->{damaged}) {
263                 $status = "Damaged"; 
264             }
265             if (defined $transfertwhen && $transfertwhen ne '') {
266                 $status = 'In transit';
267             }
268             if (defined $reservestatus && $reservestatus eq "Waiting") {
269                 $status = 'Waiting';
270             }
271         } else {
272             $status = "available";
273         }
274         my $homebranch = $item->{homebranch}? xml_escape($branches->{$item->{homebranch}}->{'branchname'}):'';
275             my $itemcallnumber = xml_escape($item->{itemcallnumber});
276         $xml.= "<item><homebranch>$homebranch</homebranch>".
277                 "<status>$status</status>".
278                 "<itemcallnumber>".$itemcallnumber."</itemcallnumber>"
279         . "</item>";
280
281     }
282     $xml = "<items xmlns=\"http://www.koha-community.org/items\">".$xml."</items>";
283     return $xml;
284 }
285
286
287
288 1;
289 __END__
290
291 =head1 NOTES
292
293 =cut
294
295 =head1 AUTHOR
296
297 Joshua Ferraro <jmf@liblime.com>
298
299 =cut