View | Details | Raw Unified | Return to bug 18138
Collapse All | Expand All

(-)a/cataloguing/addbiblio.pl (+12 lines)
Lines 34-39 use C4::Koha; Link Here
34
use C4::ClassSource;
34
use C4::ClassSource;
35
use C4::ImportBatch;
35
use C4::ImportBatch;
36
use C4::Charset;
36
use C4::Charset;
37
use C4::MarcModificationTemplates;
37
use Koha::BiblioFrameworks;
38
use Koha::BiblioFrameworks;
38
use Koha::DateUtils;
39
use Koha::DateUtils;
39
40
Lines 46-51 use Date::Calc qw(Today); Link Here
46
use MARC::File::USMARC;
47
use MARC::File::USMARC;
47
use MARC::File::XML;
48
use MARC::File::XML;
48
use URI::Escape;
49
use URI::Escape;
50
use List::MoreUtils qw(firstval);
49
51
50
if ( C4::Context->preference('marcflavour') eq 'UNIMARC' ) {
52
if ( C4::Context->preference('marcflavour') eq 'UNIMARC' ) {
51
    MARC::File::XML->default_record_format('UNIMARC');
53
    MARC::File::XML->default_record_format('UNIMARC');
Lines 831-836 if ( $op eq "addbiblio" ) { Link Here
831
    # it is not a duplicate (determined either by Koha itself or by user checking it's not a duplicate)
833
    # it is not a duplicate (determined either by Koha itself or by user checking it's not a duplicate)
832
    if ( !$duplicatebiblionumber or $confirm_not_duplicate ) {
834
    if ( !$duplicatebiblionumber or $confirm_not_duplicate ) {
833
        my $oldbibitemnum;
835
        my $oldbibitemnum;
836
        my $marc_modification_template_name = C4::Context->preference("EditBiblioMarcModificationTemplate");
837
        if ($marc_modification_template_name) {
838
            my $template = firstval { $_->{'name'} eq $marc_modification_template_name } GetModificationTemplates();
839
            if ($template) {
840
                ModifyRecordWithTemplate($template->{'template_id'}, $record);
841
            }
842
            else {
843
                warn "No MARC modification template exists with name \"$marc_modification_template_name\"";
844
            }
845
        }
834
        if ( $is_a_modif ) {
846
        if ( $is_a_modif ) {
835
            ModBiblio( $record, $biblionumber, $frameworkcode );
847
            ModBiblio( $record, $biblionumber, $frameworkcode );
836
        }
848
        }
(-)a/installer/data/mysql/atomicupdate/bug_18138-edit-biblio-marc-modification-template-syspref.sql (+1 lines)
Line 0 Link Here
1
INSERT IGNORE INTO systempreferences ( `variable`, `value`, `options`, `explanation`, `type`) VALUES ('EditBiblioMarcModificationTemplate', '', '', 'MARC modification template applied when saving bibliographic record in staff client or HTTP API', 'Free');
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/cataloguing.pref (+5 lines)
Lines 149-154 Cataloging: Link Here
149
            - and record's last modifier name in MARC subfield
149
            - and record's last modifier name in MARC subfield
150
            - pref: MarcFieldForModifierName
150
            - pref: MarcFieldForModifierName
151
            - "<br/>NOTE: Use a dollar sign between field and subfield like 123$a."
151
            - "<br/>NOTE: Use a dollar sign between field and subfield like 123$a."
152
        -
153
            - Apply the MARC modification template with name
154
            - pref: EditBiblioMarcModificationTemplate
155
              class: short
156
            - on records saved using the staff client simple MARC editor, advanced editor or HTTP API.
152
    Display:
157
    Display:
153
        -
158
        -
154
            - 'Separate multiple displayed authors, series or subjects with '
159
            - 'Separate multiple displayed authors, series or subjects with '
(-)a/svc/bib (+13 lines)
Lines 25-31 use CGI qw ( -utf8 ); Link Here
25
use C4::Auth qw/check_api_auth/;
25
use C4::Auth qw/check_api_auth/;
26
use C4::Biblio;
26
use C4::Biblio;
27
use C4::Items;
27
use C4::Items;
28
use C4::MarcModificationTemplates;
28
use XML::Simple;
29
use XML::Simple;
30
use List::MoreUtils qw(firstval);
29
31
30
my $query = new CGI;
32
my $query = new CGI;
31
binmode STDOUT, ':encoding(UTF-8)';
33
binmode STDOUT, ':encoding(UTF-8)';
Lines 114-119 sub update_bib { Link Here
114
            }
116
            }
115
        }
117
        }
116
118
119
        my $marc_modification_template_name = C4::Context->preference("EditBiblioMarcModificationTemplate");
120
        if ($marc_modification_template_name) {
121
            my $template = firstval { $_->{'name'} eq $marc_modification_template_name } GetModificationTemplates();
122
            if ($template) {
123
                ModifyRecordWithTemplate($template->{'template_id'}, $record);
124
            }
125
            else {
126
                warn "No MARC modification template exists with name \"$marc_modification_template_name\"";
127
            }
128
        }
129
117
        ModBiblio( $record, $biblionumber, $frameworkcode );
130
        ModBiblio( $record, $biblionumber, $frameworkcode );
118
        my $new_record = GetMarcBiblio({
131
        my $new_record = GetMarcBiblio({
119
            biblionumber => $biblionumber,
132
            biblionumber => $biblionumber,
(-)a/t/db_dependent/www/EditBiblioMarcModificationTemplatePreference.t (-1 / +154 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
use Modern::Perl;
18
use Test::More tests => 1;
19
use Test::MockModule;
20
use Test::Warn;
21
use Test::WWW::Mechanize;
22
require HTTP::Request;
23
use MARC::Record;
24
use XML::Simple;
25
use C4::MarcModificationTemplates;
26
use C4::Biblio;
27
28
eval{
29
    use C4::Context;
30
};
31
if ($@) {
32
    plan skip_all => "Tests skip. You must have a working Context\n";
33
}
34
35
my $koha_conf = $ENV{KOHA_CONF};
36
my $koha_conf_xml = XMLin($koha_conf);
37
38
my $user     = $ENV{KOHA_USER} || $koha_conf_xml->{config}->{user};
39
my $password = $ENV{KOHA_PASS} || $koha_conf_xml->{config}->{pass};
40
my $intranet = $ENV{KOHA_INTRANET_URL};
41
42
if (not defined $intranet) {
43
    plan skip_all => "Tests skipped. You must set environment variable KOHA_INTRANET_URL to run tests\n";
44
}
45
46
subtest 'Templates applied using simple and advanced MARC Editor' => sub {
47
	plan tests => 13;
48
49
	# Create "Test" MARC modification template
50
    my $template_id = AddModificationTemplate('TEST');
51
    ok($template_id, 'MARC modification template successfully created');
52
    my $add_template_action_result = AddModificationTemplateAction(
53
        $template_id,
54
        'update_field', # Action
55
        0,
56
        250, # Field
57
        'a', # Subfield
58
        '251 bottles of beer on the wall', # Value
59
        '',
60
        '',
61
        '',
62
        '',
63
        '',
64
        '',
65
        '',
66
        '',
67
        '',
68
        '',
69
        0,
70
        ''
71
    );
72
    ok($add_template_action_result, 'MARC modification template action successfully created');
73
    my $record =  MARC::Record->new();
74
    $record->leader('     nam a22     7a 4500');
75
	$record->append_fields(
76
		MARC::Field->new('001', '12345'),
77
		MARC::Field->new('003', 'TEST'),
78
		MARC::Field->new('245', '1', '0', 'a' => 'TEST'),
79
		MARC::Field->new('250', '','', 'a' => '250 bottles of beer on the wall'),
80
        # @FIXME: Create test item type? Not super safe to rely on existing 'BK' type
81
		MARC::Field->new('942', '','', 'c' => 'BK'),
82
	);
83
    my ($biblionumber) = AddBiblio($record, '');
84
85
    my $saved_record = GetMarcBiblio($biblionumber, 0);
86
    my $saved_record_250_field = $saved_record->field('250');
87
    isa_ok($saved_record_250_field, 'MARC::Field', 'Field with tag 250 has been saved');
88
    is($saved_record_250_field->subfield('a'), '250 bottles of beer on the wall', 'Field 250a has the same value passed to AddBiblio');
89
90
    my $old_template_preference = C4::Context->preference('EditBiblioMarcModificationTemplate');
91
    C4::Context->set_preference('EditBiblioMarcModificationTemplate', 'TEST');
92
93
    my $agent = Test::WWW::Mechanize->new(autocheck => 1);
94
95
    $agent->get_ok("$intranet/cgi-bin/koha/mainpage.pl", 'Connect to intranet');
96
    $agent->form_name('loginform');
97
    $agent->field('password', $password);
98
    $agent->field('userid', $user);
99
    $agent->field('branch', '');
100
    $agent->click_ok('', 'Login to staff client');
101
102
    $agent->get_ok("$intranet/cgi-bin/koha/mainpage.pl", 'Load main page'); #FIXME: Remove?
103
    $agent->get_ok("$intranet/cgi-bin/koha/cataloguing/addbiblio.pl?biblionumber=$biblionumber", 'Load bibliographic record in simple MARC editor');
104
    $agent->submit_form_ok(
105
        {
106
            form_id => 'f',
107
            button => '',
108
        },
109
        'Save bibliographic record using simple MARC editor'
110
    );
111
112
    $saved_record = GetMarcBiblio($biblionumber, 0);
113
    $saved_record_250_field = $saved_record->field('250');
114
    is($saved_record_250_field->subfield('a'), '251 bottles of beer on the wall', 'Field with tag 250 has been modified by MARC modification template');
115
116
    # FIXME: Don't really need to to this, but could be useful so
117
    # test below does not succed by accident
118
    $saved_record->delete_fields($saved_record_250_field);
119
    $saved_record->insert_fields_ordered(MARC::Field->new('250', '','', 'a' => '250 bottles of beer on the wall'));
120
    $biblionumber = ModBiblioMarc($saved_record, $biblionumber);
121
    ok($biblionumber, 'Field with tag 250 has been restored to original value');
122
123
    # @FIXME: Leader record status n or c?
124
    my $record_xml = <<'EOF';
125
<?xml version="1.0" standalone='yes'?>
126
    <record xmlns="http://www.loc.gov/MARC21/slim">
127
        <leader>     cam a22     7a 4500</leader>
128
        <controlfield tag="001">12345</controlfield>
129
        <controlfield tag="003">TEST</controlfield>
130
        <datafield tag="245" ind1="1" ind2="0">
131
            <subfield code="a">TEST</subfield>
132
        </datafield>
133
        <datafield tag="250" ind1=" " ind2=" ">
134
            <subfield code="a">250 bottles of beer on the wall</subfield>
135
        </datafield>
136
        <datafield tag="942" ind1=" " ind2=" ">
137
            <subfield code="c">BK</subfield>
138
            <subfield code="2">ddc</subfield>
139
        </datafield></record>
140
EOF
141
    my $headers = HTTP::Headers->new(
142
        Content_Type => 'text/xml',
143
    );
144
    my $request = HTTP::Request->new('POST', "$intranet/cgi-bin/koha/svc/bib/$biblionumber", $headers, $record_xml);
145
    my $response = $agent->request($request);
146
147
    is($response->code, '200', 'Emulate save in advanced MARC editor, Koha REST API responded with 200 for update biblio request');
148
    like($response->decoded_content, qr/251 bottles of beer on the wall/, 'Field with tag 250 has been modified by MARC modification template');
149
150
    # Clean up
151
    DelBiblio($biblionumber);
152
    DelModificationTemplate($template_id);
153
    C4::Context->set_preference('EditBiblioMarcModificationTemplate', $old_template_preference);
154
};

Return to bug 18138