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 with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA 02111-1307 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
43 use C4::AuthoritiesMarc;
56 ($indicators, $value) = find_value($tag, $subfield, $record,$encoding);
58 Find the given $subfield in the given $tag in the given
59 MARC::Record $record. If the subfield is found, returns
60 the (indicators, value) pair; otherwise, (undef, undef) is
66 my ($tagfield,$insubfield,$record,$encoding) = @_;
70 if ($record->field($tagfield)) {
71 push @result, $record->field($tagfield)->data();
76 foreach my $field ($record->field($tagfield)) {
77 my @subfields = $field->subfields();
78 foreach my $subfield (@subfields) {
79 if (@$subfield[0] eq $insubfield) {
80 push @result,@$subfield[1];
81 $indicator = $field->indicator(1).$field->indicator(2);
86 return($indicator,@result);
90 =item build_authorized_values_list
94 sub build_authorized_values_list ($$$$$$$) {
95 my ( $tag, $subfield, $value, $dbh, $authorised_values_sth,$index_tag,$index_subfield ) = @_;
97 my @authorised_values;
100 # builds list, depending on authorised value...
103 if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
106 "select branchcode,branchname from branches order by branchname");
108 push @authorised_values, ""
109 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
111 while ( my ( $branchcode, $branchname ) = $sth->fetchrow_array ) {
112 push @authorised_values, $branchcode;
113 $authorised_lib{$branchcode} = $branchname;
118 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes" ) {
121 "select itemtype,description from itemtypes order by description");
123 push @authorised_values, ""
124 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
128 while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) {
129 push @authorised_values, $itemtype;
130 $authorised_lib{$itemtype} = $description;
132 $value = $itemtype unless ($value);
134 #---- "true" authorised value
137 $authorised_values_sth->execute(
138 $tagslib->{$tag}->{$subfield}->{authorised_value} );
140 push @authorised_values, ""
141 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
143 while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
144 push @authorised_values, $value;
145 $authorised_lib{$value} = $lib;
148 return CGI::scrolling_list(
149 -name => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
150 -values => \@authorised_values,
152 -labels => \%authorised_lib,
157 -id => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
158 -class => "input_marceditor",
164 builds the <input ...> entry for a subfield.
168 my ( $tag, $subfield, $value, $index_tag, $tabloop, $rec, $authorised_values_sth,$cgi ) = @_;
170 my $index_subfield = CreateKey(); # create a specifique key for each subfield
172 $value =~ s/"/"/g;
174 # if there is no value provided but a default value in parameters, get it
176 $value = $tagslib->{$tag}->{$subfield}->{defaultvalue};
178 # get today date & replace YYYY, MM, DD if provided in the default value
179 my ( $year, $month, $day ) = Today();
180 $month = sprintf( "%02d", $month );
181 $day = sprintf( "%02d", $day );
182 $value =~ s/YYYY/$year/g;
183 $value =~ s/MM/$month/g;
184 $value =~ s/DD/$day/g;
186 my $dbh = C4::Context->dbh;
187 my %subfield_data = (
189 subfield => $subfield,
190 marc_lib => substr( $tagslib->{$tag}->{$subfield}->{lib}, 0, 22 ),
191 marc_lib_plain => $tagslib->{$tag}->{$subfield}->{lib},
192 tag_mandatory => $tagslib->{$tag}->{mandatory},
193 mandatory => $tagslib->{$tag}->{$subfield}->{mandatory},
194 repeatable => $tagslib->{$tag}->{$subfield}->{repeatable},
195 kohafield => $tagslib->{$tag}->{$subfield}->{kohafield},
197 id => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
200 if($subfield eq '@'){
201 $subfield_data{id} = "tag_".$tag."_subfield_00_".$index_tag."_".$index_subfield;
203 $subfield_data{id} = "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield;
207 $subfield_data{visibility} = "display:none;"
208 if ( ($tagslib->{$tag}->{$subfield}->{hidden} % 2 == 1) and $value ne ''
209 or ($value eq '' and !$tagslib->{$tag}->{$subfield}->{mandatory})
212 # it's an authorised field
213 if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) {
214 $subfield_data{marc_value} =
215 build_authorized_values_list( $tag, $subfield, $value, $dbh,
216 $authorised_values_sth,$index_tag,$index_subfield );
218 # it's a thesaurus / authority field
220 elsif ( $tagslib->{$tag}->{$subfield}->{authtypecode} ) {
221 $subfield_data{marc_value} =
222 "<input type=\"text\"
223 id=\"".$subfield_data{id}."\"
224 name=\"".$subfield_data{id}."\"
226 class=\"input_marceditor\"
229 <span class=\"buttonDot\"
230 onclick=\"Dopop('/cgi-bin/koha/authorities/auth_finder.pl?authtypecode=".$tagslib->{$tag}->{$subfield}->{authtypecode}."&index=$subfield_data{id}','$subfield_data{id}')\">...</span>
232 # it's a plugin field
234 elsif ( $tagslib->{$tag}->{$subfield}->{'value_builder'} ) {
236 # opening plugin. Just check wether we are on a developper computer on a production one
237 # (the cgidir differs)
238 my $cgidir = C4::Context->intranetdir . "/cgi-bin/cataloguing/value_builder";
239 unless ( opendir( DIR, "$cgidir" ) ) {
240 $cgidir = C4::Context->intranetdir . "/cataloguing/value_builder";
242 my $plugin = $cgidir . "/" . $tagslib->{$tag}->{$subfield}->{'value_builder'};
243 do $plugin || die "Plugin Failed: ".$plugin;
244 my $extended_param = plugin_parameters( $dbh, $rec, $tagslib, $subfield_data{id}, $tabloop );
245 my ( $function_name, $javascript ) = plugin_javascript( $dbh, $rec, $tagslib, $subfield_data{id}, $tabloop );
246 # my ( $function_name, $javascript,$extended_param );
248 $subfield_data{marc_value} =
249 "<input tabindex=\"1\"
251 id=".$subfield_data{id}."
252 name=".$subfield_data{id}."
254 class=\"input_marceditor\"
255 onfocus=\"javascript:Focus$function_name($index_tag)\"
256 onblur=\"javascript:Blur$function_name($index_tag); \" \/>
257 <span class=\"buttonDot\"
258 onclick=\"Clic$function_name('$subfield_data{id}')\">...</a>
260 # it's an hidden field
262 elsif ( $tag eq '' ) {
263 $subfield_data{marc_value} =
264 "<input tabindex=\"1\"
266 id=".$subfield_data{id}."
267 name=".$subfield_data{id}."
271 elsif ( $tagslib->{$tag}->{$subfield}->{'hidden'} ) {
272 $subfield_data{marc_value} =
273 "<input type=\"text\"
274 id=".$subfield_data{id}."
275 name=".$subfield_data{id}."
276 class=\"input_marceditor\"
281 # it's a standard field
287 ( C4::Context->preference("marcflavour") eq "UNIMARC" && $tag >= 300
288 and $tag < 400 && $subfield eq 'a' )
291 && C4::Context->preference("marcflavour") eq "MARC21" )
294 $subfield_data{marc_value} =
295 "<textarea cols=\"70\"
297 id=".$subfield_data{id}."
298 name=".$subfield_data{id}."
299 class=\"input_marceditor\"
305 $subfield_data{marc_value} =
306 "<input type=\"text\"
307 id=".$subfield_data{id}."
308 name=".$subfield_data{id}."
311 class=\"input_marceditor\"
316 $subfield_data{'index_subfield'} = $index_subfield;
317 return \%subfield_data;
322 Create a random value to set it into the input name
327 return int(rand(1000000));
330 sub build_tabs ($$$$$) {
331 my ( $template, $record, $dbh, $encoding,$input ) = @_;
337 my $authorised_values_sth = $dbh->prepare(
338 "select authorised_value,lib
339 from authorised_values
340 where category=? order by lib"
343 # in this array, we will push all the 10 tabs
344 # to avoid having 10 tabs in the template : they will all be in the same BIG_LOOP
347 my @tab_data; # all tags to display
349 foreach my $used ( keys %$tagslib ){
350 push @tab_data,$used if not $seen{$used};
355 # loop through each tab 0 through 9
356 for ( my $tabloop = 0 ; $tabloop <= $max_num_tab ; $tabloop++ ) {
357 my @loop_data = (); #innerloop in the template.
359 foreach my $tag (sort @tab_data) {
363 my $index_tag = CreateKey;
365 # if MARC::Record is not empty =>use it as master loop, then add missing subfields that should be in the tab.
366 # if MARC::Record is empty => use tab as master loop.
367 if ( $record ne -1 && ( $record->field($tag) || $tag eq '000' ) ) {
369 if ( $tag ne '000' ) {
370 @fields = $record->field($tag);
373 push @fields, $record->leader(); # if tag == 000
375 # loop through each field
376 foreach my $field (@fields) {
378 if ($field->tag()<10) {
381 $tagslib->{ $field->tag() }->{ '@' }->{tab}
383 next if ($tagslib->{$field->tag()}->{'@'}->{hidden});
385 $subfield_data{marc_lib}=$tagslib->{$field->tag()}->{'@'}->{lib};
386 $subfield_data{marc_value}=$field->data();
387 $subfield_data{marc_subfield}='@';
388 $subfield_data{marc_tag}=$field->tag();
389 push(@subfields_data, \%subfield_data);
391 my @subf=$field->subfields;
392 # loop through each subfield
393 for my $i (0..$#subf) {
394 $subf[$i][0] = "@" unless $subf[$i][0];
397 $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{tab}
400 if ( $tagslib->{ $field->tag() }->{ $subf[$i][0] }
403 $subfield_data{marc_lib}=$tagslib->{$field->tag()}->{$subf[$i][0]}->{lib};
404 if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{isurl}) {
405 $subfield_data{marc_value}="<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
407 $subfield_data{marc_value}=$subf[$i][1];
409 $subfield_data{short_desc} = substr(
410 $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{lib},
413 $subfield_data{long_desc} =
414 $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{lib};
415 $subfield_data{marc_subfield}=$subf[$i][0];
416 $subfield_data{marc_tag}=$field->tag();
417 push(@subfields_data, \%subfield_data);
420 if ($#subfields_data>=0) {
422 $tag_data{tag}=$field->tag().' - '. $tagslib->{$field->tag()}->{lib};
423 $tag_data{subfield} = \@subfields_data;
424 push (@loop_data, \%tag_data);
429 if ( $#loop_data >= 0 ) {
432 innerloop => \@loop_data,
436 $template->param( singletab => (scalar(@BIG_LOOP)==1), BIG_LOOP => \@BIG_LOOP );
443 my $dbh=C4::Context->dbh;
446 my ($template, $loggedinuser, $cookie)
447 = get_template_and_user({template_name => "authorities/detail.tmpl",
450 authnotrequired => 0,
451 flagsrequired => {catalogue => 1},
455 my $authid = $query->param('authid');
459 my $authtypecode = &GetAuthTypeCode($authid);
460 $tagslib = &GetTagsLabels(1,$authtypecode);
463 if (C4::Context->preference("AuthDisplayHierarchy")){
464 my $trees=BuildUnimarcHierarchies($authid);
465 my @trees = split /;/,$trees ;
466 push @trees,$trees unless (@trees);
468 foreach my $tree (@trees){
469 my @tree=split /,/,$tree;
470 push @tree,$tree unless (@tree);
473 foreach my $element (@tree){
475 my $elementdata = GetAuthority($element);
476 $record= $elementdata if ($authid==$element);
477 push @loophierarchy, BuildUnimarcHierarchy($elementdata,"child".$cnt, $authid);
480 push @loophierarchies, { 'loopelement' =>\@loophierarchy};
483 'displayhierarchy' =>C4::Context->preference("AuthDisplayHierarchy"),
484 'loophierarchies' =>\@loophierarchies,
487 $record=GetAuthority($authid);
489 my $count = CountUsage($authid);
491 # find the marc field/subfield used in biblio by this authority
492 my $sth = $dbh->prepare("select distinct tagfield from marc_subfield_structure where authtypecode=?");
493 $sth->execute($authtypecode);
495 while (my ($tagfield) = $sth->fetchrow) {
496 $biblio_fields.= $tagfield."9,";
504 # loop through each tab 0 through 9
505 # for (my $tabloop = 0; $tabloop<=10;$tabloop++) {
506 # loop through each tag
507 build_tabs ($template, $record, $dbh,"",$query);
509 my $authtypes = getauthtypes;
511 foreach my $thisauthtype (keys %$authtypes) {
512 my $selected = 1 if $thisauthtype eq $authtypecode;
513 my %row =(value => $thisauthtype,
514 selected => $selected,
515 authtypetext => $authtypes->{$thisauthtype}{'authtypetext'},
517 push @authtypesloop, \%row;
520 $template->param(authid => $authid,
522 biblio_fields => $biblio_fields,
523 authtypetext => $authtypes->{$authtypecode}{'authtypetext'},
524 authtypesloop => \@authtypesloop,
526 output_html_with_http_headers $query, $cookie, $template->output;