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 MARC::Record object
54 sub transformMARCXML4XSLT {
55 my ($biblionumber, $orig_record) = @_;
56 my $record = $orig_record->clone(); # not updating original record; this may be unnecessarily paranoid
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},
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 # Cache for tagfield-tagsubfield to decode per framework.
97 # Should be preferably be placed in Koha-core...
98 my %authval_per_framework;
100 sub getAuthorisedValues4MARCSubfields {
101 my ($frameworkcode) = @_;
102 unless ( $authval_per_framework{ $frameworkcode } ) {
104 my $dbh = C4::Context->dbh;
105 my $sth = $dbh->prepare("SELECT DISTINCT tagfield, tagsubfield
106 FROM marc_subfield_structure
107 WHERE authorised_value IS NOT NULL
108 AND authorised_value!=''
109 AND frameworkcode=?");
110 $sth->execute( $frameworkcode );
111 while ( my $result = $sth->fetchrow_hashref() ) {
112 push ( @results, $result );
114 $authval_per_framework{ $frameworkcode } = \@results;
116 return $authval_per_framework{ $frameworkcode };
121 sub XSLTParse4Display {
122 my ( $biblionumber, $orig_record, $xsl_suffix ) = @_;
123 # grab the XML, run it through our stylesheet, push it out to the browser
124 my $record = transformMARCXML4XSLT($biblionumber, $orig_record);
125 my $itemsxml = buildKohaItemsNamespace($biblionumber);
126 my $xmlrecord = $record->as_xml();
127 $xmlrecord =~ s/\<\/record\>/$itemsxml\<\/record\>/;
128 my $parser = XML::LibXML->new();
129 # don't die when you find &, >, etc
130 $parser->recover_silently(1);
131 my $source = $parser->parse_string($xmlrecord);
132 unless ( $stylesheet ) {
133 my $xslt = XML::LibXSLT->new();
134 my $xslfile = C4::Context->config('opachtdocs') .
136 C4::Context->preference('marcflavour') .
137 "slim2OPAC$xsl_suffix.xsl";
138 my $style_doc = $parser->parse_file($xslfile);
139 $stylesheet = $xslt->parse_stylesheet($style_doc);
141 my $results = $stylesheet->transform($source);
142 my $newxmlrecord = $stylesheet->output_string($results);
143 return $newxmlrecord;
146 sub buildKohaItemsNamespace {
147 my ($biblionumber) = @_;
148 my @items = C4::Items::GetItemsInfo($biblionumber);
149 my $branches = GetBranches();
150 my $itemtypes = GetItemTypes();
153 for my $item (@items) {
156 my ( $transfertwhen, $transfertfrom, $transfertto ) = C4::Circulation::GetTransfers($item->{itemnumber});
158 if ( $itemtypes->{ $item->{itype} }->{notforloan} == 1 || $item->{notforloan} || $item->{onloan} || $item->{wthdrawn} || $item->{itemlost} || $item->{damaged} ||
159 ($transfertwhen ne '') || $item->{itemnotforloan} ) {
160 if ( $item->{notforloan} < 0) {
161 $status = "On order";
163 if ( $item->{itemnotforloan} > 0 || $item->{notforloan} > 0 || $itemtypes->{ $item->{itype} }->{notforloan} == 1 ) {
164 $status = "reference";
166 if ($item->{onloan}) {
167 $status = "Checked out";
169 if ( $item->{wthdrawn}) {
170 $status = "Withdrawn";
172 if ($item->{itemlost}) {
175 if ($item->{damaged}) {
178 if ($transfertwhen ne '') {
179 $status = 'In transit';
182 $status = "available";
184 $xml.="<item><homebranch>".$branches->{$item->{homebranch}}->{'branchname'}."</homebranch>".
185 "<status>$status</status>".
186 "<itemcallnumber>".$item->{'itemcallnumber'}."</itemcallnumber></item>";
189 return "<items xmlns='http://www.koha.org/items'>".$xml."</items>";
201 Joshua Ferraro <jmf@liblime.com>