Lines 53-58
BEGIN {
Link Here
|
53 |
GetMarcFromKohaField |
53 |
GetMarcFromKohaField |
54 |
GetMarcSubfieldStructureFromKohaField |
54 |
GetMarcSubfieldStructureFromKohaField |
55 |
GetFrameworkCode |
55 |
GetFrameworkCode |
|
|
56 |
GetAutoFrameworkCode |
56 |
TransformKohaToMarc |
57 |
TransformKohaToMarc |
57 |
PrepHostMarcField |
58 |
PrepHostMarcField |
58 |
CountItemsIssued |
59 |
CountItemsIssued |
Lines 88-93
use MARC::File::USMARC;
Link Here
|
88 |
use MARC::File::XML; |
89 |
use MARC::File::XML; |
89 |
use POSIX qw(strftime); |
90 |
use POSIX qw(strftime); |
90 |
use Module::Load::Conditional qw(can_load); |
91 |
use Module::Load::Conditional qw(can_load); |
|
|
92 |
use YAML qw(Load); |
91 |
|
93 |
|
92 |
use C4::Koha; |
94 |
use C4::Koha; |
93 |
use C4::Log; # logaction |
95 |
use C4::Log; # logaction |
Lines 216-221
sub AddBiblio {
Link Here
|
216 |
|
218 |
|
217 |
# transform the data into koha-table style data |
219 |
# transform the data into koha-table style data |
218 |
SetUTF8Flag($record); |
220 |
SetUTF8Flag($record); |
|
|
221 |
|
222 |
$frameworkcode = GetAutoFrameworkCode($record) if (!$frameworkcode || $frameworkcode eq ''); |
223 |
|
219 |
my $olddata = TransformMarcToKoha( $record, $frameworkcode ); |
224 |
my $olddata = TransformMarcToKoha( $record, $frameworkcode ); |
220 |
( $biblionumber, $error ) = _koha_add_biblio( $dbh, $olddata, $frameworkcode ); |
225 |
( $biblionumber, $error ) = _koha_add_biblio( $dbh, $olddata, $frameworkcode ); |
221 |
$olddata->{'biblionumber'} = $biblionumber; |
226 |
$olddata->{'biblionumber'} = $biblionumber; |
Lines 290-295
sub ModBiblio {
Link Here
|
290 |
my $dbh = C4::Context->dbh; |
295 |
my $dbh = C4::Context->dbh; |
291 |
|
296 |
|
292 |
$frameworkcode = "" if !$frameworkcode || $frameworkcode eq "Default"; # XXX |
297 |
$frameworkcode = "" if !$frameworkcode || $frameworkcode eq "Default"; # XXX |
|
|
298 |
$frameworkcode = GetAutoFrameworkCode($record) if ($frameworkcode eq ""); |
293 |
|
299 |
|
294 |
_strip_item_fields($record, $frameworkcode); |
300 |
_strip_item_fields($record, $frameworkcode); |
295 |
|
301 |
|
Lines 2114-2119
sub GetFrameworkCode {
Link Here
|
2114 |
return $frameworkcode; |
2120 |
return $frameworkcode; |
2115 |
} |
2121 |
} |
2116 |
|
2122 |
|
|
|
2123 |
=head2 _matchRecordFieldspec |
2124 |
|
2125 |
$language = _matchRecordFieldspec($record, '008/35-37'); |
2126 |
|
2127 |
Returns field value from record. Fieldspec is a string of the following type: |
2128 |
'003', '100$a', '000/07', '008/35-37', or any of those types joined with plus sign. |
2129 |
If a matching field has been repeated in the record, the value from the first one is returned. |
2130 |
|
2131 |
=cut |
2132 |
|
2133 |
sub _matchRecordFieldspec { |
2134 |
my ($record, $fieldstr) = @_; |
2135 |
|
2136 |
$fieldstr =~ s/^\s+//; |
2137 |
$fieldstr =~ s/\s+$//; |
2138 |
|
2139 |
if ($fieldstr =~ /^(\d\d\d)$/) { |
2140 |
my $fld = $1; |
2141 |
my $data = ''; |
2142 |
if ($fld eq '000') { |
2143 |
$data = $record->leader(); |
2144 |
} else { |
2145 |
my $field = $record->field($fld); |
2146 |
$data = $field->data() if ($field && $field->is_control_field()); |
2147 |
} |
2148 |
return $data; |
2149 |
} elsif ($fieldstr =~ /^(\d\d\d)\$(\S)$/) { |
2150 |
my ($fld, $subfld) = ($1, $2); |
2151 |
my $data = ''; |
2152 |
my @fields = $record->field($fld); |
2153 |
foreach my $field (@fields) { |
2154 |
if ($field && !$field->is_control_field() && $field->subfield($subfld)) { |
2155 |
return $field->subfield($subfld); |
2156 |
} |
2157 |
} |
2158 |
return $data; |
2159 |
} elsif ($fieldstr =~ /^(\d\d\d)\/(\d+)$/) { |
2160 |
my ($fld, $pos) = ($1, int($2)); |
2161 |
my $data = ''; |
2162 |
if ($fld eq '000') { |
2163 |
$data = $record->leader(); |
2164 |
} else { |
2165 |
my $field = $record->field($fld); |
2166 |
$data = $field->data() if ($field && $field->is_control_field()); |
2167 |
} |
2168 |
return substr($data, $pos, 1); |
2169 |
} elsif ($fieldstr =~ /^(\d\d\d)\/(\d+)-(\d+)$/) { |
2170 |
my ($fld, $spos, $epos) = ($1, int($2), int($3)); |
2171 |
my $data = ''; |
2172 |
if ($fld eq '000') { |
2173 |
$data = $record->leader(); |
2174 |
} else { |
2175 |
my $field = $record->field($fld); |
2176 |
$data = $field->data() if ($field && $field->is_control_field()); |
2177 |
} |
2178 |
return substr($data, $spos, ($epos-$spos)+1); |
2179 |
} elsif ($fieldstr =~ /^(.+)\+(.+)$/) { |
2180 |
my ($fld1, $fld2) = ($1, $2); |
2181 |
return _matchRecordFieldspec($record, $fld1) . '+' . _matchRecordFieldspec($record, $fld2); |
2182 |
} else { |
2183 |
warn "_matchRecordFieldspec: unknown fieldspec '$fieldstr'"; |
2184 |
} |
2185 |
return ''; |
2186 |
} |
2187 |
|
2188 |
=head2 GetAutoFrameworkCode |
2189 |
|
2190 |
$frameworkcode = GetAutoFrameworkCode( $marcRecord ); |
2191 |
|
2192 |
Uses the MarcToFrameworkcodeAutoconvert system preference to determine what |
2193 |
framework code the MARC record should have, based on the record field values. |
2194 |
|
2195 |
=cut |
2196 |
|
2197 |
sub GetAutoFrameworkCode { |
2198 |
my ($record) = @_; |
2199 |
|
2200 |
my $prefname = 'MarcToFrameworkcodeAutoconvert'; |
2201 |
|
2202 |
my $cache = Koha::Caches->get_instance(); |
2203 |
my $cache_key = "parsed-pref-$prefname"; |
2204 |
my $fwcoderules = $cache->get_from_cache($cache_key); |
2205 |
|
2206 |
if (!$fwcoderules) { |
2207 |
my $yaml = C4::Context->preference($prefname) || ''; |
2208 |
return '' if ($yaml !~ /\S/); |
2209 |
|
2210 |
$yaml = "$yaml\n\n"; |
2211 |
eval { |
2212 |
$fwcoderules = YAML::Load($yaml); |
2213 |
}; |
2214 |
if ($@) { |
2215 |
warn "Unable to parse $prefname syspref: $@"; |
2216 |
return ''; |
2217 |
} |
2218 |
|
2219 |
if (ref($fwcoderules) ne 'ARRAY') { |
2220 |
warn "$prefname YAML root element is not array"; |
2221 |
return ''; |
2222 |
} |
2223 |
|
2224 |
$cache->set_in_cache($cache_key, $fwcoderules); |
2225 |
} |
2226 |
|
2227 |
foreach my $elem (@$fwcoderules) { |
2228 |
if (ref($elem) ne 'HASH') { |
2229 |
warn "$prefname 2nd level YAML element not a hash"; |
2230 |
$cache->clear_from_cache($cache_key); |
2231 |
return ''; |
2232 |
} |
2233 |
foreach my $ekey (keys(%{$elem})) { |
2234 |
my $matchvalue = _matchRecordFieldspec($record, $ekey) || ''; |
2235 |
if (defined($elem->{$ekey})) { |
2236 |
my $matches = $elem->{$ekey}; |
2237 |
if (ref($elem->{$ekey}) ne 'HASH') { |
2238 |
warn "$prefname 3rd level YAML element not a hash"; |
2239 |
$cache->clear_from_cache($cache_key); |
2240 |
return ''; |
2241 |
} |
2242 |
my %hmatches = %{$matches}; |
2243 |
foreach my $elm (keys(%hmatches)) { |
2244 |
return $hmatches{$elm} if ($elm eq $matchvalue); |
2245 |
} |
2246 |
} |
2247 |
} |
2248 |
} |
2249 |
return ''; |
2250 |
} |
2251 |
|
2117 |
=head2 TransformKohaToMarc |
2252 |
=head2 TransformKohaToMarc |
2118 |
|
2253 |
|
2119 |
$record = TransformKohaToMarc( $hash [, $params ] ) |
2254 |
$record = TransformKohaToMarc( $hash [, $params ] ) |
Lines 3247-3252
sub ModBiblioMarc {
Link Here
|
3247 |
if ( !$frameworkcode ) { |
3382 |
if ( !$frameworkcode ) { |
3248 |
$frameworkcode = ""; |
3383 |
$frameworkcode = ""; |
3249 |
} |
3384 |
} |
|
|
3385 |
$frameworkcode = GetAutoFrameworkCode($record) if ($frameworkcode eq ""); |
3250 |
my $sth = $dbh->prepare("UPDATE biblio SET frameworkcode=? WHERE biblionumber=?"); |
3386 |
my $sth = $dbh->prepare("UPDATE biblio SET frameworkcode=? WHERE biblionumber=?"); |
3251 |
$sth->execute( $frameworkcode, $biblionumber ); |
3387 |
$sth->execute( $frameworkcode, $biblionumber ); |
3252 |
$sth->finish; |
3388 |
$sth->finish; |