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;
55 =item build_authorized_values_list
59 sub build_authorized_values_list ($$$$$$$) {
60 my ( $tag, $subfield, $value, $dbh, $authorised_values_sth,$index_tag,$index_subfield ) = @_;
62 my @authorised_values;
65 # builds list, depending on authorised value...
68 if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
71 "select branchcode,branchname from branches order by branchname");
73 push @authorised_values, ""
74 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
76 while ( my ( $branchcode, $branchname ) = $sth->fetchrow_array ) {
77 push @authorised_values, $branchcode;
78 $authorised_lib{$branchcode} = $branchname;
83 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes" ) {
86 "select itemtype,description from itemtypes order by description");
88 push @authorised_values, ""
89 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
93 while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) {
94 push @authorised_values, $itemtype;
95 $authorised_lib{$itemtype} = $description;
97 $value = $itemtype unless ($value);
99 #---- "true" authorised value
102 $authorised_values_sth->execute(
103 $tagslib->{$tag}->{$subfield}->{authorised_value} );
105 push @authorised_values, ""
106 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
108 while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
109 push @authorised_values, $value;
110 $authorised_lib{$value} = $lib;
113 return CGI::scrolling_list(
114 -name => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
115 -values => \@authorised_values,
117 -labels => \%authorised_lib,
122 -id => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
123 -class => "input_marceditor",
129 builds the <input ...> entry for a subfield.
133 my ( $tag, $subfield, $value, $index_tag, $tabloop, $rec, $authorised_values_sth,$cgi ) = @_;
135 my $index_subfield = CreateKey(); # create a specifique key for each subfield
137 $value =~ s/"/"/g;
139 # if there is no value provided but a default value in parameters, get it
141 $value = $tagslib->{$tag}->{$subfield}->{defaultvalue};
143 # get today date & replace YYYY, MM, DD if provided in the default value
144 my ( $year, $month, $day ) = Today();
145 $month = sprintf( "%02d", $month );
146 $day = sprintf( "%02d", $day );
147 $value =~ s/YYYY/$year/g;
148 $value =~ s/MM/$month/g;
149 $value =~ s/DD/$day/g;
151 my $dbh = C4::Context->dbh;
152 my %subfield_data = (
154 subfield => $subfield,
155 marc_lib => substr( $tagslib->{$tag}->{$subfield}->{lib}, 0, 22 ),
156 marc_lib_plain => $tagslib->{$tag}->{$subfield}->{lib},
157 tag_mandatory => $tagslib->{$tag}->{mandatory},
158 mandatory => $tagslib->{$tag}->{$subfield}->{mandatory},
159 repeatable => $tagslib->{$tag}->{$subfield}->{repeatable},
160 kohafield => $tagslib->{$tag}->{$subfield}->{kohafield},
162 id => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
165 if($subfield eq '@'){
166 $subfield_data{id} = "tag_".$tag."_subfield_00_".$index_tag."_".$index_subfield;
168 $subfield_data{id} = "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield;
172 $subfield_data{visibility} = "display:none;"
173 if ( ($tagslib->{$tag}->{$subfield}->{hidden} % 2 == 1) and $value ne ''
174 or ($value eq '' and !$tagslib->{$tag}->{$subfield}->{mandatory})
177 # it's an authorised field
178 if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) {
179 $subfield_data{marc_value} =
180 build_authorized_values_list( $tag, $subfield, $value, $dbh,
181 $authorised_values_sth,$index_tag,$index_subfield );
183 # it's a thesaurus / authority field
185 elsif ( $tagslib->{$tag}->{$subfield}->{authtypecode} ) {
186 $subfield_data{marc_value} =
187 "<input type=\"text\"
188 id=\"".$subfield_data{id}."\"
189 name=\"".$subfield_data{id}."\"
191 class=\"input_marceditor\"
194 <span class=\"buttonDot\"
195 onclick=\"Dopop('/cgi-bin/koha/authorities/auth_finder.pl?authtypecode=".$tagslib->{$tag}->{$subfield}->{authtypecode}."&index=$subfield_data{id}','$subfield_data{id}')\">...</span>
197 # it's a plugin field
199 elsif ( $tagslib->{$tag}->{$subfield}->{'value_builder'} ) {
201 # opening plugin. Just check wether we are on a developper computer on a production one
202 # (the cgidir differs)
203 my $cgidir = C4::Context->intranetdir . "/cgi-bin/cataloguing/value_builder";
204 unless (-r $cgidir and -d $cgidir) {
205 $cgidir = C4::Context->intranetdir . "/cataloguing/value_builder";
207 my $plugin = $cgidir . "/" . $tagslib->{$tag}->{$subfield}->{'value_builder'};
208 do $plugin || die "Plugin Failed: ".$plugin;
209 my $extended_param = plugin_parameters( $dbh, $rec, $tagslib, $subfield_data{id}, $tabloop );
210 my ( $function_name, $javascript ) = plugin_javascript( $dbh, $rec, $tagslib, $subfield_data{id}, $tabloop );
211 # my ( $function_name, $javascript,$extended_param );
213 $subfield_data{marc_value} =
214 "<input tabindex=\"1\"
216 id=".$subfield_data{id}."
217 name=".$subfield_data{id}."
219 class=\"input_marceditor\"
220 onfocus=\"javascript:Focus$function_name($index_tag)\"
221 onblur=\"javascript:Blur$function_name($index_tag); \" \/>
222 <span class=\"buttonDot\"
223 onclick=\"Clic$function_name('$subfield_data{id}')\">...</a>
225 # it's an hidden field
227 elsif ( $tag eq '' ) {
228 $subfield_data{marc_value} =
229 "<input tabindex=\"1\"
231 id=".$subfield_data{id}."
232 name=".$subfield_data{id}."
236 elsif ( $tagslib->{$tag}->{$subfield}->{'hidden'} ) {
237 $subfield_data{marc_value} =
238 "<input type=\"text\"
239 id=".$subfield_data{id}."
240 name=".$subfield_data{id}."
241 class=\"input_marceditor\"
246 # it's a standard field
252 ( C4::Context->preference("marcflavour") eq "UNIMARC" && $tag >= 300
253 and $tag < 400 && $subfield eq 'a' )
256 && C4::Context->preference("marcflavour") eq "MARC21" )
259 $subfield_data{marc_value} =
260 "<textarea cols=\"70\"
262 id=".$subfield_data{id}."
263 name=".$subfield_data{id}."
264 class=\"input_marceditor\"
270 $subfield_data{marc_value} =
271 "<input type=\"text\"
272 id=".$subfield_data{id}."
273 name=".$subfield_data{id}."
276 class=\"input_marceditor\"
281 $subfield_data{'index_subfield'} = $index_subfield;
282 return \%subfield_data;
287 Create a random value to set it into the input name
292 return int(rand(1000000));
295 sub build_tabs ($$$$$) {
296 my ( $template, $record, $dbh, $encoding,$input ) = @_;
302 my $authorised_values_sth = $dbh->prepare(
303 "select authorised_value,lib
304 from authorised_values
305 where category=? order by lib"
308 # in this array, we will push all the 10 tabs
309 # to avoid having 10 tabs in the template : they will all be in the same BIG_LOOP
312 my @tab_data; # all tags to display
314 foreach my $used ( keys %$tagslib ){
315 push @tab_data,$used if not $seen{$used};
320 # loop through each tab 0 through 9
321 for ( my $tabloop = 0 ; $tabloop <= $max_num_tab ; $tabloop++ ) {
322 my @loop_data = (); #innerloop in the template.
324 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 ne -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){
443 my $elementdata = GetAuthority($element);
444 $record= $elementdata if ($authid==$element);
445 push @loophierarchy, BuildUnimarcHierarchy($elementdata,"child".$cnt, $authid);
448 push @loophierarchies, { 'loopelement' =>\@loophierarchy};
451 'displayhierarchy' =>C4::Context->preference("AuthDisplayHierarchy"),
452 'loophierarchies' =>\@loophierarchies,
455 $record=GetAuthority($authid);
457 my $count = CountUsage($authid);
459 # find the marc field/subfield used in biblio by this authority
460 my $sth = $dbh->prepare("select distinct tagfield from marc_subfield_structure where authtypecode=?");
461 $sth->execute($authtypecode);
463 while (my ($tagfield) = $sth->fetchrow) {
464 $biblio_fields.= $tagfield."9,";
472 # loop through each tab 0 through 9
473 # for (my $tabloop = 0; $tabloop<=10;$tabloop++) {
474 # loop through each tag
475 build_tabs ($template, $record, $dbh,"",$query);
477 my $authtypes = getauthtypes;
479 foreach my $thisauthtype (sort { $authtypes->{$b} cmp $authtypes->{$a} } keys %$authtypes) {
480 my $selected = 1 if $thisauthtype eq $authtypecode;
481 my %row =(value => $thisauthtype,
482 selected => $selected,
483 authtypetext => $authtypes->{$thisauthtype}{'authtypetext'},
485 push @authtypesloop, \%row;
488 $template->param(authid => $authid,
490 biblio_fields => $biblio_fields,
491 authtypetext => $authtypes->{$authtypecode}{'authtypetext'},
492 authtypesloop => \@authtypesloop,
494 output_html_with_http_headers $query, $cookie, $template->output;