Lines 1108-1113
sub GetMarcSubfieldStructureFromKohaField {
Link Here
|
1108 |
return wantarray ? @{$mss->{$kohafield}} : $mss->{$kohafield}->[0]; |
1108 |
return wantarray ? @{$mss->{$kohafield}} : $mss->{$kohafield}->[0]; |
1109 |
} |
1109 |
} |
1110 |
|
1110 |
|
|
|
1111 |
|
1112 |
sub GetMarcBiblio { |
1113 |
my ($params) = @_; |
1114 |
|
1115 |
if (not defined $params) { |
1116 |
carp 'GetMarcBiblio called without parameters'; |
1117 |
return; |
1118 |
} |
1119 |
my $biblionumber = $params->{biblionumber}; |
1120 |
|
1121 |
if (not defined $biblionumber) { |
1122 |
carp 'GetMarcBiblio called with undefined biblionumber'; |
1123 |
return; |
1124 |
} |
1125 |
delete $params->{biblionumber}; |
1126 |
$params->{biblionumbers} = [$biblionumber]; |
1127 |
my ($biblio) = GetMarcBiblios($params); |
1128 |
return $biblio; |
1129 |
} |
1130 |
|
1111 |
=head2 GetMarcBiblio |
1131 |
=head2 GetMarcBiblio |
1112 |
|
1132 |
|
1113 |
my $record = GetMarcBiblio({ |
1133 |
my $record = GetMarcBiblio({ |
Lines 1148-1204
It only makes sense in combination both embed_items and opac values true.
Link Here
|
1148 |
|
1168 |
|
1149 |
=cut |
1169 |
=cut |
1150 |
|
1170 |
|
1151 |
sub GetMarcBiblio { |
1171 |
sub GetMarcBiblios { |
1152 |
my ($params) = @_; |
1172 |
my ($params) = @_; |
1153 |
|
1173 |
|
1154 |
if (not defined $params) { |
1174 |
if (not defined $params) { |
1155 |
carp 'GetMarcBiblio called without parameters'; |
1175 |
carp 'GetMarcBiblios called without parameters'; |
1156 |
return; |
1176 |
return; |
1157 |
} |
1177 |
} |
1158 |
|
1178 |
|
1159 |
my $biblionumber = $params->{biblionumber}; |
1179 |
my $biblionumbers = $params->{biblionumbers}; |
1160 |
my $embeditems = $params->{embed_items} || 0; |
1180 |
my $embeditems = $params->{embed_items} || 0; |
1161 |
my $opac = $params->{opac} || 0; |
1181 |
my $opac = $params->{opac} || 0; |
1162 |
my $borcat = $params->{borcat} // q{}; |
1182 |
my $borcat = $params->{borcat} // q{}; |
1163 |
|
1183 |
|
1164 |
if (not defined $biblionumber) { |
1184 |
## START HERE ## |
1165 |
carp 'GetMarcBiblio called with undefined biblionumber'; |
1185 |
|
|
|
1186 |
if (not defined $biblionumbers) { |
1187 |
carp 'GetMarcBiblio called with undefined biblionumbers'; |
1166 |
return; |
1188 |
return; |
1167 |
} |
1189 |
} |
1168 |
|
1190 |
|
1169 |
my $dbh = C4::Context->dbh; |
1191 |
my $in_placeholders = join ', ', (('?') x @{$biblionumbers}); |
1170 |
my $sth = $dbh->prepare("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber=? "); |
1192 |
my $dbh = C4::Context->dbh; |
1171 |
$sth->execute($biblionumber); |
1193 |
my $sth = $dbh->prepare("SELECT biblionumber, biblioitemnumber FROM biblioitems WHERE biblionumber IN ($in_placeholders)"); |
1172 |
my $row = $sth->fetchrow_hashref; |
1194 |
$sth->execute(@{$biblionumbers}); |
1173 |
my $biblioitemnumber = $row->{'biblioitemnumber'}; |
1195 |
|
1174 |
my $marcxml = GetXmlBiblio( $biblionumber ); |
1196 |
my %marc_records; |
1175 |
$marcxml = StripNonXmlChars( $marcxml ); |
1197 |
my ($biblionumber, $biblioitemnumber); |
1176 |
my $frameworkcode = GetFrameworkCode($biblionumber); |
1198 |
my $marc_flavour = C4::Context->preference('marcflavour'); |
1177 |
MARC::File::XML->default_record_format( C4::Context->preference('marcflavour') ); |
1199 |
while (my $biblio_keys = $sth->fetchrow_arrayref) { |
1178 |
my $record = MARC::Record->new(); |
1200 |
($biblionumber, $biblioitemnumber) = @{$biblio_keys}; |
|
|
1201 |
|
1202 |
my $marcxml = GetXmlBiblio( $biblionumber ); |
1203 |
$marcxml = StripNonXmlChars( $marcxml ); |
1204 |
my $frameworkcode = GetFrameworkCode($biblionumber); |
1205 |
MARC::File::XML->default_record_format( $marc_flavour ); |
1206 |
my $record = MARC::Record->new(); |
1207 |
|
1208 |
if ($marcxml) { |
1209 |
$record = eval { |
1210 |
MARC::Record::new_from_xml( $marcxml, "utf8", $marc_flavour); |
1211 |
}; |
1212 |
if ($@) { warn " problem with :$biblionumber : $@ \n$marcxml"; } |
1213 |
next unless $record; |
1214 |
|
1215 |
C4::Biblio::_koha_marc_update_bib_ids( |
1216 |
$record, |
1217 |
$frameworkcode, |
1218 |
$biblionumber, |
1219 |
$biblioitemnumber |
1220 |
); |
1221 |
$marc_records{$biblionumber} = $record; |
1222 |
} |
1223 |
} |
1179 |
|
1224 |
|
1180 |
if ($marcxml) { |
1225 |
if ($embeditems) { |
1181 |
$record = eval { |
1226 |
C4::Biblio::EmbedItemsInMarcBiblios({ |
1182 |
MARC::Record::new_from_xml( $marcxml, "utf8", |
1227 |
marc_records => \%marc_records, |
1183 |
C4::Context->preference('marcflavour') ); |
|
|
1184 |
}; |
1185 |
if ($@) { warn " problem with :$biblionumber : $@ \n$marcxml"; } |
1186 |
return unless $record; |
1187 |
|
1188 |
C4::Biblio::_koha_marc_update_bib_ids( $record, $frameworkcode, $biblionumber, |
1189 |
$biblioitemnumber ); |
1190 |
C4::Biblio::EmbedItemsInMarcBiblio({ |
1191 |
marc_record => $record, |
1192 |
biblionumber => $biblionumber, |
1193 |
opac => $opac, |
1228 |
opac => $opac, |
1194 |
borcat => $borcat }) |
1229 |
borcat => $borcat |
1195 |
if ($embeditems); |
1230 |
}); |
1196 |
|
|
|
1197 |
return $record; |
1198 |
} |
1199 |
else { |
1200 |
return; |
1201 |
} |
1231 |
} |
|
|
1232 |
return wantarray ? values %marc_records : \%marc_records; |
1202 |
} |
1233 |
} |
1203 |
|
1234 |
|
1204 |
=head2 GetXmlBiblio |
1235 |
=head2 GetXmlBiblio |
Lines 2790-2846
override if necessary.
Link Here
|
2790 |
|
2821 |
|
2791 |
sub EmbedItemsInMarcBiblio { |
2822 |
sub EmbedItemsInMarcBiblio { |
2792 |
my ($params) = @_; |
2823 |
my ($params) = @_; |
2793 |
my ($marc, $biblionumber, $itemnumbers, $opac, $borcat); |
2824 |
|
2794 |
$marc = $params->{marc_record}; |
2825 |
if ( !$params->{marc_record} ) { |
2795 |
if ( !$marc ) { |
|
|
2796 |
carp 'EmbedItemsInMarcBiblio: No MARC record passed'; |
2826 |
carp 'EmbedItemsInMarcBiblio: No MARC record passed'; |
2797 |
return; |
2827 |
return; |
2798 |
} |
2828 |
} |
2799 |
$biblionumber = $params->{biblionumber}; |
2829 |
if ( !$params->{biblionumber} ) { |
|
|
2830 |
carp 'EmbedItemsInMarcBiblio: No biblionumber passed'; |
2831 |
return; |
2832 |
} |
2833 |
|
2834 |
$params->{marc_records} = { $params->{biblionumber} => $params->{marc_record} }; |
2835 |
delete $params->{marc_record}; |
2836 |
delete $params->{biblionumber}; |
2837 |
|
2838 |
my ($marc_with_items) = EmbedItemsInMarcBiblios($params); |
2839 |
return $marc_with_items; |
2840 |
} |
2841 |
|
2842 |
sub EmbedItemsInMarcBiblios { |
2843 |
my ($params) = @_; |
2844 |
my ($marc_records, $biblionumber, $itemnumbers, $opac, $borcat); |
2845 |
$marc_records = $params->{marc_records}; |
2846 |
|
2800 |
$itemnumbers = $params->{item_numbers}; |
2847 |
$itemnumbers = $params->{item_numbers}; |
2801 |
$opac = $params->{opac}; |
2848 |
$opac = $params->{opac}; |
2802 |
$borcat = $params->{borcat} // q{}; |
2849 |
$borcat = $params->{borcat} // q{}; |
2803 |
|
2850 |
|
2804 |
$itemnumbers = [] unless defined $itemnumbers; |
2851 |
$itemnumbers = [] unless defined $itemnumbers; |
2805 |
|
2852 |
|
2806 |
my $frameworkcode = GetFrameworkCode($biblionumber); |
|
|
2807 |
_strip_item_fields($marc, $frameworkcode); |
2808 |
|
2809 |
# ... and embed the current items |
2853 |
# ... and embed the current items |
2810 |
my $dbh = C4::Context->dbh; |
2854 |
my $dbh = C4::Context->dbh; |
2811 |
my $sth = $dbh->prepare("SELECT itemnumber FROM items WHERE biblionumber = ?"); |
|
|
2812 |
$sth->execute($biblionumber); |
2813 |
my ( $itemtag, $itemsubfield ) = GetMarcFromKohaField( "items.itemnumber", $frameworkcode ); |
2814 |
|
2855 |
|
2815 |
my @item_fields; # Array holding the actual MARC data for items to be included. |
2856 |
my @biblionumbers = keys %{$marc_records}; |
2816 |
my @items; # Array holding items which are both in the list (sitenumbers) |
2857 |
my $in_placeholders = join ', ', (('?') x @biblionumbers); |
2817 |
# and on this biblionumber |
|
|
2818 |
|
2858 |
|
2819 |
# Flag indicating if there is potential hiding. |
2859 |
my $biblio_itemnumbers = $dbh->selectcol_arrayref( |
2820 |
my $opachiddenitems = $opac |
2860 |
"SELECT itemnumber FROM items WHERE biblionumber IN ($in_placeholders)", |
2821 |
&& ( C4::Context->preference('OpacHiddenItems') !~ /^\s*$/ ); |
2861 |
undef, |
|
|
2862 |
@biblionumbers |
2863 |
); |
2864 |
|
2865 |
# Remove invalid item numbers by intersecting with all valid item numbers |
2866 |
# if $itemnumbers argument was provided |
2822 |
|
2867 |
|
|
|
2868 |
# TODO: MAKE SURE THERE IS A TEST FOR THIS |
2869 |
if(@{$itemnumbers}) { |
2870 |
my %h = map { $_ => undef } @{$itemnumbers}; |
2871 |
$itemnumbers = [grep { exists $h{$_} } @{$biblio_itemnumbers}]; |
2872 |
} |
2873 |
else { |
2874 |
$itemnumbers = $biblio_itemnumbers; |
2875 |
} |
2876 |
# If no item numbers then no point in continuing |
2877 |
return unless (@{$itemnumbers}); |
2878 |
|
2879 |
|
2880 |
my $opachiddenitems = $opac |
2881 |
&& ( C4::Context->preference('OpacHiddenItems') !~ /^\s*$/ ); |
2823 |
require C4::Items; |
2882 |
require C4::Items; |
2824 |
while ( my ($itemnumber) = $sth->fetchrow_array ) { |
2883 |
|
2825 |
next if @$itemnumbers and not grep { $_ == $itemnumber } @$itemnumbers; |
2884 |
my $items = C4::Items::GetItems($itemnumbers); |
2826 |
my $i = $opachiddenitems ? C4::Items::GetItem($itemnumber) : undef; |
2885 |
# We have now fetched all items, time to partion by biblionumber |
2827 |
push @items, { itemnumber => $itemnumber, item => $i }; |
2886 |
my %items_by_biblionumber = (map { ($_, []) } @biblionumbers); |
2828 |
} |
2887 |
foreach my $item (@{$items}) { |
2829 |
my @items2pass = map { $_->{item} } @items; |
2888 |
push @{$items_by_biblionumber{$item->{biblionumber}}}, $item; |
2830 |
my @hiddenitems = |
2889 |
} |
2831 |
$opachiddenitems |
2890 |
|
2832 |
? C4::Items::GetHiddenItemnumbers({ |
2891 |
foreach my $biblionumber (@biblionumbers) { |
2833 |
items => \@items2pass, |
2892 |
my $marc_record = $marc_records->{$biblionumber}; |
2834 |
borcat => $borcat }) |
2893 |
|
2835 |
: (); |
2894 |
# Array holding items which are both in itemnumbers |
2836 |
# Convert to a hash for quick searching |
2895 |
# (if provided) and in biblionumbers itemnumbers |
2837 |
my %hiddenitems = map { $_ => 1 } @hiddenitems; |
2896 |
my $items = $items_by_biblionumber{$biblionumber}; |
2838 |
foreach my $itemnumber ( map { $_->{itemnumber} } @items ) { |
2897 |
|
2839 |
next if $hiddenitems{$itemnumber}; |
2898 |
# Could this be moved lower down and run only when items |
2840 |
my $item_marc = C4::Items::GetMarcItem( $biblionumber, $itemnumber ); |
2899 |
# exists for improved performance? Probably.. |
2841 |
push @item_fields, $item_marc->field($itemtag); |
2900 |
my $frameworkcode = GetFrameworkCode($biblionumber); |
2842 |
} |
2901 |
_strip_item_fields($marc_record, $frameworkcode); |
2843 |
$marc->append_fields(@item_fields); |
2902 |
|
|
|
2903 |
# Flag indicating if there is potential hiding |
2904 |
if ($opachiddenitems) { |
2905 |
my %hidden_items = map { $_ => undef } C4::Items::GetHiddenItemnumbers({ |
2906 |
items => @{$items}, |
2907 |
borcat => $borcat |
2908 |
}); |
2909 |
# Reduce items to non hidden items |
2910 |
$items = [grep { !(exists $hidden_items{$_->{itemnumber}}) } @{$items}]; |
2911 |
} |
2912 |
|
2913 |
my ($itemtag) = GetMarcFromKohaField("items.itemnumber", $frameworkcode); |
2914 |
my @item_fields; # Array holding the actual MARC data for items to be included. |
2915 |
foreach my $item (@{$items}) { |
2916 |
my $item_marc = C4::Items::GetMarcItem($biblionumber, $item); |
2917 |
push @item_fields, $item_marc->field($itemtag); |
2918 |
} |
2919 |
$marc_record->append_fields(@item_fields); |
2920 |
} |
2921 |
return $marc_records; |
2844 |
} |
2922 |
} |
2845 |
|
2923 |
|
2846 |
=head1 INTERNAL FUNCTIONS |
2924 |
=head1 INTERNAL FUNCTIONS |
Lines 3292-3298
sub ModBiblioMarc {
Link Here
|
3292 |
|
3370 |
|
3293 |
# deal with UNIMARC field 100 (encoding) : create it if needed & set encoding to unicode |
3371 |
# deal with UNIMARC field 100 (encoding) : create it if needed & set encoding to unicode |
3294 |
if ( $encoding eq "UNIMARC" ) { |
3372 |
if ( $encoding eq "UNIMARC" ) { |
3295 |
my $defaultlanguage = C4::Context->preference("UNIMARCField100Language"); |
3373 |
my $defaultlanguage = C4::Context->preference("UNIMARCField100Language"); |
3296 |
$defaultlanguage = "fre" if (!$defaultlanguage || length($defaultlanguage) != 3); |
3374 |
$defaultlanguage = "fre" if (!$defaultlanguage || length($defaultlanguage) != 3); |
3297 |
my $string = $record->subfield( 100, "a" ); |
3375 |
my $string = $record->subfield( 100, "a" ); |
3298 |
if ( ($string) && ( length( $record->subfield( 100, "a" ) ) == 36 ) ) { |
3376 |
if ( ($string) && ( length( $record->subfield( 100, "a" ) ) == 36 ) ) { |
Lines 3302-3308
sub ModBiblioMarc {
Link Here
|
3302 |
$string = POSIX::strftime( "%Y%m%d", localtime ); |
3380 |
$string = POSIX::strftime( "%Y%m%d", localtime ); |
3303 |
$string =~ s/\-//g; |
3381 |
$string =~ s/\-//g; |
3304 |
$string = sprintf( "%-*s", 35, $string ); |
3382 |
$string = sprintf( "%-*s", 35, $string ); |
3305 |
substr ( $string, 22, 3, $defaultlanguage); |
3383 |
substr ( $string, 22, 3, $defaultlanguage); |
3306 |
} |
3384 |
} |
3307 |
substr( $string, 25, 3, "y50" ); |
3385 |
substr( $string, 25, 3, "y50" ); |
3308 |
unless ( $record->subfield( 100, "a" ) ) { |
3386 |
unless ( $record->subfield( 100, "a" ) ) { |