Lines 102-107
BEGIN {
Link Here
|
102 |
|
102 |
|
103 |
&CountItemsIssued |
103 |
&CountItemsIssued |
104 |
&CountBiblioInOrders |
104 |
&CountBiblioInOrders |
|
|
105 |
|
106 |
&GetMarcPermissionsRules |
107 |
&GetMarcPermissionsModules |
108 |
&ModMarcPermissionsRule |
109 |
&AddMarcPermissionsRule |
110 |
&DelMarcPermissionsRule |
105 |
); |
111 |
); |
106 |
|
112 |
|
107 |
# To modify something |
113 |
# To modify something |
Lines 129-134
BEGIN {
Link Here
|
129 |
# they are useful in a few circumstances, so they are exported, |
135 |
# they are useful in a few circumstances, so they are exported, |
130 |
# but don't use them unless you are a core developer ;-) |
136 |
# but don't use them unless you are a core developer ;-) |
131 |
push @EXPORT, qw( |
137 |
push @EXPORT, qw( |
|
|
138 |
&ApplyMarcPermissions |
132 |
&ModBiblioMarc |
139 |
&ModBiblioMarc |
133 |
); |
140 |
); |
134 |
|
141 |
|
Lines 266-272
sub AddBiblio {
Link Here
|
266 |
|
273 |
|
267 |
=head2 ModBiblio |
274 |
=head2 ModBiblio |
268 |
|
275 |
|
269 |
ModBiblio( $record,$biblionumber,$frameworkcode); |
276 |
ModBiblio($record, $biblionumber, $frameworkcode, $options); |
270 |
|
277 |
|
271 |
Replace an existing bib record identified by C<$biblionumber> |
278 |
Replace an existing bib record identified by C<$biblionumber> |
272 |
with one supplied by the MARC::Record object C<$record>. The embedded |
279 |
with one supplied by the MARC::Record object C<$record>. The embedded |
Lines 287-293
Returns 1 on success 0 on failure
Link Here
|
287 |
=cut |
294 |
=cut |
288 |
|
295 |
|
289 |
sub ModBiblio { |
296 |
sub ModBiblio { |
290 |
my ( $record, $biblionumber, $frameworkcode ) = @_; |
297 |
my ( $record, $biblionumber, $frameworkcode, $options ) = @_; |
|
|
298 |
$options //= {}; |
299 |
|
291 |
if (!$record) { |
300 |
if (!$record) { |
292 |
carp 'No record passed to ModBiblio'; |
301 |
carp 'No record passed to ModBiblio'; |
293 |
return 0; |
302 |
return 0; |
Lines 315-320
sub ModBiblio {
Link Here
|
315 |
|
324 |
|
316 |
_strip_item_fields($record, $frameworkcode); |
325 |
_strip_item_fields($record, $frameworkcode); |
317 |
|
326 |
|
|
|
327 |
# apply permissions |
328 |
if (C4::Context->preference('MARCPermissions') && $biblionumber && defined $options && exists $options->{'context'}) { |
329 |
$record = ApplyMarcPermissions({ |
330 |
biblionumber => $biblionumber, |
331 |
record => $record, |
332 |
filter => $options->{'context'}, |
333 |
} |
334 |
); |
335 |
} |
336 |
|
318 |
# update biblionumber and biblioitemnumber in MARC |
337 |
# update biblionumber and biblioitemnumber in MARC |
319 |
# FIXME - this is assuming a 1 to 1 relationship between |
338 |
# FIXME - this is assuming a 1 to 1 relationship between |
320 |
# biblios and biblioitems |
339 |
# biblios and biblioitems |
Lines 325-337
sub ModBiblio {
Link Here
|
325 |
_koha_marc_update_bib_ids( $record, $frameworkcode, $biblionumber, $biblioitemnumber ); |
344 |
_koha_marc_update_bib_ids( $record, $frameworkcode, $biblionumber, $biblioitemnumber ); |
326 |
|
345 |
|
327 |
# load the koha-table data object |
346 |
# load the koha-table data object |
328 |
my $oldbiblio = TransformMarcToKoha( $record, $frameworkcode ); |
347 |
my $oldbiblio = TransformMarcToKoha( $record, $frameworkcode, undef, $biblionumber); |
329 |
|
348 |
|
330 |
# update MARC subfield that stores biblioitems.cn_sort |
349 |
# update MARC subfield that stores biblioitems.cn_sort |
331 |
_koha_marc_update_biblioitem_cn_sort( $record, $oldbiblio, $frameworkcode ); |
350 |
_koha_marc_update_biblioitem_cn_sort( $record, $oldbiblio, $frameworkcode ); |
332 |
|
351 |
|
333 |
# update the MARC record (that now contains biblio and items) with the new record data |
352 |
# update the MARC record (that now contains biblio and items) with the new record data |
334 |
&ModBiblioMarc( $record, $biblionumber, $frameworkcode ); |
353 |
ModBiblioMarc( $record, $biblionumber, $frameworkcode ); |
335 |
|
354 |
|
336 |
# modify the other koha tables |
355 |
# modify the other koha tables |
337 |
_koha_modify_biblio( $dbh, $oldbiblio, $frameworkcode ); |
356 |
_koha_modify_biblio( $dbh, $oldbiblio, $frameworkcode ); |
Lines 2573-2579
sub TransformHtmlToMarc {
Link Here
|
2573 |
|
2592 |
|
2574 |
=head2 TransformMarcToKoha |
2593 |
=head2 TransformMarcToKoha |
2575 |
|
2594 |
|
2576 |
$result = TransformMarcToKoha( $record, undef, $limit ) |
2595 |
$result = TransformMarcToKoha($record, undef, $biblionumber, $option) |
2577 |
|
2596 |
|
2578 |
Extract data from a MARC bib record into a hashref representing |
2597 |
Extract data from a MARC bib record into a hashref representing |
2579 |
Koha biblio, biblioitems, and items fields. |
2598 |
Koha biblio, biblioitems, and items fields. |
Lines 2584-2590
hash_ref.
Link Here
|
2584 |
=cut |
2603 |
=cut |
2585 |
|
2604 |
|
2586 |
sub TransformMarcToKoha { |
2605 |
sub TransformMarcToKoha { |
2587 |
my ( $record, $frameworkcode, $limit_table ) = @_; |
2606 |
my ( $record, $frameworkcode, $limit_table, $biblionumber ) = @_; |
2588 |
# FIXME Parameter $frameworkcode is obsolete and will be removed |
2607 |
# FIXME Parameter $frameworkcode is obsolete and will be removed |
2589 |
$limit_table //= q{}; |
2608 |
$limit_table //= q{}; |
2590 |
|
2609 |
|
Lines 3271-3277
sub _koha_delete_biblio_metadata {
Link Here
|
3271 |
|
3290 |
|
3272 |
=head2 ModBiblioMarc |
3291 |
=head2 ModBiblioMarc |
3273 |
|
3292 |
|
3274 |
&ModBiblioMarc($newrec,$biblionumber,$frameworkcode); |
3293 |
&ModBiblioMarc($newrec, $biblionumber, $frameworkcode, $options); |
3275 |
|
3294 |
|
3276 |
Add MARC XML data for a biblio to koha |
3295 |
Add MARC XML data for a biblio to koha |
3277 |
|
3296 |
|
Lines 3283-3288
sub ModBiblioMarc {
Link Here
|
3283 |
# pass the MARC::Record to this function, and it will create the records in |
3302 |
# pass the MARC::Record to this function, and it will create the records in |
3284 |
# the marcxml field |
3303 |
# the marcxml field |
3285 |
my ( $record, $biblionumber, $frameworkcode ) = @_; |
3304 |
my ( $record, $biblionumber, $frameworkcode ) = @_; |
|
|
3305 |
|
3286 |
if ( !$record ) { |
3306 |
if ( !$record ) { |
3287 |
carp 'ModBiblioMarc passed an undefined record'; |
3307 |
carp 'ModBiblioMarc passed an undefined record'; |
3288 |
return; |
3308 |
return; |
Lines 3295-3300
sub ModBiblioMarc {
Link Here
|
3295 |
if ( !$frameworkcode ) { |
3315 |
if ( !$frameworkcode ) { |
3296 |
$frameworkcode = ""; |
3316 |
$frameworkcode = ""; |
3297 |
} |
3317 |
} |
|
|
3318 |
|
3298 |
my $sth = $dbh->prepare("UPDATE biblio SET frameworkcode=? WHERE biblionumber=?"); |
3319 |
my $sth = $dbh->prepare("UPDATE biblio SET frameworkcode=? WHERE biblionumber=?"); |
3299 |
$sth->execute( $frameworkcode, $biblionumber ); |
3320 |
$sth->execute( $frameworkcode, $biblionumber ); |
3300 |
$sth->finish; |
3321 |
$sth->finish; |
Lines 3596-3603
sub RemoveAllNsb {
Link Here
|
3596 |
return $record; |
3617 |
return $record; |
3597 |
} |
3618 |
} |
3598 |
|
3619 |
|
3599 |
1; |
3620 |
=head2 ApplyMarcPermissions |
|
|
3621 |
|
3622 |
my $record = ApplyMarcPermissions($arguments) |
3623 |
|
3624 |
Applies marc permission rules to a record. |
3625 |
|
3626 |
C<$arguments> is expected to be a hashref with below keys defined. |
3627 |
|
3628 |
=over 4 |
3629 |
|
3630 |
=item C<biblionumber> |
3631 |
biblionumber of old record |
3632 |
|
3633 |
=item C<record> |
3634 |
record that will modify old record |
3635 |
|
3636 |
=item C<frameworkcode> |
3637 |
only tags included in framework will be processed |
3638 |
|
3639 |
=item C<filter> |
3640 |
hashref containing at least one filter module from the marc_permissions_modules |
3641 |
table in form {module => filter}. Three predefined filter modules exists: |
3642 |
|
3643 |
* source |
3644 |
* category |
3645 |
* borrower |
3646 |
|
3647 |
=item C<log> |
3648 |
optional reference to array that will be filled with rule evaluation log |
3649 |
entries. |
3650 |
|
3651 |
=item C<nolog> |
3652 |
optional boolean which when true disables logging to action log. |
3653 |
|
3654 |
=back |
3655 |
|
3656 |
Returns: |
3657 |
|
3658 |
=over 4 |
3659 |
|
3660 |
=item C<$record> |
3661 |
|
3662 |
new MARC record based on C<record> with C<filter> applied. If no old |
3663 |
record for C<biblionumber> can be found, C<record> is returned unchanged. |
3664 |
Default action when no matching filter found is to leave old record unchanged. |
3665 |
|
3666 |
=back |
3667 |
|
3668 |
=cut |
3669 |
|
3670 |
sub ApplyMarcPermissions { |
3671 |
my ($arguments) = @_; |
3672 |
my $biblionumber = $arguments->{biblionumber}; |
3673 |
my $incoming_record = $arguments->{record}; |
3674 |
|
3675 |
if ( !$biblionumber ) { |
3676 |
carp 'ApplyMarcPermissions called on undefined biblionumber'; |
3677 |
return; |
3678 |
} |
3679 |
if ( !$incoming_record ) { |
3680 |
carp 'ApplyMarcPermissions called on undefined record'; |
3681 |
return; |
3682 |
} |
3683 |
my $old_record = GetMarcBiblio({ biblionumber => $biblionumber }); |
3684 |
|
3685 |
my $merge_rules = undef; |
3686 |
if ($old_record && $arguments->{filter} && ($merge_rules = GetMarcPermissions($arguments->{filter}))) { |
3687 |
return MergeRecords($old_record, $incoming_record, $merge_rules); |
3688 |
} |
3689 |
return $incoming_record; |
3690 |
} |
3691 |
|
3692 |
sub MergeRecords { |
3693 |
my ($old_record, $incoming_record, $merge_rules) = @_; |
3694 |
my $is_matching_regex = sub { |
3695 |
my ( $tag, $m ) = @_; |
3696 |
|
3697 |
# tag is not exactly same as possible regex |
3698 |
$tag ne $m && |
3699 |
|
3700 |
# wildcard |
3701 |
$m ne '*' && |
3702 |
|
3703 |
# valid tagDataType |
3704 |
$m !~ /^(0[1-9A-z][\dA-Z]) | |
3705 |
([1-9A-z][\dA-z]{2})$/x && |
3706 |
|
3707 |
# nor valid controltagDataType |
3708 |
$m !~ /00[1-9A-Za-z]{1}/ && |
3709 |
|
3710 |
# so we try it as a regex |
3711 |
$tag =~ /^$m$/ |
3712 |
}; |
3713 |
|
3714 |
my $fields_by_tag = sub { |
3715 |
my ($record) = @_; |
3716 |
my $fields = {}; |
3717 |
foreach my $field ($record->fields()) { |
3718 |
$fields->{$field->tag()} //= []; |
3719 |
push @{$fields->{$field->tag()}}, $field; |
3720 |
} |
3721 |
return $fields; |
3722 |
}; |
3723 |
|
3724 |
my $hash_field_data = sub { |
3725 |
my ($field) = @_; |
3726 |
my $indicators = join("\x1E", map { $field->indicator($_) } (1, 2)); |
3727 |
return $indicators . "\x1E" . join("\x1E", sort map { join "\x1E", @{$_} } $field->subfields()); |
3728 |
}; |
3729 |
|
3730 |
my $diff_by_key = sub { |
3731 |
my ($a, $b) = @_; |
3732 |
my @removed; |
3733 |
my @intersecting; |
3734 |
my @added; |
3735 |
my %keys_index = map { $_ => undef } (keys %{$a}, keys %{$b}); |
3736 |
foreach my $key (keys %keys_index) { |
3737 |
if ($a->{$key} && $b->{$key}) { |
3738 |
push @intersecting, $a->{$key}; |
3739 |
} |
3740 |
elsif ($a->{$key}) { |
3741 |
push @removed, $a->{$key}; |
3742 |
} |
3743 |
else { |
3744 |
push @added, $b->{$key}; |
3745 |
} |
3746 |
} |
3747 |
return (\@removed, \@intersecting, \@added); |
3748 |
}; |
3749 |
|
3750 |
my $get_matching_field_rule = sub { |
3751 |
my ($tag) = @_; |
3752 |
my $matched_rule = undef; |
3753 |
# Exact match takes precedence |
3754 |
if (exists $merge_rules->{$tag}) { |
3755 |
$matched_rule = $merge_rules->{$tag}; |
3756 |
} |
3757 |
else { |
3758 |
# TODO: sorty by module/weight rule id or something, grab first matching via listutils thingy |
3759 |
my @matching_rules = map { $merge_rules->{$_} } grep { $is_matching_regex->($tag, $_) } sort keys %{$merge_rules}; |
3760 |
# TODO: fix |
3761 |
if (@matching_rules) { |
3762 |
$matched_rule = pop @matching_rules; |
3763 |
} |
3764 |
elsif($merge_rules->{'*'}) { |
3765 |
$matched_rule = $merge_rules->{'*'}; |
3766 |
} |
3767 |
} |
3768 |
return $matched_rule; |
3769 |
}; |
3770 |
|
3771 |
my $merged_record = MARC::Record->new(); |
3772 |
my @merged_record_fields; |
3773 |
|
3774 |
# Leader is always overwritten, or kept??? |
3775 |
$merged_record->leader($incoming_record->leader()); |
3776 |
|
3777 |
my $current_fields = $fields_by_tag->($old_record); |
3778 |
my $incoming_fields = $fields_by_tag->($incoming_record); |
3779 |
|
3780 |
# First we get all new incoming control fields |
3781 |
my @new_field_tags = grep { !(exists $current_fields->{$_}) } keys %{$incoming_fields}; |
3782 |
|
3783 |
foreach my $tag (@new_field_tags) { |
3784 |
my $rule = $get_matching_field_rule->($tag) // { |
3785 |
on_new => {'action' => 'skip', 'rule' => 0} |
3786 |
}; |
3787 |
if ( |
3788 |
$rule->{on_new}->{action} eq 'add' || |
3789 |
$rule->{on_new}->{action} eq 'overwrite' # ??? |
3790 |
) { # Or could just be write/protect? |
3791 |
# Hmm, only one control field possible?? |
3792 |
push @merged_record_fields, @{$incoming_fields->{$tag}}; |
3793 |
} |
3794 |
} |
3795 |
|
3796 |
# Then we get all control fields no longer present in incoming fields |
3797 |
# (removed) |
3798 |
my @deleted_field_tags = grep { !(exists $incoming_fields->{$_}) } keys %{$current_fields}; |
3799 |
foreach my $tag (@deleted_field_tags) { |
3800 |
my $rule = $get_matching_field_rule->($tag) // { |
3801 |
on_deleted => {'action' => 'skip', 'rule' => 0} |
3802 |
}; |
3803 |
if ($rule->{on_deleted}->{action} eq 'skip') { |
3804 |
push @merged_record_fields, @{$current_fields->{$tag}}; |
3805 |
} |
3806 |
} |
3807 |
|
3808 |
# Then we get the intersection of control fields, present both in |
3809 |
# current and incoming record (possibly to be overwritten) |
3810 |
my @common_field_tags = grep { exists $incoming_fields->{$_} } keys %{$current_fields}; |
3811 |
foreach my $tag (@common_field_tags) { |
3812 |
# Is control field |
3813 |
my $rule = $get_matching_field_rule->($tag) // { |
3814 |
on_removed => {'action' => 'skip', 'rule' => 0}, |
3815 |
on_appended => {'action' => 'skip', 'rule' => 0} |
3816 |
}; |
3817 |
if ($tag < 10) { |
3818 |
# on_existing = on_match |
3819 |
if ($rule->{on_appended}->{action} eq 'skip') { # TODO: replace with "protect", "keep" |
3820 |
push @merged_record_fields, @{$current_fields->{$tag}}; |
3821 |
} |
3822 |
elsif ($rule->{on_appended}->{action} eq 'append') { |
3823 |
push @merged_record_fields, @{$incoming_fields->{$tag}}; |
3824 |
} |
3825 |
if ( |
3826 |
$rule->{on_appended}->{action} eq 'append' && |
3827 |
$rule->{on_removed}->{action} eq 'skip' |
3828 |
) { |
3829 |
#TODO: This is an invalid combination for control fields, warn!! |
3830 |
# Or should perform client/server side validation to prevent this choice |
3831 |
} |
3832 |
} |
3833 |
else { |
3834 |
# Compute intersection and diff using field data |
3835 |
my %current_fields_by_data = map { $hash_field_data->($_) => $_ } @{$current_fields->{$tag}}; |
3836 |
my %incoming_fields_by_data = map { $hash_field_data->($_) => $_ } @{$incoming_fields->{$tag}}; |
3837 |
my ($current_fields_only, $common_fields, $incoming_fields_only) = $diff_by_key->(\%current_fields_by_data, \%incoming_fields_by_data); |
3838 |
|
3839 |
# First add common fields (intersection) |
3840 |
# Unchanged |
3841 |
if (@{$common_fields}) { |
3842 |
push @merged_record_fields, @{$common_fields}; |
3843 |
} |
3844 |
# Removed |
3845 |
if (@{$current_fields_only}) { |
3846 |
if ($rule->{on_removed}->{action} eq 'skip') { |
3847 |
push @merged_record_fields, @{$current_fields_only}; |
3848 |
} |
3849 |
} |
3850 |
# Appended |
3851 |
if (@{$incoming_fields_only}) { |
3852 |
if ($rule->{on_appended}->{action} eq 'append') { |
3853 |
push @merged_record_fields, @{$incoming_fields_only}; |
3854 |
} |
3855 |
} |
3856 |
} |
3857 |
} |
3858 |
if ($#merged_record_fields != 0) { |
3859 |
$merged_record->insert_fields_ordered(@merged_record_fields); |
3860 |
} |
3861 |
return $merged_record; |
3862 |
} |
3863 |
|
3864 |
=head2 GetMarcPermissions |
3865 |
|
3866 |
my $marc_permissions = GetMarcPermissions() |
3867 |
|
3868 |
Loads MARC field permissions from the marc_permissions table. |
3869 |
|
3870 |
Returns: |
3871 |
|
3872 |
=over 4 |
3873 |
|
3874 |
=item C<$marc_permissions> |
3875 |
|
3876 |
hashref with permissions structure for use with GetMarcPermissionsAction. |
3877 |
|
3878 |
=back |
3879 |
|
3880 |
=cut |
3881 |
|
3882 |
sub GetMarcPermissions { |
3883 |
my ($filter) = @_; |
3884 |
my $dbh = C4::Context->dbh; |
3885 |
my $rule_count = 0; |
3886 |
my $modules = GetMarcPermissionsModules(); |
3887 |
# We only care about modules included in the context/filter |
3888 |
# TODO: Perhaps make sure source => '*' is default? |
3889 |
my @filter_modules = grep { exists $filter->{$_->{name}} } @{$modules}; |
3890 |
|
3891 |
my $cache = Koha::Caches->get_instance(); |
3892 |
my $permissions = $cache->get_from_cache('marc_permissions', { unsafe => 1 }); |
3893 |
|
3894 |
if (!$permissions) { |
3895 |
my $query = ' |
3896 |
SELECT `marc_permissions`.*, |
3897 |
`marc_permissions_modules`.`name`, |
3898 |
`marc_permissions_modules`.`description`, |
3899 |
`marc_permissions_modules`.`specificity` |
3900 |
FROM `marc_permissions` |
3901 |
LEFT JOIN `marc_permissions_modules` ON `module` = `marc_permissions_modules`.`id` |
3902 |
ORDER BY `marc_permissions_modules`.`specificity`, `id` |
3903 |
'; |
3904 |
my $sth = $dbh->prepare($query); |
3905 |
$sth->execute(); |
3906 |
while (my $perm = $sth->fetchrow_hashref) { |
3907 |
my $target = ($permissions->{$perm->{name}}->{$perm->{filter}}->{$perm->{tagfield}} //= {}); |
3908 |
foreach my $event (GetMarcPermissionEvents()) { |
3909 |
$target->{$event} = { action => $perm->{$event}, rule => $perm->{'id'} }; |
3910 |
} |
3911 |
} |
3912 |
$cache->set_in_cache('marc_permissions', $permissions); |
3913 |
} |
3914 |
|
3915 |
my $filtered_permissions = undef; |
3916 |
foreach my $module (@filter_modules) { |
3917 |
if ( |
3918 |
exists $permissions->{$module->{name}} && |
3919 |
exists $permissions->{$module->{name}}->{$filter->{$module->{name}}} |
3920 |
) { |
3921 |
# TODO: Support multiple overlapping filters/context?? |
3922 |
$filtered_permissions = $permissions->{$module->{name}}->{$filter->{$module->{name}}}; |
3923 |
last; |
3924 |
} |
3925 |
} |
3926 |
if (!$filtered_permissions) { |
3927 |
# No perms matching specific context conditions found, try wildcard value for each active context |
3928 |
foreach my $module (@filter_modules) { |
3929 |
if (exists $permissions->{$module->{name}}->{'*'}) { |
3930 |
$filtered_permissions = $permissions->{$module->{name}}->{'*'}; |
3931 |
last; |
3932 |
} |
3933 |
} |
3934 |
} |
3935 |
return $filtered_permissions; |
3936 |
} |
3937 |
|
3938 |
# TODO: Use this in GetMarcPermissions |
3939 |
=head2 GetMarcPermissionsRules |
3940 |
|
3941 |
my $rules = GetMarcPermissionsRules() |
3942 |
|
3943 |
Returns: |
3944 |
|
3945 |
=over 4 |
3946 |
|
3947 |
=item C<$rules> |
3948 |
|
3949 |
array (in list context, arrayref otherwise) of hashrefs from marc_permissions |
3950 |
table in order of module specificity and rule id. |
3951 |
|
3952 |
=back |
3953 |
|
3954 |
=cut |
3955 |
|
3956 |
sub GetMarcPermissionsRules { |
3957 |
my $dbh = C4::Context->dbh; |
3958 |
my @rules = (); |
3959 |
|
3960 |
my $query = ' |
3961 |
SELECT `marc_permissions`.`id`, |
3962 |
`marc_permissions`.`tagfield`, |
3963 |
`marc_permissions`.`filter`, |
3964 |
`marc_permissions`.`on_new`, |
3965 |
`marc_permissions`.`on_appended`, |
3966 |
`marc_permissions`.`on_removed`, |
3967 |
`marc_permissions`.`on_deleted`, |
3968 |
`marc_permissions_modules`.`name` as `module`, |
3969 |
`marc_permissions_modules`.`description`, |
3970 |
`marc_permissions_modules`.`specificity` |
3971 |
FROM `marc_permissions` |
3972 |
LEFT JOIN `marc_permissions_modules` ON `module` = `marc_permissions_modules`.`id` |
3973 |
ORDER BY `marc_permissions_modules`.`specificity`, `id` |
3974 |
'; |
3975 |
my $sth = $dbh->prepare($query); |
3976 |
$sth->execute(); |
3977 |
while ( my $row = $sth->fetchrow_hashref ) { |
3978 |
push(@rules, $row); |
3979 |
} |
3980 |
|
3981 |
return wantarray ? @rules : \@rules; |
3982 |
} |
3983 |
|
3984 |
=head2 GetMarcPermissionsModules |
3985 |
|
3986 |
my $modules = GetMarcPermissionsModules() |
3987 |
|
3988 |
Returns: |
3989 |
|
3990 |
=over 4 |
3991 |
|
3992 |
=item C<$modules> |
3993 |
|
3994 |
array (in list context, arrayref otherwise) of hashrefs from |
3995 |
marc_permissions_modules table in order of specificity. |
3996 |
|
3997 |
=back |
3998 |
|
3999 |
=cut |
4000 |
|
4001 |
sub GetMarcPermissionsModules { |
4002 |
my $dbh = C4::Context->dbh; |
4003 |
my @modules = (); |
4004 |
|
4005 |
my $query = ' |
4006 |
SELECT * |
4007 |
FROM `marc_permissions_modules` |
4008 |
ORDER BY `specificity` DESC |
4009 |
'; |
4010 |
my $sth = $dbh->prepare($query); |
4011 |
$sth->execute(); |
4012 |
while ( my $row = $sth->fetchrow_hashref ) { |
4013 |
push(@modules, $row); |
4014 |
} |
4015 |
|
4016 |
return wantarray ? @modules : \@modules; |
4017 |
} |
4018 |
|
4019 |
sub GetMarcPermissionEvents { |
4020 |
return ('on_new', 'on_appended', 'on_removed', 'on_deleted'); |
4021 |
} |
4022 |
|
4023 |
=head2 ModMarcPermissionsRule |
4024 |
|
4025 |
my $success = ModMarcPermissionsRule($id, $fields) |
4026 |
|
4027 |
Modifies rule in the marc_permissions table. |
4028 |
|
4029 |
=over 4 |
4030 |
|
4031 |
=item C<$id> |
4032 |
|
4033 |
rule id to modify |
4034 |
|
4035 |
=item C<$fields> |
4036 |
|
4037 |
hashref defining the table fields |
4038 |
|
4039 |
* tagfield - required |
4040 |
* module - required |
4041 |
* filter - required |
4042 |
* on_new - required |
4043 |
* on_appended - required |
4044 |
* on_removed - required |
4045 |
* on_deleted - required |
4046 |
|
4047 |
=back |
4048 |
|
4049 |
Returns: |
4050 |
|
4051 |
=over 4 |
4052 |
|
4053 |
=item C<$success> |
4054 |
|
4055 |
undef if an error occurs, otherwise true. |
4056 |
|
4057 |
=back |
3600 |
|
4058 |
|
|
|
4059 |
=cut |
4060 |
|
4061 |
sub ModMarcPermissionsRule { |
4062 |
my ($id, $f) = @_; |
4063 |
my $dbh = C4::Context->dbh; |
4064 |
|
4065 |
my $query = ' |
4066 |
UPDATE `marc_permissions` |
4067 |
SET |
4068 |
tagfield = ?, |
4069 |
module = ?, |
4070 |
filter = ?, |
4071 |
on_new = ?, |
4072 |
on_appended = ?, |
4073 |
on_removed = ?, |
4074 |
on_deleted = ? |
4075 |
WHERE |
4076 |
id = ? |
4077 |
'; |
4078 |
my $sth = $dbh->prepare($query); |
4079 |
my $result = $sth->execute ( |
4080 |
$f->{tagfield}, |
4081 |
$f->{module}, |
4082 |
$f->{filter}, |
4083 |
$f->{on_new}, |
4084 |
$f->{on_appended}, |
4085 |
$f->{on_removed}, |
4086 |
$f->{on_deleted}, |
4087 |
$id |
4088 |
); |
4089 |
ClearMarcPermissionsRulesCache(); |
4090 |
return $result; |
4091 |
} |
4092 |
|
4093 |
sub ClearMarcPermissionsRulesCache { |
4094 |
my $cache = Koha::Caches->get_instance(); |
4095 |
$cache->clear_from_cache('marc_permissions'); |
4096 |
} |
4097 |
|
4098 |
=head2 AddMarcPermissionsRule |
4099 |
|
4100 |
my $success = AddMarcPermissionsRule($fields) |
4101 |
|
4102 |
Add rule to the marc_permissions table. |
4103 |
|
4104 |
=over 4 |
4105 |
|
4106 |
=item C<$fields> |
4107 |
|
4108 |
hashref defining the table fields |
4109 |
|
4110 |
tagfield - required |
4111 |
module - required |
4112 |
filter - required |
4113 |
on_new - required |
4114 |
on_appended - required |
4115 |
on_removed - required |
4116 |
on_deleted - required |
4117 |
|
4118 |
=back |
4119 |
|
4120 |
Returns: |
4121 |
|
4122 |
=over 4 |
4123 |
|
4124 |
=item C<$success> |
4125 |
|
4126 |
undef if an error occurs, otherwise true. |
4127 |
|
4128 |
=back |
4129 |
|
4130 |
=cut |
4131 |
|
4132 |
sub AddMarcPermissionsRule { |
4133 |
my $f = shift; |
4134 |
my $dbh = C4::Context->dbh; |
4135 |
my $query = ' |
4136 |
INSERT INTO `marc_permissions` |
4137 |
( |
4138 |
tagfield, |
4139 |
module, |
4140 |
filter, |
4141 |
on_new, |
4142 |
on_appended, |
4143 |
on_removed, |
4144 |
on_deleted |
4145 |
) |
4146 |
VALUES (?, ?, ?, ?, ?, ?, ?) |
4147 |
'; |
4148 |
my $sth = $dbh->prepare($query); |
4149 |
my $result = $sth->execute ( |
4150 |
$f->{tagfield}, |
4151 |
$f->{module}, |
4152 |
$f->{filter}, |
4153 |
$f->{on_new}, |
4154 |
$f->{on_appended}, |
4155 |
$f->{on_removed}, |
4156 |
$f->{on_deleted} |
4157 |
); |
4158 |
ClearMarcPermissionsRulesCache(); |
4159 |
return $result; |
4160 |
} |
4161 |
|
4162 |
=head2 DelMarcPermissionsRule |
4163 |
|
4164 |
my $success = DelMarcPermissionsRule($id) |
4165 |
|
4166 |
Deletes rule from the marc_permissions table. |
4167 |
|
4168 |
=over 4 |
4169 |
|
4170 |
=item C<$id> |
4171 |
|
4172 |
rule id to delete |
4173 |
|
4174 |
=back |
4175 |
|
4176 |
Returns: |
4177 |
|
4178 |
=over 4 |
4179 |
|
4180 |
=item C<$success> |
4181 |
|
4182 |
undef if an error occurs, otherwise true. |
4183 |
|
4184 |
=back |
4185 |
|
4186 |
=cut |
4187 |
|
4188 |
sub DelMarcPermissionsRule { |
4189 |
my $id = shift; |
4190 |
my $dbh = C4::Context->dbh; |
4191 |
my $query = ' |
4192 |
DELETE FROM `marc_permissions` |
4193 |
WHERE |
4194 |
id = ? |
4195 |
'; |
4196 |
my $sth = $dbh->prepare($query); |
4197 |
my $result = $sth->execute($id); |
4198 |
ClearMarcPermissionsRulesCache(); |
4199 |
return $result; |
4200 |
} |
4201 |
1; |
3601 |
|
4202 |
|
3602 |
__END__ |
4203 |
__END__ |
3603 |
|
4204 |
|