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

(-)a/cataloguing/addbiblio.pl (-1 / +13 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 45-50 use Date::Calc qw(Today); Link Here
45
use MARC::File::USMARC;
46
use MARC::File::USMARC;
46
use MARC::File::XML;
47
use MARC::File::XML;
47
use URI::Escape;
48
use URI::Escape;
49
use List::MoreUtils qw(firstval);
48
50
49
if ( C4::Context->preference('marcflavour') eq 'UNIMARC' ) {
51
if ( C4::Context->preference('marcflavour') eq 'UNIMARC' ) {
50
    MARC::File::XML->default_record_format('UNIMARC');
52
    MARC::File::XML->default_record_format('UNIMARC');
Lines 835-841 if ( $op eq "addbiblio" ) { Link Here
835
        my $oldbibitemnum;
837
        my $oldbibitemnum;
836
        if (C4::Context->preference("BiblioAddsAuthorities")){
838
        if (C4::Context->preference("BiblioAddsAuthorities")){
837
            BiblioAutoLink( $record, $frameworkcode );
839
            BiblioAutoLink( $record, $frameworkcode );
838
        } 
840
        }
841
        my $marc_modification_template_name = C4::Context->preference("EditBiblioMarcModificationTemplate");
842
        if ($marc_modification_template_name) {
843
            my $template = firstval { $_->{'name'} eq $marc_modification_template_name } GetModificationTemplates();
844
            if ($template) {
845
                ModifyRecordWithTemplate($template->{'template_id'}, $record);
846
            }
847
            else {
848
                warn "No MARC modification template exists with name \"$marc_modification_template_name\"";
849
            }
850
        }
839
        if ( $is_a_modif ) {
851
        if ( $is_a_modif ) {
840
            ModBiblio( $record, $biblionumber, $frameworkcode );
852
            ModBiblio( $record, $biblionumber, $frameworkcode );
841
        }
853
        }
(-)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 125-130 Cataloging: Link Here
125
            - 'MARC21: "952$a 952$b 952$c"'
125
            - 'MARC21: "952$a 952$b 952$c"'
126
            - Note that the FA framework is excluded from the permission.
126
            - Note that the FA framework is excluded from the permission.
127
            - If the pref is empty, no fields are restricted.
127
            - If the pref is empty, no fields are restricted.
128
        -
129
            - Apply the MARC modification template with name
130
            - pref: EditBiblioMarcModificationTemplate
131
              class: short
132
            - on records saved using the staff client simple MARC editor, advanced editor or HTTP API.
128
    Display:
133
    Display:
129
        -
134
        -
130
            - 'Separate multiple displayed authors, series or subjects with '
135
            - 'Separate multiple displayed authors, series or subjects with '
(-)a/svc/bib (+13 lines)
Lines 26-32 use CGI qw ( -utf8 ); Link Here
26
use C4::Auth qw/check_api_auth/;
26
use C4::Auth qw/check_api_auth/;
27
use C4::Biblio;
27
use C4::Biblio;
28
use C4::Items;
28
use C4::Items;
29
use C4::MarcModificationTemplates;
29
use XML::Simple;
30
use XML::Simple;
31
use List::MoreUtils qw(firstval);
30
32
31
my $query = new CGI;
33
my $query = new CGI;
32
binmode STDOUT, ':encoding(UTF-8)';
34
binmode STDOUT, ':encoding(UTF-8)';
Lines 112-117 sub update_bib { Link Here
112
            }
114
            }
113
        }
115
        }
114
116
117
        my $marc_modification_template_name = C4::Context->preference("EditBiblioMarcModificationTemplate");
118
        if ($marc_modification_template_name) {
119
            my $template = firstval { $_->{'name'} eq $marc_modification_template_name } GetModificationTemplates();
120
            if ($template) {
121
                ModifyRecordWithTemplate($template->{'template_id'}, $record);
122
            }
123
            else {
124
                warn "No MARC modification template exists with name \"$marc_modification_template_name\"";
125
            }
126
        }
127
115
        ModBiblio( $record, $biblionumber, '' );
128
        ModBiblio( $record, $biblionumber, '' );
116
        my $new_record =
129
        my $new_record =
117
          GetMarcBiblio( $biblionumber, $query->url_param('items') );
130
          GetMarcBiblio( $biblionumber, $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($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