Lines 106-111
BEGIN {
Link Here
|
106 |
&CountItemsIssued |
106 |
&CountItemsIssued |
107 |
&CountBiblioInOrders |
107 |
&CountBiblioInOrders |
108 |
&GetSubscriptionsId |
108 |
&GetSubscriptionsId |
|
|
109 |
|
110 |
&GetMarcPermissionsRules |
111 |
&GetMarcPermissionsModules |
112 |
&ModMarcPermissionsRule |
113 |
&AddMarcPermissionsRule |
114 |
&DelMarcPermissionsRule |
109 |
); |
115 |
); |
110 |
|
116 |
|
111 |
# To modify something |
117 |
# To modify something |
Lines 133-138
BEGIN {
Link Here
|
133 |
# they are useful in a few circumstances, so they are exported, |
139 |
# they are useful in a few circumstances, so they are exported, |
134 |
# but don't use them unless you are a core developer ;-) |
140 |
# but don't use them unless you are a core developer ;-) |
135 |
push @EXPORT, qw( |
141 |
push @EXPORT, qw( |
|
|
142 |
&ApplyMarcPermissions |
136 |
&ModBiblioMarc |
143 |
&ModBiblioMarc |
137 |
); |
144 |
); |
138 |
|
145 |
|
Lines 257-263
sub AddBiblio {
Link Here
|
257 |
_koha_marc_update_biblioitem_cn_sort( $record, $olddata, $frameworkcode ); |
264 |
_koha_marc_update_biblioitem_cn_sort( $record, $olddata, $frameworkcode ); |
258 |
|
265 |
|
259 |
# now add the record |
266 |
# now add the record |
260 |
ModBiblioMarc( $record, $biblionumber, $frameworkcode ) unless $defer_marc_save; |
267 |
ModBiblioMarc( $record, $biblionumber, $frameworkcode, $options->{filter} ) unless $defer_marc_save; |
261 |
|
268 |
|
262 |
# update OAI-PMH sets |
269 |
# update OAI-PMH sets |
263 |
if(C4::Context->preference("OAI-PMH:AutoUpdateSets")) { |
270 |
if(C4::Context->preference("OAI-PMH:AutoUpdateSets")) { |
Lines 270-276
sub AddBiblio {
Link Here
|
270 |
|
277 |
|
271 |
=head2 ModBiblio |
278 |
=head2 ModBiblio |
272 |
|
279 |
|
273 |
ModBiblio( $record,$biblionumber,$frameworkcode); |
280 |
ModBiblio($record, $biblionumber, $frameworkcode, $filter); |
274 |
|
281 |
|
275 |
Replace an existing bib record identified by C<$biblionumber> |
282 |
Replace an existing bib record identified by C<$biblionumber> |
276 |
with one supplied by the MARC::Record object C<$record>. The embedded |
283 |
with one supplied by the MARC::Record object C<$record>. The embedded |
Lines 291-297
Returns 1 on success 0 on failure
Link Here
|
291 |
=cut |
298 |
=cut |
292 |
|
299 |
|
293 |
sub ModBiblio { |
300 |
sub ModBiblio { |
294 |
my ( $record, $biblionumber, $frameworkcode ) = @_; |
301 |
my ( $record, $biblionumber, $frameworkcode, $filter ) = @_; |
|
|
302 |
|
295 |
if (!$record) { |
303 |
if (!$record) { |
296 |
carp 'No record passed to ModBiblio'; |
304 |
carp 'No record passed to ModBiblio'; |
297 |
return 0; |
305 |
return 0; |
Lines 329-341
sub ModBiblio {
Link Here
|
329 |
_koha_marc_update_bib_ids( $record, $frameworkcode, $biblionumber, $biblioitemnumber ); |
337 |
_koha_marc_update_bib_ids( $record, $frameworkcode, $biblionumber, $biblioitemnumber ); |
330 |
|
338 |
|
331 |
# load the koha-table data object |
339 |
# load the koha-table data object |
332 |
my $oldbiblio = TransformMarcToKoha( $record, $frameworkcode ); |
340 |
my $oldbiblio = TransformMarcToKoha( $record, $frameworkcode, undef, $biblionumber, $filter ); |
333 |
|
341 |
|
334 |
# update MARC subfield that stores biblioitems.cn_sort |
342 |
# update MARC subfield that stores biblioitems.cn_sort |
335 |
_koha_marc_update_biblioitem_cn_sort( $record, $oldbiblio, $frameworkcode ); |
343 |
_koha_marc_update_biblioitem_cn_sort( $record, $oldbiblio, $frameworkcode ); |
336 |
|
344 |
|
337 |
# update the MARC record (that now contains biblio and items) with the new record data |
345 |
# update the MARC record (that now contains biblio and items) with the new record data |
338 |
&ModBiblioMarc( $record, $biblionumber, $frameworkcode ); |
346 |
ModBiblioMarc( $record, $biblionumber, $frameworkcode, $filter ); |
339 |
|
347 |
|
340 |
# modify the other koha tables |
348 |
# modify the other koha tables |
341 |
_koha_modify_biblio( $dbh, $oldbiblio, $frameworkcode ); |
349 |
_koha_modify_biblio( $dbh, $oldbiblio, $frameworkcode ); |
Lines 2656-2662
sub TransformHtmlToMarc {
Link Here
|
2656 |
|
2664 |
|
2657 |
=head2 TransformMarcToKoha |
2665 |
=head2 TransformMarcToKoha |
2658 |
|
2666 |
|
2659 |
$result = TransformMarcToKoha( $record, $frameworkcode ) |
2667 |
$result = TransformMarcToKoha( $record, $frameworkcode, $biblionumber, $filter ) |
2660 |
|
2668 |
|
2661 |
Extract data from a MARC bib record into a hashref representing |
2669 |
Extract data from a MARC bib record into a hashref representing |
2662 |
Koha biblio, biblioitems, and items fields. |
2670 |
Koha biblio, biblioitems, and items fields. |
Lines 2667-2673
hash_ref
Link Here
|
2667 |
=cut |
2675 |
=cut |
2668 |
|
2676 |
|
2669 |
sub TransformMarcToKoha { |
2677 |
sub TransformMarcToKoha { |
2670 |
my ( $record, $frameworkcode, $limit_table ) = @_; |
2678 |
my ( $record, $frameworkcode, $limit_table, $biblionumber, $filter ) = @_; |
2671 |
|
2679 |
|
2672 |
my $result = {}; |
2680 |
my $result = {}; |
2673 |
if (!defined $record) { |
2681 |
if (!defined $record) { |
Lines 2688-2693
sub TransformMarcToKoha {
Link Here
|
2688 |
$tables{'biblioitems'} = 1; |
2696 |
$tables{'biblioitems'} = 1; |
2689 |
} |
2697 |
} |
2690 |
|
2698 |
|
|
|
2699 |
# apply permissions |
2700 |
if ( C4::Context->preference('MARCPermissions') && $biblionumber && $filter) { |
2701 |
$record = ApplyMarcPermissions({ |
2702 |
biblionumber => $biblionumber, |
2703 |
record => $record, |
2704 |
frameworkcode => $frameworkcode, |
2705 |
filter => $filter, |
2706 |
nolog => 1 |
2707 |
}); |
2708 |
} |
2709 |
|
2691 |
# traverse through record |
2710 |
# traverse through record |
2692 |
MARCFIELD: foreach my $field ( $record->fields() ) { |
2711 |
MARCFIELD: foreach my $field ( $record->fields() ) { |
2693 |
my $tag = $field->tag(); |
2712 |
my $tag = $field->tag(); |
Lines 3442-3448
sub _koha_delete_biblioitems {
Link Here
|
3442 |
|
3461 |
|
3443 |
=head2 ModBiblioMarc |
3462 |
=head2 ModBiblioMarc |
3444 |
|
3463 |
|
3445 |
&ModBiblioMarc($newrec,$biblionumber,$frameworkcode); |
3464 |
&ModBiblioMarc($newrec,$biblionumber,$frameworkcode,$filter); |
3446 |
|
3465 |
|
3447 |
Add MARC XML data for a biblio to koha |
3466 |
Add MARC XML data for a biblio to koha |
3448 |
|
3467 |
|
Lines 3453-3459
Function exported, but should NOT be used, unless you really know what you're do
Link Here
|
3453 |
sub ModBiblioMarc { |
3472 |
sub ModBiblioMarc { |
3454 |
# pass the MARC::Record to this function, and it will create the records in |
3473 |
# pass the MARC::Record to this function, and it will create the records in |
3455 |
# the marcxml field |
3474 |
# the marcxml field |
3456 |
my ( $record, $biblionumber, $frameworkcode ) = @_; |
3475 |
my ( $record, $biblionumber, $frameworkcode, $filter ) = @_; |
|
|
3476 |
|
3457 |
if ( !$record ) { |
3477 |
if ( !$record ) { |
3458 |
carp 'ModBiblioMarc passed an undefined record'; |
3478 |
carp 'ModBiblioMarc passed an undefined record'; |
3459 |
return; |
3479 |
return; |
Lines 3466-3471
sub ModBiblioMarc {
Link Here
|
3466 |
if ( !$frameworkcode ) { |
3486 |
if ( !$frameworkcode ) { |
3467 |
$frameworkcode = ""; |
3487 |
$frameworkcode = ""; |
3468 |
} |
3488 |
} |
|
|
3489 |
|
3490 |
# apply permissions |
3491 |
if ( C4::Context->preference('MARCPermissions') ) { |
3492 |
$record = ApplyMarcPermissions({ |
3493 |
biblionumber => $biblionumber, |
3494 |
record => $record, |
3495 |
frameworkcode => $frameworkcode, |
3496 |
filter => $filter |
3497 |
}); |
3498 |
} |
3499 |
|
3469 |
my $sth = $dbh->prepare("UPDATE biblio SET frameworkcode=? WHERE biblionumber=?"); |
3500 |
my $sth = $dbh->prepare("UPDATE biblio SET frameworkcode=? WHERE biblionumber=?"); |
3470 |
$sth->execute( $frameworkcode, $biblionumber ); |
3501 |
$sth->execute( $frameworkcode, $biblionumber ); |
3471 |
$sth->finish; |
3502 |
$sth->finish; |
Lines 3786-3791
sub RemoveAllNsb {
Link Here
|
3786 |
return $record; |
3817 |
return $record; |
3787 |
} |
3818 |
} |
3788 |
|
3819 |
|
|
|
3820 |
=head2 ApplyMarcPermissions |
3821 |
|
3822 |
my $record = ApplyMarcPermissions($arguments) |
3823 |
|
3824 |
Applies marc permission rules to a record. |
3825 |
|
3826 |
C<$arguments> is expected to be a hashref with below keys defined. |
3827 |
|
3828 |
=over 4 |
3829 |
|
3830 |
=item C<biblionumber> |
3831 |
biblionumber of old record |
3832 |
|
3833 |
=item C<record> |
3834 |
record that will modify old record |
3835 |
|
3836 |
=item C<frameworkcode> |
3837 |
only tags included in framework will be processed |
3838 |
|
3839 |
=item C<filter> |
3840 |
hashref containing at least one filter module from the marc_permissions_modules |
3841 |
table in form {module => filter}. Three predefined filter modules exists: |
3842 |
|
3843 |
* source |
3844 |
* category |
3845 |
* borrower |
3846 |
|
3847 |
=item C<log> |
3848 |
optional reference to array that will be filled with rule evaluation log |
3849 |
entries. |
3850 |
|
3851 |
=item C<nolog> |
3852 |
optional boolean which when true disables logging to action log. |
3853 |
|
3854 |
=back |
3855 |
|
3856 |
Returns: |
3857 |
|
3858 |
=over 4 |
3859 |
|
3860 |
=item C<$record> |
3861 |
|
3862 |
new MARC record based on C<record> with C<filter> applied. If no old |
3863 |
record for C<biblionumber> can be found, C<record> is returned unchanged. |
3864 |
Default action when no matching filter found is to leave old record unchanged. |
3865 |
|
3866 |
=back |
3867 |
|
3868 |
=cut |
3869 |
|
3870 |
sub ApplyMarcPermissions { |
3871 |
my $arguments = shift; |
3872 |
my $biblionumber = $arguments->{biblionumber}; |
3873 |
my $new_record = $arguments->{record}; |
3874 |
my $frameworkcode = $arguments->{frameworkcode} || ''; |
3875 |
|
3876 |
if ( !$biblionumber ) { |
3877 |
carp 'ApplyMarcPermissions called on undefined biblionumber'; |
3878 |
return; |
3879 |
} |
3880 |
if ( !$new_record ) { |
3881 |
carp 'ApplyMarcPermissions called on undefined record'; |
3882 |
return; |
3883 |
} |
3884 |
my $filter = $arguments->{filter} |
3885 |
or return $new_record; |
3886 |
|
3887 |
my $log = $arguments->{log} || []; |
3888 |
my $nolog = $arguments->{nolog}; |
3889 |
|
3890 |
my $dbh = C4::Context->dbh; |
3891 |
|
3892 |
my $is_regex = sub { |
3893 |
my ( $tag, $m ) = @_; |
3894 |
# tag is not exactly same as possible regex |
3895 |
$tag ne $m && |
3896 |
|
3897 |
# wildcard |
3898 |
$m ne '*' && |
3899 |
|
3900 |
# valid tagDataType |
3901 |
$m !~ /^(0[1-9A-z][\dA-Z]) | |
3902 |
([1-9A-z][\dA-z]{2})$/x && |
3903 |
|
3904 |
# nor valid controltagDataType |
3905 |
$m !~ /00[1-9A-Za-z]{1}/ && |
3906 |
|
3907 |
# so we try it as a regex |
3908 |
$tag =~ /^$m$/ |
3909 |
}; |
3910 |
|
3911 |
my $old_record = GetMarcBiblio($biblionumber); |
3912 |
if ( $old_record ) { |
3913 |
my $ret_record = MARC::Record->new(); |
3914 |
my $perm = GetMarcPermissions() or return $new_record; |
3915 |
my $marc_struct = GetMarcStructure(1, $frameworkcode); |
3916 |
my $similarity_pref = C4::Context->preference("MARCPermissionsCorrectionSimilarity"); |
3917 |
$similarity_pref = defined $similarity_pref && |
3918 |
$similarity_pref >= 0 && |
3919 |
$similarity_pref <= 100 ? |
3920 |
$similarity_pref/100.0 : undef; |
3921 |
|
3922 |
# First came the leader ... |
3923 |
$ret_record->leader($old_record->leader()); |
3924 |
|
3925 |
# ... and then came all the tags in the framework |
3926 |
for my $tag (sort keys %{$marc_struct}) { |
3927 |
my @old_fields = $old_record->field($tag); |
3928 |
my @new_fields = $new_record->field($tag); |
3929 |
next unless @old_fields or @new_fields; |
3930 |
my @perms = ( |
3931 |
$perm->{'*'}, |
3932 |
( # regex tags |
3933 |
map { $perm->{$_} } |
3934 |
grep { $is_regex->( $tag, $_ ) } sort keys %{$perm} |
3935 |
), |
3936 |
$perm->{$tag} |
3937 |
); |
3938 |
|
3939 |
# There can be more than one field for each tag, so we have to |
3940 |
# iterate all fields and distinguish which are new, removed or |
3941 |
# modified |
3942 |
my $max_fields = ($#old_fields, $#new_fields) |
3943 |
[$#old_fields< $#new_fields]; |
3944 |
for ( my $i_field = 0; $i_field <= $max_fields; $i_field++ ) { |
3945 |
my $old_field = $old_fields[$i_field]; |
3946 |
my $new_field = $new_fields[$i_field]; |
3947 |
|
3948 |
# Existing field |
3949 |
if ( $old_field and $new_field ) { |
3950 |
# Control fields |
3951 |
if ( $old_field->is_control_field() ) { |
3952 |
# Existing control field |
3953 |
if ( $old_field and $new_field ) { |
3954 |
my $on_existing = GetMarcPermissionsAction('on_existing', $filter, @perms); |
3955 |
if ( defined $on_existing->{action} ) { |
3956 |
push @{$log}, |
3957 |
{ |
3958 |
rule => $on_existing->{rule}, |
3959 |
event => "existing", |
3960 |
action => $on_existing->{action}, |
3961 |
tag => $tag, |
3962 |
subfieldcode => undef, |
3963 |
filter => $filter |
3964 |
}; |
3965 |
if ( $on_existing->{action} eq 'skip' ) { |
3966 |
$ret_record->insert_fields_ordered($old_field); |
3967 |
next; |
3968 |
} elsif ( $on_existing->{action} eq 'overwrite' ) { |
3969 |
$ret_record->insert_fields_ordered($new_field); |
3970 |
next; |
3971 |
} elsif ( $on_existing->{action} eq 'add' ) { |
3972 |
$ret_record->insert_fields_ordered($old_field); |
3973 |
$ret_record->insert_fields_ordered($new_field); |
3974 |
next; |
3975 |
} elsif ( $on_existing->{action} eq 'add_or_correct' ) { |
3976 |
if ( $similarity_pref ) { |
3977 |
my $similarity = similarity $old_field->data(), $new_field->data(); |
3978 |
if ( $similarity >= $similarity_pref ) { |
3979 |
$ret_record->insert_fields_ordered($new_field); |
3980 |
} else { |
3981 |
$ret_record->insert_fields_ordered($old_field); |
3982 |
$ret_record->insert_fields_ordered($new_field); |
3983 |
} |
3984 |
} else { |
3985 |
$ret_record->insert_fields_ordered($old_field); |
3986 |
} |
3987 |
} |
3988 |
} else { # default to skip |
3989 |
$ret_record->insert_fields_ordered($old_field); |
3990 |
next; |
3991 |
} |
3992 |
# New control field |
3993 |
} elsif ( not $old_field and $new_field ) { |
3994 |
my $on_new = GetMarcPermissionsAction('on_new', $filter, @perms); |
3995 |
if ( defined $on_new->{action} ) { |
3996 |
push @{$log}, |
3997 |
{ |
3998 |
rule => $on_new->{rule}, |
3999 |
event => "new", |
4000 |
action => $on_new->{action}, |
4001 |
tag => $tag, |
4002 |
subfieldcode => undef, |
4003 |
filter => $filter |
4004 |
}; |
4005 |
if ( $on_new->{action} eq 'skip' ) { # Redundant |
4006 |
next; |
4007 |
} elsif ( $on_new->{action} eq 'add' ) { |
4008 |
$ret_record->insert_fields_ordered($new_field); |
4009 |
next; |
4010 |
} |
4011 |
} |
4012 |
# Removed control field |
4013 |
} elsif ( $old_field and not $new_field ) { |
4014 |
my $on_removed = GetMarcPermissionsAction('on_removed', $filter, @perms); |
4015 |
if ( defined $on_removed->{action} ) { |
4016 |
push @{$log}, |
4017 |
{ |
4018 |
rule => $on_removed->{rule}, |
4019 |
event => "removed", |
4020 |
action => $on_removed->{action}, |
4021 |
tag => $tag, |
4022 |
subfieldcode => undef |
4023 |
}; |
4024 |
if ( $on_removed->{action} eq 'skip' ) { |
4025 |
$ret_record->insert_fields_ordered($old_field); |
4026 |
next; |
4027 |
} elsif ( $on_removed->{action} eq 'remove' ) { |
4028 |
next; |
4029 |
} |
4030 |
} else { # default to skip |
4031 |
$ret_record->insert_fields_ordered($old_field); |
4032 |
next; |
4033 |
} |
4034 |
} |
4035 |
# Indicators and subfields |
4036 |
} else { |
4037 |
# Indicators |
4038 |
my @old_ind = map { $old_field->indicator($_) } (1,2); |
4039 |
my @new_ind = map { $new_field->indicator($_) } (1,2); |
4040 |
my @ind = ('',''); |
4041 |
|
4042 |
for my $i ((0,1)) { |
4043 |
my @ind_perms = ( |
4044 |
@perms, |
4045 |
$perm->{$tag}->{subfields}->{ 'i' . ( $i + 1 ) } |
4046 |
); |
4047 |
|
4048 |
# Existing indicator |
4049 |
if ( defined $old_ind[$i] and defined $new_ind[$i] ) { |
4050 |
my $on_existing = GetMarcPermissionsAction('on_existing', $filter, @ind_perms); |
4051 |
if ( defined $on_existing->{action} ) { |
4052 |
push @{$log}, |
4053 |
{ |
4054 |
rule => $on_existing->{rule}, |
4055 |
event => "existing", |
4056 |
action => $on_existing->{action}, |
4057 |
tag => $tag, |
4058 |
subfieldcode => 'i' . ( $i + 1 ), |
4059 |
filter => $filter |
4060 |
}; |
4061 |
if ( $on_existing->{action} eq 'skip' ) { |
4062 |
$ind[$i] = $old_ind[$i]; |
4063 |
} elsif ( $on_existing->{action} eq 'overwrite' ) { |
4064 |
$ind[$i] = $new_ind[$i]; |
4065 |
} |
4066 |
} else { # default to skip |
4067 |
$ind[$i] = $old_ind[$i]; |
4068 |
} |
4069 |
# New indicator |
4070 |
} elsif ( not defined $old_ind[$i] and defined $new_ind[$i] ) { |
4071 |
my $on_new = GetMarcPermissionsAction('on_new', $filter, @ind_perms); |
4072 |
if ( defined $on_new->{action} ) { |
4073 |
push @{$log}, |
4074 |
{ |
4075 |
rule => $on_new->{rule}, |
4076 |
event => "new", |
4077 |
action => $on_new->{action}, |
4078 |
tag => $tag, |
4079 |
subfieldcode => 'i' . ( $i + 1 ), |
4080 |
filter => $filter |
4081 |
}; |
4082 |
if ( $on_new->{action} eq 'skip' ) { |
4083 |
$ind[$i] = $old_ind[$i]; |
4084 |
} elsif ( $on_new->{action} eq 'add' ) { |
4085 |
$ind[$i] = $new_ind[$i]; |
4086 |
} |
4087 |
} |
4088 |
# Removed indicator |
4089 |
} elsif ( defined $old_ind[$i] and not defined $new_ind[$i] ) { |
4090 |
my $on_removed = GetMarcPermissionsAction('on_removed', $filter, @ind_perms); |
4091 |
if ( defined $on_removed->{action} ) { |
4092 |
push @{$log}, |
4093 |
{ |
4094 |
rule => $on_removed->{rule}, |
4095 |
event => "removed", |
4096 |
action => $on_removed->{action}, |
4097 |
tag => $tag, |
4098 |
subfieldcode => 'i' . ( $i + 1 ), |
4099 |
filter => $filter |
4100 |
}; |
4101 |
if ( $on_removed->{action} eq 'skip' ) { |
4102 |
$ind[$i] = $old_ind[$i]; |
4103 |
} elsif ( $on_removed->{action} eq 'remove' ) { |
4104 |
$ind[$i] = ''; |
4105 |
} |
4106 |
} else { # default to skip |
4107 |
$ind[$i] = $old_ind[$i]; |
4108 |
} |
4109 |
} |
4110 |
} |
4111 |
|
4112 |
# Subfields |
4113 |
my @subfields = (); |
4114 |
my %proccessed_subcodes = (); |
4115 |
|
4116 |
# Try to preserve subfield order by first processing |
4117 |
# subfields from the old record, and then subfields from |
4118 |
# the framework structure unless include in old record. |
4119 |
my @sorted_subfields = ( |
4120 |
map { shift @{$_} } $old_field->subfields(), |
4121 |
() = sort keys %{ $marc_struct->{$tag} } |
4122 |
); |
4123 |
for my $subcode ( @sorted_subfields ) { |
4124 |
next if $proccessed_subcodes{$subcode}; |
4125 |
$proccessed_subcodes{$subcode} = 1; |
4126 |
next unless ref $marc_struct->{$tag}{$subcode} eq 'HASH'; |
4127 |
my @old_subfields = $old_field->subfield($subcode); |
4128 |
my @new_subfields = $new_field->subfield($subcode); |
4129 |
next unless @old_subfields or @new_subfields; |
4130 |
|
4131 |
my @subfield_perms = ( |
4132 |
@perms, |
4133 |
$perm->{'*'}->{subfields}->{'*'}, |
4134 |
$perm->{$tag}->{subfields}->{'*'}, |
4135 |
( # tag->regex |
4136 |
map { $perm->{$tag}->{subfields}->{$_} } |
4137 |
grep { |
4138 |
# subcode is not exactly same as |
4139 |
# possible regex |
4140 |
$subcode ne $_ && |
4141 |
|
4142 |
# wildcard, not checking for valid |
4143 |
# subfieldcodeDataType since it |
4144 |
# contains too many regex characters |
4145 |
$_ !~ /^(\*)$/ && |
4146 |
|
4147 |
# so we try it as a regex |
4148 |
$subcode =~ /$_/ |
4149 |
} sort |
4150 |
keys %{ $perm->{$tag}->{subfields} } |
4151 |
), |
4152 |
( # regex->* |
4153 |
map { $perm->{$_}->{subfields}->{'*'} } |
4154 |
grep { $is_regex->( $tag, $_ ) } |
4155 |
sort keys %{$perm} |
4156 |
), |
4157 |
$perm->{'*'}->{subfields}->{$subcode}, |
4158 |
( # regex->subcode |
4159 |
map { $perm->{$_}->{subfields}->{$subcode} } |
4160 |
grep { $is_regex->( $tag, $_ ) } |
4161 |
sort keys %{$perm} |
4162 |
), |
4163 |
$perm->{$tag}->{subfields}->{$subcode} |
4164 |
); |
4165 |
|
4166 |
# Existing subfield |
4167 |
if ( @old_subfields and @new_subfields ) { |
4168 |
my $on_existing = GetMarcPermissionsAction('on_existing', $filter, @subfield_perms); |
4169 |
if ( defined $on_existing->{action} ) { |
4170 |
push @{$log}, |
4171 |
{ |
4172 |
rule => $on_existing->{rule}, |
4173 |
event => "existing", |
4174 |
action => $on_existing->{action}, |
4175 |
tag => $tag, |
4176 |
subfieldcode => $subcode, |
4177 |
filter => $filter |
4178 |
}; |
4179 |
if ( $on_existing->{action} eq 'skip' ) { |
4180 |
push(@subfields, map {($subcode,$_)} @old_subfields); |
4181 |
next; |
4182 |
} elsif ( $on_existing->{action} eq 'overwrite' ) { |
4183 |
push(@subfields, map {($subcode,$_)} @new_subfields); |
4184 |
next; |
4185 |
} elsif ( $on_existing->{action} eq 'add' ) { |
4186 |
push(@subfields, map {($subcode,$_)} @old_subfields); |
4187 |
push(@subfields, map {($subcode,$_)} @new_subfields); |
4188 |
next; |
4189 |
} elsif ( $on_existing->{action} eq 'add_or_correct' ) { |
4190 |
if ( $similarity_pref ) { |
4191 |
my @corrected_vals = (); |
4192 |
|
4193 |
# correct all old subfields |
4194 |
for ( my $i_new = 0; $i_new <= $#new_subfields; $i_new++ ) { |
4195 |
my $new_val = $new_subfields[$i_new]; |
4196 |
for ( my $i_old = 0; $i_old <= $#old_subfields; $i_old++ ) { |
4197 |
my $old_val = $old_subfields[$i_old]; |
4198 |
next if not defined $old_val or not defined $new_val; |
4199 |
my $similarity = similarity ($old_val, $new_val); |
4200 |
if ( $similarity >= $similarity_pref ) { |
4201 |
$corrected_vals[$i_old] = $new_val; |
4202 |
$new_subfields[$i_new] = undef; |
4203 |
$old_subfields[$i_old] = undef; |
4204 |
last; |
4205 |
} |
4206 |
} |
4207 |
} |
4208 |
|
4209 |
# insert all unchanged old subfields |
4210 |
map { |
4211 |
$corrected_vals[$_] = $old_subfields[$_] |
4212 |
if defined $old_subfields[$_] |
4213 |
} 0 .. $#old_subfields; |
4214 |
|
4215 |
|
4216 |
# append all remaning new subfields |
4217 |
map {push @corrected_vals, $_ if defined} @new_subfields; |
4218 |
|
4219 |
push(@subfields, map {($subcode,$_)} @corrected_vals); |
4220 |
} else { |
4221 |
push(@subfields, map {($subcode,$_)} @old_subfields); |
4222 |
} |
4223 |
} |
4224 |
} else { # default to skip |
4225 |
push(@subfields, map {($subcode,$_)} @old_subfields); |
4226 |
next; |
4227 |
} |
4228 |
# New subfield |
4229 |
} elsif ( not @old_subfields and @new_subfields ) { |
4230 |
my $on_new = GetMarcPermissionsAction('on_new', $filter, @subfield_perms); |
4231 |
if ( defined $on_new->{action} ) { |
4232 |
push @{$log}, |
4233 |
{ |
4234 |
rule => $on_new->{rule}, |
4235 |
event => "new", |
4236 |
action => $on_new->{action}, |
4237 |
tag => $tag, |
4238 |
subfieldcode => $subcode, |
4239 |
filter => $filter |
4240 |
}; |
4241 |
if ( $on_new->{action} eq 'skip' ) { |
4242 |
next; |
4243 |
} elsif ( $on_new->{action} eq 'add' ) { |
4244 |
push(@subfields, map {($subcode,$_)} @new_subfields); |
4245 |
next; |
4246 |
} |
4247 |
} |
4248 |
# Removed subfield |
4249 |
} elsif ( @old_subfields and not @new_subfields ) { |
4250 |
my $on_removed = GetMarcPermissionsAction('on_removed', $filter, @subfield_perms); |
4251 |
if ( defined $on_removed->{action} ) { |
4252 |
push @{$log}, |
4253 |
{ |
4254 |
rule => $on_removed->{rule}, |
4255 |
event => "removed", |
4256 |
action => $on_removed->{action}, |
4257 |
tag => $tag, |
4258 |
subfieldcode => $subcode, |
4259 |
filter => $filter |
4260 |
}; |
4261 |
if ( $on_removed->{action} eq 'skip' ) { |
4262 |
push(@subfields, ($subcode => @old_subfields)); |
4263 |
next; |
4264 |
} elsif ( $on_removed->{action} eq 'remove' ) { |
4265 |
next; |
4266 |
} |
4267 |
} else { # default to skip |
4268 |
push(@subfields, ($subcode => @old_subfields)); |
4269 |
next; |
4270 |
} |
4271 |
} |
4272 |
} # / for each subfield |
4273 |
$ret_record->insert_grouped_field(MARC::Field->new($tag, $ind[0], $ind[1], @subfields)); |
4274 |
} |
4275 |
# New field |
4276 |
} elsif ( not $old_field and $new_field ) { |
4277 |
my $on_new = GetMarcPermissionsAction('on_new', $filter, @perms); |
4278 |
|
4279 |
if ( defined $on_new->{action} ) { |
4280 |
push @{$log}, |
4281 |
{ |
4282 |
rule => $on_new->{rule}, |
4283 |
event => "new", |
4284 |
action => $on_new->{action}, |
4285 |
tag => $tag, |
4286 |
subfieldcode => undef, |
4287 |
filter => $filter |
4288 |
}; |
4289 |
if ( $on_new->{action} eq 'skip' ) { |
4290 |
next; |
4291 |
} elsif ( $on_new->{action} eq 'add' ) { |
4292 |
$ret_record->insert_fields_ordered($new_field); |
4293 |
next; |
4294 |
} |
4295 |
} |
4296 |
# Removed field |
4297 |
} elsif ( $old_field and not $new_field ) { |
4298 |
my $on_removed = GetMarcPermissionsAction('on_removed', $filter, @perms); |
4299 |
if ( defined $on_removed->{action} ) { |
4300 |
push @{$log}, |
4301 |
{ |
4302 |
rule => $on_removed->{rule}, |
4303 |
event => "removed", |
4304 |
action => $on_removed->{action}, |
4305 |
tag => $tag, |
4306 |
subfieldcode => undef, |
4307 |
filter => $filter |
4308 |
}; |
4309 |
if ( $on_removed->{action} eq 'skip' ) { |
4310 |
$ret_record->insert_grouped_field($old_field); |
4311 |
next; |
4312 |
} elsif ( $on_removed->{action} eq 'remove' ) { |
4313 |
next; |
4314 |
} |
4315 |
} else { # default to skip |
4316 |
$ret_record->insert_grouped_field($old_field); |
4317 |
next; |
4318 |
} |
4319 |
} |
4320 |
} # / for each field |
4321 |
} # / for each tag |
4322 |
if ( !$nolog && C4::Context->preference("MARCPermissionsLog") ) { |
4323 |
my $log_str = ''; |
4324 |
my $n = $#{$log}; |
4325 |
for my $l ( @{$log} ) { |
4326 |
$log_str .= "{\n"; |
4327 |
my $kn = keys %{$l}; |
4328 |
for my $k ( sort keys %{$l} ) { |
4329 |
if ( $k eq 'filter' ) { |
4330 |
$log_str .= " filter => {\n"; |
4331 |
my $fkn = keys %{ $l->{$k} }; |
4332 |
for my $fk ( sort keys %{ $l->{$k} } ) { |
4333 |
$log_str .= ' ' |
4334 |
. $fk . ' => ' |
4335 |
. $l->{$k}->{$fk} |
4336 |
. ( --$fkn ? ',' : '' ) . "\n"; |
4337 |
} |
4338 |
$log_str .= " }" . ( --$kn ? ',' : '' ) . "\n"; |
4339 |
} |
4340 |
else { |
4341 |
$log_str .= ' ' |
4342 |
. $k . ' => ' |
4343 |
. $l->{$k} |
4344 |
. ( --$kn ? ',' : '' ) . "\n" |
4345 |
if defined $l->{$k}; |
4346 |
} |
4347 |
} |
4348 |
$log_str .= '}' . ( $n-- ? ",\n" : '' ); |
4349 |
} |
4350 |
logaction( "CATALOGUING", "MODIFY", $biblionumber, $log_str ); |
4351 |
} |
4352 |
return $ret_record; |
4353 |
} |
4354 |
return $new_record; |
4355 |
} |
4356 |
|
4357 |
|
4358 |
=head2 GetMarcPermissions |
4359 |
|
4360 |
my $marc_permissions = GetMarcPermissions() |
4361 |
|
4362 |
Loads MARC field permissions from the marc_permissions table. |
4363 |
|
4364 |
Returns: |
4365 |
|
4366 |
=over 4 |
4367 |
|
4368 |
=item C<$marc_permissions> |
4369 |
|
4370 |
hashref with permissions structure for use with GetMarcPermissionsAction. |
4371 |
|
4372 |
=back |
4373 |
|
4374 |
=cut |
4375 |
|
4376 |
sub GetMarcPermissions { |
4377 |
my $dbh = C4::Context->dbh; |
4378 |
my $rule_count = 0; |
4379 |
my %perms = (); |
4380 |
|
4381 |
my $query = ' |
4382 |
SELECT `marc_permissions`.*, |
4383 |
`marc_permissions_modules`.`name`, |
4384 |
`marc_permissions_modules`.`description`, |
4385 |
`marc_permissions_modules`.`specificity` |
4386 |
FROM `marc_permissions` |
4387 |
LEFT JOIN `marc_permissions_modules` ON `module` = `marc_permissions_modules`.`id` |
4388 |
ORDER BY `marc_permissions_modules`.`specificity`, `id` |
4389 |
'; |
4390 |
my $sth = $dbh->prepare($query); |
4391 |
$sth->execute(); |
4392 |
while ( my $row = $sth->fetchrow_hashref ) { |
4393 |
$rule_count++; |
4394 |
if ( defined $row->{tagsubfield} and $row->{tagsubfield} ) { |
4395 |
$perms{ $row->{tagfield} }->{subfields}->{ $row->{tagsubfield} } |
4396 |
->{on_existing}->{ $row->{name} }->{ $row->{filter} } = |
4397 |
{ action => $row->{on_existing}, rule => $row->{'id'} }; |
4398 |
$perms{ $row->{tagfield} }->{subfields}->{ $row->{tagsubfield} } |
4399 |
->{on_new}->{ $row->{name} }->{ $row->{filter} } = |
4400 |
{ action => $row->{on_new}, rule => $row->{'id'} }; |
4401 |
$perms{ $row->{tagfield} }->{subfields}->{ $row->{tagsubfield} } |
4402 |
->{on_removed}->{ $row->{name} }->{ $row->{filter} } = |
4403 |
{ action => $row->{on_removed}, rule => $row->{'id'} }; |
4404 |
} |
4405 |
else { |
4406 |
$perms{ $row->{tagfield} }->{on_existing}->{ $row->{name} } |
4407 |
->{ $row->{filter} } = |
4408 |
{ action => $row->{on_existing}, rule => $row->{'id'} }; |
4409 |
$perms{ $row->{tagfield} }->{on_new}->{ $row->{name} } |
4410 |
->{ $row->{filter} } = |
4411 |
{ action => $row->{on_new}, rule => $row->{'id'} }; |
4412 |
$perms{ $row->{tagfield} }->{on_removed}->{ $row->{name} } |
4413 |
->{ $row->{filter} } = |
4414 |
{ action => $row->{on_removed}, rule => $row->{'id'} }; |
4415 |
} |
4416 |
} |
4417 |
|
4418 |
return unless $rule_count; |
4419 |
return \%perms; |
4420 |
} |
4421 |
|
4422 |
=head2 GetMarcPermissionsAction |
4423 |
|
4424 |
my $action = GetMarcPermissionsAction($event, $filter, @permissions) |
4425 |
|
4426 |
Gets action based on C<$event>, C<$filter> and C<@permissions>. |
4427 |
|
4428 |
=over 4 |
4429 |
|
4430 |
=item C<$event> |
4431 |
|
4432 |
which event: 'on_existing', 'on_new' or 'on_removed' |
4433 |
|
4434 |
=item C<$filter> |
4435 |
|
4436 |
hashref containing at least one filter module from the marc_permissions_modules |
4437 |
table in form {module => filter}. Three predefined filter modules exists: |
4438 |
|
4439 |
* source |
4440 |
* category |
4441 |
* borrower |
4442 |
|
4443 |
=item C<@permissions> |
4444 |
|
4445 |
list of permission structures in order of specificity from least to most |
4446 |
specific. |
4447 |
|
4448 |
=back |
4449 |
|
4450 |
Returns: |
4451 |
|
4452 |
=over 4 |
4453 |
|
4454 |
=item C<$action> |
4455 |
|
4456 |
hashref defining matching action. |
4457 |
|
4458 |
=back |
4459 |
|
4460 |
=cut |
4461 |
|
4462 |
sub GetMarcPermissionsAction { |
4463 |
my $what = shift or return; |
4464 |
my $filter = shift or return; |
4465 |
my @perms = @_; |
4466 |
my $modules = GetMarcPermissionsModules(); |
4467 |
my $action = undef; |
4468 |
|
4469 |
for my $perm (@perms) { |
4470 |
next if not defined $perm; |
4471 |
next if not defined $perm->{$what}; |
4472 |
my $tmp_action = undef; |
4473 |
for my $module ( @{$modules} ) { |
4474 |
my $action_candidate = defined $perm->{$what}->{ $module->{'name'} } ? $perm->{$what}->{ $module->{'name'} } : undef; |
4475 |
if ($action_candidate) { |
4476 |
if ( |
4477 |
defined $filter->{ $module->{'name'} } and |
4478 |
defined $action_candidate->{ $filter->{ $module->{'name'} } }->{action} |
4479 |
) { |
4480 |
$tmp_action = $perm->{$what}->{ $module->{'name'} }->{ $filter->{ $module->{'name'} } }; |
4481 |
last; |
4482 |
} |
4483 |
elsif (defined $action_candidate->{'*'}->{action} and !$tmp_action) { |
4484 |
$tmp_action = $perm->{$what}->{ $module->{'name'} }->{'*'}; |
4485 |
} |
4486 |
} |
4487 |
} |
4488 |
$action = $tmp_action if defined $tmp_action; |
4489 |
} |
4490 |
return $action; |
4491 |
} |
4492 |
|
4493 |
=head2 GetMarcPermissionsRules |
4494 |
|
4495 |
my $rules = GetMarcPermissionsRules() |
4496 |
|
4497 |
Returns: |
4498 |
|
4499 |
=over 4 |
4500 |
|
4501 |
=item C<$rules> |
4502 |
|
4503 |
array (in list context, arrayref otherwise) of hashrefs from marc_permissions |
4504 |
table in order of module specificity and rule id. |
4505 |
|
4506 |
=back |
4507 |
|
4508 |
=cut |
4509 |
|
4510 |
sub GetMarcPermissionsRules { |
4511 |
my $dbh = C4::Context->dbh; |
4512 |
my @rules = (); |
4513 |
|
4514 |
my $query = ' |
4515 |
SELECT `marc_permissions`.`id`, |
4516 |
`marc_permissions`.`tagfield`, |
4517 |
`marc_permissions`.`tagsubfield`, |
4518 |
`marc_permissions`.`filter`, |
4519 |
`marc_permissions`.`on_existing`, |
4520 |
`marc_permissions`.`on_new`, |
4521 |
`marc_permissions`.`on_removed`, |
4522 |
`marc_permissions_modules`.`name` as `module`, |
4523 |
`marc_permissions_modules`.`description`, |
4524 |
`marc_permissions_modules`.`specificity` |
4525 |
FROM `marc_permissions` |
4526 |
LEFT JOIN `marc_permissions_modules` ON `module` = `marc_permissions_modules`.`id` |
4527 |
ORDER BY `marc_permissions_modules`.`specificity`, `id` |
4528 |
'; |
4529 |
my $sth = $dbh->prepare($query); |
4530 |
$sth->execute(); |
4531 |
while ( my $row = $sth->fetchrow_hashref ) { |
4532 |
push(@rules, $row); |
4533 |
} |
4534 |
|
4535 |
return wantarray ? @rules : \@rules; |
4536 |
} |
4537 |
|
4538 |
=head2 GetMarcPermissionsModules |
4539 |
|
4540 |
my $modules = GetMarcPermissionsModules() |
4541 |
|
4542 |
Returns: |
4543 |
|
4544 |
=over 4 |
4545 |
|
4546 |
=item C<$modules> |
4547 |
|
4548 |
array (in list context, arrayref otherwise) of hashrefs from |
4549 |
marc_permissions_modules table in order of specificity. |
4550 |
|
4551 |
=back |
4552 |
|
4553 |
=cut |
4554 |
|
4555 |
sub GetMarcPermissionsModules { |
4556 |
my $dbh = C4::Context->dbh; |
4557 |
my @modules = (); |
4558 |
|
4559 |
my $query = ' |
4560 |
SELECT * |
4561 |
FROM `marc_permissions_modules` |
4562 |
ORDER BY `specificity` DESC |
4563 |
'; |
4564 |
my $sth = $dbh->prepare($query); |
4565 |
$sth->execute(); |
4566 |
while ( my $row = $sth->fetchrow_hashref ) { |
4567 |
push(@modules, $row); |
4568 |
} |
4569 |
|
4570 |
return wantarray ? @modules : \@modules; |
4571 |
} |
4572 |
|
4573 |
=head2 ModMarcPermissionsRule |
4574 |
|
4575 |
my $success = ModMarcPermissionsRule($id, $fields) |
4576 |
|
4577 |
Modifies rule in the marc_permissions table. |
4578 |
|
4579 |
=over 4 |
4580 |
|
4581 |
=item C<$id> |
4582 |
|
4583 |
rule id to modify |
4584 |
|
4585 |
=item C<$fields> |
4586 |
|
4587 |
hashref defining the table fields |
4588 |
|
4589 |
* tagfield - required |
4590 |
* tagsubfield |
4591 |
* module - required |
4592 |
* filter - required |
4593 |
* on_existing - required |
4594 |
* on_new - required |
4595 |
* on_removed - required |
4596 |
|
4597 |
=back |
4598 |
|
4599 |
Returns: |
4600 |
|
4601 |
=over 4 |
4602 |
|
4603 |
=item C<$success> |
4604 |
|
4605 |
undef if an error occurs, otherwise true. |
4606 |
|
4607 |
=back |
4608 |
|
4609 |
=cut |
4610 |
|
4611 |
sub ModMarcPermissionsRule { |
4612 |
my ($id, $f) = @_; |
4613 |
my $dbh = C4::Context->dbh; |
4614 |
my $query = ' |
4615 |
UPDATE `marc_permissions` |
4616 |
SET |
4617 |
tagfield = ?, |
4618 |
tagsubfield = ?, |
4619 |
module = ?, |
4620 |
filter = ?, |
4621 |
on_existing = ?, |
4622 |
on_new = ?, |
4623 |
on_removed = ? |
4624 |
WHERE |
4625 |
id = ? |
4626 |
'; |
4627 |
my $sth = $dbh->prepare($query); |
4628 |
return $sth->execute ( |
4629 |
$f->{tagfield}, |
4630 |
$f->{tagsubfield}, |
4631 |
$f->{module}, |
4632 |
$f->{filter}, |
4633 |
$f->{on_existing}, |
4634 |
$f->{on_new}, |
4635 |
$f->{on_removed}, |
4636 |
$id |
4637 |
); |
4638 |
} |
4639 |
|
4640 |
=head2 AddMarcPermissionsRule |
4641 |
|
4642 |
my $success = AddMarcPermissionsRule($fields) |
4643 |
|
4644 |
Add rule to the marc_permissions table. |
4645 |
|
4646 |
=over 4 |
4647 |
|
4648 |
=item C<$fields> |
4649 |
|
4650 |
hashref defining the table fields |
4651 |
|
4652 |
tagfield - required |
4653 |
tagsubfield |
4654 |
module - required |
4655 |
filter - required |
4656 |
on_existing - required |
4657 |
on_new - required |
4658 |
on_removed - required |
4659 |
|
4660 |
=back |
4661 |
|
4662 |
Returns: |
4663 |
|
4664 |
=over 4 |
4665 |
|
4666 |
=item C<$success> |
4667 |
|
4668 |
undef if an error occurs, otherwise true. |
4669 |
|
4670 |
=back |
4671 |
|
4672 |
=cut |
4673 |
|
4674 |
sub AddMarcPermissionsRule { |
4675 |
my $f = shift; |
4676 |
my $dbh = C4::Context->dbh; |
4677 |
my $query = ' |
4678 |
INSERT INTO `marc_permissions` |
4679 |
( |
4680 |
tagfield, |
4681 |
tagsubfield, |
4682 |
module, |
4683 |
filter, |
4684 |
on_existing, |
4685 |
on_new, |
4686 |
on_removed |
4687 |
) |
4688 |
VALUES (?, ?, ?, ?, ?, ?, ?) |
4689 |
'; |
4690 |
my $sth = $dbh->prepare($query); |
4691 |
return $sth->execute ( |
4692 |
$f->{tagfield}, |
4693 |
$f->{tagsubfield}, |
4694 |
$f->{module}, |
4695 |
$f->{filter}, |
4696 |
$f->{on_existing}, |
4697 |
$f->{on_new}, |
4698 |
$f->{on_removed} |
4699 |
); |
4700 |
} |
4701 |
|
4702 |
=head2 DelMarcPermissionsRule |
4703 |
|
4704 |
my $success = DelMarcPermissionsRule($id) |
4705 |
|
4706 |
Deletes rule from the marc_permissions table. |
4707 |
|
4708 |
=over 4 |
4709 |
|
4710 |
=item C<$id> |
4711 |
|
4712 |
rule id to delete |
4713 |
|
4714 |
=back |
4715 |
|
4716 |
Returns: |
4717 |
|
4718 |
=over 4 |
4719 |
|
4720 |
=item C<$success> |
4721 |
|
4722 |
undef if an error occurs, otherwise true. |
4723 |
|
4724 |
=back |
4725 |
|
4726 |
=cut |
4727 |
|
4728 |
sub DelMarcPermissionsRule { |
4729 |
my $id = shift; |
4730 |
my $dbh = C4::Context->dbh; |
4731 |
my $query = ' |
4732 |
DELETE FROM `marc_permissions` |
4733 |
WHERE |
4734 |
id = ? |
4735 |
'; |
4736 |
my $sth = $dbh->prepare($query); |
4737 |
return $sth->execute($id); |
4738 |
} |
3789 |
1; |
4739 |
1; |
3790 |
|
4740 |
|
3791 |
|
4741 |
|