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