3 # Copyright 2000-2002 Katipo Communications
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.
22 detail.pl : script to show an authority in MARC format
29 This script needs an authid
31 It shows the authority in a (nice) MARC format depending on authority MARC
44 use C4
::AuthoritiesMarc
;
56 =item build_authorized_values_list
60 sub build_authorized_values_list
($$$$$$$) {
61 my ( $tag, $subfield, $value, $dbh, $authorised_values_sth,$index_tag,$index_subfield ) = @_;
63 my @authorised_values;
66 # builds list, depending on authorised value...
69 if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
72 "select branchcode,branchname from branches order by branchname");
74 push @authorised_values, ""
75 unless ( $tagslib->{$tag}->{$subfield}->{mandatory
} );
77 while ( my ( $branchcode, $branchname ) = $sth->fetchrow_array ) {
78 push @authorised_values, $branchcode;
79 $authorised_lib{$branchcode} = $branchname;
84 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value
} eq "itemtypes" ) {
87 "select itemtype,description from itemtypes order by description");
89 push @authorised_values, ""
90 unless ( $tagslib->{$tag}->{$subfield}->{mandatory
} );
94 while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) {
95 push @authorised_values, $itemtype;
96 $authorised_lib{$itemtype} = $description;
98 $value = $itemtype unless ($value);
100 #---- "true" authorised value
103 $authorised_values_sth->execute(
104 $tagslib->{$tag}->{$subfield}->{authorised_value
} );
106 push @authorised_values, ""
107 unless ( $tagslib->{$tag}->{$subfield}->{mandatory
} );
109 while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
110 push @authorised_values, $value;
111 $authorised_lib{$value} = $lib;
114 return CGI
::scrolling_list
(
115 -name
=> "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
116 -values => \
@authorised_values,
118 -labels
=> \
%authorised_lib,
123 -id
=> "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
124 -class => "input_marceditor",
130 builds the <input ...> entry for a subfield.
134 my ( $tag, $subfield, $value, $index_tag, $tabloop, $rec, $authorised_values_sth,$cgi ) = @_;
136 my $index_subfield = CreateKey
(); # create a specifique key for each subfield
138 $value =~ s/"/"/g;
140 # if there is no value provided but a default value in parameters, get it
142 $value = $tagslib->{$tag}->{$subfield}->{defaultvalue
};
144 # get today date & replace YYYY, MM, DD if provided in the default value
145 my ( $year, $month, $day ) = Today
();
146 $month = sprintf( "%02d", $month );
147 $day = sprintf( "%02d", $day );
148 $value =~ s/YYYY/$year/g;
149 $value =~ s/MM/$month/g;
150 $value =~ s/DD/$day/g;
152 my $dbh = C4
::Context
->dbh;
153 my %subfield_data = (
155 subfield
=> $subfield,
156 marc_lib
=> substr( $tagslib->{$tag}->{$subfield}->{lib
}, 0, 22 ),
157 marc_lib_plain
=> $tagslib->{$tag}->{$subfield}->{lib
},
158 tag_mandatory
=> $tagslib->{$tag}->{mandatory
},
159 mandatory
=> $tagslib->{$tag}->{$subfield}->{mandatory
},
160 repeatable
=> $tagslib->{$tag}->{$subfield}->{repeatable
},
161 kohafield
=> $tagslib->{$tag}->{$subfield}->{kohafield
},
163 id
=> "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
166 if($subfield eq '@'){
167 $subfield_data{id
} = "tag_".$tag."_subfield_00_".$index_tag."_".$index_subfield;
169 $subfield_data{id
} = "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield;
173 $subfield_data{visibility
} = "display:none;"
174 if ( ($tagslib->{$tag}->{$subfield}->{hidden
} % 2 == 1) and $value ne ''
175 or ($value eq '' and !$tagslib->{$tag}->{$subfield}->{mandatory
})
178 # it's an authorised field
179 if ( $tagslib->{$tag}->{$subfield}->{authorised_value
} ) {
180 $subfield_data{marc_value
} =
181 build_authorized_values_list
( $tag, $subfield, $value, $dbh,
182 $authorised_values_sth,$index_tag,$index_subfield );
184 # it's a thesaurus / authority field
186 elsif ( $tagslib->{$tag}->{$subfield}->{authtypecode
} ) {
187 $subfield_data{marc_value
} =
188 "<input type=\"text\"
189 id=\"".$subfield_data{id
}."\"
190 name=\"".$subfield_data{id
}."\"
192 class=\"input_marceditor\"
195 <span class=\"buttonDot\"
196 onclick=\"Dopop('/cgi-bin/koha/authorities/auth_finder.pl?authtypecode=".$tagslib->{$tag}->{$subfield}->{authtypecode
}."&index=$subfield_data{id}','$subfield_data{id}')\">...</span>
198 # it's a plugin field
200 elsif ( $tagslib->{$tag}->{$subfield}->{'value_builder'} ) {
202 # opening plugin. Just check wether we are on a developper computer on a production one
203 # (the cgidir differs)
204 my $cgidir = C4
::Context
->intranetdir . "/cgi-bin/cataloguing/value_builder";
205 unless (-r
$cgidir and -d
$cgidir) {
206 $cgidir = C4
::Context
->intranetdir . "/cataloguing/value_builder";
208 my $plugin = $cgidir . "/" . $tagslib->{$tag}->{$subfield}->{'value_builder'};
209 do $plugin || die "Plugin Failed: ".$plugin;
210 my $extended_param = plugin_parameters
( $dbh, $rec, $tagslib, $subfield_data{id
}, $tabloop );
211 my ( $function_name, $javascript ) = plugin_javascript
( $dbh, $rec, $tagslib, $subfield_data{id
}, $tabloop );
212 # my ( $function_name, $javascript,$extended_param );
214 $subfield_data{marc_value
} =
215 "<input tabindex=\"1\"
217 id=".$subfield_data{id
}."
218 name=".$subfield_data{id
}."
220 class=\"input_marceditor\"
221 onfocus=\"javascript:Focus$function_name($index_tag)\"
222 onblur=\"javascript:Blur$function_name($index_tag); \" \/>
223 <span class=\"buttonDot\"
224 onclick=\"Clic$function_name('$subfield_data{id}')\">...</a>
226 # it's an hidden field
228 elsif ( $tag eq '' ) {
229 $subfield_data{marc_value
} =
230 "<input tabindex=\"1\"
232 id=".$subfield_data{id
}."
233 name=".$subfield_data{id
}."
237 elsif ( $tagslib->{$tag}->{$subfield}->{'hidden'} ) {
238 $subfield_data{marc_value
} =
239 "<input type=\"text\"
240 id=".$subfield_data{id
}."
241 name=".$subfield_data{id
}."
242 class=\"input_marceditor\"
247 # it's a standard field
253 ( C4
::Context
->preference("marcflavour") eq "UNIMARC" && $tag >= 300
254 and $tag < 400 && $subfield eq 'a' )
257 && C4
::Context
->preference("marcflavour") eq "MARC21" )
260 $subfield_data{marc_value
} =
261 "<textarea cols=\"70\"
263 id=".$subfield_data{id
}."
264 name=".$subfield_data{id
}."
265 class=\"input_marceditor\"
271 $subfield_data{marc_value
} =
272 "<input type=\"text\"
273 id=".$subfield_data{id
}."
274 name=".$subfield_data{id
}."
277 class=\"input_marceditor\"
282 $subfield_data{'index_subfield'} = $index_subfield;
283 return \
%subfield_data;
288 Create a random value to set it into the input name
293 return int(rand(1000000));
296 sub build_tabs
($$$$$) {
297 my ( $template, $record, $dbh, $encoding,$input ) = @_;
303 my $authorised_values_sth = $dbh->prepare(
304 "select authorised_value,lib
305 from authorised_values
306 where category=? order by lib"
309 # in this array, we will push all the 10 tabs
310 # to avoid having 10 tabs in the template : they will all be in the same BIG_LOOP
313 my @tab_data; # all tags to display
315 foreach my $used ( keys %$tagslib ){
316 push @tab_data,$used if not $seen{$used};
321 # loop through each tab 0 through 9
322 for ( my $tabloop = 0 ; $tabloop <= $max_num_tab ; $tabloop++ ) {
323 my @loop_data = (); #innerloop in the template.
325 foreach my $tag (sort @tab_data) {
328 my $index_tag = CreateKey
;
330 # if MARC::Record is not empty =>use it as master loop, then add missing subfields that should be in the tab.
331 # if MARC::Record is empty => use tab as master loop.
332 if ( $record != -1 && ( $record->field($tag) || $tag eq '000' ) ) {
334 if ( $tag ne '000' ) {
335 @fields = $record->field($tag);
338 push @fields, MARC
::Field
->new('000', $record->leader()); # if tag == 000
340 # loop through each field
341 foreach my $field (@fields) {
343 if ($field->tag()<10) {
346 $tagslib->{ $field->tag() }->{ '@' }->{tab
}
348 next if ($tagslib->{$field->tag()}->{'@'}->{hidden
});
350 $subfield_data{marc_lib
}=$tagslib->{$field->tag()}->{'@'}->{lib
};
351 $subfield_data{marc_value
}=$field->data();
352 $subfield_data{marc_subfield
}='@';
353 $subfield_data{marc_tag
}=$field->tag();
354 push(@subfields_data, \
%subfield_data);
356 my @subf=$field->subfields;
357 # loop through each subfield
358 for my $i (0..$#subf) {
359 $subf[$i][0] = "@" unless $subf[$i][0];
362 $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{tab
}
365 if ( $tagslib->{ $field->tag() }->{ $subf[$i][0] }
368 $subfield_data{marc_lib
}=$tagslib->{$field->tag()}->{$subf[$i][0]}->{lib
};
369 if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{isurl
}) {
370 $subfield_data{marc_value
}="<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
372 $subfield_data{marc_value
}=$subf[$i][1];
374 $subfield_data{short_desc
} = substr(
375 $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{lib
},
378 $subfield_data{long_desc
} =
379 $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{lib
};
380 $subfield_data{marc_subfield
}=$subf[$i][0];
381 $subfield_data{marc_tag
}=$field->tag();
382 push(@subfields_data, \
%subfield_data);
385 if ($#subfields_data>=0) {
387 $tag_data{tag
}=$field->tag(). ' '
388 . C4
::Koha
::display_marc_indicators
($field)
390 . $tagslib->{$field->tag()}->{lib
};
391 $tag_data{subfield
} = \
@subfields_data;
392 push (@loop_data, \
%tag_data);
397 if ( $#loop_data >= 0 ) {
400 innerloop
=> \
@loop_data,
404 $template->param( singletab
=> (scalar(@BIG_LOOP)==1), BIG_LOOP
=> \
@BIG_LOOP );
411 my $dbh=C4
::Context
->dbh;
414 my ($template, $loggedinuser, $cookie)
415 = get_template_and_user
({template_name
=> "authorities/detail.tmpl",
418 authnotrequired
=> 0,
419 flagsrequired
=> {catalogue
=> 1},
423 my $authid = $query->param('authid');
427 my $authtypecode = &GetAuthTypeCode
($authid);
428 $tagslib = &GetTagsLabels
(1,$authtypecode);
431 if (C4
::Context
->preference("AuthDisplayHierarchy")){
432 my $trees=BuildUnimarcHierarchies
($authid);
433 my @trees = split /;/,$trees ;
434 push @trees,$trees unless (@trees);
436 foreach my $tree (@trees){
437 my @tree=split /,/,$tree;
438 push @tree,$tree unless (@tree);
441 foreach my $element (@tree){
442 my $elementdata = GetAuthority
($element);
443 $record= $elementdata if ($authid==$element);
444 push @loophierarchy, BuildUnimarcHierarchy
($elementdata,"child".$cnt, $authid);
447 push @loophierarchies, { 'loopelement' =>\
@loophierarchy};
450 'displayhierarchy' =>C4
::Context
->preference("AuthDisplayHierarchy"),
451 'loophierarchies' =>\
@loophierarchies,
454 $record=GetAuthority
($authid);
456 my $count = CountUsage
($authid);
458 # find the marc field/subfield used in biblio by this authority
459 my $sth = $dbh->prepare("select distinct tagfield from marc_subfield_structure where authtypecode=?");
460 $sth->execute($authtypecode);
462 while (my ($tagfield) = $sth->fetchrow) {
463 $biblio_fields.= $tagfield."9,";
471 # loop through each tab 0 through 9
472 # for (my $tabloop = 0; $tabloop<=10;$tabloop++) {
473 # loop through each tag
474 build_tabs
($template, $record, $dbh,"",$query);
476 my $authtypes = getauthtypes
;
478 foreach my $thisauthtype (sort { $authtypes->{$b} cmp $authtypes->{$a} } keys %$authtypes) {
479 my %row =(value
=> $thisauthtype,
480 selected
=> $thisauthtype eq $authtypecode,
481 authtypetext
=> $authtypes->{$thisauthtype}{'authtypetext'},
483 push @authtypesloop, \
%row;
486 $template->param(authid
=> $authid,
488 biblio_fields
=> $biblio_fields,
489 authtypetext
=> $authtypes->{$authtypecode}{'authtypetext'},
490 authtypesloop
=> \
@authtypesloop,
492 output_html_with_http_headers
$query, $cookie, $template->output;