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