memberentry.pl - $debug-ify warn messages
[koha.git] / authorities / detail.pl
blob2ed7f272307f42534ae057919158717132e13b6f
1 #!/usr/bin/perl
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
10 # version.
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
20 =head1 NAME
22 detail.pl : script to show an authority in MARC format
24 =head1 SYNOPSIS
27 =head1 DESCRIPTION
29 This script needs an authid
31 It shows the authority in a (nice) MARC format depending on authority MARC
32 parameters tables.
34 =head1 FUNCTIONS
36 =over 2
38 =cut
41 use strict;
42 require Exporter;
43 use C4::AuthoritiesMarc;
44 use C4::Auth;
45 use C4::Context;
46 use C4::Output;
47 use CGI;
48 use MARC::Record;
49 use C4::Koha;
50 # use C4::Biblio;
51 # use C4::Catalogue;
53 our ($tagslib);
54 =item find_value
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
61 returned.
63 =cut
65 sub find_value {
66 my ($tagfield,$insubfield,$record,$encoding) = @_;
67 my @result;
68 my $indicator;
69 if ($tagfield <10) {
70 if ($record->field($tagfield)) {
71 push @result, $record->field($tagfield)->data();
72 } else {
73 push @result,"";
75 } else {
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
92 =cut
94 sub build_authorized_values_list ($$$$$$$) {
95 my ( $tag, $subfield, $value, $dbh, $authorised_values_sth,$index_tag,$index_subfield ) = @_;
97 my @authorised_values;
98 my %authorised_lib;
100 # builds list, depending on authorised value...
102 #---- branch
103 if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
104 my $sth =
105 $dbh->prepare(
106 "select branchcode,branchname from branches order by branchname");
107 $sth->execute;
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;
116 #----- itemtypes
118 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes" ) {
119 my $sth =
120 $dbh->prepare(
121 "select itemtype,description from itemtypes order by description");
122 $sth->execute;
123 push @authorised_values, ""
124 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
126 my $itemtype;
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
136 else {
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,
151 -default => $value,
152 -labels => \%authorised_lib,
153 -override => 1,
154 -size => 1,
155 -multiple => 0,
156 -tabindex => 1,
157 -id => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
158 -class => "input_marceditor",
163 =item create_input
164 builds the <input ...> entry for a subfield.
165 =cut
167 sub create_input {
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/"/&quot;/g;
174 # if there is no value provided but a default value in parameters, get it
175 unless ($value) {
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 = (
188 tag => $tag,
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},
196 index => $index_tag,
197 id => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
198 value => $value,
200 if($subfield eq '@'){
201 $subfield_data{id} = "tag_".$tag."_subfield_00_".$index_tag."_".$index_subfield;
202 } else {
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}."\"
225 value=\"$value\"
226 class=\"input_marceditor\"
227 tabindex=\"1\"
228 DISABLE READONLY \/>
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 (-r $cgidir and -d $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\"
250 type=\"text\"
251 id=".$subfield_data{id}."
252 name=".$subfield_data{id}."
253 value=\"$value\"
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>
259 $javascript";
260 # it's an hidden field
262 elsif ( $tag eq '' ) {
263 $subfield_data{marc_value} =
264 "<input tabindex=\"1\"
265 type=\"hidden\"
266 id=".$subfield_data{id}."
267 name=".$subfield_data{id}."
268 value=\"$value\" \/>
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\"
277 tabindex=\"1\"
278 value=\"$value\"
279 \/>";
281 # it's a standard field
283 else {
284 if (
285 length($value) > 100
287 ( C4::Context->preference("marcflavour") eq "UNIMARC" && $tag >= 300
288 and $tag < 400 && $subfield eq 'a' )
289 or ( $tag >= 500
290 and $tag < 600
291 && C4::Context->preference("marcflavour") eq "MARC21" )
294 $subfield_data{marc_value} =
295 "<textarea cols=\"70\"
296 rows=\"4\"
297 id=".$subfield_data{id}."
298 name=".$subfield_data{id}."
299 class=\"input_marceditor\"
300 tabindex=\"1\"
301 >$value</textarea>
304 else {
305 $subfield_data{marc_value} =
306 "<input type=\"text\"
307 id=".$subfield_data{id}."
308 name=".$subfield_data{id}."
309 value=\"$value\"
310 tabindex=\"1\"
311 class=\"input_marceditor\"
316 $subfield_data{'index_subfield'} = $index_subfield;
317 return \%subfield_data;
320 =item CreateKey
322 Create a random value to set it into the input name
324 =cut
326 sub CreateKey(){
327 return int(rand(1000000));
330 sub build_tabs ($$$$$) {
331 my ( $template, $record, $dbh, $encoding,$input ) = @_;
333 # fill arrays
334 my @loop_data = ();
335 my $tag;
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
345 my @BIG_LOOP;
346 my %seen;
347 my @tab_data; # all tags to display
349 foreach my $used ( keys %$tagslib ){
350 push @tab_data,$used if not $seen{$used};
351 $seen{$used}++;
354 my $max_num_tab=9;
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.
358 my $i = 0;
359 foreach my $tag (sort @tab_data) {
360 $i++;
361 next if ! $tag;
362 my $indicator;
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' ) ) {
368 my @fields;
369 if ( $tag ne '000' ) {
370 @fields = $record->field($tag);
372 else {
373 push @fields, MARC::Field->new('000', $record->leader()); # if tag == 000
375 # loop through each field
376 foreach my $field (@fields) {
377 my @subfields_data;
378 if ($field->tag()<10) {
379 next
380 if (
381 $tagslib->{ $field->tag() }->{ '@' }->{tab}
382 ne $tabloop );
383 next if ($tagslib->{$field->tag()}->{'@'}->{hidden});
384 my %subfield_data;
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);
390 } else {
391 my @subf=$field->subfields;
392 # loop through each subfield
393 for my $i (0..$#subf) {
394 $subf[$i][0] = "@" unless $subf[$i][0];
395 next
396 if (
397 $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{tab}
398 ne $tabloop );
399 next
400 if ( $tagslib->{ $field->tag() }->{ $subf[$i][0] }
401 ->{hidden} );
402 my %subfield_data;
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>";
406 } else {
407 $subfield_data{marc_value}=$subf[$i][1];
409 $subfield_data{short_desc} = substr(
410 $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{lib},
411 0, 20
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) {
421 my %tag_data;
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 ) {
430 push @BIG_LOOP, {
431 number => $tabloop,
432 innerloop => \@loop_data,
436 $template->param( singletab => (scalar(@BIG_LOOP)==1), BIG_LOOP => \@BIG_LOOP );
441 my $query=new CGI;
443 my $dbh=C4::Context->dbh;
445 # open template
446 my ($template, $loggedinuser, $cookie)
447 = get_template_and_user({template_name => "authorities/detail.tmpl",
448 query => $query,
449 type => "intranet",
450 authnotrequired => 0,
451 flagsrequired => {catalogue => 1},
452 debug => 1,
455 my $authid = $query->param('authid');
459 my $authtypecode = &GetAuthTypeCode($authid);
460 $tagslib = &GetTagsLabels(1,$authtypecode);
462 my $record;
463 if (C4::Context->preference("AuthDisplayHierarchy")){
464 my $trees=BuildUnimarcHierarchies($authid);
465 my @trees = split /;/,$trees ;
466 push @trees,$trees unless (@trees);
467 my @loophierarchies;
468 foreach my $tree (@trees){
469 my @tree=split /,/,$tree;
470 push @tree,$tree unless (@tree);
471 my $cnt=0;
472 my @loophierarchy;
473 foreach my $element (@tree){
474 my $cell;
475 my $elementdata = GetAuthority($element);
476 $record= $elementdata if ($authid==$element);
477 push @loophierarchy, BuildUnimarcHierarchy($elementdata,"child".$cnt, $authid);
478 $cnt++;
480 push @loophierarchies, { 'loopelement' =>\@loophierarchy};
482 $template->param(
483 'displayhierarchy' =>C4::Context->preference("AuthDisplayHierarchy"),
484 'loophierarchies' =>\@loophierarchies,
486 } else {
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);
494 my $biblio_fields;
495 while (my ($tagfield) = $sth->fetchrow) {
496 $biblio_fields.= $tagfield."9,";
498 chop $biblio_fields;
501 # fill arrays
502 my @loop_data =();
503 my $tag;
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;
510 my @authtypesloop;
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,
521 count => $count,
522 biblio_fields => $biblio_fields,
523 authtypetext => $authtypes->{$authtypecode}{'authtypetext'},
524 authtypesloop => \@authtypesloop,
526 output_html_with_http_headers $query, $cookie, $template->output;