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
33 use vars qw($VERSION @ISA @EXPORT);
47 C4::XSLT - Functions for displaying XSLT-generated content
53 =head2 GetURI file and returns the xslt as a string
64 =head1 transformMARCXML4XSLT
66 =head2 replaces codes with authorized values in a MARC::Record object
70 sub transformMARCXML4XSLT {
71 my ($biblionumber, $orig_record) = @_;
72 my $record = $orig_record->clone(); # not updating original record; this may be unnecessarily paranoid
73 my $frameworkcode = GetFrameworkCode($biblionumber);
74 my $tagslib = &GetMarcStructure(1,$frameworkcode);
76 # FIXME: wish there was a better way to handle exceptions
78 @fields = $record->fields();
80 if ($@) { warn "PROBLEM WITH RECORD"; return}
81 my $av = getAuthorisedValues4MARCSubfields($frameworkcode);
82 foreach my $tag ( keys %$av ) {
83 foreach my $field ( $record->field( $tag ) ) {
84 if ( $av->{ $tag } ) {
85 my @new_subfields = ();
86 for my $subfield ( $field->subfields() ) {
87 my ( $letter, $value ) = @$subfield;
88 $value = GetAuthorisedValueDesc( $tag, $letter, $value, '', $tagslib )
89 if $av->{ $tag }->{ $letter };
90 push( @new_subfields, $letter, $value );
92 $field ->replace_with( MARC::Field->new(
104 =head1 getAuthorisedValues4MARCSubfields
106 =head2 returns an ref of hash of ref of hash for tag -> letter controled bu authorised values
110 # Cache for tagfield-tagsubfield to decode per framework.
111 # Should be preferably be placed in Koha-core...
112 my %authval_per_framework;
114 sub getAuthorisedValues4MARCSubfields {
115 my ($frameworkcode) = @_;
116 unless ( $authval_per_framework{ $frameworkcode } ) {
117 my $dbh = C4::Context->dbh;
118 my $sth = $dbh->prepare("SELECT DISTINCT tagfield, tagsubfield
119 FROM marc_subfield_structure
120 WHERE authorised_value IS NOT NULL
121 AND authorised_value!=''
122 AND frameworkcode=?");
123 $sth->execute( $frameworkcode );
125 while ( my ( $tag, $letter ) = $sth->fetchrow() ) {
126 $av->{ $tag }->{ $letter } = 1;
128 $authval_per_framework{ $frameworkcode } = $av;
130 return $authval_per_framework{ $frameworkcode };
135 sub XSLTParse4Display {
136 my ( $biblionumber, $orig_record, $xslfilename ) = @_;
137 # grab the XML, run it through our stylesheet, push it out to the browser
138 my $record = transformMARCXML4XSLT($biblionumber, $orig_record);
139 #return $record->as_formatted();
140 my $itemsxml = buildKohaItemsNamespace($biblionumber);
141 my $xmlrecord = $record->as_xml(C4::Context->preference('marcflavour'));
143 foreach my $syspref ( qw/OPACURLOpenInNewWindow DisplayOPACiconsXSLT URLLinkText/ ) {
144 if (C4::Context->preference( $syspref ) ){
145 $sysxml .= "<syspref name=\"$syspref\">" .
146 C4::Context->preference( $syspref ) .
150 $sysxml = "<sysprefs>\n".$sysxml."</sysprefs>\n" if length($sysxml);
151 $xmlrecord =~ s/\<\/record\>/$itemsxml$sysxml\<\/record\>/;
152 $xmlrecord =~ s/\& /\&\; /;
154 my $parser = XML::LibXML->new();
155 # don't die when you find &, >, etc
156 $parser->recover_silently(0);
157 my $source = $parser->parse_string($xmlrecord);
158 unless ( $stylesheet->{$xslfilename} ) {
159 my $xslt = XML::LibXSLT->new();
161 if ($xslfilename=~/http:/){
162 my $xsltstring=GetURI($xslfilename);
163 $style_doc = $parser->parse_string($xsltstring);
168 $style_doc = $parser->parse_file($xslfilename);
170 $stylesheet->{$xslfilename} = $xslt->parse_stylesheet($style_doc);
172 my $results = $stylesheet->{$xslfilename}->transform($source);
173 my $newxmlrecord = $stylesheet->{$xslfilename}->output_string($results);
174 return $newxmlrecord;
177 sub buildKohaItemsNamespace {
178 my ($biblionumber) = @_;
179 my @items = C4::Items::GetItemsInfo($biblionumber);
180 my $branches = GetBranches();
181 my $itemtypes = GetItemTypes();
184 for my $item (@items) {
187 my ( $transfertwhen, $transfertfrom, $transfertto ) = C4::Circulation::GetTransfers($item->{itemnumber});
189 if ( $itemtypes->{ $item->{itype} }->{notforloan} == 1 || $item->{notforloan} || $item->{onloan} || $item->{wthdrawn} || $item->{itemlost} || $item->{damaged} ||
190 ($transfertwhen ne '') || $item->{itemnotforloan} ) {
191 if ( $item->{notforloan} < 0) {
192 $status = "On order";
194 if ( $item->{itemnotforloan} > 0 || $item->{notforloan} > 0 || $itemtypes->{ $item->{itype} }->{notforloan} == 1 ) {
195 $status = "reference";
197 if ($item->{onloan}) {
198 $status = "Checked out";
200 if ( $item->{wthdrawn}) {
201 $status = "Withdrawn";
203 if ($item->{itemlost}) {
206 if ($item->{damaged}) {
209 if ($transfertwhen ne '') {
210 $status = 'In transit';
213 $status = "available";
215 my $homebranch = $branches->{$item->{homebranch}}->{'branchname'};
216 $xml.= "<item><homebranch>$homebranch</homebranch>".
217 "<status>$status</status>".
218 "<itemcallnumber>".$item->{'itemcallnumber'}."</itemcallnumber></item>";
221 $xml = "<items xmlns=\"http://www.koha.org/items\">".$xml."</items>";
234 Joshua Ferraro <jmf@liblime.com>