+
+ $sth->execute($frameworkcode);
+
+ my $subfield;
+ my $authorised_value;
+ my $authtypecode;
+ my $value_builder;
+ my $kohafield;
+ my $seealso;
+ my $hidden;
+ my $isurl;
+ my $link;
+ my $defaultvalue;
+
+ while (
+ (
+ $tag, $subfield, $liblibrarian,
+ , $libopac, $tab,
+ $mandatory, $repeatable, $authorised_value,
+ $authtypecode, $value_builder, $kohafield,
+ $seealso, $hidden, $isurl,
+ $link,$defaultvalue
+ )
+ = $sth->fetchrow
+ )
+ {
+ $res->{$tag}->{$subfield}->{lib} =
+ ( $forlibrarian or !$libopac ) ? $liblibrarian : $libopac;
+ $res->{$tag}->{$subfield}->{tab} = $tab;
+ $res->{$tag}->{$subfield}->{mandatory} = $mandatory;
+ $res->{$tag}->{$subfield}->{repeatable} = $repeatable;
+ $res->{$tag}->{$subfield}->{authorised_value} = $authorised_value;
+ $res->{$tag}->{$subfield}->{authtypecode} = $authtypecode;
+ $res->{$tag}->{$subfield}->{value_builder} = $value_builder;
+ $res->{$tag}->{$subfield}->{kohafield} = $kohafield;
+ $res->{$tag}->{$subfield}->{seealso} = $seealso;
+ $res->{$tag}->{$subfield}->{hidden} = $hidden;
+ $res->{$tag}->{$subfield}->{isurl} = $isurl;
+ $res->{$tag}->{$subfield}->{'link'} = $link;
+ $res->{$tag}->{$subfield}->{defaultvalue} = $defaultvalue;
+ }
+ return $res;
+}
+
+=head2 GetUsedMarcStructure
+
+ the same function as GetMarcStructure expcet it just take field
+ in tab 0-9. (used field)
+
+ my $results = GetUsedMarcStructure($frameworkcode);
+
+ L<$results> is a ref to an array which each case containts a ref
+ to a hash which each keys is the columns from marc_subfield_structure
+
+ L<$frameworkcode> is the framework code.
+
+=cut
+
+sub GetUsedMarcStructure($){
+ my $frameworkcode = shift || '';
+ my $dbh = C4::Context->dbh;
+ my $query = qq/
+ SELECT *
+ FROM marc_subfield_structure
+ WHERE tab > -1
+ AND frameworkcode = ?
+ /;
+ my @results;
+ my $sth = $dbh->prepare($query);
+ $sth->execute($frameworkcode);
+ while (my $row = $sth->fetchrow_hashref){
+ push @results,$row;
+ }
+ return \@results;
+}
+
+=head2 GetMarcFromKohaField
+
+=over 4
+
+($MARCfield,$MARCsubfield)=GetMarcFromKohaField($kohafield,$frameworkcode);
+Returns the MARC fields & subfields mapped to the koha field
+for the given frameworkcode
+
+=back
+
+=cut
+
+sub GetMarcFromKohaField {
+ my ( $kohafield, $frameworkcode ) = @_;
+ return 0, 0 unless $kohafield;
+ my $relations = C4::Context->marcfromkohafield;
+ return (
+ $relations->{$frameworkcode}->{$kohafield}->[0],
+ $relations->{$frameworkcode}->{$kohafield}->[1]
+ );
+}
+
+=head2 GetMarcBiblio
+
+=over 4
+
+Returns MARC::Record of the biblionumber passed in parameter.
+the marc record contains both biblio & item datas
+
+=back
+
+=cut
+
+sub GetMarcBiblio {
+ my $biblionumber = shift;
+ my $dbh = C4::Context->dbh;
+ my $sth =
+ $dbh->prepare("SELECT marcxml FROM biblioitems WHERE biblionumber=? ");
+ $sth->execute($biblionumber);
+ my ($marcxml) = $sth->fetchrow;
+ MARC::File::XML->default_record_format(C4::Context->preference('marcflavour'));
+ $marcxml =~ s/\x1e//g;
+ $marcxml =~ s/\x1f//g;
+ $marcxml =~ s/\x1d//g;
+ $marcxml =~ s/\x0f//g;
+ $marcxml =~ s/\x0c//g;
+# warn $marcxml;
+ my $record = MARC::Record->new();
+ if ($marcxml) {
+ $record = eval {MARC::Record::new_from_xml( $marcxml, "utf8", C4::Context->preference('marcflavour'))};
+ if ($@) {warn $@;}
+# $record = MARC::Record::new_from_usmarc( $marc) if $marc;
+ return $record;
+ } else {
+ return undef;
+ }
+}
+
+=head2 GetXmlBiblio
+
+=over 4
+
+my $marcxml = GetXmlBiblio($biblionumber);
+
+Returns biblioitems.marcxml of the biblionumber passed in parameter.
+The XML contains both biblio & item datas
+
+=back
+
+=cut
+
+sub GetXmlBiblio {
+ my ( $biblionumber ) = @_;
+ my $dbh = C4::Context->dbh;
+ my $sth =
+ $dbh->prepare("SELECT marcxml FROM biblioitems WHERE biblionumber=? ");
+ $sth->execute($biblionumber);
+ my ($marcxml) = $sth->fetchrow;
+ return $marcxml;
+}
+
+=head2 GetAuthorisedValueDesc
+
+=over 4
+
+my $subfieldvalue =get_authorised_value_desc(
+ $tag, $subf[$i][0],$subf[$i][1], '', $taglib, $category);
+Retrieve the complete description for a given authorised value.
+
+Now takes $category and $value pair too.
+my $auth_value_desc =GetAuthorisedValueDesc(
+ '','', 'DVD' ,'','','CCODE');
+
+=back
+
+=cut
+
+sub GetAuthorisedValueDesc {
+ my ( $tag, $subfield, $value, $framework, $tagslib, $category ) = @_;
+ my $dbh = C4::Context->dbh;
+
+ if (!$category) {
+#---- branch
+ if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
+ return C4::Branch::GetBranchName($value);
+ }
+
+#---- itemtypes
+ if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "itemtypes" ) {
+ return getitemtypeinfo($value)->{description};
+ }
+
+#---- "true" authorized value
+ $category = $tagslib->{$tag}->{$subfield}->{'authorised_value'}
+ }
+
+ if ( $category ne "" ) {
+ my $sth =
+ $dbh->prepare(
+ "SELECT lib FROM authorised_values WHERE category = ? AND authorised_value = ?"
+ );
+ $sth->execute( $category, $value );
+ my $data = $sth->fetchrow_hashref;
+ return $data->{'lib'};
+ }
+ else {
+ return $value; # if nothing is found return the original value
+ }
+}
+
+=head2 GetMarcNotes
+
+=over 4
+
+$marcnotesarray = GetMarcNotes( $record, $marcflavour );
+Get all notes from the MARC record and returns them in an array.
+The note are stored in differents places depending on MARC flavour
+
+=back
+
+=cut
+
+sub GetMarcNotes {
+ my ( $record, $marcflavour ) = @_;
+ my $scope;
+ if ( $marcflavour eq "MARC21" ) {
+ $scope = '5..';
+ }
+ else { # assume unimarc if not marc21
+ $scope = '3..';
+ }
+ my @marcnotes;
+ my $note = "";
+ my $tag = "";
+ my $marcnote;
+ foreach my $field ( $record->field($scope) ) {
+ my $value = $field->as_string();
+ if ( $note ne "" ) {
+ $marcnote = { marcnote => $note, };
+ push @marcnotes, $marcnote;
+ $note = $value;
+ }
+ if ( $note ne $value ) {
+ $note = $note . " " . $value;
+ }
+ }
+
+ if ( $note ) {
+ $marcnote = { marcnote => $note };
+ push @marcnotes, $marcnote; #load last tag into array
+ }
+ return \@marcnotes;
+} # end GetMarcNotes
+
+=head2 GetMarcSubjects
+
+=over 4
+
+$marcsubjcts = GetMarcSubjects($record,$marcflavour);
+Get all subjects from the MARC record and returns them in an array.
+The subjects are stored in differents places depending on MARC flavour
+
+=back
+
+=cut
+
+sub GetMarcSubjects {
+ my ( $record, $marcflavour ) = @_;
+ my ( $mintag, $maxtag );
+ if ( $marcflavour eq "MARC21" ) {
+ $mintag = "600";
+ $maxtag = "699";
+ }
+ else { # assume unimarc if not marc21
+ $mintag = "600";
+ $maxtag = "611";
+ }
+
+ my @marcsubjects;
+ my $subject = "";
+ my $subfield = "";
+ my $marcsubject;
+
+ foreach my $field ( $record->field('6..' )) {
+ next unless $field->tag() >= $mintag && $field->tag() <= $maxtag;
+ my @subfields_loop;
+ my @subfields = $field->subfields();
+ my $counter = 0;
+ my @link_loop;
+ # if there is an authority link, build the link with an= subfield9
+ my $subfield9 = $field->subfield('9');
+ for my $subject_subfield (@subfields ) {
+ # don't load unimarc subfields 3,4,5
+ next if (($marcflavour eq "UNIMARC") and ($subject_subfield->[0] =~ (3|4|5) ) );
+ my $code = $subject_subfield->[0];
+ my $value = $subject_subfield->[1];
+ my $linkvalue = $value;
+ $linkvalue =~ s/(\(|\))//g;
+ my $operator = " and " unless $counter==0;
+ if ($subfield9) {
+ @link_loop = ({'limit' => 'an' ,link => "$subfield9" });
+ } else {
+ push @link_loop, {'limit' => 'su', link => $linkvalue, operator => $operator };
+ }
+ my $separator = C4::Context->preference("authoritysep") unless $counter==0;
+ # ignore $9
+ my @this_link_loop = @link_loop;
+ push @subfields_loop, {code => $code, value => $value, link_loop => \@this_link_loop, separator => $separator} unless ($subject_subfield->[0] == 9 );
+ $counter++;
+ }
+
+ push @marcsubjects, { MARCSUBJECT_SUBFIELDS_LOOP => \@subfields_loop };
+
+ }
+ return \@marcsubjects;
+} #end getMARCsubjects
+
+=head2 GetMarcAuthors
+
+=over 4
+
+authors = GetMarcAuthors($record,$marcflavour);
+Get all authors from the MARC record and returns them in an array.
+The authors are stored in differents places depending on MARC flavour
+
+=back
+
+=cut
+
+sub GetMarcAuthors {
+ my ( $record, $marcflavour ) = @_;
+ my ( $mintag, $maxtag );
+ # tagslib useful for UNIMARC author reponsabilities
+ my $tagslib = &GetMarcStructure( 1, '' ); # FIXME : we don't have the framework available, we take the default framework. May be bugguy on some setups, will be usually correct.
+ if ( $marcflavour eq "MARC21" ) {
+ $mintag = "700";
+ $maxtag = "720";
+ }
+ elsif ( $marcflavour eq "UNIMARC" ) { # assume unimarc if not marc21
+ $mintag = "700";
+ $maxtag = "712";
+ }
+ else {
+ return;
+ }
+ my @marcauthors;
+
+ foreach my $field ( $record->fields ) {
+ next unless $field->tag() >= $mintag && $field->tag() <= $maxtag;
+ my @subfields_loop;
+ my @link_loop;
+ my @subfields = $field->subfields();
+ my $count_auth = 0;
+ # if there is an authority link, build the link with Koha-Auth-Number: subfield9
+ my $subfield9 = $field->subfield('9');
+ for my $authors_subfield (@subfields) {
+ # don't load unimarc subfields 3, 5
+ next if ($marcflavour eq 'UNIMARC' and ($authors_subfield->[0] =~ (3|5) ) );
+ my $subfieldcode = $authors_subfield->[0];
+ my $value = $authors_subfield->[1];
+ my $linkvalue = $value;
+ $linkvalue =~ s/(\(|\))//g;
+ my $operator = " and " unless $count_auth==0;
+ # if we have an authority link, use that as the link, otherwise use standard searching
+ if ($subfield9) {
+ @link_loop = ({'limit' => 'Koha-Auth-Number' ,link => "$subfield9" });
+ }
+ else {
+ # reset $linkvalue if UNIMARC author responsibility
+ if ( $marcflavour eq 'UNIMARC' and ($authors_subfield->[0] eq '4')) {
+ $linkvalue = "(".GetAuthorisedValueDesc( $field->tag(), $authors_subfield->[0], $authors_subfield->[1], '', $tagslib ).")";
+ }
+ push @link_loop, {'limit' => 'au', link => $linkvalue, operator => $operator };
+ }
+ my @this_link_loop = @link_loop;
+ my $separator = C4::Context->preference("authoritysep") unless $count_auth==0;
+ push @subfields_loop, {code => $subfieldcode, value => $value, link_loop => \@this_link_loop, separator => $separator} unless ($authors_subfield->[0] == 9 );
+ $count_auth++;
+ }
+ push @marcauthors, { MARCAUTHOR_SUBFIELDS_LOOP => \@subfields_loop };
+ }
+ return \@marcauthors;
+}
+
+=head2 GetMarcUrls
+
+=over 4
+
+$marcurls = GetMarcUrls($record,$marcflavour);
+Returns arrayref of URLs from MARC data, suitable to pass to tmpl loop.
+Assumes web resources (not uncommon in MARC21 to omit resource type ind)
+
+=back
+
+=cut
+
+sub GetMarcUrls {
+ my ($record, $marcflavour) = @_;
+ my @marcurls;
+ my $marcurl;
+ for my $field ($record->field('856')) {
+ my $url = $field->subfield('u');
+ my @notes;
+ for my $note ( $field->subfield('z')) {
+ push @notes , {note => $note};
+ }
+ $marcurl = { MARCURL => $url,
+ notes => \@notes,
+ };
+ if($marcflavour eq 'MARC21') {
+ my $s3 = $field->subfield('3');
+ my $link = $field->subfield('y');
+ $marcurl->{'linktext'} = $link || $s3 || $url ;;
+ $marcurl->{'part'} = $s3 if($link);
+ $marcurl->{'toc'} = 1 if($s3 =~ /^[Tt]able/) ;
+ } else {
+ $marcurl->{'linktext'} = $url;
+ }
+ push @marcurls, $marcurl;
+ }
+ return \@marcurls;
+} #end GetMarcUrls
+
+=head2 GetMarcSeries
+
+=over 4
+
+$marcseriesarray = GetMarcSeries($record,$marcflavour);
+Get all series from the MARC record and returns them in an array.
+The series are stored in differents places depending on MARC flavour
+
+=back
+
+=cut
+
+sub GetMarcSeries {
+ my ($record, $marcflavour) = @_;
+ my ($mintag, $maxtag);
+ if ($marcflavour eq "MARC21") {
+ $mintag = "440";
+ $maxtag = "490";
+ } else { # assume unimarc if not marc21
+ $mintag = "600";
+ $maxtag = "619";
+ }
+
+ my @marcseries;
+ my $subjct = "";
+ my $subfield = "";
+ my $marcsubjct;
+
+ foreach my $field ($record->field('440'), $record->field('490')) {
+ my @subfields_loop;
+ #my $value = $field->subfield('a');
+ #$marcsubjct = {MARCSUBJCT => $value,};
+ my @subfields = $field->subfields();
+ #warn "subfields:".join " ", @$subfields;
+ my $counter = 0;
+ my @link_loop;
+ for my $series_subfield (@subfields) {
+ my $volume_number;
+ undef $volume_number;
+ # see if this is an instance of a volume
+ if ($series_subfield->[0] eq 'v') {
+ $volume_number=1;
+ }
+
+ my $code = $series_subfield->[0];
+ my $value = $series_subfield->[1];
+ my $linkvalue = $value;
+ $linkvalue =~ s/(\(|\))//g;
+ my $operator = " and " unless $counter==0;
+ push @link_loop, {link => $linkvalue, operator => $operator };
+ my $separator = C4::Context->preference("authoritysep") unless $counter==0;
+ if ($volume_number) {
+ push @subfields_loop, {volumenum => $value};
+ }
+ else {
+ push @subfields_loop, {code => $code, value => $value, link_loop => \@link_loop, separator => $separator, volumenum => $volume_number};
+ }
+ $counter++;
+ }
+ push @marcseries, { MARCSERIES_SUBFIELDS_LOOP => \@subfields_loop };
+ #$marcsubjct = {MARCSUBJCT => $field->as_string(),};
+ #push @marcsubjcts, $marcsubjct;
+ #$subjct = $value;
+
+ }
+ my $marcseriessarray=\@marcseries;
+ return $marcseriessarray;
+} #end getMARCseriess
+
+=head2 GetFrameworkCode
+
+=over 4
+
+ $frameworkcode = GetFrameworkCode( $biblionumber )
+
+=back
+
+=cut
+
+sub GetFrameworkCode {
+ my ( $biblionumber ) = @_;
+ my $dbh = C4::Context->dbh;
+ my $sth = $dbh->prepare("SELECT frameworkcode FROM biblio WHERE biblionumber=?");
+ $sth->execute($biblionumber);
+ my ($frameworkcode) = $sth->fetchrow;
+ return $frameworkcode;
+}
+
+=head2 GetPublisherNameFromIsbn
+
+ $name = GetPublishercodeFromIsbn($isbn);
+ if(defined $name){
+ ...
+ }
+
+=cut
+
+sub GetPublisherNameFromIsbn($){
+ my $isbn = shift;
+ $isbn =~ s/[- _]//g;
+ $isbn =~ s/^0*//;
+ my @codes = (split '-', DisplayISBN($isbn));
+ my $code = $codes[0].$codes[1].$codes[2];
+ my $dbh = C4::Context->dbh;
+ my $query = qq{
+ SELECT distinct publishercode
+ FROM biblioitems
+ WHERE isbn LIKE ?
+ AND publishercode IS NOT NULL
+ LIMIT 1
+ };
+ my $sth = $dbh->prepare($query);
+ $sth->execute("$code%");
+ my $name = $sth->fetchrow;
+ return $name if length $name;
+ return undef;
+}
+
+=head2 TransformKohaToMarc
+
+=over 4
+
+ $record = TransformKohaToMarc( $hash )
+ This function builds partial MARC::Record from a hash
+ Hash entries can be from biblio or biblioitems.
+ This function is called in acquisition module, to create a basic catalogue entry from user entry
+
+=back
+
+=cut
+
+sub TransformKohaToMarc {
+
+ my ( $hash ) = @_;
+ my $dbh = C4::Context->dbh;
+ my $sth =
+ $dbh->prepare(
+ "SELECT tagfield,tagsubfield FROM marc_subfield_structure WHERE frameworkcode=? AND kohafield=?"
+ );
+ my $record = MARC::Record->new();
+ foreach (keys %{$hash}) {
+ &TransformKohaToMarcOneField( $sth, $record, $_,
+ $hash->{$_}, '' );
+ }
+ return $record;
+}
+
+=head2 TransformKohaToMarcOneField
+
+=over 4
+
+ $record = TransformKohaToMarcOneField( $sth, $record, $kohafieldname, $value, $frameworkcode );
+
+=back
+
+=cut
+
+sub TransformKohaToMarcOneField {
+ my ( $sth, $record, $kohafieldname, $value, $frameworkcode ) = @_;
+ $frameworkcode='' unless $frameworkcode;
+ my $tagfield;
+ my $tagsubfield;
+
+ if ( !defined $sth ) {
+ my $dbh = C4::Context->dbh;
+ $sth = $dbh->prepare(
+ "SELECT tagfield,tagsubfield FROM marc_subfield_structure WHERE frameworkcode=? AND kohafield=?"
+ );
+ }
+ $sth->execute( $frameworkcode, $kohafieldname );
+ if ( ( $tagfield, $tagsubfield ) = $sth->fetchrow ) {
+ my $tag = $record->field($tagfield);
+ if ($tag) {
+ $tag->update( $tagsubfield => $value );
+ $record->delete_field($tag);
+ $record->insert_fields_ordered($tag);
+ }
+ else {
+ $record->add_fields( $tagfield, " ", " ", $tagsubfield => $value );
+ }
+ }
+ return $record;
+}
+
+=head2 TransformHtmlToXml
+
+=over 4
+
+$xml = TransformHtmlToXml( $tags, $subfields, $values, $indicator, $ind_tag, $auth_type )
+
+$auth_type contains :
+- nothing : rebuild a biblio, un UNIMARC the encoding is in 100$a pos 26/27
+- UNIMARCAUTH : rebuild an authority. In UNIMARC, the encoding is in 100$a pos 13/14
+- ITEM : rebuild an item : in UNIMARC, 100$a, it's in the biblio ! (otherwise, we would get 2 100 fields !)
+
+=back
+
+=cut
+
+sub TransformHtmlToXml {
+ my ( $tags, $subfields, $values, $indicator, $ind_tag, $auth_type ) = @_;
+ my $xml = MARC::File::XML::header('UTF-8');
+ $auth_type = C4::Context->preference('marcflavour') unless $auth_type;
+ MARC::File::XML->default_record_format($auth_type);
+ # in UNIMARC, field 100 contains the encoding
+ # check that there is one, otherwise the
+ # MARC::Record->new_from_xml will fail (and Koha will die)
+ my $unimarc_and_100_exist=0;
+ $unimarc_and_100_exist=1 if $auth_type eq 'ITEM'; # if we rebuild an item, no need of a 100 field
+ my $prevvalue;
+ my $prevtag = -1;
+ my $first = 1;
+ my $j = -1;
+ for ( my $i = 0 ; $i <= @$tags ; $i++ ) {
+ if (C4::Context->preference('marcflavour') eq 'UNIMARC' and @$tags[$i] eq "100" and @$subfields[$i] eq "a") {
+ # if we have a 100 field and it's values are not correct, skip them.
+ # if we don't have any valid 100 field, we will create a default one at the end
+ my $enc = substr( @$values[$i], 26, 2 );
+ if ($enc eq '01' or $enc eq '50' or $enc eq '03') {
+ $unimarc_and_100_exist=1;
+ } else {
+ next;
+ }
+ }
+ @$values[$i] =~ s/&/&/g;
+ @$values[$i] =~ s/</</g;
+ @$values[$i] =~ s/>/>/g;
+ @$values[$i] =~ s/"/"/g;
+ @$values[$i] =~ s/'/'/g;
+# if ( !utf8::is_utf8( @$values[$i] ) ) {
+# utf8::decode( @$values[$i] );
+# }
+ if ( ( @$tags[$i] ne $prevtag ) ) {
+ $j++ unless ( @$tags[$i] eq "" );
+ if ( !$first ) {
+ $xml .= "</datafield>\n";
+ if ( ( @$tags[$i] && @$tags[$i] > 10 )
+ && ( @$values[$i] ne "" ) )
+ {
+ my $ind1 = substr( @$indicator[$j], 0, 1 );
+ my $ind2;
+ if ( @$indicator[$j] ) {
+ $ind2 = substr( @$indicator[$j], 1, 1 );
+ }
+ else {
+ warn "Indicator in @$tags[$i] is empty";
+ $ind2 = " ";
+ }
+ $xml .=
+"<datafield tag=\"@$tags[$i]\" ind1=\"$ind1\" ind2=\"$ind2\">\n";
+ $xml .=
+"<subfield code=\"@$subfields[$i]\">@$values[$i]</subfield>\n";
+ $first = 0;
+ }
+ else {
+ $first = 1;
+ }
+ }
+ else {
+ if ( @$values[$i] ne "" ) {
+
+ # leader
+ if ( @$tags[$i] eq "000" ) {
+ $xml .= "<leader>@$values[$i]</leader>\n";
+ $first = 1;
+
+ # rest of the fixed fields
+ }
+ elsif ( @$tags[$i] < 10 ) {
+ $xml .=
+"<controlfield tag=\"@$tags[$i]\">@$values[$i]</controlfield>\n";
+ $first = 1;
+ }
+ else {
+ my $ind1 = substr( @$indicator[$j], 0, 1 );
+ my $ind2 = substr( @$indicator[$j], 1, 1 );
+ $xml .=
+"<datafield tag=\"@$tags[$i]\" ind1=\"$ind1\" ind2=\"$ind2\">\n";
+ $xml .=
+"<subfield code=\"@$subfields[$i]\">@$values[$i]</subfield>\n";
+ $first = 0;
+ }
+ }
+ }
+ }
+ else { # @$tags[$i] eq $prevtag
+ if ( @$values[$i] eq "" ) {
+ }
+ else {
+ if ($first) {
+ my $ind1 = substr( @$indicator[$j], 0, 1 );
+ my $ind2 = substr( @$indicator[$j], 1, 1 );
+ $xml .=
+"<datafield tag=\"@$tags[$i]\" ind1=\"$ind1\" ind2=\"$ind2\">\n";
+ $first = 0;
+ }
+ $xml .=
+"<subfield code=\"@$subfields[$i]\">@$values[$i]</subfield>\n";
+ }
+ }
+ $prevtag = @$tags[$i];
+ }
+ if (C4::Context->preference('marcflavour') eq 'UNIMARC' and !$unimarc_and_100_exist) {
+# warn "SETTING 100 for $auth_type";
+ use POSIX qw(strftime);
+ my $string = strftime( "%Y%m%d", localtime(time) );
+ # set 50 to position 26 is biblios, 13 if authorities
+ my $pos=26;
+ $pos=13 if $auth_type eq 'UNIMARCAUTH';
+ $string = sprintf( "%-*s", 35, $string );
+ substr( $string, $pos , 6, "50" );
+ $xml .= "<datafield tag=\"100\" ind1=\"\" ind2=\"\">\n";
+ $xml .= "<subfield code=\"a\">$string</subfield>\n";
+ $xml .= "</datafield>\n";
+ }
+ $xml .= MARC::File::XML::footer();
+ return $xml;
+}
+
+=head2 TransformHtmlToMarc
+
+ L<$record> = TransformHtmlToMarc(L<$params>,L<$cgi>)
+ L<$params> is a ref to an array as below:
+ {
+ 'tag_010_indicator_531951' ,
+ 'tag_010_code_a_531951_145735' ,
+ 'tag_010_subfield_a_531951_145735' ,
+ 'tag_200_indicator_873510' ,
+ 'tag_200_code_a_873510_673465' ,
+ 'tag_200_subfield_a_873510_673465' ,
+ 'tag_200_code_b_873510_704318' ,
+ 'tag_200_subfield_b_873510_704318' ,
+ 'tag_200_code_e_873510_280822' ,
+ 'tag_200_subfield_e_873510_280822' ,
+ 'tag_200_code_f_873510_110730' ,
+ 'tag_200_subfield_f_873510_110730' ,
+ }
+ L<$cgi> is the CGI object which containts the value.
+ L<$record> is the MARC::Record object.
+
+=cut
+
+sub TransformHtmlToMarc {
+ my $params = shift;
+ my $cgi = shift;
+
+ # creating a new record
+ my $record = MARC::Record->new();
+ my $i=0;
+ my @fields;
+ while ($params->[$i]){ # browse all CGI params
+ my $param = $params->[$i];
+ my $newfield=0;
+ # if we are on biblionumber, store it in the MARC::Record (it may not be in the edited fields)
+ if ($param eq 'biblionumber') {
+ my ( $biblionumbertagfield, $biblionumbertagsubfield ) =
+ &GetMarcFromKohaField( "biblio.biblionumber", '' );
+ if ($biblionumbertagfield < 10) {
+ $newfield = MARC::Field->new(
+ $biblionumbertagfield,
+ $cgi->param($param),
+ );
+ } else {
+ $newfield = MARC::Field->new(
+ $biblionumbertagfield,
+ '',
+ '',
+ "$biblionumbertagsubfield" => $cgi->param($param),
+ );
+ }
+ push @fields,$newfield if($newfield);
+ }
+ elsif ($param =~ /^tag_(\d*)_indicator_/){ # new field start when having 'input name="..._indicator_..."
+ my $tag = $1;
+
+ my $ind1 = substr($cgi->param($param),0,1);
+ my $ind2 = substr($cgi->param($param),1,1);
+ $newfield=0;
+ my $j=$i+1;
+
+ if($tag < 10){ # no code for theses fields
+ # in MARC editor, 000 contains the leader.
+ if ($tag eq '000' ) {
+ $record->leader($cgi->param($params->[$j+1])) if length($cgi->param($params->[$j+1]))==24;
+ # between 001 and 009 (included)
+ } else {
+ $newfield = MARC::Field->new(
+ $tag,
+ $cgi->param($params->[$j+1]),
+ );
+ }
+ # > 009, deal with subfields
+ } else {
+ while($params->[$j] =~ /_code_/){ # browse all it's subfield
+ my $inner_param = $params->[$j];
+ if ($newfield){
+ if($cgi->param($params->[$j+1])){ # only if there is a value (code => value)
+ $newfield->add_subfields(
+ $cgi->param($inner_param) => $cgi->param($params->[$j+1])
+ );
+ }
+ } else {
+ if ( $cgi->param($params->[$j+1]) ) { # creating only if there is a value (code => value)
+ $newfield = MARC::Field->new(
+ $tag,
+ ''.$ind1,
+ ''.$ind2,
+ $cgi->param($inner_param) => $cgi->param($params->[$j+1]),
+ );
+ }
+ }
+ $j+=2;
+ }
+ }
+ push @fields,$newfield if($newfield);
+ }
+ $i++;
+ }
+
+ $record->append_fields(@fields);
+ return $record;
+}