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
31 use vars
qw($VERSION @ISA @EXPORT);
44 C4::XSLT - Functions for displaying XSLT-generated content
48 =head1 transformMARCXML4XSLT
50 =head2 replaces codes with authorized values in a MARCXML record
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);
61 # FIXME: wish there was a better way to handle exceptions
63 @fields = $record->fields();
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 );
75 my $newvalue = GetAuthorisedValueDesc
( $authvalue->{tagfield
}, $code, $data, '', $tagslib );
76 push ( @newSubfields, $code, $newvalue );
79 my $newField = MARC
::Field
->new(
80 $authvalue->{tagfield
},
83 $authvalue->{tagsubfield
} => @newSubfields
85 $field->replace_with($newField);
91 =head1 getAuthorisedValues4MARCSubfields
93 =head2 returns an array of hash refs for authorised value tag/subfield combos for a given framework
97 sub getAuthorisedValues4MARCSubfields
{
98 my ($frameworkcode) = @_;
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;
111 sub XSLTParse4Display
{
112 my ($biblionumber,$xslfile) = @_;
113 # grab the XML, run it through our stylesheet, push it out to the browser
114 my $record = transformMARCXML4XSLT
($biblionumber);
115 my $itemsxml = buildKohaItemsNamespace
($biblionumber);
116 my $xmlrecord = $record->as_xml();
117 $xmlrecord =~ s/\<\/record\>/$itemsxml\
<\
/record\>/;
118 my $parser = XML
::LibXML
->new();
119 # don't die when you find &, >, etc
120 $parser->recover_silently(1);
121 my $source = $parser->parse_string($xmlrecord);
122 unless ( $stylesheet ) {
123 my $xslt = XML
::LibXSLT
->new();
124 my $style_doc = $parser->parse_file($xslfile);
125 $stylesheet = $xslt->parse_stylesheet($style_doc);
127 my $results = $stylesheet->transform($source);
128 my $newxmlrecord = $stylesheet->output_string($results);
129 return $newxmlrecord;
132 sub buildKohaItemsNamespace
{
133 my ($biblionumber) = @_;
134 my @items = C4
::Items
::GetItemsInfo
($biblionumber);
135 my $branches = GetBranches
();
136 my $itemtypes = GetItemTypes
();
138 for my $item (@items) {
140 my ( $transfertwhen, $transfertfrom, $transfertto ) = C4
::Circulation
::GetTransfers
($item->{itemnumber
});
141 if ( $item->{notforloan
} == -1 || $item->{onloan
} || $item->{wthdrawn
} || $item->{itemlost
} || $item->{damaged
} ||
142 ($transfertwhen ne '') || $item->{itemnotforloan
} ) {
143 if ( $item->{notforloan
} == -1) {
144 $status = "On order";
146 if ( $item->{itemnotforloan
} ) {
147 $status = "Not for loan";
149 if ($item->{onloan
}) {
150 $status = "Checked out";
152 if ( $item->{wthdrawn
}) {
153 $status = "Withdrawn";
155 if ($item->{itemlost
}) {
158 if ($item->{damaged
}) {
161 if ($transfertwhen ne '') {
162 $status = 'In transit';
165 $status = "available";
167 $xml.="<item><homebranch>".$branches->{$item->{homebranch
}}->{'branchname'}."</homebranch>".
168 "<status>$status</status>".
169 "<itemcallnumber>".$item->{'itemcallnumber'}."</itemcallnumber></item>";
172 return "<items xmlns='http://www.koha.org/items'>".$xml."</items>";
184 Joshua Ferraro <jmf@liblime.com>