From b5ecefd485a75d54a5fa26fff5a0cc890541e2c3 Mon Sep 17 00:00:00 2001 From: acli Date: Mon, 3 Feb 2003 18:46:00 +0000 Subject: [PATCH] Minor factoring in C4/Biblio.pm, plus change to export the per-tag 'mandatory' property to a per-subfield 'tag_mandatory' template parameter, so that addbiblio.tmpl can distinguish between mandatory subfields in a mandatory tag and mandatory subfields in an optional tag Not-minor factoring in acqui.simple/addbiblio.pl to make the if-else blocks smaller, and to add some POD; need further testing for this Added function to check if a MARC subfield name is "koha-internal" (instead of checking it for 'lib' and 'tag' everywhere); temporarily added to Koha.pm Use above function in acqui.simple/additem.pl and search.marc/search.pl --- C4/Biblio.pm | 32 ++-- C4/Koha.pm | 12 ++ acqui.simple/addbiblio.pl | 362 ++++++++++++++++++++------------------ acqui.simple/additem.pl | 4 +- search.marc/search.pl | 4 +- 5 files changed, 222 insertions(+), 192 deletions(-) diff --git a/C4/Biblio.pm b/C4/Biblio.pm index 3bece70888..6af767c5ab 100644 --- a/C4/Biblio.pm +++ b/C4/Biblio.pm @@ -1,6 +1,20 @@ package C4::Biblio; # $Id$ # $Log$ +# Revision 1.35 2003/02/03 18:46:00 acli +# Minor factoring in C4/Biblio.pm, plus change to export the per-tag +# 'mandatory' property to a per-subfield 'tag_mandatory' template parameter, +# so that addbiblio.tmpl can distinguish between mandatory subfields in a +# mandatory tag and mandatory subfields in an optional tag +# +# Not-minor factoring in acqui.simple/addbiblio.pl to make the if-else blocks +# smaller, and to add some POD; need further testing for this +# +# Added function to check if a MARC subfield name is "koha-internal" (instead +# of checking it for 'lib' and 'tag' everywhere); temporarily added to Koha.pm +# +# Use above function in acqui.simple/additem.pl and search.marc/search.pl +# # Revision 1.34 2003/01/28 14:50:04 tipaul # fixing MARCmodbiblio API and reindenting code # @@ -376,23 +390,17 @@ used to manage MARC_word table and should not be useful elsewhere sub MARCgettagslib { my ($dbh,$forlibrarian)= @_; my $sth; - if ($forlibrarian eq 1) { - $sth=$dbh->prepare("select tagfield,liblibrarian as lib from marc_tag_structure order by tagfield"); - } else { - $sth=$dbh->prepare("select tagfield,libopac as lib from marc_tag_structure order by tagfield"); - } + my $libfield = ($forlibrarian eq 1)? 'liblibrarian' : 'libopac'; + $sth=$dbh->prepare("select tagfield,$libfield as lib,mandatory from marc_tag_structure order by tagfield"); $sth->execute; my ($lib,$tag,$res,$tab,$mandatory,$repeatable); - while ( ($tag,$lib,$tab) = $sth->fetchrow) { + while ( ($tag,$lib,$mandatory) = $sth->fetchrow) { $res->{$tag}->{lib}=$lib; - $res->{$tab}->{tab}=""; + $res->{$tab}->{tab}=""; # XXX + $res->{$tag}->{mandatory}=$mandatory; } - if ($forlibrarian eq 1) { - $sth=$dbh->prepare("select tagfield,tagsubfield,liblibrarian as lib,tab, mandatory, repeatable,authorised_value,thesaurus_category,value_builder from marc_subfield_structure order by tagfield,tagsubfield"); - } else { - $sth=$dbh->prepare("select tagfield,tagsubfield,libopac as lib,tab, mandatory, repeatable,authorised_value,thesaurus_category,value_builder from marc_subfield_structure order by tagfield,tagsubfield"); - } + $sth=$dbh->prepare("select tagfield,tagsubfield,$libfield as lib,tab, mandatory, repeatable,authorised_value,thesaurus_category,value_builder from marc_subfield_structure order by tagfield,tagsubfield"); $sth->execute; my $subfield; diff --git a/C4/Koha.pm b/C4/Koha.pm index cd97111a25..4cac54dfd4 100644 --- a/C4/Koha.pm +++ b/C4/Koha.pm @@ -54,6 +54,7 @@ Koha.pm provides many functions for Koha scripts. &fixEthnicity &borrowercategories ðnicitycategories + &subfield_is_koha_internal_p $DEBUG); use vars qw(); @@ -150,6 +151,17 @@ sub ethnicitycategories { return(\@codes,\%labels); } +# FIXME.. this should be moved to a MARC-specific module +sub subfield_is_koha_internal_p ($) { + my($subfield) = @_; + + # We could match on 'lib' and 'tab' (and 'mandatory', & more to come!) + # But real MARC subfields are always single-character + # so it really is safer just to check the length + + return length $subfield != 1; +} + 1; __END__ diff --git a/acqui.simple/addbiblio.pl b/acqui.simple/addbiblio.pl index 2b2c1a0799..5d51a25145 100755 --- a/acqui.simple/addbiblio.pl +++ b/acqui.simple/addbiblio.pl @@ -26,9 +26,13 @@ use C4::Output; use C4::Interface::CGI::Output; use C4::Biblio; use C4::Context; +use C4::Koha; # XXX subfield_is_koha_internal_p use HTML::Template; use MARC::File::USMARC; +use vars qw( $tagslib ); +use vars qw( $is_a_modif ); + =item find_value @@ -86,6 +90,176 @@ sub MARCfindbreeding { } +=item build_authorized_values_list + +=cut + +sub build_authorized_values_list ($$$$$) { + my($tag, $subfield, $value, $dbh, $authorised_values_sth) = @_; + + my @authorised_values; + my %authorised_lib; + + # builds list, depending on authorised value... + + #---- branch + if ($tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) { + my $sth=$dbh->prepare("select branchcode,branchname from branches"); + $sth->execute; + push @authorised_values, "" + unless ($tagslib->{$tag}->{$subfield}->{mandatory}); + + while (my ($branchcode,$branchname) = $sth->fetchrow_array) { + push @authorised_values, $branchcode; + $authorised_lib{$branchcode}=$branchname; + } + + #----- itemtypes + } elsif ($tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes") { + my $sth=$dbh->prepare("select itemtype,description from itemtypes"); + $sth->execute; + push @authorised_values, "" + unless ($tagslib->{$tag}->{$subfield}->{mandatory}); + + while (my ($itemtype,$description) = $sth->fetchrow_array) { + push @authorised_values, $itemtype; + $authorised_lib{$itemtype}=$description; + } + + #---- "true" authorised value + } else { + $authorised_values_sth->execute + ($tagslib->{$tag}->{$subfield}->{authorised_value}); + + push @authorised_values, "" + unless ($tagslib->{$tag}->{$subfield}->{mandatory}); + + while (my ($value,$lib) = $authorised_values_sth->fetchrow_array) { + push @authorised_values, $value; + $authorised_lib{$value}=$lib; + } + } + return CGI::scrolling_list( -name => 'field_value', + -values => \@authorised_values, + -default => $value, + -labels => \%authorised_lib, + -size => 1, + -multiple => 0 ); +} + +sub build_tabs ($$$) { + my($template, $record, $dbh) = @_; + + # fill arrays + my @loop_data =(); + my $tag; + my $i=0; + my $authorised_values_sth = $dbh->prepare("select authorised_value,lib + from authorised_values + where category=? order by authorised_value"); + + # loop through each tab 0 through 9 + for (my $tabloop = 0; $tabloop <= 9; $tabloop++) { + # my @fields = $record->fields(); + my @loop_data = (); + foreach my $tag (sort(keys (%{$tagslib}))) { + my $previous_tag = ''; + my @subfields_data; + my $indicator; + + # loop through each subfield + foreach my $subfield (sort(keys %{$tagslib->{$tag}})) { + next if subfield_is_koha_internal_p($subfield); + next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop); + my %subfield_data; + $subfield_data{tag}=$tag; + $subfield_data{subfield}=$subfield; + $subfield_data{marc_lib}="
".$tagslib->{$tag}->{$subfield}->{lib}."
"; + $subfield_data{tag_mandatory}=$tagslib->{$tag}->{mandatory}; + $subfield_data{mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory}; + $subfield_data{repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable}; + # if breeding is not empty + if ($record ne -1) { + my ($x,$value) = find_value($tag,$subfield,$record); + $value=char_decode($value) unless ($is_a_modif); + $indicator = $x if $x; #XXX + if ($tagslib->{$tag}->{$subfield}->{authorised_value}) { + $subfield_data{marc_value}= build_authorized_values_list($tag, $subfield, $value, $dbh, $authorised_values_sth); + } elsif ($tagslib->{$tag}->{$subfield}->{thesaurus_category}) { + $subfield_data{marc_value}=" {$tag}->{$subfield}->{thesaurus_category}&index=$i',$i)\">..."; #" + } elsif ($tagslib->{$tag}->{$subfield}->{'value_builder'}) { + my $plugin="../value_builder/".$tagslib->{$tag}->{$subfield}->{'value_builder'}; + require $plugin; + my $extended_param = plugin_parameters($dbh,$record,$tagslib,$i,$tabloop); + my ($function_name,$javascript) = plugin_javascript($dbh,$record,$tagslib,$i,$tabloop); + $subfield_data{marc_value}=" ... $javascript"; + } else { + $subfield_data{marc_value}=""; + } + + # if breeding is empty + } else { + my ($x,$value); + ($x,$value) = find_value($tag,$subfield,$record) if ($record ne -1); + $value=char_decode($value) unless ($is_a_modif); + if ($tagslib->{$tag}->{$subfield}->{authorised_value}) { + $subfield_data{marc_value}= build_authorized_values_list($tag, $subfield, $value, $dbh, $authorised_values_sth); + } elsif ($tagslib->{$tag}->{$subfield}->{thesaurus_category}) { + $subfield_data{marc_value}=" {$tag}->{$subfield}->{thesaurus_category}&index=$i',$i)\">..."; + } elsif ($tagslib->{$tag}->{$subfield}->{'value_builder'}) { + my $plugin="../value_builder/".$tagslib->{$tag}->{$subfield}->{'value_builder'}; + require $plugin; + my $extended_param = plugin_parameters($dbh,$record,$tagslib,$i,$tabloop); + my ($function_name,$javascript) = plugin_javascript($dbh,$record,$tagslib,$i,$tabloop); + $subfield_data{marc_value}=" ... $javascript"; + } else { + $subfield_data{marc_value}=""; + } + } + push(@subfields_data, \%subfield_data); + $i++; + } + if ($#subfields_data >= 0) { + my %tag_data; + $tag_data{tag} = $tag; + $tag_data{tag_lib} = $tagslib->{$tag}->{lib}; + $tag_data{indicator} = $indicator; + $tag_data{subfield_loop} = \@subfields_data; + push (@loop_data, \%tag_data); + } + } + $template->param($tabloop."XX" =>\@loop_data); + } +} + + +sub build_hidden_data () { + # build hidden data => + # we store everything, even if we show only requested subfields. + + my @loop_data =(); + my $i=0; + foreach my $tag (keys %{$tagslib}) { + my $previous_tag = ''; + + # loop through each subfield + foreach my $subfield (keys %{$tagslib->{$tag}}) { + next if ($subfield eq 'lib'); + next if ($subfield eq 'tab'); + next if ($subfield eq 'mandatory'); + next if ($tagslib->{$tag}->{$subfield}->{'tab'} ne "-1"); + my %subfield_data; + $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib}; + $subfield_data{marc_mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory}; + $subfield_data{marc_repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable}; + $subfield_data{marc_value}=""; + push(@loop_data, \%subfield_data); + $i++ + } + } +} + + my $input = new CGI; my $error = $input->param('error'); my $oldbiblionumber=$input->param('oldbiblionumber'); # if bib exists, it's a modif, not a new biblio. @@ -107,12 +281,12 @@ my ($template, $loggedinuser, $cookie) debug => 1, }); -my $tagslib = &MARCgettagslib($dbh,1); +$tagslib = &MARCgettagslib($dbh,1); my $record=-1; $record = MARCgetbiblio($dbh,$bibid) if ($bibid); #warn "1= ".$record->as_formatted; $record = MARCfindbreeding($dbh,$breedingid) if ($breedingid); -my $is_a_modif=0; +$is_a_modif=0; my ($oldbiblionumtagfield,$oldbiblionumtagsubfield); my ($oldbiblioitemnumtagfield,$oldbiblioitemnumtagsubfield,$bibitem,$oldbiblioitemnumber); if ($bibid) { @@ -149,184 +323,20 @@ if ($op eq "addbiblio") { ($bibid,$oldbibnum,$oldbibitemnum) = NEWnewbiblio($dbh,$record); } # now, redirect to additem page - print "Content-Type: text/html\n\n"; + print $input->redirect("additem.pl?bibid=$bibid"); exit; #------------------------------------------------------------------------------------------------------------------------------ } else { #------------------------------------------------------------------------------------------------------------------------------ - # fill arrays - my @loop_data =(); - my $tag; - my $i=0; - my $authorised_values_sth = $dbh->prepare("select authorised_value,lib from authorised_values where category=? order by authorised_value"); -# loop through each tab 0 through 9 - for (my $tabloop = 0; $tabloop<=9;$tabloop++) { - # my @fields = $record->fields(); - my @loop_data =(); - foreach my $tag (sort(keys (%{$tagslib}))) { - my $previous_tag = ''; - my @subfields_data; - my $indicator; -# loop through each subfield - foreach my $subfield (sort(keys %{$tagslib->{$tag}})) { - next if ($subfield eq 'lib'); # skip lib and tabs, which are koha internal - next if ($subfield eq 'tab'); - next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop); - my %subfield_data; - $subfield_data{tag}=$tag; - $subfield_data{subfield}=$subfield; - $subfield_data{marc_lib}="
".$tagslib->{$tag}->{$subfield}->{lib}."
"; - $subfield_data{mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory}; - $subfield_data{repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable}; - # if breeding is not empty - if ($record ne -1) { - my ($x,$value) = find_value($tag,$subfield,$record); - $value=char_decode($value) unless ($is_a_modif); - $indicator = $x if $x; - if ($tagslib->{$tag}->{$subfield}->{authorised_value}) { - my @authorised_values; - my %authorised_lib; - # builds list, depending on authorised value... - #---- branch - if ($tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) { - my $sth=$dbh->prepare("select branchcode,branchname from branches"); - $sth->execute; - push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory}); - while (my ($branchcode,$branchname) = $sth->fetchrow_array) { - push @authorised_values, $branchcode; - $authorised_lib{$branchcode}=$branchname; - } - #----- itemtypes - } elsif ($tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes") { - my $sth=$dbh->prepare("select itemtype,description from itemtypes"); - $sth->execute; - push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory}); - while (my ($itemtype,$description) = $sth->fetchrow_array) { - push @authorised_values, $itemtype; - $authorised_lib{$itemtype}=$description; - } - #---- "true" authorised value - } else { - $authorised_values_sth->execute($tagslib->{$tag}->{$subfield}->{authorised_value}); - push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory}); - while (my ($value,$lib) = $authorised_values_sth->fetchrow_array) { - push @authorised_values, $value; - $authorised_lib{$value}=$lib; - } - } - $subfield_data{marc_value}= CGI::scrolling_list(-name=>'field_value', - -values=> \@authorised_values, - -default=>"$value", - -labels => \%authorised_lib, - -size=>1, - -multiple=>0, - ); - } elsif ($tagslib->{$tag}->{$subfield}->{thesaurus_category}) { - $subfield_data{marc_value}=" {$tag}->{$subfield}->{thesaurus_category}&index=$i',$i)\">..."; #" - } elsif ($tagslib->{$tag}->{$subfield}->{'value_builder'}) { - my $plugin="../value_builder/".$tagslib->{$tag}->{$subfield}->{'value_builder'}; - require $plugin; - my $extended_param = plugin_parameters($dbh,$record,$tagslib,$i,$tabloop); - my ($function_name,$javascript) = plugin_javascript($dbh,$record,$tagslib,$i,$tabloop); - $subfield_data{marc_value}=" ... $javascript"; - } else { - $subfield_data{marc_value}=""; - } - # if breeding is empty - } else { - my ($x,$value); - ($x,$value) = find_value($tag,$subfield,$record) if ($record ne -1); - $value=char_decode($value) unless ($is_a_modif); - if ($tagslib->{$tag}->{$subfield}->{authorised_value}) { - my @authorised_values; - my %authorised_lib; - # builds list, depending on authorised value... - #---- branch - if ($tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) { - my $sth=$dbh->prepare("select branchcode,branchname from branches order by branchcode"); - $sth->execute; - push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory}); - while (my ($branchcode,$branchname) = $sth->fetchrow_array) { - push @authorised_values, $branchcode; - $authorised_lib{$branchcode}=$branchname; - } - #----- itemtypes - } elsif ($tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes") { - my $sth=$dbh->prepare("select itemtype,description from itemtypes order by itemtype"); - $sth->execute; - push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory}); - while (my ($itemtype,$description) = $sth->fetchrow_array) { - push @authorised_values, $itemtype; - $authorised_lib{$itemtype}=$description; - } - #---- "true" authorised value - } else { - $authorised_values_sth->execute($tagslib->{$tag}->{$subfield}->{authorised_value}); - push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory}); - while (my ($value,$lib) = $authorised_values_sth->fetchrow_array) { - push @authorised_values, $value; - $authorised_lib{$value}=$lib; - } - } - $subfield_data{marc_value}= CGI::scrolling_list(-name=>'field_value', - -values=> \@authorised_values, - -default=>"$value", - -labels => \%authorised_lib, - -size=>1, - -multiple=>0, - ); - } elsif ($tagslib->{$tag}->{$subfield}->{thesaurus_category}) { - $subfield_data{marc_value}=" {$tag}->{$subfield}->{thesaurus_category}&index=$i',$i)\">..."; - } elsif ($tagslib->{$tag}->{$subfield}->{'value_builder'}) { - my $plugin="../value_builder/".$tagslib->{$tag}->{$subfield}->{'value_builder'}; - require $plugin; - my $extended_param = plugin_parameters($dbh,$record,$tagslib,$i,$tabloop); - my ($function_name,$javascript) = plugin_javascript($dbh,$record,$tagslib,$i,$tabloop); - $subfield_data{marc_value}=" ... $javascript"; - } else { - $subfield_data{marc_value}=""; - } - } - push(@subfields_data, \%subfield_data); - $i++; - } - if ($#subfields_data>=0) { - my %tag_data; - $tag_data{tag}=$tag; - $tag_data{tag_lib} = $tagslib->{$tag}->{lib}; - $tag_data{indicator} = $indicator; - $tag_data{subfield_loop} = \@subfields_data; - push (@loop_data, \%tag_data); - } - } - $template->param($tabloop."XX" =>\@loop_data); - } - # now, build hidden data => we store everything, even if we show only requested subfields. - my @loop_data =(); - my $i=0; - foreach my $tag (keys %{$tagslib}) { - my $previous_tag = ''; - # loop through each subfield - foreach my $subfield (keys %{$tagslib->{$tag}}) { - next if ($subfield eq 'lib'); - next if ($subfield eq 'tab'); - next if ($tagslib->{$tag}->{$subfield}->{'tab'} ne "-1"); - my %subfield_data; - $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib}; - $subfield_data{marc_mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory}; - $subfield_data{marc_repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable}; - $subfield_data{marc_value}=""; - push(@loop_data, \%subfield_data); - $i++ - } - } + build_tabs ($template, $record, $dbh); + build_hidden_data; $template->param( - oldbiblionumber => $oldbiblionumber, - bibid => $bibid, - oldbiblionumtagfield => $oldbiblionumtagfield, - oldbiblionumtagsubfield => $oldbiblionumtagsubfield, - oldbiblioitemnumtagfield => $oldbiblioitemnumtagfield, - oldbiblioitemnumtagsubfield => $oldbiblioitemnumtagsubfield, - oldbiblioitemnumber => $oldbiblioitemnumber); + oldbiblionumber => $oldbiblionumber, + bibid => $bibid, + oldbiblionumtagfield => $oldbiblionumtagfield, + oldbiblionumtagsubfield => $oldbiblionumtagsubfield, + oldbiblioitemnumtagfield => $oldbiblioitemnumtagfield, + oldbiblioitemnumtagsubfield => $oldbiblioitemnumtagsubfield, + oldbiblioitemnumber => $oldbiblioitemnumber ); } output_html_with_http_headers $input, $cookie, $template->output; diff --git a/acqui.simple/additem.pl b/acqui.simple/additem.pl index 1ed08bdd4e..59fc93f68f 100755 --- a/acqui.simple/additem.pl +++ b/acqui.simple/additem.pl @@ -26,6 +26,7 @@ use C4::Output; use C4::Interface::CGI::Output; use C4::Biblio; use C4::Context; +use C4::Koha; # XXX subfield_is_koha_internal_p use HTML::Template; use MARC::File::USMARC; @@ -166,8 +167,7 @@ foreach my $tag (sort keys %{$tagslib}) { my $previous_tag = ''; # loop through each subfield foreach my $subfield (sort keys %{$tagslib->{$tag}}) { - next if ($subfield eq 'lib'); - next if ($subfield eq 'tab'); + next if subfield_is_koha_internal_p($subfield); next if ($tagslib->{$tag}->{$subfield}->{'tab'} ne "10"); my %subfield_data; $subfield_data{tag}=$tag; diff --git a/search.marc/search.pl b/search.marc/search.pl index d77502abf2..6c3bf5d60a 100755 --- a/search.marc/search.pl +++ b/search.marc/search.pl @@ -29,6 +29,7 @@ use C4::Output; use C4::Interface::CGI::Output; use C4::Biblio; use C4::SearchMarc; +use C4::Koha; # XXX subfield_is_koha_internal_p my $query=new CGI; my $type=$query->param('type'); @@ -92,8 +93,7 @@ if ($op eq "do_search") { push @marcarray,"--------------------------------------- $tabloop ---------------------------------------"; foreach my $tag (sort(keys (%{$tagslib}))) { foreach my $subfield (sort(keys %{$tagslib->{$tag}})) { - next if ($subfield eq 'lib'); # skip lib and tabs, which are koha internal - next if ($subfield eq 'tab'); + next if subfield_is_koha_internal_p($subfield); next unless ($tagslib->{$tag}->{$subfield}->{tab} eq $tabloop); push @marcarray, "$tag$subfield - $tagslib->{$tag}->{$subfield}->{lib}"; } -- 2.39.5