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

(-)a/cataloguing/addbiblio.pl (+12 lines)
Lines 44-49 use MARC::Record; Link Here
44
use C4::ClassSource qw( GetClassSources );
44
use C4::ClassSource qw( GetClassSources );
45
use C4::ImportBatch qw( GetImportRecordMarc );
45
use C4::ImportBatch qw( GetImportRecordMarc );
46
use C4::Charset qw( SetMarcUnicodeFlag );
46
use C4::Charset qw( SetMarcUnicodeFlag );
47
use C4::MarcModificationTemplates;
47
use Koha::BiblioFrameworks;
48
use Koha::BiblioFrameworks;
48
use Koha::DateUtils qw( dt_from_string );
49
use Koha::DateUtils qw( dt_from_string );
49
50
Lines 55-60 use Koha::BiblioFrameworks; Link Here
55
use MARC::File::USMARC;
56
use MARC::File::USMARC;
56
use MARC::File::XML;
57
use MARC::File::XML;
57
use URI::Escape qw( uri_escape_utf8 );
58
use URI::Escape qw( uri_escape_utf8 );
59
use List::MoreUtils qw(firstval);
58
60
59
if ( C4::Context->preference('marcflavour') eq 'UNIMARC' ) {
61
if ( C4::Context->preference('marcflavour') eq 'UNIMARC' ) {
60
    MARC::File::XML->default_record_format('UNIMARC');
62
    MARC::File::XML->default_record_format('UNIMARC');
Lines 858-863 if ( $op eq "addbiblio" ) { Link Here
858
    # it is not a duplicate (determined either by Koha itself or by user checking it's not a duplicate)
860
    # it is not a duplicate (determined either by Koha itself or by user checking it's not a duplicate)
859
    if ( !$duplicatebiblionumber or $confirm_not_duplicate ) {
861
    if ( !$duplicatebiblionumber or $confirm_not_duplicate ) {
860
        my $oldbibitemnum;
862
        my $oldbibitemnum;
863
        my $marc_modification_template_name = C4::Context->preference("SaveBiblioMarcModificationTemplate");
864
        if ($marc_modification_template_name) {
865
            my $template = firstval { $_->{'name'} eq $marc_modification_template_name } GetModificationTemplates();
866
            if ($template) {
867
                ModifyRecordWithTemplate($template->{'template_id'}, $record);
868
            }
869
            else {
870
                warn "No MARC modification template exists with name \"$marc_modification_template_name\"";
871
            }
872
        }
861
        if ( $is_a_modif ) {
873
        if ( $is_a_modif ) {
862
            ModBiblio( $record, $biblionumber, $frameworkcode );
874
            ModBiblio( $record, $biblionumber, $frameworkcode );
863
        }
875
        }
(-)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 ('SaveBiblioMarcModificationTemplate', '', NULL, 'MARC modification template applied when saving bibliographic record in staff client or HTTP API', 'Free');
(-)a/installer/data/mysql/mandatory/sysprefs.sql (+1 lines)
Lines 586-591 INSERT INTO systempreferences ( `variable`, `value`, `options`, `explanation`, ` Link Here
586
('RoutingListAddReserves','0','','If ON the patrons on routing lists are automatically added to holds on the issue.','YesNo'),
586
('RoutingListAddReserves','0','','If ON the patrons on routing lists are automatically added to holds on the issue.','YesNo'),
587
('RoutingListNote','To change this note edit <a href=\"/cgi-bin/koha/admin/preferences.pl?op=search&searchfield=RoutingListNote#jumped\">RoutingListNote</a> system preference.','70|10','Define a note to be shown on all routing lists','Textarea'),
587
('RoutingListNote','To change this note edit <a href=\"/cgi-bin/koha/admin/preferences.pl?op=search&searchfield=RoutingListNote#jumped\">RoutingListNote</a> system preference.','70|10','Define a note to be shown on all routing lists','Textarea'),
588
('RoutingSerials','1',NULL,'If ON, serials routing is enabled','YesNo'),
588
('RoutingSerials','1',NULL,'If ON, serials routing is enabled','YesNo'),
589
('SaveBiblioMarcModificationTemplate', '', NULL, 'MARC modification template applied when saving bibliographic record in staff client or HTTP API', 'Free'),
589
('SCOAllowCheckin','0','','If enabled, patrons may return items through the Web-based Self Checkout','YesNo'),
590
('SCOAllowCheckin','0','','If enabled, patrons may return items through the Web-based Self Checkout','YesNo'),
590
('SCOMainUserBlock','','70|10','Add a block of HTML that will display on the self checkout screen','Textarea'),
591
('SCOMainUserBlock','','70|10','Add a block of HTML that will display on the self checkout screen','Textarea'),
591
('SCOUserCSS','',NULL,'Add CSS to be included in the SCO module in an embedded <style> tag.','free'),
592
('SCOUserCSS','',NULL,'Add CSS to be included in the SCO module in an embedded <style> tag.','free'),
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/cataloguing.pref (+5 lines)
Lines 158-163 Cataloging: Link Here
158
            - and record's last modifier name in MARC subfield
158
            - and record's last modifier name in MARC subfield
159
            - pref: MarcFieldForModifierName
159
            - pref: MarcFieldForModifierName
160
            - ". <br/><strong>NOTE:</strong> Use a dollar sign between field and subfield like 123$a."
160
            - ". <br/><strong>NOTE:</strong> Use a dollar sign between field and subfield like 123$a."
161
        -
162
            - Apply the MARC modification template with name
163
            - pref: SaveBiblioMarcModificationTemplate
164
              class: short
165
            - on records saved using the staff client simple MARC editor, advanced editor or HTTP API.
161
    Display:
166
    Display:
162
        -
167
        -
163
            - 'Separate main entry and subdivisions with '
168
            - 'Separate main entry and subdivisions 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 qw( GetFrameworkCode );
26
use C4::Biblio qw( GetFrameworkCode );
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 = CGI->new;
32
my $query = CGI->new;
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("SaveBiblioMarcModificationTemplate");
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
        C4::Biblio::ModBiblio( $record, $biblionumber, $frameworkcode );
130
        C4::Biblio::ModBiblio( $record, $biblionumber, $frameworkcode );
118
        my $new_record = C4::Biblio::GetMarcBiblio({
131
        my $new_record = C4::Biblio::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({ biblio => $biblionumber, embed_items => 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('SaveBiblioMarcModificationTemplate');
91
    C4::Context->set_preference('SaveBiblioMarcModificationTemplate', '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({ biblio => $biblionumber, embed_items => 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('SaveBiblioMarcModificationTemplate', $old_template_preference);
154
};

Return to bug 18138