#$XML::SAX::ParserPackage = "XML::LibXML::SAX";
use MARC::File::XML;
use ZOOM;
+use POSIX qw(strftime);
use C4::Koha;
use C4::Dates qw/format_date/;
require C4::Heading;
require C4::Serials;
-use vars qw($VERSION @ISA @EXPORT);
+use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
BEGIN {
$VERSION = 1.00;
# to add biblios
# EXPORTED FUNCTIONS.
+ push @EXPORT_OK, qw(
+ &GetRecordValue
+ );
+
push @EXPORT, qw(
&AddBiblio
);
&GetBiblioItemInfosOf
&GetBiblioItemByBiblioNumber
&GetBiblioFromItemNumber
+ &GetBiblioSummary
+
+ &GetRecordValue
+ &GetFieldMapping
+ &SetFieldMapping
+ &DeleteFieldMapping
+
+ &GetISBDView
&GetMarcNotes
&GetMarcSubjects
GetMarcUrls
&GetUsedMarcStructure
&GetXmlBiblio
- &GetCOinSBiblio
+ &GetCOinSBiblio
&GetAuthorisedValueDesc
&GetMarcStructure
&GetFrameworkCode
&GetPublisherNameFromIsbn
&TransformKohaToMarc
+
+ &CountItemsIssued
);
# To modify something
=cut
sub AddBiblio {
- my $record = shift;
- my $frameworkcode = shift;
- my $options = @_ ? shift : undef;
- my $defer_marc_save = 0;
- if (defined $options and exists $options->{'defer_marc_save'} and $options->{'defer_marc_save'}) {
- $defer_marc_save = 1;
- }
-
- my ($biblionumber,$biblioitemnumber,$error);
+ my ( $record, $frameworkcode, $options ) = @_;
my $dbh = C4::Context->dbh;
# transform the data into koha-table style data
my $olddata = TransformMarcToKoha( $dbh, $record, $frameworkcode );
- ($biblionumber,$error) = _koha_add_biblio( $dbh, $olddata, $frameworkcode );
+ my ($biblionumber) = _koha_add_biblio( $dbh, $olddata, $frameworkcode );
$olddata->{'biblionumber'} = $biblionumber;
- ($biblioitemnumber,$error) = _koha_add_biblioitem( $dbh, $olddata );
+ my ($biblioitemnumber) = _koha_add_biblioitem( $dbh, $olddata );
_koha_marc_update_bib_ids($record, $frameworkcode, $biblionumber, $biblioitemnumber);
_koha_marc_update_biblioitem_cn_sort($record, $olddata, $frameworkcode);
# now add the record
- $biblionumber = ModBiblioMarc( $record, $biblionumber, $frameworkcode ) unless $defer_marc_save;
+ ModBiblioMarc( $record, $biblionumber, $frameworkcode ) unless $$options{defer_marc_save} ||= 0;
logaction("CATALOGUING", "ADD", $biblionumber, "biblio") if C4::Context->preference("CataloguingLog");
-
return ( $biblionumber, $biblioitemnumber );
}
return $error if $error;
# We delete attached subscriptions
- if(C4::Serials::CountSubscriptionFromBiblionumber($biblionumber) != 0){
- my $subscriptions = &C4::Serials::GetFullSubscriptionsFromBiblionumber($biblionumber);
- foreach my $subscription (@$subscriptions){
- &C4::Serials::DelSubscription($subscription->{subscriptionid});
- }
+ my $subscriptions = &C4::Serials::GetFullSubscriptionsFromBiblionumber($biblionumber);
+ foreach my $subscription (@$subscriptions){
+ &C4::Serials::DelSubscription($subscription->{subscriptionid});
}
# Delete in Zebra. Be careful NOT to move this line after _koha_delete_biblio
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
FROM biblio LEFT JOIN biblioitems on biblio.biblionumber=biblioitems.biblionumber ";
unless(C4::Context->preference('item-level_itypes')) {
$query .= "LEFT JOIN itemtypes on biblioitems.itemtype=itemtypes.itemtype ";
- }
- $query .= " WHERE biblioitemnumber = ? ";
+ } else {
+ $query .= "LEFT JOIN items on biblio.biblionumber=items.biblionumber ";
+ }
+ $query .= " WHERE biblioitems.biblioitemnumber = ? ";
my $sth = $dbh->prepare($query);
my $data;
$sth->execute($biblioitemnumber);
return ($data);
}
+=head2 GetISBDView
+
+=over 4
+
+$isbd = &GetISBDView($biblionumber);
+
+Return the ISBD view which can be included in opac and intranet
+
+=back
+
+=cut
+
+sub GetISBDView {
+ my ($biblionumber, $template) = @_;
+ my $record = GetMarcBiblio($biblionumber);
+ my $itemtype = &GetFrameworkCode($biblionumber);
+ my ($holdingbrtagf,$holdingbrtagsubf) = &GetMarcFromKohaField("items.holdingbranch",$itemtype);
+ my $tagslib = &GetMarcStructure( 1, $itemtype );
+
+ my $ISBD = C4::Context->preference('ISBD');
+ if($template eq "opac"){
+ $ISBD = C4::Context->preference('OPACISBD');
+ }
+
+ my $bloc = $ISBD;
+ my $res;
+ my $blocres;
+
+ foreach my $isbdfield ( split (/#/, $bloc) ) {
+
+ # $isbdfield= /(.?.?.?)/;
+ $isbdfield =~ /(\d\d\d)([^\|])?\|(.*)\|(.*)\|(.*)/;
+ my $fieldvalue = $1 || 0;
+ my $subfvalue = $2 || "";
+ my $textbefore = $3;
+ my $analysestring = $4;
+ my $textafter = $5;
+
+ # warn "==> $1 / $2 / $3 / $4";
+ # my $fieldvalue=substr($isbdfield,0,3);
+ if ( $fieldvalue > 0 ) {
+ my $hasputtextbefore = 0;
+ my @fieldslist = $record->field($fieldvalue);
+ @fieldslist = sort {$a->subfield($holdingbrtagsubf) cmp $b->subfield($holdingbrtagsubf)} @fieldslist if ($fieldvalue eq $holdingbrtagf);
+
+ # warn "ERROR IN ISBD DEFINITION at : $isbdfield" unless $fieldvalue;
+ # warn "FV : $fieldvalue";
+ if ($subfvalue ne ""){
+ foreach my $field ( @fieldslist ) {
+ foreach my $subfield ($field->subfield($subfvalue)){
+ my $calculated = $analysestring;
+ my $tag = $field->tag();
+ if ( $tag < 10 ) {
+ }
+ else {
+ my $subfieldvalue =
+ GetAuthorisedValueDesc( $tag, $subfvalue,
+ $subfield, '', $tagslib );
+ my $tagsubf = $tag . $subfvalue;
+ $calculated =~
+ s/\{(.?.?.?.?)$tagsubf(.*?)\}/$1$subfieldvalue$2\{$1$tagsubf$2\}/g;
+
+ # field builded, store the result
+ if ( $calculated && !$hasputtextbefore )
+ { # put textbefore if not done
+ $blocres .= $textbefore;
+ $hasputtextbefore = 1;
+ }
+
+ # remove punctuation at start
+ $calculated =~ s/^( |;|:|\.|-)*//g;
+ $blocres .= $calculated;
+
+ }
+ }
+ }
+ $blocres .= $textafter if $hasputtextbefore;
+ } else {
+ foreach my $field ( @fieldslist ) {
+ my $calculated = $analysestring;
+ my $tag = $field->tag();
+ if ( $tag < 10 ) {
+ }
+ else {
+ my @subf = $field->subfields;
+ for my $i ( 0 .. $#subf ) {
+ my $valuecode = $subf[$i][1];
+ my $subfieldcode = $subf[$i][0];
+ my $subfieldvalue =
+ GetAuthorisedValueDesc( $tag, $subf[$i][0],
+ $subf[$i][1], '', $tagslib );
+ my $tagsubf = $tag . $subfieldcode;
+
+ $calculated =~ s/\{\{$tagsubf\}\}/$valuecode/gx;
+ $calculated =~
+ s/\{(.?.?.?.?)$tagsubf(.*?)\}/$1$subfieldvalue$2\{$1$tagsubf$2\}/g;
+ }
+
+ # field builded, store the result
+ if ( $calculated && !$hasputtextbefore )
+ { # put textbefore if not done
+ $blocres .= $textbefore;
+ $hasputtextbefore = 1;
+ }
+
+ # remove punctuation at start
+ $calculated =~ s/^( |;|:|\.|-)*//g;
+ $blocres .= $calculated;
+ }
+ }
+ $blocres .= $textafter if $hasputtextbefore;
+ }
+ }
+ else {
+ $blocres .= $isbdfield;
+ }
+ }
+ $res .= $blocres;
+
+ $res =~ s/\{(.*?)\}//g;
+ $res =~ s/\\n/\n/g;
+ $res =~ s/\n/<br\/>/g;
+
+ # remove empty ()
+ $res =~ s/\(\)//g;
+
+ return $res;
+}
+
=head2 GetBiblio
=over 4
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 );
$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);
while (
(
$tag, $subfield, $liblibrarian,
- , $libopac, $tab,
+ $libopac, $tab,
$mandatory, $repeatable, $authorised_value,
$authtypecode, $value_builder, $kohafield,
$seealso, $hidden, $isurl,
=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);
sub GetUsedMarcStructure($){
my $frameworkcode = shift || '';
- my $dbh = C4::Context->dbh;
my $query = qq/
SELECT *
FROM marc_subfield_structure
WHERE tab > -1
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
my $pos6 = substr $record->leader(), 6,1;
my $mtx;
my $genre;
- my ($aulast, $aufirst);
- my $oauthors;
- my $title;
- 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;
$genre = ($mtx eq 'dc') ? "&rft.type=$genre" : "&rft.genre=$genre";
# Setting datas
- $aulast = $record->subfield('700','a');
- $aufirst = $record->subfield('700','b');
+ $aulast = $record->subfield('700','a') || '';
+ $aufirst = $record->subfield('700','b') || '';
$oauthors = "&rft.au=$aufirst $aulast";
# others authors
if($record->field('200')){
}
$title = ( $mtx eq 'dc' ) ? "&rft.title=".$record->subfield('200','a') :
"&rft.title=".$record->subfield('200','a')."&rft.btitle=".$record->subfield('200','a');
- $pubyear = $record->subfield('210','d');
- $publisher = $record->subfield('210','c');
- $isbn = $record->subfield('010','a');
- $issn = $record->subfield('011','a');
+ $pubyear = $record->subfield('210','d') || '';
+ $publisher = $record->subfield('210','c') || '';
+ $isbn = $record->subfield('010','a') || '';
+ $issn = $record->subfield('011','a') || '';
}else{
# MARC21 need some improve
my $fmts;
$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')){
$oauthors .= "&rft.au=$au";
}
}
- $title = "&rft.btitle=".$record->subfield('245','a');
- $pubyear = $record->subfield('260','c');
- $publisher = $record->subfield('260','b');
- $isbn = $record->subfield('020','a');
- $issn = $record->subfield('022','a');
+ $title = "&rft.btitle=".$record->subfield('245','a');
+ $subtitle = $record->subfield('245', 'b') || '';
+ $title .= $subtitle;
+ $pubyear = $record->subfield('260', 'c') || '';
+ $publisher = $record->subfield('260', 'b') || '';
+ $isbn = $record->subfield('020', 'a') || '';
+ $issn = $record->subfield('022', 'a') || '';
}
$coins_value = "ctx_ver=Z39.88-2004&rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3A$mtx$genre$title&rft.isbn=$isbn&rft.issn=$issn&rft.aulast=$aulast&rft.aufirst=$aufirst$oauthors&rft.pub=$publisher&rft.date=$pubyear";
return $coins_value;
}
+=head2 GetBiblioSummary
+
+=over 4
+
+$summary = GetBiblioSummary($marcrecord);
+
+Return the summary of a record.
+
+=back
+
+=cut
+
+sub GetBiblioSummary {
+ my $recorddata =shift @_;
+
+ return unless $recorddata;
+ my $marcflavour = C4::Context->preference("marcflavour");
+ my $marc=MARC::Record::new_from_xml($recorddata,"utf-8",$marcflavour);
+ return unless $marc;
+
+ my $str;
+
+ if($marcflavour eq "MARC21"){
+ $str="<b>".$marc->subfield('245',"a")."</b>" if $marc->subfield('245','a');
+ $str.= " <i>".$marc->subfield('245',"b")."</i> " if $marc->subfield('245','b');
+
+ if ($marc->field('245')){
+ $str.=" / ";
+ foreach ($marc->field('100')->subfield("a")) {
+ $str.=$_." ; ";
+ }
+ $str=~s/ ; $/. /;
+ }
+
+ if ($marc->field('260')){
+ $str.=" - ";
+ $str.=$marc->subfield('260',"a")." " if $marc->subfield('260','a');
+ $str.=" : ".$marc->subfield('260',"b")." " if $marc->subfield('260','b');
+ $str.=", ".$marc->subfield('260',"c")." " if $marc->subfield('260','c');
+ }
+ if ($marc->field('300')){
+ $str.=" - ";
+ $str.=$marc->subfield('300','a') if ($marc->subfield(300,'a'));
+ $str.=" ; ".$marc->subfield('300','b') if $marc->subfield('300','b');
+ $str.=" ; ".$marc->subfield('300','c') if $marc->subfield('300','c');
+ $str.=" ; ".$marc->subfield('300','e') if $marc->subfield('300','e');
+ }
+ foreach ($marc->field('500')){
+ $str.= " - ";
+ foreach ($_->subfield("a")){
+ $str.=$_."; "
+ }
+ }
+ my $itemtypes=GetItemTypes();
+ $str.=" - <u>".$itemtypes->{$marc->subfield('942','c')}->{'description'}."</u> ";
+ $str.="<br />\n";
+
+ }else{
+ $str = "<b>".$marc->subfield('200','a')."</b>" if $marc->subfield('200','a');
+ $str.= " <i>".$marc->subfield('200','e')."</i> " if $marc->subfield('200','e');
+ if ($marc->field('200')){
+ $str.=" / ";
+ foreach ($marc->field('200')->subfield("f")) {
+ $str.=$_." ; ";
+ }
+ $str=~s/ ; $/. /;
+ }
+
+ if ($marc->subfield('200','g')){
+ $str.=" ; ";
+ foreach ($marc->field('200')->subfield("g")){
+ $str.=$_." ; ";
+ }
+ $str=~s/ ; $/. /;
+ }
+
+ if ($marc->field('461')){
+ $str.="- In :";
+ $str.= $marc->subfield('461','t') if $marc->subfield('461','t');
+ $str.=", ".$marc->subfield('461','d') if $marc->subfield('461','d');
+ $str.=", ".$marc->subfield('461','v') if $marc->subfield('461','v');
+ $str.=", ".$marc->subfield('461','h') if $marc->subfield('461','h');
+ $str.=" ; ".$marc->subfield('461','x') if $marc->subfield('461','x');
+ }
+
+ if ($marc->field('210')){
+ $str.=" - ";
+ $str.=$marc->subfield('210',"a")." " if $marc->subfield('210','a');
+ $str.=" : ".$marc->subfield('210',"c")." " if $marc->subfield('210','c');
+ $str.=", ".$marc->subfield('210',"d")." " if $marc->subfield('210','d');
+ }
+
+ if ($marc->field('215')){
+ $str.=" - ";
+ $str.=$marc->subfield('215','a') if ($marc->subfield(215,'a'));
+ $str.=" ; ".$marc->subfield('215','d') if $marc->subfield('215','d');
+ $str.=" ; ".$marc->subfield('215','c') if $marc->subfield('215','c');
+ $str.=" ; ".$marc->subfield('215','e') if $marc->subfield('215','e');
+ }
+ foreach ($marc->field('300')){
+ $str.=" - ";
+ foreach ($_->subfield("a")){
+ $str.=$_."; "
+ }
+ }
+
+ my $itemtypes=GetItemTypes;
+ if($itemtypes->{$marc->subfield('200','b')}){
+ $str.=" - <u>".$itemtypes->{$marc->subfield('200','b')}->{'description'}."</u> ";
+ }
+ $str.="<br />\n";
+ }
+ return $str;
+}
+
=head2 GetAuthorisedValueDesc
=over 4
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";
my @marcurls;
for my $field ( $record->field('856') ) {
- my $marcurl;
my @notes;
for my $note ( $field->subfield('z') ) {
push @notes, { note => $note };
}
my @urls = $field->subfield('u');
foreach my $url (@urls) {
+ my $marcurl;
if ( $marcflavour eq 'MARC21' ) {
my $s3 = $field->subfield('3');
my $link = $field->subfield('y');
=cut
sub TransformKohaToMarc {
-
my ( $hash ) = @_;
- my $dbh = C4::Context->dbh;
- my $sth =
- $dbh->prepare(
+ my $sth = C4::Context->dbh->prepare(
"SELECT tagfield,tagsubfield FROM marc_subfield_structure WHERE frameworkcode=? AND kohafield=?"
);
my $record = MARC::Record->new();
+ SetMarcUnicodeFlag($record, C4::Context->preference("marcflavour"));
foreach (keys %{$hash}) {
- &TransformKohaToMarcOneField( $sth, $record, $_,
- $hash->{$_}, '' );
- }
+ &TransformKohaToMarcOneField( $sth, $record, $_, $hash->{$_}, '' );
+ }
return $record;
}
}
$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 );
+ my @values = split(/\s?\|\s?/, $value, -1);
+
+ foreach my $itemvalue (@values){
+ my $tag = $record->field($tagfield);
+ if ($tag) {
+ $tag->add_subfields( $tagsubfield => $itemvalue );
+ $record->delete_field($tag);
+ $record->insert_fields_ordered($tag);
+ }
+ else {
+ $record->add_fields( $tagfield, " ", " ", $tagsubfield => $itemvalue );
+ }
}
}
return $record;
sub TransformHtmlToXml {
my ( $tags, $subfields, $values, $indicator, $ind_tag, $auth_type ) = @_;
my $xml = MARC::File::XML::header('UTF-8');
+ $xml .= "<record>\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
# }
if ( ( @$tags[$i] ne $prevtag ) ) {
$j++ unless ( @$tags[$i] eq "" );
+ my $indicator1=eval{substr( @$indicator[$j], 0, 1 )};
+ my $indicator2=eval{substr( @$indicator[$j], 1, 1 )};
+ my $ind1 = _default_ind_to_space($indicator1);
+ my $ind2;
+ if ( @$indicator[$j] ) {
+ $ind2 = _default_ind_to_space($indicator2);
+ }
+ else {
+ warn "Indicator in @$tags[$i] is empty";
+ $ind2 = " ";
+ }
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";
+ $xml .= "<datafield tag=\"@$tags[$i]\" ind1=\"$ind1\" ind2=\"$ind2\">\n";
+ $xml .= "<subfield code=\"@$subfields[$i]\">@$values[$i]</subfield>\n";
$first = 0;
}
else {
# rest of the fixed fields
}
elsif ( @$tags[$i] < 10 ) {
- $xml .=
-"<controlfield tag=\"@$tags[$i]\">@$values[$i]</controlfield>\n";
+ $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";
+ $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 "" ) {
+ my $indicator1=eval{substr( @$indicator[$j], 0, 1 )};
+ my $indicator2=eval{substr( @$indicator[$j], 1, 1 )};
+ my $ind1 = _default_ind_to_space($indicator1);
+ my $ind2;
+ if ( @$indicator[$j] ) {
+ $ind2 = _default_ind_to_space($indicator2);
+ }
+ else {
+ warn "Indicator in @$tags[$i] is empty";
+ $ind2 = " ";
+ }
+ 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";
+ $xml .= "<datafield tag=\"@$tags[$i]\" ind1=\"$ind1\" ind2=\"$ind2\">\n";
$first = 0;
}
- $xml .=
-"<subfield code=\"@$subfields[$i]\">@$values[$i]</subfield>\n";
+ $xml .= "<subfield code=\"@$subfields[$i]\">@$values[$i]</subfield>\n";
}
}
$prevtag = @$tags[$i];
$xml .= "</datafield>\n" if @$tags > 0;
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;
$xml .= "<subfield code=\"a\">$string</subfield>\n";
$xml .= "</datafield>\n";
}
+ $xml .= "</record>\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>)
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;
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]),
);
}
=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") {
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 );
my $tagslib = &GetMarcStructure( 1, $frameworkcode );
my $itemrecord = C4::Items::GetMarcItem( $bibnum, $itemnum) if ($itemnum);
+ # FIXME : I'd rather have GetMarcBiblio called out of this.
+ # Since it gets the whole Biblio record for each item
+ my $marcrecord = GetMarcBiblio( $bibnum) if ($bibnum);
my @loop_data;
my $authorised_values_sth =
$dbh->prepare(
$tagslib->{$tag}->{$subfield}->{repeatable};
$subfield_data{hidden} = "display:none"
if $tagslib->{$tag}->{$subfield}->{hidden};
- my ( $x, $value );
- ( $x, $value ) = _find_value( $tag, $subfield, $itemrecord )
- if ($itemrecord);
- $value =~ s/"/"/g;
+ my ( $x, $value );
+ if ($itemrecord) {
+ ( $x, $value ) = _find_value( $tag, $subfield, $itemrecord );
+ }
+ 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
if ( $tagslib->{$tag}->{$subfield}->{kohafield} eq
substr( C4::Context->preference('itemcallnumber'), 0, 3 );
my $CNsubfield =
substr( C4::Context->preference('itemcallnumber'), 3, 1 );
- my $temp = $itemrecord->field($CNtag) if ($itemrecord);
+ my $temp = $marcrecord->field($CNtag) if ($marcrecord);
if ($temp) {
$value = $temp->subfield($CNsubfield);
}
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;";
# 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
sub _koha_add_biblioitem {
my ( $dbh, $biblioitem ) = @_;
- my $error;
+ my @fields = qw/ biblionumber
+ cn_class cn_item cn_sort cn_source cn_suffix
+ collectionissn collectiontitle collectionvolume
+ editionresponsibility editionstatement
+ illus isbn issn itemtype lccn marc
+ notes number pages place
+ publicationyear publishercode size
+ totalissues url
+ volume volumedate volumedesc
+ /;
+
+ ($$biblioitem{cn_sort}) = GetClassSort( @$biblioitem{qw/ biblioitems.cn_source cn_class cn_item /} );
+
+ my $query = 'INSERT INTO biblioitems SET '
+ . join ( ',', map { "$_ =?" } @fields )
+ . ';'
+ ;
- my ($cn_sort) = GetClassSort($biblioitem->{'biblioitems.cn_source'}, $biblioitem->{'cn_class'}, $biblioitem->{'cn_item'} );
- my $query =
- "INSERT INTO biblioitems SET
- biblionumber = ?,
- volume = ?,
- number = ?,
- itemtype = ?,
- isbn = ?,
- issn = ?,
- publicationyear = ?,
- publishercode = ?,
- volumedate = ?,
- volumedesc = ?,
- collectiontitle = ?,
- collectionissn = ?,
- collectionvolume= ?,
- editionstatement= ?,
- editionresponsibility = ?,
- illus = ?,
- pages = ?,
- notes = ?,
- size = ?,
- place = ?,
- lccn = ?,
- marc = ?,
- url = ?,
- cn_source = ?,
- cn_class = ?,
- cn_item = ?,
- cn_suffix = ?,
- cn_sort = ?,
- totalissues = ?
- ";
my $sth = $dbh->prepare($query);
- $sth->execute(
- $biblioitem->{'biblionumber'},
- $biblioitem->{'volume'},
- $biblioitem->{'number'},
- $biblioitem->{'itemtype'},
- $biblioitem->{'isbn'},
- $biblioitem->{'issn'},
- $biblioitem->{'publicationyear'},
- $biblioitem->{'publishercode'},
- $biblioitem->{'volumedate'},
- $biblioitem->{'volumedesc'},
- $biblioitem->{'collectiontitle'},
- $biblioitem->{'collectionissn'},
- $biblioitem->{'collectionvolume'},
- $biblioitem->{'editionstatement'},
- $biblioitem->{'editionresponsibility'},
- $biblioitem->{'illus'},
- $biblioitem->{'pages'},
- $biblioitem->{'bnotes'},
- $biblioitem->{'size'},
- $biblioitem->{'place'},
- $biblioitem->{'lccn'},
- $biblioitem->{'marc'},
- $biblioitem->{'url'},
- $biblioitem->{'biblioitems.cn_source'},
- $biblioitem->{'cn_class'},
- $biblioitem->{'cn_item'},
- $biblioitem->{'cn_suffix'},
- $cn_sort,
- $biblioitem->{'totalissues'}
- );
+ $sth->execute( @$biblioitem{@fields} );
my $bibitemnum = $dbh->{'mysql_insertid'};
- if ( $dbh->errstr ) {
- $error.="ERROR in _koha_add_biblioitem $query".$dbh->errstr;
- warn $error;
- }
+ my $error = '';
+ $dbh->errstr and warn $error .=
+ 'ERROR in _koha_add_biblioitem '
+ . $query
+ . $dbh->errstr
+ ;
$sth->finish();
return ($bibitemnum,$error);
}
# 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);
}
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',