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