iMore updates to translations
[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 XML::LibXML;
27 use XML::LibXSLT;
28
29 use strict;
30
31 use vars qw($VERSION @ISA @EXPORT);
32
33 BEGIN {
34     require Exporter;
35     $VERSION = 0.03;
36     @ISA = qw(Exporter);
37     @EXPORT = qw(
38         &XSLTParse4Display
39     );
40 }
41
42 =head1 NAME
43
44 C4::XSLT - Functions for displaying XSLT-generated content
45
46 =head1 FUNCTIONS
47
48 =head1 transformMARCXML4XSLT
49
50 =head2 replaces codes with authorized values in a MARCXML record
51
52 =cut
53
54 sub transformMARCXML4XSLT {
55     my ($biblionumber) = @_;
56     my $record = GetMarcBiblio($biblionumber);
57     my $biblio = GetBiblioData($biblionumber);
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();
64     };
65     if ($@) { warn "PROBLEM WITH RECORD"; next; }
66     my $list_of_authvalues = getAuthorisedValues4MARCSubfields($frameworkcode);
67     for my $authvalue (@$list_of_authvalues) {
68         for my $field ( $record->field($authvalue->{tagfield}) ) {
69             my @newSubfields = ();
70             for my $subfield ( $field->subfields() ) {
71                 my ($code,$data) = @$subfield;
72                 unless ($code eq $authvalue->{tagsubfield}) {
73                     push ( @newSubfields, $code, $data );
74                 } else {
75                     my $newvalue = GetAuthorisedValueDesc( $authvalue->{tagfield}, $code, $data, '', $tagslib );
76                     push ( @newSubfields, $code, $newvalue );
77                 }
78             }
79             my $newField = MARC::Field->new(
80                 $authvalue->{tagfield},
81                 $field->indicator(1),
82                 $field->indicator(2),
83                 $authvalue->{tagsubfield} => @newSubfields
84             );
85             $field->replace_with($newField);
86         }
87     }
88     return $record;
89 }
90
91 =head1 getAuthorisedValues4MARCSubfields
92
93 =head2 returns an array of hash refs for authorised value tag/subfield combos for a given framework
94
95 =cut
96
97 sub getAuthorisedValues4MARCSubfields {
98     my ($frameworkcode) = @_;
99     my @results;
100     my $dbh = C4::Context->dbh;
101     my $sth = $dbh->prepare("SELECT DISTINCT tagfield,tagsubfield FROM marc_subfield_structure WHERE authorised_value IS NOT NULL AND authorised_value!='' AND frameworkcode=?");
102     $sth->execute($frameworkcode);
103     while (my $result = $sth->fetchrow_hashref()) {
104         push @results, $result;
105     }
106     return \@results;
107 }
108
109 sub XSLTParse4Display {
110     my ($biblionumber,$xslfile) = @_;
111     # grab the XML, run it through our stylesheet, push it out to the browser
112     my $record = transformMARCXML4XSLT($biblionumber);
113     my $itemsxml  = buildKohaItemsNamespace($biblionumber);
114     my $xmlrecord = $record->as_xml();
115     $xmlrecord =~ s/\<\/record\>/$itemsxml\<\/record\>/;
116     my $parser = XML::LibXML->new();
117     # don't die when you find &, >, etc
118     $parser->recover_silently(1);
119     my $xslt = XML::LibXSLT->new();
120     my $source = $parser->parse_string($xmlrecord);
121     my $style_doc = $parser->parse_file($xslfile);
122     my $stylesheet = $xslt->parse_stylesheet($style_doc);
123     my $results = $stylesheet->transform($source);
124     my $newxmlrecord = $stylesheet->output_string($results);
125     return $newxmlrecord;
126 }
127
128 sub buildKohaItemsNamespace {
129     my ($biblionumber) = @_;
130     my @items = C4::Items::GetItemsInfo($biblionumber);
131     my $branches = GetBranches();
132     my $itemtypes = GetItemTypes();
133     my $xml;
134     for my $item (@items) {
135         my $status;
136         my ( $transfertwhen, $transfertfrom, $transfertto ) = C4::Circulation::GetTransfers($item->{itemnumber});
137         if ( $item->{notforloan} == -1 || $item->{onloan} || $item->{wthdrawn} || $item->{itemlost} || $item->{damaged} ||
138              ($transfertwhen ne '') || $item->{itemnotforloan} ) {
139             if ( $item->{notforloan} == -1) {
140                 $status = "On order";
141             } 
142             if ( $item->{itemnotforloan} ) {
143                 $status = "Not for loan";
144             }
145             if ($item->{onloan}) {
146                 $status = "Checked out";
147             }
148             if ( $item->{wthdrawn}) {
149                 $status = "Withdrawn";
150             }
151             if ($item->{itemlost}) {
152                 $status = "Lost";
153             }
154             if ($item->{damaged}) {
155                 $status = "Damaged"; 
156             }
157             if ($transfertwhen ne '') {
158                 $status = 'In transit';
159             }
160         } else {
161             $status = "available";
162         }
163         $xml.="<item><homebranch>".$branches->{$item->{homebranch}}->{'branchname'}."</homebranch>"."<status>$status</status></item>";
164     }
165     return "<items xmlns='http://www.koha.org/items'>".$xml."</items>";
166 }
167
168
169
170 1;
171 __END__
172
173 =head1 NOTES
174
175 =head1 AUTHOR
176
177 Joshua Ferraro <jmf@liblime.com>
178
179 =cut