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

(-)a/C4/Record.pm (-5 / +14 lines)
Lines 29-34 use Unicode::Normalize qw( NFC ); # _entity_encode Link Here
29
use C4::Biblio         qw( GetFrameworkCode );
29
use C4::Biblio         qw( GetFrameworkCode );
30
use C4::Koha;                                    #marc2csv
30
use C4::Koha;                                    #marc2csv
31
use C4::XSLT;
31
use C4::XSLT;
32
use C4::MarcModificationTemplates;
32
use YAML::XS;                                    #marcrecords2csv
33
use YAML::XS;                                    #marcrecords2csv
33
use Encode;
34
use Encode;
34
use Template;
35
use Template;
Lines 420-429 C<$csvprofileid> - the id of the CSV profile to use for the export (see export_f Link Here
420
421
421
C<$itemnumbers> - a list of itemnumbers to export
422
C<$itemnumbers> - a list of itemnumbers to export
422
423
424
C<$marc_modification_template_id> - MARC modification template to apply
425
423
=cut
426
=cut
424
427
425
sub marc2csv {
428
sub marc2csv {
426
    my ( $biblios, $id, $itemnumbers ) = @_;
429
    my ( $biblios, $id, $itemnumbers, $marc_modification_template_id ) = @_;
427
    $itemnumbers ||= [];
430
    $itemnumbers ||= [];
428
    my $output;
431
    my $output;
429
    my $csv = Text::CSV::Encoded->new( { formula => "empty" } );
432
    my $csv = Text::CSV::Encoded->new( { formula => "empty" } );
Lines 443-454 sub marc2csv { Link Here
443
        for my $itemnumber (@$itemnumbers) {
446
        for my $itemnumber (@$itemnumbers) {
444
            my $item         = Koha::Items->find($itemnumber);
447
            my $item         = Koha::Items->find($itemnumber);
445
            my $biblionumber = $item->biblio->biblionumber;
448
            my $biblionumber = $item->biblio->biblionumber;
446
            $output .= marcrecord2csv( $biblionumber, $id, $firstpass, $csv, $fieldprocessing, [$itemnumber] ) // '';
449
            $output .= marcrecord2csv( $biblionumber, $id, $firstpass, $csv, $fieldprocessing, [$itemnumber], $marc_modification_template_id ) // '';
447
            $firstpass = 0;
450
            $firstpass = 0;
448
        }
451
        }
449
    } else {
452
    } else {
450
        foreach my $biblio (@$biblios) {
453
        foreach my $biblio (@$biblios) {
451
            $output .= marcrecord2csv( $biblio, $id, $firstpass, $csv, $fieldprocessing ) // '';
454
            $output .= marcrecord2csv( $biblio, $id, $firstpass, $csv, $fieldprocessing, undef, $marc_modification_template_id ) // '';
452
            $firstpass = 0;
455
            $firstpass = 0;
453
        }
456
        }
454
    }
457
    }
Lines 461-467 sub marc2csv { Link Here
461
464
462
=head2 marcrecord2csv - Convert a single record from UNIMARC to CSV
465
=head2 marcrecord2csv - Convert a single record from UNIMARC to CSV
463
466
464
  my ($csv) = marcrecord2csv($biblio, $csvprofileid, $header);
467
  my ($csv) = marcrecord2csv($biblio, $csvprofileid, $header, $csv, $fieldprocessing, $itemnumbers, $marc_modification_template_id);
465
468
466
Returns a CSV scalar
469
Returns a CSV scalar
467
470
Lines 477-486 C<$fieldprocessing> Link Here
477
480
478
C<$itemnumbers> a list of itemnumbers to export
481
C<$itemnumbers> a list of itemnumbers to export
479
482
483
C<$marc_modification_template_id> - MARC modification template to apply
484
480
=cut
485
=cut
481
486
482
sub marcrecord2csv {
487
sub marcrecord2csv {
483
    my ( $biblionumber, $id, $header, $csv, $fieldprocessing, $itemnumbers ) = @_;
488
    my ( $biblionumber, $id, $header, $csv, $fieldprocessing, $itemnumbers, $marc_modification_template_id ) = @_;
484
    my $output;
489
    my $output;
485
490
486
    # Getting the record
491
    # Getting the record
Lines 489-494 sub marcrecord2csv { Link Here
489
    my $record = eval { $biblio->metadata->record( { embed_items => 1, itemnumbers => $itemnumbers } ); };
494
    my $record = eval { $biblio->metadata->record( { embed_items => 1, itemnumbers => $itemnumbers } ); };
490
    return unless $record;
495
    return unless $record;
491
496
497
    if ($marc_modification_template_id) {
498
        C4::MarcModificationTemplates::ModifyRecordWithTemplate($marc_modification_template_id, $record);
499
    }
500
492
    # Getting the framework
501
    # Getting the framework
493
    my $frameworkcode = $biblio->frameworkcode;
502
    my $frameworkcode = $biblio->frameworkcode;
494
503
(-)a/Koha/Exporter/Record.pm (-1 / +17 lines)
Lines 7-12 use MARC::File::USMARC; Link Here
7
use C4::AuthoritiesMarc;
7
use C4::AuthoritiesMarc;
8
use C4::Biblio qw( GetMarcFromKohaField );
8
use C4::Biblio qw( GetMarcFromKohaField );
9
use C4::Record;
9
use C4::Record;
10
use C4::MarcModificationTemplates;
10
use Koha::Biblios;
11
use Koha::Biblios;
11
use Koha::CsvProfiles;
12
use Koha::CsvProfiles;
12
use Koha::Logger;
13
use Koha::Logger;
Lines 166-171 sub export { Link Here
166
    my $dont_export_fields = $params->{dont_export_fields};
167
    my $dont_export_fields = $params->{dont_export_fields};
167
    my $csv_profile_id     = $params->{csv_profile_id};
168
    my $csv_profile_id     = $params->{csv_profile_id};
168
    my $output_filepath    = $params->{output_filepath};
169
    my $output_filepath    = $params->{output_filepath};
170
    my $marc_modification_template_id = $params->{marc_modification_template_id};
169
171
170
    if ( !$record_type ) {
172
    if ( !$record_type ) {
171
        Koha::Logger->get->warn("No record_type given.");
173
        Koha::Logger->get->warn("No record_type given.");
Lines 193-198 sub export { Link Here
193
                Koha::Logger->get->info($msg);
195
                Koha::Logger->get->info($msg);
194
                next;
196
                next;
195
            }
197
            }
198
199
            if ($marc_modification_template_id) {
200
                C4::MarcModificationTemplates::ModifyRecordWithTemplate($marc_modification_template_id, $record);
201
            }
202
196
            print $record->as_usmarc();
203
            print $record->as_usmarc();
197
        }
204
        }
198
    } elsif ( $format eq 'xml' ) {
205
    } elsif ( $format eq 'xml' ) {
Lines 205-210 sub export { Link Here
205
        for my $record_id (@$record_ids) {
212
        for my $record_id (@$record_ids) {
206
            my $record = _get_record_for_export( { %$params, record_id => $record_id } );
213
            my $record = _get_record_for_export( { %$params, record_id => $record_id } );
207
            next unless $record;
214
            next unless $record;
215
216
            if ($marc_modification_template_id) {
217
                C4::MarcModificationTemplates::ModifyRecordWithTemplate($marc_modification_template_id, $record);
218
            }
219
208
            print MARC::File::XML::record($record);
220
            print MARC::File::XML::record($record);
209
            print "\n";
221
            print "\n";
210
        }
222
        }
Lines 213-219 sub export { Link Here
213
    } elsif ( $format eq 'csv' ) {
225
    } elsif ( $format eq 'csv' ) {
214
        die 'There is no valid csv profile defined for this export'
226
        die 'There is no valid csv profile defined for this export'
215
            unless Koha::CsvProfiles->find($csv_profile_id);
227
            unless Koha::CsvProfiles->find($csv_profile_id);
216
        print marc2csv( $record_ids, $csv_profile_id, $itemnumbers );
228
        print marc2csv( $record_ids, $csv_profile_id, $itemnumbers, $marc_modification_template_id );
217
    }
229
    }
218
230
219
    close $fh if $output_filepath;
231
    close $fh if $output_filepath;
Lines 274-279 It will displays on STDOUT the generated file. Link Here
274
286
275
  If the format is csv, you have to define a csv_profile_id.
287
  If the format is csv, you have to define a csv_profile_id.
276
288
289
=item marc_modification_template_id
290
291
  Apply MARC modification template to records before export
292
277
=cut
293
=cut
278
294
279
=back
295
=back
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/export.tt (+22 lines)
Lines 151-156 Link Here
151
                                <input id="export_remove_fields" type="text" name="export_remove_fields" value="[% export_remove_fields | html %]" />
151
                                <input id="export_remove_fields" type="text" name="export_remove_fields" value="[% export_remove_fields | html %]" />
152
                                separate by a blank. (e.g., 100a 200 606)
152
                                separate by a blank. (e.g., 100a 200 606)
153
                            </li>
153
                            </li>
154
                            [% IF marc_modification_templates.size > 0 %]
155
                                <li>
156
                                    <label for="marc_modification_template_id">Modify records using template:</label>
157
                                    <select id="marc_modification_template_id" name="marc_modification_template_id">
158
                                        <option value=""></option>
159
                                        [% FOREACH marc_modification_template IN marc_modification_templates %]
160
                                            <option value="[% marc_modification_template.template_id | html %]">[% marc_modification_template.name | html %]</option>
161
                                        [% END %]
162
                                    </select>
163
                                </li>
164
                            [% END %]
154
                        </ol>
165
                        </ol>
155
                    </fieldset>
166
                    </fieldset>
156
167
Lines 238-243 Link Here
238
                                <input id="export_remove_fields" type="text" name="export_remove_fields" />
249
                                <input id="export_remove_fields" type="text" name="export_remove_fields" />
239
                                <div class="hint">separate by a blank. (e.g., 100a 200 606)</div>
250
                                <div class="hint">separate by a blank. (e.g., 100a 200 606)</div>
240
                            </li>
251
                            </li>
252
                            [% IF marc_modification_templates.size > 0 %]
253
                                <li>
254
                                    <label for="marc_modification_template_id">Modify records using template:</label>
255
                                    <select id="marc_modification_template_id" name="marc_modification_template_id">
256
                                        <option value=""></option>
257
                                        [% FOREACH marc_modification_template IN marc_modification_templates %]
258
                                            <option value="[% marc_modification_template.template_id | html %]">[% marc_modification_template.name | html %]</option>
259
                                        [% END %]
260
                                    </select>
261
                                </li>
262
                            [% END %]
241
                        </ol>
263
                        </ol>
242
                    </fieldset>
264
                    </fieldset>
243
265
(-)a/t/db_dependent/Exporter/Record.t (-1 / +81 lines)
Lines 18-24 Link Here
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Test::NoWarnings;
20
use Test::NoWarnings;
21
use Test::More tests => 7;
21
use Test::More tests => 10;
22
use Test::Warn;
22
use Test::Warn;
23
use t::lib::TestBuilder;
23
use t::lib::TestBuilder;
24
24
Lines 436-439 subtest '_get_record_for_export MARC field conditions' => sub { Link Here
436
    is( $record, undef, "Record condition \"not_exists(035a)\" should not match" );
436
    is( $record, undef, "Record condition \"not_exists(035a)\" should not match" );
437
};
437
};
438
438
439
subtest 'export csv with marc modification template' => sub {
440
    plan tests => 1;
441
    my $csv_content = qq{Title=$title_field\$$title_subfield|Test=999\$9};
442
    $dbh->do(q|INSERT INTO export_format(profile, description, content, csv_separator, field_separator, subfield_separator, encoding, type) VALUES (?, ?, ?, ?, ?, ?, ?, ?)|, {}, "TEST_PROFILE_Records.t", "my useless desc", $csv_content, '|', ';', ',', 'utf8', 'marc');
443
    my $csv_profile_id = $dbh->last_insert_id( undef, undef, 'export_format', undef );
444
445
    my $template_id = C4::MarcModificationTemplates::AddModificationTemplate('test exporter');
446
    C4::MarcModificationTemplates::AddModificationTemplateAction($template_id, 'add_field', 0, '999', '9', 'Foobar');
447
448
    my $generated_csv_file = '/tmp/test_export_1.csv';
449
    Koha::Exporter::Record::export({
450
        record_type     => 'bibs',
451
        record_ids      => [ $biblionumber_1 ],
452
        format          => 'csv',
453
        csv_profile_id  => $csv_profile_id,
454
        marc_modification_template_id => $template_id,
455
        output_filepath => $generated_csv_file,
456
    });
457
458
    my $expected_csv = <<EOF;
459
Title|Test
460
"$biblio_1_title"|Foobar
461
EOF
462
    my $generated_csv_content = read_file( $generated_csv_file );
463
    is( $generated_csv_content, $expected_csv, "Export CSV: Modification template should have been applied" );
464
};
465
466
subtest 'export xml with marc modification template' => sub {
467
    plan tests => 2;
468
469
    my $template_id = C4::MarcModificationTemplates::AddModificationTemplate('test exporter');
470
    C4::MarcModificationTemplates::AddModificationTemplateAction($template_id, 'add_field', 0, '999', '9', 'Foobar');
471
472
    my $generated_xml_file = '/tmp/test_export.xml';
473
    Koha::Exporter::Record::export({
474
        record_type     => 'bibs',
475
        record_ids      => [ $biblionumber_1 ],
476
        format          => 'xml',
477
        marc_modification_template_id => $template_id,
478
        output_filepath => $generated_xml_file,
479
    });
480
481
    my $generated_xml_content = read_file( $generated_xml_file );
482
    $MARC::File::XML::_load_args{BinaryEncoding} = 'utf-8';
483
    open my $fh, '<', $generated_xml_file;
484
    my $records = MARC::Batch->new( 'XML', $fh );
485
    my @records;
486
    while ( my $record = $records->next ) {
487
        push @records, $record;
488
    }
489
    is( scalar( @records ), 1, 'Export XML: 1 record should have been exported' );
490
    my $record = $records[0];
491
    is( $record->subfield('999', '9'), 'Foobar', 'Export XML: marc modification template should have added 999$9' );
492
};
493
494
subtest 'export iso2709 with marc modification template' => sub {
495
    plan tests => 2;
496
497
    my $template_id = C4::MarcModificationTemplates::AddModificationTemplate('test exporter');
498
    C4::MarcModificationTemplates::AddModificationTemplateAction($template_id, 'add_field', 0, '999', '9', 'Foobar');
499
500
    my $generated_mrc_file = '/tmp/test_export.mrc';
501
    Koha::Exporter::Record::export({
502
        record_type     => 'bibs',
503
        record_ids      => [ $biblionumber_1 ],
504
        format          => 'iso2709',
505
        marc_modification_template_id => $template_id,
506
        output_filepath => $generated_mrc_file,
507
    });
508
509
    my $records = MARC::File::USMARC->in( $generated_mrc_file );
510
    my @records;
511
    while ( my $record = $records->next ) {
512
        push @records, $record;
513
    }
514
    is( scalar( @records ), 1, 'Export ISO2709: 1 record should have been exported' );
515
    my $record = $records[0];
516
    is( $record->subfield('999', '9'), 'Foobar', 'Export ISO2709: marc modification template should have added 999$9' );
517
};
518
439
$schema->storage->txn_rollback;
519
$schema->storage->txn_rollback;
(-)a/tools/export.pl (-1 / +5 lines)
Lines 22-27 use MARC::File::XML; Link Here
22
use List::MoreUtils qw( uniq );
22
use List::MoreUtils qw( uniq );
23
use C4::Auth        qw( get_template_and_user );
23
use C4::Auth        qw( get_template_and_user );
24
use C4::Output      qw( output_html_with_http_headers );
24
use C4::Output      qw( output_html_with_http_headers );
25
use C4::MarcModificationTemplates;
25
26
26
use Koha::Authority::Types;
27
use Koha::Authority::Types;
27
use Koha::Biblioitems;
28
use Koha::Biblioitems;
Lines 235-240 if ( $op eq "cud-export" ) { Link Here
235
                csv_profile_id                 => $csv_profile_id,
236
                csv_profile_id                 => $csv_profile_id,
236
                export_items                   => ( not $dont_export_items ),
237
                export_items                   => ( not $dont_export_items ),
237
                only_export_items_for_branches => $only_export_items_for_branches,
238
                only_export_items_for_branches => $only_export_items_for_branches,
239
                marc_modification_template_id => scalar $query->param('marc_modification_template_id'),
238
            }
240
            }
239
        );
241
        );
240
    } elsif ( $record_type eq 'db' or $record_type eq 'conf' ) {
242
    } elsif ( $record_type eq 'db' or $record_type eq 'conf' ) {
Lines 316-321 if ( $op eq "cud-export" ) { Link Here
316
        $template->{VARS}->{'conffiles'} = getbackupfilelist( { directory => "$backupdir", extension => 'tar' } );
318
        $template->{VARS}->{'conffiles'} = getbackupfilelist( { directory => "$backupdir", extension => 'tar' } );
317
    }
319
    }
318
320
321
    my @marc_modification_templates = C4::MarcModificationTemplates::GetModificationTemplates();
322
319
    $template->param(
323
    $template->param(
320
        libraries            => $libraries,
324
        libraries            => $libraries,
321
        itemtypes            => $itemtypes,
325
        itemtypes            => $itemtypes,
Lines 323-328 if ( $op eq "cud-export" ) { Link Here
323
        export_remove_fields => C4::Context->preference("ExportRemoveFields"),
327
        export_remove_fields => C4::Context->preference("ExportRemoveFields"),
324
        csv_profiles => [ Koha::CsvProfiles->search( { type => 'marc', used_for => 'export_records' } )->as_list ],
328
        csv_profiles => [ Koha::CsvProfiles->search( { type => 'marc', used_for => 'export_records' } )->as_list ],
325
        messages     => \@messages,
329
        messages     => \@messages,
330
        marc_modification_templates => \@marc_modification_templates,
326
    );
331
    );
327
332
328
    output_html_with_http_headers $query, $cookie, $template->output;
333
    output_html_with_http_headers $query, $cookie, $template->output;
329
- 

Return to bug 24679