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);
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 );
69 my $newvalue = GetAuthorisedValueDesc( $authvalue->{tagfield}, $code, $data, '', $tagslib );
70 push ( @newSubfields, $code, $newvalue );
73 my $newField = MARC::Field->new(
74 $authvalue->{tagfield},
77 $authvalue->{tagsubfield} => @newSubfields
79 $field->replace_with($newField);
85 =head1 getAuthorisedValues4MARCSubfields
87 =head2 returns an array of hash refs for authorised value tag/subfield combos for a given framework
91 sub getAuthorisedValues4MARCSubfields {
92 my ($frameworkcode) = @_;
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;
103 sub XSLTParse4Display {
104 my ($biblionumber,$type) = @_;
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 $xslfile = C4::Context->config('opachtdocs')."/prog/en/xslt/MARC21slim2OPAC$type.xsl";
111 my $parser = XML::LibXML->new();
112 # don't die when you find &, >, etc
113 $parser->recover_silently(1);
114 my $xslt = XML::LibXSLT->new();
115 my $source = $parser->parse_string($xmlrecord);
116 my $style_doc = $parser->parse_file($xslfile);
117 my $stylesheet = $xslt->parse_stylesheet($style_doc);
118 my $results = $stylesheet->transform($source);
119 my $newxmlrecord = $stylesheet->output_string($results);
120 return $newxmlrecord;
123 sub buildKohaItemsNamespace {
124 my ($biblionumber) = @_;
125 my @items = C4::Items::GetItemsInfo($biblionumber);
126 my $branches = GetBranches();
127 my $itemtypes = GetItemTypes();
129 for my $item (@items) {
131 if ($item->{onloan}) {
134 $status = "available";
136 $xml.="<item><homebranch>".$branches->{$item->{homebranch}}->{'branchname'}."</homebranch>"."<status>$status</status></item>";
138 return "<items xmlns='http://www.koha.org/items'>".$xml."</items>";
150 Joshua Ferraro <jmf@liblime.com>