|
Lines 34-39
use Koha::SimpleMARC qw(
Link Here
|
| 34 |
); |
34 |
); |
| 35 |
use Koha::MoreUtils; |
35 |
use Koha::MoreUtils; |
| 36 |
use Koha::DateUtils qw( dt_from_string ); |
36 |
use Koha::DateUtils qw( dt_from_string ); |
|
|
37 |
use JSON qw( encode_json decode_json ); |
| 37 |
|
38 |
|
| 38 |
use vars qw(@ISA @EXPORT); |
39 |
use vars qw(@ISA @EXPORT); |
| 39 |
|
40 |
|
|
Lines 54-59
BEGIN {
Link Here
|
| 54 |
|
55 |
|
| 55 |
ModifyRecordsWithTemplate |
56 |
ModifyRecordsWithTemplate |
| 56 |
ModifyRecordWithTemplate |
57 |
ModifyRecordWithTemplate |
|
|
58 |
|
| 59 |
ExportModificationTemplates |
| 60 |
ImportModificationTemplates |
| 57 |
); |
61 |
); |
| 58 |
} |
62 |
} |
| 59 |
|
63 |
|
|
Lines 735-740
sub ModifyRecordWithTemplate {
Link Here
|
| 735 |
|
739 |
|
| 736 |
return; |
740 |
return; |
| 737 |
} |
741 |
} |
|
|
742 |
|
| 743 |
=head2 |
| 744 |
ExportModificationTemplates |
| 745 |
|
| 746 |
my $json = ExportModificationTemplates( $template_ids ); |
| 747 |
|
| 748 |
Exports MARC modification templates to JSON format. |
| 749 |
If $template_ids is provided, only export those templates. |
| 750 |
If not provided, export all templates. |
| 751 |
|
| 752 |
Returns a JSON string containing the exported templates. |
| 753 |
=cut |
| 754 |
|
| 755 |
sub ExportModificationTemplates { |
| 756 |
my ($template_ids) = @_; |
| 757 |
|
| 758 |
my $dbh = C4::Context->dbh; |
| 759 |
|
| 760 |
my @templates; |
| 761 |
if ( $template_ids && @$template_ids ) { |
| 762 |
my $placeholders = join( ',', ('?') x @$template_ids ); |
| 763 |
my $sth = $dbh->prepare( |
| 764 |
"SELECT * FROM marc_modification_templates WHERE template_id IN ($placeholders) ORDER BY name"); |
| 765 |
$sth->execute(@$template_ids); |
| 766 |
while ( my $template = $sth->fetchrow_hashref() ) { |
| 767 |
push( @templates, $template ); |
| 768 |
} |
| 769 |
} else { |
| 770 |
my $sth = $dbh->prepare("SELECT * FROM marc_modification_templates ORDER BY name"); |
| 771 |
$sth->execute(); |
| 772 |
while ( my $template = $sth->fetchrow_hashref() ) { |
| 773 |
push( @templates, $template ); |
| 774 |
} |
| 775 |
} |
| 776 |
|
| 777 |
my $export_data = { |
| 778 |
version => 1, |
| 779 |
templates => [], |
| 780 |
}; |
| 781 |
|
| 782 |
foreach my $template (@templates) { |
| 783 |
my $template_id = $template->{'template_id'}; |
| 784 |
my @actions = GetModificationTemplateActions($template_id); |
| 785 |
|
| 786 |
my $template_export = { |
| 787 |
template_id => $template_id, |
| 788 |
name => $template->{'name'}, |
| 789 |
actions => [], |
| 790 |
}; |
| 791 |
|
| 792 |
foreach my $action (@actions) { |
| 793 |
my $action_export = { |
| 794 |
mmta_id => $action->{'mmta_id'}, |
| 795 |
ordering => $action->{'ordering'}, |
| 796 |
action => $action->{'action'}, |
| 797 |
field_number => $action->{'field_number'}, |
| 798 |
from_field => $action->{'from_field'}, |
| 799 |
from_subfield => $action->{'from_subfield'}, |
| 800 |
field_value => $action->{'field_value'}, |
| 801 |
to_field => $action->{'to_field'}, |
| 802 |
to_subfield => $action->{'to_subfield'}, |
| 803 |
to_regex_search => $action->{'to_regex_search'}, |
| 804 |
to_regex_replace => $action->{'to_regex_replace'}, |
| 805 |
to_regex_modifiers => $action->{'to_regex_modifiers'}, |
| 806 |
conditional => $action->{'conditional'}, |
| 807 |
conditional_field => $action->{'conditional_field'}, |
| 808 |
conditional_subfield => $action->{'conditional_subfield'}, |
| 809 |
conditional_comparison => $action->{'conditional_comparison'}, |
| 810 |
conditional_value => $action->{'conditional_value'}, |
| 811 |
conditional_regex => $action->{'conditional_regex'}, |
| 812 |
description => $action->{'description'}, |
| 813 |
}; |
| 814 |
|
| 815 |
push( @{ $template_export->{actions} }, $action_export ); |
| 816 |
} |
| 817 |
|
| 818 |
push( @{ $export_data->{templates} }, $template_export ); |
| 819 |
} |
| 820 |
|
| 821 |
return encode_json($export_data); |
| 822 |
} |
| 823 |
|
| 824 |
=head2 |
| 825 |
ImportModificationTemplates |
| 826 |
|
| 827 |
my $result = ImportModificationTemplates( $json, $options ); |
| 828 |
|
| 829 |
Imports MARC modification templates from JSON format. |
| 830 |
$json is the JSON string to import. |
| 831 |
$options is a hashref with import options: |
| 832 |
- skip_existing: if true, skip templates that already exist (default: false) |
| 833 |
- update_existing: if true, update templates that already exist (default: false) |
| 834 |
|
| 835 |
Returns a hashref with: |
| 836 |
- success: number of successfully imported templates |
| 837 |
- skipped: number of skipped templates |
| 838 |
- errors: arrayref of error messages |
| 839 |
=cut |
| 840 |
|
| 841 |
sub ImportModificationTemplates { |
| 842 |
my ( $json, $skip_existing ) = @_; |
| 843 |
|
| 844 |
my $result = { |
| 845 |
success => 0, |
| 846 |
skipped => 0, |
| 847 |
errors => [], |
| 848 |
}; |
| 849 |
|
| 850 |
my $data; |
| 851 |
eval { $data = decode_json($json); }; |
| 852 |
if ($@) { |
| 853 |
push( @{ $result->{errors} }, "Failed to parse JSON: $@" ); |
| 854 |
return $result; |
| 855 |
} |
| 856 |
|
| 857 |
my $version = $data->{version} || 0; |
| 858 |
if ( $version != 1 ) { |
| 859 |
push( @{ $result->{errors} }, "Unsupported export version: $version" ); |
| 860 |
return $result; |
| 861 |
} |
| 862 |
|
| 863 |
my $templates = $data->{templates} || []; |
| 864 |
my $dbh = C4::Context->dbh; |
| 865 |
|
| 866 |
foreach my $template_data (@$templates) { |
| 867 |
my $template_name = $template_data->{name}; |
| 868 |
my $actions = $template_data->{actions} || []; |
| 869 |
|
| 870 |
# Check if template already exists |
| 871 |
my $sth = $dbh->prepare("SELECT template_id FROM marc_modification_templates WHERE name = ?"); |
| 872 |
$sth->execute($template_name); |
| 873 |
my $existing = $sth->fetchrow_hashref(); |
| 874 |
|
| 875 |
if ( $existing && $skip_existing ) { |
| 876 |
$result->{skipped}++; |
| 877 |
next; |
| 878 |
} |
| 879 |
|
| 880 |
my $template_id; |
| 881 |
if ($existing) { |
| 882 |
$template_id = $existing->{template_id}; |
| 883 |
|
| 884 |
# Delete existing actions for this template |
| 885 |
$sth = $dbh->prepare("DELETE FROM marc_modification_template_actions WHERE template_id = ?"); |
| 886 |
$sth->execute($template_id); |
| 887 |
$result->{overwrite}++; |
| 888 |
} else { |
| 889 |
|
| 890 |
# Create new template |
| 891 |
$sth = $dbh->prepare("INSERT INTO marc_modification_templates (name) VALUES (?)"); |
| 892 |
$sth->execute($template_name); |
| 893 |
|
| 894 |
$sth = $dbh->prepare("SELECT template_id FROM marc_modification_templates WHERE name = ?"); |
| 895 |
$sth->execute($template_name); |
| 896 |
my $row = $sth->fetchrow_hashref(); |
| 897 |
$template_id = $row->{template_id}; |
| 898 |
} |
| 899 |
|
| 900 |
# Add new actions |
| 901 |
foreach my $action_data (@$actions) { |
| 902 |
AddModificationTemplateAction( |
| 903 |
$template_id, |
| 904 |
$action_data->{action}, |
| 905 |
$action_data->{field_number}, |
| 906 |
$action_data->{from_field}, |
| 907 |
$action_data->{from_subfield}, |
| 908 |
$action_data->{field_value}, |
| 909 |
$action_data->{to_field}, |
| 910 |
$action_data->{to_subfield}, |
| 911 |
$action_data->{to_regex_search}, |
| 912 |
$action_data->{to_regex_replace}, |
| 913 |
$action_data->{to_regex_modifiers}, |
| 914 |
$action_data->{conditional}, |
| 915 |
$action_data->{conditional_field}, |
| 916 |
$action_data->{conditional_subfield}, |
| 917 |
$action_data->{conditional_comparison}, |
| 918 |
$action_data->{conditional_value}, |
| 919 |
$action_data->{conditional_regex}, |
| 920 |
$action_data->{description}, |
| 921 |
); |
| 922 |
} |
| 923 |
|
| 924 |
$result->{success}++; |
| 925 |
} |
| 926 |
|
| 927 |
return $result; |
| 928 |
} |
| 738 |
1; |
929 |
1; |
| 739 |
__END__ |
930 |
__END__ |
| 740 |
|
931 |
|