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
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
35 use vars
qw($VERSION @ISA @EXPORT);
48 C4::XSLT - Functions for displaying XSLT-generated content
52 =head2 transformMARCXML4XSLT
54 Replaces codes with authorized values in a MARC::Record object
58 sub transformMARCXML4XSLT
{
59 my ($biblionumber, $record) = @_;
60 my $frameworkcode = GetFrameworkCode
($biblionumber);
61 my $tagslib = &GetMarcStructure
(1,$frameworkcode);
63 # FIXME: wish there was a better way to handle exceptions
65 @fields = $record->fields();
67 if ($@
) { warn "PROBLEM WITH RECORD"; next; }
68 my $av = getAuthorisedValues4MARCSubfields
($frameworkcode);
69 foreach my $tag ( keys %$av ) {
70 foreach my $field ( $record->field( $tag ) ) {
71 if ( $av->{ $tag } ) {
72 my @new_subfields = ();
73 for my $subfield ( $field->subfields() ) {
74 my ( $letter, $value ) = @
$subfield;
75 $value = GetAuthorisedValueDesc
( $tag, $letter, $value, '', $tagslib )
76 if $av->{ $tag }->{ $letter };
77 push( @new_subfields, $letter, $value );
79 $field ->replace_with( MARC
::Field
->new(
91 =head2 getAuthorisedValues4MARCSubfields
93 Returns a ref of hash of ref of hash for tag -> letter controled by authorised values
97 # Cache for tagfield-tagsubfield to decode per framework.
98 # Should be preferably be placed in Koha-core...
99 my %authval_per_framework;
101 sub getAuthorisedValues4MARCSubfields
{
102 my ($frameworkcode) = @_;
103 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 );
112 while ( my ( $tag, $letter ) = $sth->fetchrow() ) {
113 $av->{ $tag }->{ $letter } = 1;
115 $authval_per_framework{ $frameworkcode } = $av;
117 return $authval_per_framework{ $frameworkcode };
122 sub XSLTParse4Display
{
123 my ( $biblionumber, $orig_record, $xsl_suffix, $interface ) = @_;
124 $interface = 'opac' unless $interface;
125 # grab the XML, run it through our stylesheet, push it out to the browser
126 my $record = transformMARCXML4XSLT
($biblionumber, $orig_record);
127 #return $record->as_formatted();
128 my $itemsxml = buildKohaItemsNamespace
($biblionumber);
129 my $xmlrecord = $record->as_xml(C4
::Context
->preference('marcflavour'));
130 my $sysxml = "<sysprefs>\n";
131 foreach my $syspref ( qw
/OPACURLOpenInNewWindow DisplayOPACiconsXSLT URLLinkText viewISBD OPACBaseURL/ ) {
132 $sysxml .= "<syspref name=\"$syspref\">" .
133 C4
::Context
->preference( $syspref ) .
136 $sysxml .= "</sysprefs>\n";
137 $xmlrecord =~ s/\<\/record\>/$itemsxml$sysxml\
<\
/record\>/;
138 $xmlrecord =~ s/\& /\&\; /;
139 $xmlrecord=~ s/\&\;amp\; /\&\; /;
141 my $parser = XML
::LibXML
->new();
142 # don't die when you find &, >, etc
143 $parser->recover_silently(0);
144 my $source = $parser->parse_string($xmlrecord);
145 unless ( $stylesheet ) {
146 my $xslt = XML
::LibXSLT
->new();
148 if ($interface eq 'intranet') {
149 $xslfile = C4
::Context
->config('intrahtdocs') .
150 '/' . C4
::Context
->preference("template") .
151 '/' . C4
::Output
::_current_language
() .
153 C4
::Context
->preference('marcflavour') .
154 "slim2intranet$xsl_suffix.xsl";
156 $xslfile = C4
::Context
->config('opachtdocs') .
157 '/' . C4
::Context
->preference("opacthemes") .
158 '/' . C4
::Output
::_current_language
() .
160 C4
::Context
->preference('marcflavour') .
161 "slim2OPAC$xsl_suffix.xsl";
163 my $style_doc = $parser->parse_file($xslfile);
164 $stylesheet = $xslt->parse_stylesheet($style_doc);
166 my $results = $stylesheet->transform($source);
167 my $newxmlrecord = $stylesheet->output_string($results);
168 return $newxmlrecord;
171 sub buildKohaItemsNamespace
{
172 my ($biblionumber) = @_;
173 my @items = C4
::Items
::GetItemsInfo
($biblionumber);
174 my $branches = GetBranches
();
175 my $itemtypes = GetItemTypes
();
177 for my $item (@items) {
180 my ( $transfertwhen, $transfertfrom, $transfertto ) = C4
::Circulation
::GetTransfers
($item->{itemnumber
});
182 my ( $reservestatus, $reserveitem ) = C4
::Reserves
::CheckReserves
($item->{itemnumber
});
184 if ( $itemtypes->{ $item->{itype
} }->{notforloan
} || $item->{notforloan
} || $item->{onloan
} || $item->{wthdrawn
} || $item->{itemlost
} || $item->{damaged
} ||
185 (defined $transfertwhen && $transfertwhen ne '') || $item->{itemnotforloan
} || (defined $reservestatus && $reservestatus eq "Waiting") ){
186 if ( $item->{notforloan
} < 0) {
187 $status = "On order";
189 if ( $item->{itemnotforloan
} > 0 || $item->{notforloan
} > 0 || $itemtypes->{ $item->{itype
} }->{notforloan
} == 1 ) {
190 $status = "reference";
192 if ($item->{onloan
}) {
193 $status = "Checked out";
195 if ( $item->{wthdrawn
}) {
196 $status = "Withdrawn";
198 if ($item->{itemlost
}) {
201 if ($item->{damaged
}) {
204 if (defined $transfertwhen && $transfertwhen ne '') {
205 $status = 'In transit';
207 if (defined $reservestatus && $reservestatus eq "Waiting") {
211 $status = "available";
213 my $homebranch = $branches->{$item->{homebranch
}}->{'branchname'};
214 my $itemcallnumber = $item->{itemcallnumber
} || '';
215 $itemcallnumber =~ s/\&/\&\;/g;
216 $xml.= "<item><homebranch>$homebranch</homebranch>".
217 "<status>$status</status>".
218 "<itemcallnumber>".$itemcallnumber."</itemcallnumber>"
222 $xml = "<items xmlns=\"http://www.koha.org/items\">".$xml."</items>";
237 Joshua Ferraro <jmf@liblime.com>