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;
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;
128 sub buildKohaItemsNamespace {
129 my ($biblionumber) = @_;
130 my @items = C4::Items::GetItemsInfo($biblionumber);
131 my $branches = GetBranches();
132 my $itemtypes = GetItemTypes();
134 for my $item (@items) {
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";
142 if ( $item->{itemnotforloan} ) {
143 $status = "Not for loan";
145 if ($item->{onloan}) {
146 $status = "Checked out";
148 if ( $item->{wthdrawn}) {
149 $status = "Withdrawn";
151 if ($item->{itemlost}) {
154 if ($item->{damaged}) {
157 if ($transfertwhen ne '') {
158 $status = 'In transit';
161 $status = "available";
163 $xml.="<item><homebranch>".$branches->{$item->{homebranch}}->{'branchname'}."</homebranch>"."<status>$status</status></item>";
165 return "<items xmlns='http://www.koha.org/items'>".$xml."</items>";
177 Joshua Ferraro <jmf@liblime.com>