2 # Copyright (C) 2006 LibLime
3 # <jmf at liblime dot com>
5 # This file is part of Koha.
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
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.
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
30 use vars
qw($VERSION @ISA @EXPORT);
43 C4::XSLT - Functions for displaying XSLT-generated content
47 =head1 transformMARCXML4XSLT
49 =head2 replaces codes with authorized values in a MARCXML record
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);
60 # FIXME: wish there was a better way to handle exceptions
62 @fields = $record->fields();
64 if ($@
) { warn "PROBLEM WITH RECORD"; next; }
65 my $list_of_authvalues = getAuthorisedValues4MARCSubfields
($frameworkcode);
66 for my $authvalue (@
$list_of_authvalues) {
67 for my $field ( $record->field($authvalue->{tagfield
}) ) {
68 my @newSubfields = ();
69 for my $subfield ( $field->subfields() ) {
70 my ($code,$data) = @
$subfield;
71 unless ($code eq $authvalue->{tagsubfield
}) {
72 push ( @newSubfields, $code, $data );
74 my $newvalue = GetAuthorisedValueDesc
( $authvalue->{tagfield
}, $code, $data, '', $tagslib );
75 push ( @newSubfields, $code, $newvalue );
78 my $newField = MARC
::Field
->new(
79 $authvalue->{tagfield
},
82 $authvalue->{tagsubfield
} => @newSubfields
84 $field->replace_with($newField);
90 =head1 getAuthorisedValues4MARCSubfields
92 =head2 returns an array of hash refs for authorised value tag/subfield combos for a given framework
96 sub getAuthorisedValues4MARCSubfields
{
97 my ($frameworkcode) = @_;
99 my $dbh = C4
::Context
->dbh;
100 my $sth = $dbh->prepare("SELECT DISTINCT tagfield,tagsubfield FROM marc_subfield_structure WHERE authorised_value IS NOT NULL AND authorised_value!='' AND frameworkcode=?");
101 $sth->execute($frameworkcode);
102 while (my $result = $sth->fetchrow_hashref()) {
103 push @results, $result;
108 sub XSLTParse4Display
{
109 my ($biblionumber,$xslfile) = @_;
110 # grab the XML, run it through our stylesheet, push it out to the browser
111 my $record = transformMARCXML4XSLT
($biblionumber);
112 my $itemsxml = buildKohaItemsNamespace
($biblionumber);
113 my $xmlrecord = $record->as_xml();
114 $xmlrecord =~ s/\<\/record\>/$itemsxml\
<\
/record\>/;
115 my $parser = XML
::LibXML
->new();
116 # don't die when you find &, >, etc
117 $parser->recover_silently(1);
118 my $xslt = XML
::LibXSLT
->new();
119 my $source = $parser->parse_string($xmlrecord);
120 my $style_doc = $parser->parse_file($xslfile);
121 my $stylesheet = $xslt->parse_stylesheet($style_doc);
122 my $results = $stylesheet->transform($source);
123 my $newxmlrecord = $stylesheet->output_string($results);
124 return $newxmlrecord;
127 sub buildKohaItemsNamespace
{
128 my ($biblionumber) = @_;
129 my @items = C4
::Items
::GetItemsInfo
($biblionumber);
130 my $branches = GetBranches
();
131 my $itemtypes = GetItemTypes
();
133 for my $item (@items) {
135 if ( $item->{notforloan
} == -1 || $item->{onloan
} || $item->{wthdrawn
} || $item->{itemlost
} || $item->{damaged
}) {
136 if ( $item->{notforloan
} == -1) {
137 $status = "On order";
139 if ($item->{onloan
}) {
140 $status = "Checked out";
142 if ( $item->{wthdrawn
}) {
143 $status = "Withdrawn";
145 if ($item->{itemlost
}) {
148 if ($item->{damaged
}) {
152 $status = "available";
154 $xml.="<item><homebranch>".$branches->{$item->{homebranch
}}->{'branchname'}."</homebranch>"."<status>$status</status></item>";
156 return "<items xmlns='http://www.koha.org/items'>".$xml."</items>";
168 Joshua Ferraro <jmf@liblime.com>