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 58-63 use Koha::UI::Form::Builder::Biblio; Link Here
58
use MARC::File::USMARC;
59
use MARC::File::USMARC;
59
use MARC::File::XML;
60
use MARC::File::XML;
60
use URI::Escape qw( uri_escape_utf8 );
61
use URI::Escape qw( uri_escape_utf8 );
62
use List::MoreUtils qw(firstval);
61
63
62
if ( C4::Context->preference('marcflavour') eq 'UNIMARC' ) {
64
if ( C4::Context->preference('marcflavour') eq 'UNIMARC' ) {
63
    MARC::File::XML->default_record_format('UNIMARC');
65
    MARC::File::XML->default_record_format('UNIMARC');
Lines 659-664 if ( $op eq "addbiblio" ) { Link Here
659
    # it is not a duplicate (determined either by Koha itself or by user checking it's not a duplicate)
661
    # it is not a duplicate (determined either by Koha itself or by user checking it's not a duplicate)
660
    if ( !$duplicatebiblionumber or $confirm_not_duplicate ) {
662
    if ( !$duplicatebiblionumber or $confirm_not_duplicate ) {
661
        my $oldbibitemnum;
663
        my $oldbibitemnum;
664
        my $marc_modification_template_name = C4::Context->preference("SaveBiblioMarcModificationTemplate");
665
        if ($marc_modification_template_name) {
666
            my $template = firstval { $_->{'name'} eq $marc_modification_template_name } GetModificationTemplates();
667
            if ($template) {
668
                ModifyRecordWithTemplate($template->{'template_id'}, $record);
669
            }
670
            else {
671
                warn "No MARC modification template exists with name \"$marc_modification_template_name\"";
672
            }
673
        }
662
        if ( $is_a_modif ) {
674
        if ( $is_a_modif ) {
663
            my $member = Koha::Patrons->find($loggedinuser);
675
            my $member = Koha::Patrons->find($loggedinuser);
664
            ModBiblio(
676
            ModBiblio(
(-)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 645-650 INSERT INTO systempreferences ( `variable`, `value`, `options`, `explanation`, ` Link Here
645
('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'),
645
('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'),
646
('RoutingSerials','1',NULL,'If ON, serials routing is enabled','YesNo'),
646
('RoutingSerials','1',NULL,'If ON, serials routing is enabled','YesNo'),
647
('SavedSearchFilters', '0', NULL, 'Allow staff with permission to create/edit custom search filters', 'YesNo'),
647
('SavedSearchFilters', '0', NULL, 'Allow staff with permission to create/edit custom search filters', 'YesNo'),
648
('SaveBiblioMarcModificationTemplate', '', NULL, 'MARC modification template applied when saving bibliographic record in staff client or HTTP API', 'Free'),
648
('SCOAllowCheckin','0','','If enabled, patrons may return items through the Web-based Self Checkout','YesNo'),
649
('SCOAllowCheckin','0','','If enabled, patrons may return items through the Web-based Self Checkout','YesNo'),
649
('SCOMainUserBlock','','70|10','Add a block of HTML that will display on the self checkout screen','Textarea'),
650
('SCOMainUserBlock','','70|10','Add a block of HTML that will display on the self checkout screen','Textarea'),
650
('SCOUserCSS','',NULL,'Add CSS to be included in the SCO module in an embedded <style> tag.','free'),
651
('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 217-222 Cataloging: Link Here
217
                  1: Strip
217
                  1: Strip
218
                  0: "Don't strip"
218
                  0: "Don't strip"
219
            - leading and trailing whitespace characters (including spaces, tabs, line breaks and carriage returns) and inner newlines from data fields when cataloguing bibliographic and authority records. The leader and control fields will not be affected.
219
            - leading and trailing whitespace characters (including spaces, tabs, line breaks and carriage returns) and inner newlines from data fields when cataloguing bibliographic and authority records. The leader and control fields will not be affected.
220
        -
221
            - Apply the MARC modification template with name
222
            - pref: SaveBiblioMarcModificationTemplate
223
              class: short
224
            - on records saved using the staff client simple MARC editor, advanced editor or HTTP API.
220
    Display:
225
    Display:
221
        -
226
        -
222
            - 'Separate main entry and subdivisions with '
227
            - 'Separate main entry and subdivisions with '
(-)a/svc/bib (+13 lines)
Lines 25-32 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;
29
use Koha::Biblios;
30
use Koha::Biblios;
31
use List::MoreUtils qw(firstval);
30
32
31
my $query = CGI->new;
33
my $query = CGI->new;
32
binmode STDOUT, ':encoding(UTF-8)';
34
binmode STDOUT, ':encoding(UTF-8)';
Lines 115-120 sub update_bib { Link Here
115
            }
117
            }
116
        }
118
        }
117
119
120
        my $marc_modification_template_name = C4::Context->preference("SaveBiblioMarcModificationTemplate");
121
        if ($marc_modification_template_name) {
122
            my $template = firstval { $_->{'name'} eq $marc_modification_template_name } GetModificationTemplates();
123
            if ($template) {
124
                ModifyRecordWithTemplate($template->{'template_id'}, $record);
125
            }
126
            else {
127
                warn "No MARC modification template exists with name \"$marc_modification_template_name\"";
128
            }
129
        }
130
118
        C4::Biblio::ModBiblio( $record, $biblionumber, $frameworkcode );
131
        C4::Biblio::ModBiblio( $record, $biblionumber, $frameworkcode );
119
        my $biblio = Koha::Biblios->find( $biblionumber );
132
        my $biblio = Koha::Biblios->find( $biblionumber );
120
        my $new_record = $biblio->metadata->record({ embed_items => scalar $query->url_param('items') });
133
        my $new_record = $biblio->metadata->record({ embed_items => scalar $query->url_param('items') });
(-)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