X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=C4%2FBiblio.pm;h=2de61b52b809f1a026ed1f2445f5812284a2ce10;hb=6863ca9e9245724ac417f13f91616662093d5d8f;hp=74e1acef0916e9ab507211e90388d8d97c1501aa;hpb=322f1dfe081c1b9005a8b0f52840c3598d8fb7de;p=koha.git diff --git a/C4/Biblio.pm b/C4/Biblio.pm index 74e1acef09..2de61b52b8 100755 --- a/C4/Biblio.pm +++ b/C4/Biblio.pm @@ -36,7 +36,7 @@ use C4::Charset; require C4::Heading; require C4::Serials; -use vars qw($VERSION @ISA @EXPORT); +use vars qw($VERSION @ISA @EXPORT @EXPORT_OK); BEGIN { $VERSION = 1.00; @@ -46,6 +46,10 @@ BEGIN { # to add biblios # EXPORTED FUNCTIONS. + push @EXPORT_OK, qw( + &GetRecordValue + ); + push @EXPORT, qw( &AddBiblio ); @@ -59,6 +63,11 @@ BEGIN { &GetBiblioItemByBiblioNumber &GetBiblioFromItemNumber + &GetRecordValue + &GetFieldMapping + &SetFieldMapping + &DeleteFieldMapping + &GetISBDView &GetMarcNotes @@ -69,7 +78,7 @@ BEGIN { GetMarcUrls &GetUsedMarcStructure &GetXmlBiblio - &GetCOinSBiblio + &GetCOinSBiblio &GetAuthorisedValueDesc &GetMarcStructure @@ -77,6 +86,8 @@ BEGIN { &GetFrameworkCode &GetPublisherNameFromIsbn &TransformKohaToMarc + + &CountItemsIssued ); # To modify something @@ -464,6 +475,115 @@ sub LinkBibHeadingsToAuthorities { return $num_headings_changed; } +=head2 GetRecordValue + +=over 4 + +my $values = GetRecordValue($field, $record, $frameworkcode); + +=back + +Get MARC fields from a keyword defined in fieldmapping table. + +=cut + +sub GetRecordValue { + my ($field, $record, $frameworkcode) = @_; + my $dbh = C4::Context->dbh; + + my $sth = $dbh->prepare('SELECT fieldcode, subfieldcode FROM fieldmapping WHERE frameworkcode = ? AND field = ?'); + $sth->execute($frameworkcode, $field); + + my @result = (); + + while(my $row = $sth->fetchrow_hashref){ + foreach my $field ($record->field($row->{fieldcode})){ + if( ($row->{subfieldcode} ne "" && $field->subfield($row->{subfieldcode}))){ + foreach my $subfield ($field->subfield($row->{subfieldcode})){ + push @result, { 'subfield' => $subfield }; + } + + }elsif($row->{subfieldcode} eq "") { + push @result, {'subfield' => $field->as_string()}; + } + } + } + + return \@result; +} + +=head2 SetFieldMapping + +=over 4 + +SetFieldMapping($framework, $field, $fieldcode, $subfieldcode); + +=back + +Set a Field to MARC mapping value, if it already exists we don't add a new one. + +=cut + +sub SetFieldMapping { + my ($framework, $field, $fieldcode, $subfieldcode) = @_; + my $dbh = C4::Context->dbh; + + my $sth = $dbh->prepare('SELECT * FROM fieldmapping WHERE fieldcode = ? AND subfieldcode = ? AND frameworkcode = ? AND field = ?'); + $sth->execute($fieldcode, $subfieldcode, $framework, $field); + if(not $sth->fetchrow_hashref){ + my @args; + $sth = $dbh->prepare('INSERT INTO fieldmapping (fieldcode, subfieldcode, frameworkcode, field) VALUES(?,?,?,?)'); + + $sth->execute($fieldcode, $subfieldcode, $framework, $field); + } +} + +=head2 DeleteFieldMapping + +=over 4 + +DeleteFieldMapping($id); + +=back + +Delete a field mapping from an $id. + +=cut + +sub DeleteFieldMapping{ + my ($id) = @_; + my $dbh = C4::Context->dbh; + + my $sth = $dbh->prepare('DELETE FROM fieldmapping WHERE id = ?'); + $sth->execute($id); +} + +=head2 GetFieldMapping + +=over 4 + +GetFieldMapping($frameworkcode); + +=back + +Get all field mappings for a specified frameworkcode + +=cut + +sub GetFieldMapping { + my ($framework) = @_; + my $dbh = C4::Context->dbh; + + my $sth = $dbh->prepare('SELECT * FROM fieldmapping where frameworkcode = ?'); + $sth->execute($framework); + + my @return; + while(my $row = $sth->fetchrow_hashref){ + push @return, $row; + } + return \@return; +} + =head2 GetBiblioData =over 4 @@ -818,23 +938,17 @@ sub GetMarcStructure { return $marc_structure_cache->{$forlibrarian}->{$frameworkcode}; } - my $sth; - my $libfield = ( $forlibrarian eq 1 ) ? 'liblibrarian' : 'libopac'; - - # check that framework exists - $sth = - $dbh->prepare( + my $sth = $dbh->prepare( "SELECT COUNT(*) FROM marc_tag_structure WHERE frameworkcode=?"); $sth->execute($frameworkcode); my ($total) = $sth->fetchrow; $frameworkcode = "" unless ( $total > 0 ); - $sth = - $dbh->prepare( + $sth = $dbh->prepare( "SELECT tagfield,liblibrarian,libopac,mandatory,repeatable FROM marc_tag_structure WHERE frameworkcode=? ORDER BY tagfield" - ); + ); $sth->execute($frameworkcode); my ( $liblibrarian, $libopac, $tag, $res, $tab, $mandatory, $repeatable ); @@ -848,13 +962,12 @@ sub GetMarcStructure { $res->{$tag}->{repeatable} = $repeatable; } - $sth = - $dbh->prepare( - "SELECT tagfield,tagsubfield,liblibrarian,libopac,tab,mandatory,repeatable,authorised_value,authtypecode,value_builder,kohafield,seealso,hidden,isurl,link,defaultvalue - FROM marc_subfield_structure - WHERE frameworkcode=? - ORDER BY tagfield,tagsubfield - " + $sth = $dbh->prepare( + "SELECT tagfield,tagsubfield,liblibrarian,libopac,tab,mandatory,repeatable,authorised_value,authtypecode,value_builder,kohafield,seealso,hidden,isurl,link,defaultvalue + FROM marc_subfield_structure + WHERE frameworkcode=? + ORDER BY tagfield,tagsubfield + " ); $sth->execute($frameworkcode); @@ -873,7 +986,7 @@ sub GetMarcStructure { while ( ( $tag, $subfield, $liblibrarian, - , $libopac, $tab, + $libopac, $tab, $mandatory, $repeatable, $authorised_value, $authtypecode, $value_builder, $kohafield, $seealso, $hidden, $isurl, @@ -905,7 +1018,7 @@ sub GetMarcStructure { =head2 GetUsedMarcStructure - the same function as GetMarcStructure expcet it just take field + the same function as GetMarcStructure except it just takes field in tab 0-9. (used field) my $results = GetUsedMarcStructure($frameworkcode); @@ -919,7 +1032,6 @@ sub GetMarcStructure { sub GetUsedMarcStructure($){ my $frameworkcode = shift || ''; - my $dbh = C4::Context->dbh; my $query = qq/ SELECT * FROM marc_subfield_structure @@ -927,13 +1039,9 @@ sub GetUsedMarcStructure($){ AND frameworkcode = ? ORDER BY tagfield, tagsubfield /; - my @results; - my $sth = $dbh->prepare($query); + my $sth = C4::Context->dbh->prepare($query); $sth->execute($frameworkcode); - while (my $row = $sth->fetchrow_hashref){ - push @results,$row; - } - return \@results; + return $sth->fetchall_arrayref({}); } =head2 GetMarcFromKohaField @@ -1037,14 +1145,14 @@ sub GetCOinSBiblio { my $pos6 = substr $record->leader(), 6,1; my $mtx; my $genre; - my ($aulast, $aufirst); - my $oauthors; - my $title; - my $subtitle; - my $pubyear; - my $isbn; - my $issn; - my $publisher; + my ($aulast, $aufirst) = ('',''); + my $oauthors = ''; + my $title = ''; + my $subtitle = ''; + my $pubyear = ''; + my $isbn = ''; + my $issn = ''; + my $publisher = ''; if ( C4::Context->preference("marcflavour") eq "UNIMARC" ){ my $fmts6; @@ -1113,7 +1221,9 @@ sub GetCOinSBiblio { $genre = "&rft.genre=book"; # Setting datas - $oauthors .= "&rft.au=".$record->subfield('100','a'); + if ($record->field('100')) { + $oauthors .= "&rft.au=".$record->subfield('100','a'); + } # others authors if($record->field('700')){ for my $au ($record->field('700')->subfield('a')){ @@ -1315,7 +1425,7 @@ 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. + my $tagslib = &GetMarcStructure( 1, '' ); # FIXME : we don't have the framework available, we take the default framework. May be buggy on some setups, will be usually correct. if ( $marcflavour eq "MARC21" ) { $mintag = "700"; $maxtag = "720"; @@ -1622,6 +1732,7 @@ $auth_type contains : sub TransformHtmlToXml { my ( $tags, $subfields, $values, $indicator, $ind_tag, $auth_type ) = @_; my $xml = MARC::File::XML::header('UTF-8'); + $xml .= "\n"; $auth_type = C4::Context->preference('marcflavour') unless $auth_type; MARC::File::XML->default_record_format($auth_type); # in UNIMARC, field 100 contains the encoding @@ -1659,19 +1770,17 @@ sub TransformHtmlToXml { if ( ( @$tags[$i] && @$tags[$i] > 10 ) && ( @$values[$i] ne "" ) ) { - my $ind1 = substr( @$indicator[$j], 0, 1 ); + my $ind1 = _default_ind_to_space(substr( @$indicator[$j], 0, 1 )); my $ind2; if ( @$indicator[$j] ) { - $ind2 = substr( @$indicator[$j], 1, 1 ); + $ind2 = _default_ind_to_space(substr( @$indicator[$j], 1, 1 )); } else { warn "Indicator in @$tags[$i] is empty"; $ind2 = " "; } - $xml .= -"\n"; - $xml .= -"@$values[$i]\n"; + $xml .= "\n"; + $xml .= "@$values[$i]\n"; $first = 0; } else { @@ -1689,17 +1798,14 @@ sub TransformHtmlToXml { # rest of the fixed fields } elsif ( @$tags[$i] < 10 ) { - $xml .= -"@$values[$i]\n"; + $xml .= "@$values[$i]\n"; $first = 1; } else { - my $ind1 = substr( @$indicator[$j], 0, 1 ); - my $ind2 = substr( @$indicator[$j], 1, 1 ); - $xml .= -"\n"; - $xml .= -"@$values[$i]\n"; + my $ind1 = _default_ind_to_space( substr( @$indicator[$j], 0, 1 ) ); + my $ind2 = _default_ind_to_space( substr( @$indicator[$j], 1, 1 ) ); + $xml .= "\n"; + $xml .= "@$values[$i]\n"; $first = 0; } } @@ -1710,14 +1816,12 @@ sub TransformHtmlToXml { } else { if ($first) { - my $ind1 = substr( @$indicator[$j], 0, 1 ); - my $ind2 = substr( @$indicator[$j], 1, 1 ); - $xml .= -"\n"; + my $ind1 = _default_ind_to_space( substr( @$indicator[$j], 0, 1 ) ); + my $ind2 = _default_ind_to_space( substr( @$indicator[$j], 1, 1 ) ); + $xml .= "\n"; $first = 0; } - $xml .= -"@$values[$i]\n"; + $xml .= "@$values[$i]\n"; } } $prevtag = @$tags[$i]; @@ -1735,10 +1839,26 @@ sub TransformHtmlToXml { $xml .= "$string\n"; $xml .= "\n"; } + $xml .= "\n"; $xml .= MARC::File::XML::footer(); return $xml; } +=head2 _default_ind_to_space + +Passed what should be an indicator returns a space +if its undefined or zero length + +=cut + +sub _default_ind_to_space { + my $s = shift; + if (!defined $s || $s eq q{}) { + return ' '; + } + return $s; +} + =head2 TransformHtmlToMarc L<$record> = TransformHtmlToMarc(L<$params>,L<$cgi>) @@ -1811,8 +1931,8 @@ sub TransformHtmlToMarc { elsif ($param =~ /^tag_(\d*)_indicator1_/){ # new field start when having 'input name="..._indicator1_..." my $tag = $1; - my $ind1 = substr($cgi->param($param),0,1); - my $ind2 = substr($cgi->param($params->[$i+1]),0,1); + my $ind1 = _default_ind_to_space(substr($cgi->param($param), 0, 1)); + my $ind2 = _default_ind_to_space(substr($cgi->param($params->[$i+1]), 0, 1)); $newfield=0; my $j=$i+2; @@ -1841,8 +1961,8 @@ sub TransformHtmlToMarc { if ( $cgi->param($params->[$j+1]) ne '' ) { # creating only if there is a value (code => value) $newfield = MARC::Field->new( $tag, - ''.$ind1, - ''.$ind2, + $ind1, + $ind2, $cgi->param($inner_param) => $cgi->param($params->[$j+1]), ); } @@ -2012,6 +2132,15 @@ more. =cut +sub CountItemsIssued { + my ( $biblionumber ) = @_; + my $dbh = C4::Context->dbh; + my $sth = $dbh->prepare('SELECT COUNT(*) as issuedCount FROM items, issues WHERE items.itemnumber = issues.itemnumber AND items.biblionumber = ?'); + $sth->execute( $biblionumber ); + my $row = $sth->fetchrow_hashref(); + return $row->{'issuedCount'}; +} + sub _disambiguate { my ($table, $column) = @_; if ($column eq "cn_sort" or $column eq "cn_source") { @@ -2139,6 +2268,7 @@ sub PrepareItemrecordDisplay { my ( $bibnum, $itemnum, $defaultvalues ) = @_; my $dbh = C4::Context->dbh; + my $today_iso = C4::Dates->today('iso'); my $frameworkcode = &GetFrameworkCode( $bibnum ); my ( $itemtagfield, $itemtagsubfield ) = &GetMarcFromKohaField( "items.itemnumber", $frameworkcode ); @@ -2176,9 +2306,15 @@ sub PrepareItemrecordDisplay { if ($itemrecord) { ( $x, $value ) = _find_value( $tag, $subfield, $itemrecord ); } - if (!defined $value) { - $value = q||; - } + unless ($value) { + $value = $tagslib->{$tag}->{$subfield}->{defaultvalue}; + $value ||= $defaultvalues->{$tagslib->{$tag}->{$subfield}->{'kohafield'}}; + # get today date & replace YYYY, MM, DD if provided in the default value + my ( $year, $month, $day ) = split ',', $today_iso; # FIXME: iso dates don't have commas! + $value =~ s/YYYY/$year/g; + $value =~ s/MM/$month/g; + $value =~ s/DD/$day/g; + } $value =~ s/"/"/g; # search for itemcallnumber if applicable @@ -2646,7 +2782,8 @@ sub _AddBiblioNoZebra { foreach (split / /,$line) { next unless $_; # skip empty values (multiple spaces) # if the entry is already here, improve weight - if ($result{'__RAW__'}->{"$_"} =~ /$biblionumber,\Q$title\E\-(\d+);/) { + my $tmpstr = $result{'__RAW__'}->{"$_"} || ""; + if ($tmpstr =~ /$biblionumber,\Q$title\E\-(\d+);/) { my $weight=$1+1; $result{'__RAW__'}->{"$_"} =~ s/$biblionumber,\Q$title\E\-(\d+);//; $result{'__RAW__'}->{"$_"} .= "$biblionumber,$title-$weight;"; @@ -2657,7 +2794,7 @@ sub _AddBiblioNoZebra { # it exists if ($existing_biblionumbers) { $result{'__RAW__'}->{"$_"} =$existing_biblionumbers; - my $weight=$1+1; + my $weight = ($1 ? $1 : 0) + 1; $result{'__RAW__'}->{"$_"} =~ s/$biblionumber,\Q$title\E\-(\d+);//; $result{'__RAW__'}->{"$_"} .= "$biblionumber,$title-$weight;"; # create a new ligne for this entry @@ -3248,9 +3385,8 @@ sub ModBiblioMarc { # deal with UNIMARC field 100 (encoding) : create it if needed & set encoding to unicode if ( $encoding eq "UNIMARC" ) { - my $string; - if ( length($record->subfield( 100, "a" )) == 35 ) { - $string = $record->subfield( 100, "a" ); + my $string = $record->subfield( 100, "a" ); + if ( ($string) && ( length($record->subfield( 100, "a" )) == 35 ) ) { my $f100 = $record->field(100); $record->delete_field($f100); } @@ -3397,7 +3533,7 @@ sub set_service_options { biblionumber MARC::Record of the bib - returns: a hashref malling the authorised value to the value set for this biblionumber + returns: a hashref mapping the authorised value to the value set for this biblionumber $authorised_values = { 'Scent' => 'flowery',