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

(-)a/C4/Biblio.pm (-9 / +26 lines)
Lines 88-93 use MARC::File::USMARC; Link Here
88
use MARC::File::XML;
88
use MARC::File::XML;
89
use POSIX qw(strftime);
89
use POSIX qw(strftime);
90
use Module::Load::Conditional qw(can_load);
90
use Module::Load::Conditional qw(can_load);
91
use JSON qw(encode_json decode_json);
91
92
92
use C4::Koha;
93
use C4::Koha;
93
use C4::Log;    # logaction
94
use C4::Log;    # logaction
Lines 107-116 use Koha::ItemTypes; Link Here
107
use Koha::SearchEngine;
108
use Koha::SearchEngine;
108
use Koha::Libraries;
109
use Koha::Libraries;
109
use Koha::Util::MARC;
110
use Koha::Util::MARC;
111
use Koha::MetadataRecord::History;
110
112
111
use vars qw($debug $cgi_debug);
113
use vars qw($debug $cgi_debug);
112
114
113
114
=head1 NAME
115
=head1 NAME
115
116
116
C4::Biblio - cataloging management functions
117
C4::Biblio - cataloging management functions
Lines 226-233 sub AddBiblio { Link Here
226
    # update MARC subfield that stores biblioitems.cn_sort
227
    # update MARC subfield that stores biblioitems.cn_sort
227
    _koha_marc_update_biblioitem_cn_sort( $record, $olddata, $frameworkcode );
228
    _koha_marc_update_biblioitem_cn_sort( $record, $olddata, $frameworkcode );
228
229
229
    # now add the record
230
    # now add the record (history parameter is undef: no history yet])
230
    ModBiblioMarc( $record, $biblionumber, $frameworkcode ) unless $defer_marc_save;
231
    ModBiblioMarc( $record, $biblionumber, $frameworkcode, undef ) unless $defer_marc_save;
231
232
232
    # update OAI-PMH sets
233
    # update OAI-PMH sets
233
    if(C4::Context->preference("OAI-PMH:AutoUpdateSets")) {
234
    if(C4::Context->preference("OAI-PMH:AutoUpdateSets")) {
Lines 300-308 sub ModBiblio { Link Here
300
    # update biblionumber and biblioitemnumber in MARC
301
    # update biblionumber and biblioitemnumber in MARC
301
    # FIXME - this is assuming a 1 to 1 relationship between
302
    # FIXME - this is assuming a 1 to 1 relationship between
302
    # biblios and biblioitems
303
    # biblios and biblioitems
303
    my $sth = $dbh->prepare("select biblioitemnumber from biblioitems where biblionumber=?");
304
    my $sth = $dbh->prepare("select biblioitemnumber,history from biblioitems where biblionumber=?");
304
    $sth->execute($biblionumber);
305
    $sth->execute($biblionumber);
305
    my ($biblioitemnumber) = $sth->fetchrow;
306
    my ($biblioitemnumber, $history) = $sth->fetchrow;
306
    $sth->finish();
307
    $sth->finish();
307
    _koha_marc_update_bib_ids( $record, $frameworkcode, $biblionumber, $biblioitemnumber );
308
    _koha_marc_update_bib_ids( $record, $frameworkcode, $biblionumber, $biblioitemnumber );
308
309
Lines 313-319 sub ModBiblio { Link Here
313
    _koha_marc_update_biblioitem_cn_sort( $record, $oldbiblio, $frameworkcode );
314
    _koha_marc_update_biblioitem_cn_sort( $record, $oldbiblio, $frameworkcode );
314
315
315
    # update the MARC record (that now contains biblio and items) with the new record data
316
    # update the MARC record (that now contains biblio and items) with the new record data
316
    &ModBiblioMarc( $record, $biblionumber, $frameworkcode );
317
    &ModBiblioMarc( $record, $biblionumber, $frameworkcode, $history );
317
318
318
    # modify the other koha tables
319
    # modify the other koha tables
319
    _koha_modify_biblio( $dbh, $oldbiblio, $frameworkcode );
320
    _koha_modify_biblio( $dbh, $oldbiblio, $frameworkcode );
Lines 3278-3285 Function exported, but should NOT be used, unless you really know what you're do Link Here
3278
3279
3279
sub ModBiblioMarc {
3280
sub ModBiblioMarc {
3280
    # pass the MARC::Record to this function, and it will create the records in
3281
    # pass the MARC::Record to this function, and it will create the records in
3281
    # the marcxml field
3282
    # the marc field
3282
    my ( $record, $biblionumber, $frameworkcode ) = @_;
3283
    my ( $record, $biblionumber, $frameworkcode, $history ) = @_;
3283
    if ( !$record ) {
3284
    if ( !$record ) {
3284
        carp 'ModBiblioMarc passed an undefined record';
3285
        carp 'ModBiblioMarc passed an undefined record';
3285
        return;
3286
        return;
Lines 3350-3356 sub ModBiblioMarc { Link Here
3350
    $m_rs->metadata( $record->as_xml_record($encoding) );
3351
    $m_rs->metadata( $record->as_xml_record($encoding) );
3351
    $m_rs->store;
3352
    $m_rs->store;
3352
3353
3353
    ModZebra( $biblionumber, "specialUpdate", "biblioserver" );
3354
    # Decode JSON history, create new record, update and re-encode to JSON.
3355
    my $historylength = 10;
3356
    eval {
3357
        $history = decode_json($history);
3358
        1;
3359
    } or do {
3360
        $history = undef;
3361
    };
3362
    my $newrecord = HistoryRecordNew($record);
3363
    my $newhistory = HistoryUpdate($newrecord, $historylength, $history);
3364
    $newhistory = encode_json($newhistory);
3365
3366
    $sth = $dbh->prepare("UPDATE biblioitems SET marc=?,marcxml=?,history=? WHERE biblionumber=?");
3367
    $sth->execute( $record->as_usmarc(), $record->as_xml_record($encoding), $newhistory, $biblionumber );
3368
    $sth->finish;
3369
3370
    ModZebra( $biblionumber, "specialUpdate", "biblioserver", $record );
3354
    return $biblionumber;
3371
    return $biblionumber;
3355
}
3372
}
3356
3373
(-)a/Koha/MetadataRecord/History.pm (+261 lines)
Line 0 Link Here
1
package Koha::MetadataRecord::History;
2
3
# Copyright 2016 Aleisha Amohia <aleisha@catalyst.net.nz>
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
use strict;
21
use warnings;
22
23
use C4::Charset;
24
use JSON qw(decode_json);
25
26
use vars qw(@ISA @EXPORT);
27
28
BEGIN {
29
    require Exporter;
30
    @ISA = qw( Exporter );
31
32
    push @EXPORT, qw(
33
        &GetMarcBiblioHistory
34
        &HistoryUpdate
35
        &HistoryParse
36
        &HistoryMapUsers
37
        &HistoryBorrowerDetails
38
        &HistoryRecordNew
39
    );
40
 }
41
42
=head1 NAME
43
44
Koha::MetadataRecord::History - tracking changes to MARC records
45
46
=head1 DESCRIPTION
47
48
Koha::MetadataRecord::History not only tracks changes to MARC records and presents them in a log, but also allows users to
49
roll them back, either in batch to a previous point in the history or separately for each field (so you can roll back changes
50
to a field without changing fields that may have been changed after this one).
51
52
=head2 GetMarcBiblioHistory
53
54
    my ($record, $history) = GetMarcBiblio($biblionumber, [$embeditems]);
55
56
Returns MARC::Record representing bib identified by C<$biblionumber>.  If no bib exists, returns undef.
57
C<$biblionumber>.  If no bib exists, returns undef.
58
C<$embeditems>.  If set to true, items data are included.
59
The MARC record contains biblio data, and items data if $embeditems is set to true.
60
61
=cut
62
63
sub GetMarcBiblioHistory {
64
    my $biblionumber = shift;
65
    my $embeditems   = shift || 0;
66
    if (not defined $biblionumber) {
67
        warn "GetMarcBiblio called with undefined biblionumber";
68
        return;
69
    }
70
    my $dbh = C4::Context->dbh;
71
    my $sth = $dbh->prepare("SELECT marcxml,history FROM biblioitems WHERE biblionumber=? ");
72
    $sth->execute($biblionumber);
73
    my $row = $sth->fetchrow_hashref;
74
    my $marcxml = StripNonXmlChars( $row->{'marcxml'} );
75
    MARC::File::XML->default_record_format( C4::Context->preference('marcflavour') );
76
    my $record = MARC::Record->new();
77
    my ($history, $borrowers) = HistoryParse( $row->{'history'} );
78
    $borrowers = HistoryBorrowerDetails( $borrowers );
79
    $history = HistoryMapUsers( $history, $borrowers );
80
    if ($marcxml) {
81
        $record = eval { MARC::Record::new_from_xml( $marcxml, "utf8", C4::Context->preference('marcflavour') ) };
82
        if ($@) { warn " problem with :$biblionumber : $@ \n$marcxml"; }
83
            return unless $record;
84
            C4::Biblio::_koha_marc_update_bib_ids($record, '', $biblionumber, $biblionumber);
85
            C4::Biblio::EmbedItemsInMarcBiblio($record, $biblionumber) if ($embeditems);
86
            return ($record, $history);
87
    } else {
88
        return;
89
    }
90
}
91
92
=head2 HistoryParse
93
94
Input format (JSON): [ { datetime, borrowernumber, marcxml } ]
95
Output format: { tag } { subfield } [ { borrowernumber, data, datetime } ]
96
97
=cut
98
99
sub HistoryParse {
100
    my $json = shift;
101
    if (!defined $json) {
102
        warn "HistoryParse: Missing JSON parameter";
103
        return undef;
104
    }
105
    my $input = decode_json($json);
106
    # Reindex the dataset; order all values by datetime under each tag name.
107
    my $borrowers = { };
108
    my $output = { };
109
    for my $item (@{$input}) {
110
        my $datetime = $item->{datetime};
111
        $borrowers->{ $item->{borrowernumber }} = undef;
112
        my $record = MARC::File::XML::decode($item->{marcxml});
113
        for my $field ($record->fields()) {
114
            my $tag = $field->tag();
115
            $output->{$tag} = {} unless defined $output->{$tag};
116
            for my $subfield ($field->subfields()) {
117
                # Build hash ref structure.
118
                my $subfield_tag = @{$subfield}[0];
119
                $output->{$tag}->{$subfield_tag} = []
120
                unless defined $output->{$tag}->{$subfield_tag};
121
                push( @{ $output->{$tag}->{$subfield_tag} }, HistoryParsedRecordNew(
122
                    @{ $subfield }[1], $item->{borrowernumber}, $datetime));
123
                }
124
            }
125
        }
126
    return ( $output, $borrowers );
127
}
128
129
=head2 HistoryBorrowerDetails
130
131
Take a hashref with borrowernumbers as keys, returns new hash with the
132
borrowernumber as key to hash containing firstname, surname and title.
133
134
=cut
135
136
sub HistoryBorrowerDetails {
137
    my $borrowernumbers = shift;
138
    my $dbh = C4::Context->dbh;
139
    my $output = { };
140
    my @numbers = keys %{$borrowernumbers};
141
    if (@numbers > 0) {
142
        # We now need to look up the numeric user id's and map to name and title strings.
143
        my $sth = $dbh->prepare(qq{SELECT
144
            borrowernumber, title, firstname, surname
145
            FROM borrowers
146
            WHERE borrowernumber IN}
147
            . '(' . join(',', ('?') x @numbers) . ')');
148
        $sth->execute(@numbers)
149
            or die ('BorrowerDetails: Unable to execute SELECT statement');
150
        while (my $row = $sth->fetchrow_hashref()) {
151
            $output->{ $row->{borrowernumber} } = { };
152
            $output->{ $row->{borrowernumber} }->{title}     = $row->{title};
153
            $output->{ $row->{borrowernumber} }->{firstname} = $row->{firstname};
154
            $output->{ $row->{borrowernumber} }->{surname}   = $row->{surname};
155
        }
156
        $sth->finish();
157
    }
158
    return $output;
159
}
160
161
=head2 HistoryMapUsers
162
163
=cut
164
165
sub HistoryMapUsers {
166
    my ($history, $borrowers) = @_;
167
    my $mappedhistory = { };
168
    for my $tag (keys %{ $history }) {
169
        $mappedhistory->{ $tag } = { };
170
        for my $field (keys %{ $history->{ $tag } }) {
171
            $mappedhistory->{ $tag }->{ $field } = [ ];
172
            for my $entry (@{ $history->{ $tag }->{ $field } }) {
173
                # C4::Dates does not allow me to print time, only dates.
174
                #my $newtime = C4::Dates->new( $history->{ $tag }->{ $field }->{ $time }->{timestamp}, 'iso' )->output('syspref');
175
                my $newitem = { };
176
                my $user = $borrowers->{ $entry->{borrowernumber} };
177
178
                # Maybe change this to work on original instead of copy?
179
                $newitem->{title}          = $user->{title};
180
                $newitem->{firstname}      = $user->{firstname};
181
                $newitem->{surname}        = $user->{surname};
182
                $newitem->{borrowernumber} = $entry->{borrowernumber};
183
                $newitem->{data}           = $entry->{data};
184
                $newitem->{datetime}       = $entry->{datetime};
185
186
                push ( @{ $mappedhistory->{ $tag }->{ $field } } , $newitem);
187
            }
188
        }
189
    }
190
    return $mappedhistory;
191
}
192
193
=head2 HistoryParsedRecordNew
194
195
=cut
196
197
sub HistoryParsedRecordNew {
198
    my ($value, $borrowernumber, $datetime) = @_;
199
    my $newrecord = {
200
        data           => $value,
201
        borrowernumber => $borrowernumber,
202
        datetime       => $datetime,
203
    };
204
    return $newrecord;
205
}
206
207
=head2 HistoryRecordNew
208
209
=cut
210
211
sub HistoryRecordNew {
212
    my ( $record ) = @_;
213
    my $encoding = C4::Context->preference("marcflavour");
214
    my $hash_record = {
215
        borrowernumber => C4::Context->userenv()->{number},
216
        datetime       => POSIX::strftime('%Y-%m-%d %T', localtime(time)),
217
        marcxml        => $record->as_xml_record($encoding),
218
    };
219
    return $hash_record;
220
}
221
222
=head2 HistoryUpdate
223
224
=cut
225
226
sub HistoryUpdate {
227
    my ( $record, $limit, $collection ) = @_;
228
    if ( !$record ) {
229
        warn "HistoryUpdate: Called with undefined record";
230
        return;
231
    }
232
    if ( ref($record) ne 'HASH' ) {
233
        warn "HistoryUpdate: Record parameter is not a hashref";
234
        return;
235
    }
236
    $limit = 10 unless defined $limit;
237
    # We need something to append history to, even if there is none.
238
    $collection = [] unless defined $collection;
239
    # Truncate history to $histsize - 1, need space for new entry.
240
    splice(@{$collection}, $limit - 1);
241
    push(@{$collection}, $record);
242
    # Sort the resulting array, newest entries first.
243
    # Removed; used to be integer timestamps. Is now MySQL date strings.
244
    #@{$collection} = sort { $a->{timestamp} < $b->{timestamp} } @{$collection};
245
    @{$collection} = sort { $a->{datetime} lt $b->{datetime} } @{$collection};
246
    return $collection;
247
}
248
249
1;
250
251
__END__
252
253
=head1 AUTHOR
254
255
Koha Development Team <http://koha-community.org/>
256
257
Paul POULAIN paul.poulain@free.fr
258
259
Joshua Ferraro jmf@liblime.com
260
261
=cut
(-)a/cataloguing/addbiblio.pl (-19 / +30 lines)
Lines 28-34 use C4::Biblio; Link Here
28
use C4::Search;
28
use C4::Search;
29
use C4::AuthoritiesMarc;
29
use C4::AuthoritiesMarc;
30
use C4::Context;
30
use C4::Context;
31
use MARC::Record;
32
use C4::Log;
31
use C4::Log;
33
use C4::Koha;
32
use C4::Koha;
34
use C4::ClassSource;
33
use C4::ClassSource;
Lines 36-48 use C4::ImportBatch; Link Here
36
use C4::Charset;
35
use C4::Charset;
37
use Koha::BiblioFrameworks;
36
use Koha::BiblioFrameworks;
38
use Koha::DateUtils;
37
use Koha::DateUtils;
39
40
use Koha::ItemTypes;
38
use Koha::ItemTypes;
39
use Koha::MetadataRecord::History;
41
use Koha::Libraries;
40
use Koha::Libraries;
42
43
use Koha::BiblioFrameworks;
44
45
use Date::Calc qw(Today);
41
use Date::Calc qw(Today);
42
use MARC::Record;
46
use MARC::File::USMARC;
43
use MARC::File::USMARC;
47
use MARC::File::XML;
44
use MARC::File::XML;
48
use URI::Escape;
45
use URI::Escape;
Lines 271-280 sub GetMandatoryFieldZ3950 { Link Here
271
=cut
268
=cut
272
269
273
sub create_input {
270
sub create_input {
274
    my ( $tag, $subfield, $value, $index_tag, $tabloop, $rec, $authorised_values_sth,$cgi ) = @_;
271
275
    
272
    my ( $tag, $subfield, $value, $index_tag, $tabloop, $rec, $authorised_values_sth,$cgi, $history ) = @_;
273
276
    my $index_subfield = CreateKey(); # create a specifique key for each subfield
274
    my $index_subfield = CreateKey(); # create a specifique key for each subfield
277
275
276
    my $taghistory = $history->{$tag}->{$subfield};
277
278
    $value =~ s/"/&quot;/g;
279
278
    # if there is no value provided but a default value in parameters, get it
280
    # if there is no value provided but a default value in parameters, get it
279
    if ( $value eq '' ) {
281
    if ( $value eq '' ) {
280
        $value = $tagslib->{$tag}->{$subfield}->{defaultvalue} // q{};
282
        $value = $tagslib->{$tag}->{$subfield}->{defaultvalue} // q{};
Lines 290-296 sub create_input { Link Here
290
        # And <<USER>> with surname (?)
292
        # And <<USER>> with surname (?)
291
        my $username=(C4::Context->userenv?C4::Context->userenv->{'surname'}:"superlibrarian");
293
        my $username=(C4::Context->userenv?C4::Context->userenv->{'surname'}:"superlibrarian");
292
        $value=~s/<<USER>>/$username/g;
294
        $value=~s/<<USER>>/$username/g;
293
    
295
294
    }
296
    }
295
    my $dbh = C4::Context->dbh;
297
    my $dbh = C4::Context->dbh;
296
298
Lines 312-317 sub create_input { Link Here
312
        value          => $value,
314
        value          => $value,
313
        maxlength      => $tagslib->{$tag}->{$subfield}->{maxlength},
315
        maxlength      => $tagslib->{$tag}->{$subfield}->{maxlength},
314
        random         => CreateKey(),
316
        random         => CreateKey(),
317
        history        => $taghistory,
315
    );
318
    );
316
319
317
    if(exists $mandatory_z3950->{$tag.$subfield}){
320
    if(exists $mandatory_z3950->{$tag.$subfield}){
Lines 367-372 sub create_input { Link Here
367
            maxlength => $subfield_data{maxlength},
370
            maxlength => $subfield_data{maxlength},
368
            readonly  => ($is_readonly) ? 1 : 0,
371
            readonly  => ($is_readonly) ? 1 : 0,
369
            authtype  => $tagslib->{$tag}->{$subfield}->{authtypecode},
372
            authtype  => $tagslib->{$tag}->{$subfield}->{authtypecode},
373
            history   => $taghistory,
370
        };
374
        };
371
375
372
    # it's a plugin field
376
    # it's a plugin field
Lines 388-393 sub create_input { Link Here
388
                maxlength      => $subfield_data{maxlength},
392
                maxlength      => $subfield_data{maxlength},
389
                javascript     => $plugin->javascript,
393
                javascript     => $plugin->javascript,
390
                noclick        => $plugin->noclick,
394
                noclick        => $plugin->noclick,
395
                history        => $taghistory,
391
            };
396
            };
392
        } else {
397
        } else {
393
            warn $plugin->errstr;
398
            warn $plugin->errstr;
Lines 400-405 sub create_input { Link Here
400
                size      => 67,
405
                size      => 67,
401
                maxlength => $subfield_data{maxlength},
406
                maxlength => $subfield_data{maxlength},
402
                readonly  => 0,
407
                readonly  => 0,
408
                history   => $taghistory,
403
            };
409
            };
404
        }
410
        }
405
411
Lines 412-417 sub create_input { Link Here
412
            value     => $value,
418
            value     => $value,
413
            size      => 67,
419
            size      => 67,
414
            maxlength => $subfield_data{maxlength},
420
            maxlength => $subfield_data{maxlength},
421
            history   => $taghistory,
415
        };
422
        };
416
423
417
    }
424
    }
Lines 432-437 sub create_input { Link Here
432
                id        => $subfield_data{id},
439
                id        => $subfield_data{id},
433
                name      => $subfield_data{id},
440
                name      => $subfield_data{id},
434
                value     => $value,
441
                value     => $value,
442
                history   => $taghistory,
435
            };
443
            };
436
444
437
        }
445
        }
Lines 444-449 sub create_input { Link Here
444
                size      => 67,
452
                size      => 67,
445
                maxlength => $subfield_data{maxlength},
453
                maxlength => $subfield_data{maxlength},
446
                readonly  => 0,
454
                readonly  => 0,
455
                history   => $taghistory,
447
            };
456
            };
448
457
449
        }
458
        }
Lines 469-479 sub format_indicator { Link Here
469
}
478
}
470
479
471
sub build_tabs {
480
sub build_tabs {
472
    my ( $template, $record, $dbh, $encoding,$input ) = @_;
481
    my ( $template, $record, $dbh, $encoding, $input, $history ) = @_;
473
482
474
    # fill arrays
483
    # fill arrays
475
    my @loop_data = ();
484
    my @loop_data = ();
476
    my $tag;
485
    #my $tag;
477
486
478
    my $branch_limit = C4::Context->userenv ? C4::Context->userenv->{"branch"} : "";
487
    my $branch_limit = C4::Context->userenv ? C4::Context->userenv->{"branch"} : "";
479
    my $query = "SELECT authorised_value, lib
488
    my $query = "SELECT authorised_value, lib
Lines 529-535 sub build_tabs { Link Here
529
		}
538
		}
530
		# loop through each field
539
		# loop through each field
531
                foreach my $field (@fields) {
540
                foreach my $field (@fields) {
532
                    
533
                    my @subfields_data;
541
                    my @subfields_data;
534
                    if ( $tag < 10 ) {
542
                    if ( $tag < 10 ) {
535
                        my ( $value, $subfield );
543
                        my ( $value, $subfield );
Lines 549-555 sub build_tabs { Link Here
549
                            @subfields_data,
557
                            @subfields_data,
550
                            &create_input(
558
                            &create_input(
551
                                $tag, $subfield, $value, $index_tag, $tabloop, $record,
559
                                $tag, $subfield, $value, $index_tag, $tabloop, $record,
552
                                $authorised_values_sth,$input
560
                                $authorised_values_sth,$input, $history
553
                            )
561
                            )
554
                        );
562
                        );
555
                    }
563
                    }
Lines 564-570 sub build_tabs { Link Here
564
                                @subfields_data,
572
                                @subfields_data,
565
                                &create_input(
573
                                &create_input(
566
                                    $tag, $subfield, $value, $index_tag, $tabloop,
574
                                    $tag, $subfield, $value, $index_tag, $tabloop,
567
                                    $record, $authorised_values_sth,$input
575
                                    $record, $authorised_values_sth,$input, $history
568
                                )
576
                                )
569
                            );
577
                            );
570
                        }
578
                        }
Lines 592-598 sub build_tabs { Link Here
592
                            @subfields_data,
600
                            @subfields_data,
593
                            &create_input(
601
                            &create_input(
594
                                $tag, $subfield, '', $index_tag, $tabloop, $record,
602
                                $tag, $subfield, '', $index_tag, $tabloop, $record,
595
                                $authorised_values_sth,$input
603
                                $authorised_values_sth,$input, $history
596
                            )
604
                            )
597
                        );
605
                        );
598
                    }
606
                    }
Lines 638-648 sub build_tabs { Link Here
638
                           # always include in the form regardless of the hidden setting - bug 2206
646
                           # always include in the form regardless of the hidden setting - bug 2206
639
                    next
647
                    next
640
                      if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
648
                      if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
649
641
			push(
650
			push(
642
                        @subfields_data,
651
                        @subfields_data,
643
                        &create_input(
652
                        &create_input(
644
                            $tag, $subfield, '', $index_tag, $tabloop, $record,
653
                            $tag, $subfield, '', $index_tag, $tabloop, $record,
645
                            $authorised_values_sth,$input
654
                            $authorised_values_sth,$input, $history
646
                        )
655
                        )
647
                    );
656
                    );
648
                }
657
                }
Lines 659-665 sub build_tabs { Link Here
659
                        tagfirstsubfield => $subfields_data[0],
668
                        tagfirstsubfield => $subfields_data[0],
660
                        fixedfield       => $tag < 10?1:0,
669
                        fixedfield       => $tag < 10?1:0,
661
                    );
670
                    );
662
                    
671
663
                    push @loop_data, \%tag_data ;
672
                    push @loop_data, \%tag_data ;
664
                }
673
                }
665
            }
674
            }
Lines 762-767 $mandatory_z3950 = GetMandatoryFieldZ3950($frameworkcode); Link Here
762
# -- Global
771
# -- Global
763
772
764
my $record   = -1;
773
my $record   = -1;
774
my $history  = undef;
765
my $encoding = "";
775
my $encoding = "";
766
my (
776
my (
767
	$biblionumbertagfield,
777
	$biblionumbertagfield,
Lines 772-778 my ( Link Here
772
);
782
);
773
783
774
if (($biblionumber) && !($breedingid)){
784
if (($biblionumber) && !($breedingid)){
775
    $record = GetMarcBiblio({ biblionumber => $biblionumber });
785
    ($record, $history) = GetMarcBiblioHistory($biblionumber);
776
}
786
}
777
if ($breedingid) {
787
if ($breedingid) {
778
    ( $record, $encoding ) = MARCfindbreeding( $breedingid ) ;
788
    ( $record, $encoding ) = MARCfindbreeding( $breedingid ) ;
Lines 901-907 if ( $op eq "addbiblio" ) { Link Here
901
        }
911
        }
902
    } else {
912
    } else {
903
    # it may be a duplicate, warn the user and do nothing
913
    # it may be a duplicate, warn the user and do nothing
904
        build_tabs ($template, $record, $dbh,$encoding,$input);
914
        build_tabs ($template, $record, $dbh,$encoding, $input, $history);
905
        $template->param(
915
        $template->param(
906
            biblionumber             => $biblionumber,
916
            biblionumber             => $biblionumber,
907
            biblioitemnumber         => $biblioitemnumber,
917
            biblioitemnumber         => $biblioitemnumber,
Lines 948-954 elsif ( $op eq "delete" ) { Link Here
948
            $record = $urecord;
958
            $record = $urecord;
949
        };
959
        };
950
    }
960
    }
951
    build_tabs( $template, $record, $dbh, $encoding,$input );
961
962
    build_tabs( $template, $record, $dbh, $encoding, $input, $history );
952
    $template->param(
963
    $template->param(
953
        biblionumber             => $biblionumber,
964
        biblionumber             => $biblionumber,
954
        biblionumbertagfield        => $biblionumbertagfield,
965
        biblionumbertagfield        => $biblionumbertagfield,
(-)a/installer/data/mysql/atomicupdate/add_history.sql (+1 lines)
Line 0 Link Here
1
ALTER TABLE biblioitems ADD COLUMN `history` LONGTEXT DEFAULT NULL AFTER `marcxml`;
(-)a/koha-tmpl/intranet-tmpl/prog/css/addbiblio.css (+21 lines)
Lines 25-30 div#toolbar { Link Here
25
	text-decoration : none;
25
	text-decoration : none;
26
}
26
}
27
27
28
.buttonHistory {
29
	font-weight : bold;
30
	text-decoration : none;
31
}
32
28
a.expandfield {
33
a.expandfield {
29
	text-decoration : none;
34
	text-decoration : none;
30
}
35
}
Lines 136-141 a.tagnum { Link Here
136
.linktools a:hover { background-color: #FFC; }
141
.linktools a:hover { background-color: #FFC; }
137
.subfield_controls { margin : 0 .5em; }
142
.subfield_controls { margin : 0 .5em; }
138
.readonly { border-width : 1px; border-style: inset; padding-left : 15px; background: #EEE url(../img/locked.png) center left no-repeat; width:29em; }
143
.readonly { border-width : 1px; border-style: inset; padding-left : 15px; background: #EEE url(../img/locked.png) center left no-repeat; width:29em; }
144
.subfield_history { margin : 0 .5em; float: right; }
139
145
140
#cataloguing_additem_itemlist {
146
#cataloguing_additem_itemlist {
141
	margin-bottom : 1em;
147
	margin-bottom : 1em;
Lines 164-169 tbody tr.active td { Link Here
164
    width: 100%;
170
    width: 100%;
165
    z-index: 1000;
171
    z-index: 1000;
166
}
172
}
173
167
#loading div {
174
#loading div {
168
    background : transparent url(../img/loading.gif) top left no-repeat;
175
    background : transparent url(../img/loading.gif) top left no-repeat;
169
    font-size : 175%;
176
    font-size : 175%;
Lines 289-291 tbody tr.active td { Link Here
289
        width: 16em;
296
        width: 16em;
290
    }
297
    }
291
}
298
}
299
300
.history_container {
301
    display: none;
302
}
303
304
.history_table {
305
    width: 100%;
306
    display: none;
307
    float: right;
308
    clear: both;
309
    font-size: 75%;
310
    width: 75%;
311
    margin: 0.3em;
312
}
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/addbiblio.tt (-4 / +32 lines)
Lines 18-23 Link Here
18
    var Sticky;
18
    var Sticky;
19
	 $(document).ready(function() {
19
	 $(document).ready(function() {
20
20
21
        $(".input_marceditor").click(function(this){
22
            historyToggle($(this).attr('id') + '_history');
23
            return false;
24
        });
25
26
        $(".tag_editor").click(function(this){
27
            openAuth(this.parentNode.parentNode.getElementsByTagName('input')[1].id, $(this).attr('id'), 'biblio');
28
            return false;
29
        });
30
21
        [% IF bib_doesnt_exist %]
31
        [% IF bib_doesnt_exist %]
22
            $("#addbibliotabs").hide();
32
            $("#addbibliotabs").hide();
23
            $("#toolbar").hide();
33
            $("#toolbar").hide();
Lines 728-737 function Changefwk() { Link Here
728
                    [% IF ( mv.readonly == 1 ) %]
738
                    [% IF ( mv.readonly == 1 ) %]
729
                    <input type="text" id="[%- mv.id | html -%]" name="[%- mv.name | html -%]" value="[%- mv.value | html -%]" class="input_marceditor readonly" tabindex="1" size="[%- mv.size | html -%]" maxlength="[%- mv.maxlength | html -%]" readonly="readonly" />
739
                    <input type="text" id="[%- mv.id | html -%]" name="[%- mv.name | html -%]" value="[%- mv.value | html -%]" class="input_marceditor readonly" tabindex="1" size="[%- mv.size | html -%]" maxlength="[%- mv.maxlength | html -%]" readonly="readonly" />
730
                    [% ELSE %]
740
                    [% ELSE %]
731
                    <input type="text" id="[%- mv.id | html -%]" name="[%- mv.name | html -%]" value="[%- mv.value | html -%]" class="input_marceditor" tabindex="1" size="[%- mv.size | html -%]" maxlength="[%- mv.maxlength | html -%]" />
741
                    <input type="text" id="[%- mv.id | html -%]" name="[%- mv.name | html -%]" value="[%- mv.value | html -%]" class="input_marceditor" tabindex="1" size="[%- mv.size | html -%]" maxlength="[%- mv.maxlength | html -%]">
732
                    [% END %]
742
                    [% END %]
743
733
                    [% IF ( mv.authtype ) %]
744
                    [% IF ( mv.authtype ) %]
734
                    <span class="subfield_controls"><a href="#" class="buttonDot tag_editor" onclick="openAuth(this.parentNode.parentNode.getElementsByTagName('input')[1].id,'[%- mv.authtype | html -%]','biblio'); return false;" tabindex="1" title="Tag editor">Tag editor</a></span>
745
                    <span class="subfield_controls"><a id="[%- mv.authtype | html %]" href="#" class="buttonDot tag_editor" tabindex="1" title="Tag editor">Tag editor</a></span>
735
                    [% END %]
746
                    [% END %]
736
                [% ELSIF ( mv.type == 'text_complex' ) %]
747
                [% ELSIF ( mv.type == 'text_complex' ) %]
737
                    <input type="text" id="[%- mv.id | html -%]" name="[%- mv.name | html -%]" value="[%- mv.value | html -%]" class="input_marceditor framework_plugin" tabindex="1" size="[%- mv.size | html -%]" maxlength="[%- mv.maxlength | html -%]" />
748
                    <input type="text" id="[%- mv.id | html -%]" name="[%- mv.name | html -%]" value="[%- mv.value | html -%]" class="input_marceditor framework_plugin" tabindex="1" size="[%- mv.size | html -%]" maxlength="[%- mv.maxlength | html -%]" />
Lines 739-745 function Changefwk() { Link Here
739
                        [% IF mv.noclick %]
750
                        [% IF mv.noclick %]
740
                            <a href="#" class="buttonDot tag_editor disabled" tabindex="-1" title="No popup"></a>
751
                            <a href="#" class="buttonDot tag_editor disabled" tabindex="-1" title="No popup"></a>
741
                        [% ELSE %]
752
                        [% ELSE %]
742
                            <a href="#" id="buttonDot_[% mv.id | html %]" class="buttonDot tag_editor framework_plugin" tabindex="1" title="Tag editor">Tag editor</a>
753
                            <a href="#" id="buttonDot_[% mv.id | html %]" class="buttonDot tag_editor framework_plugin" tabindex="2" title="Tag editor">Tag editor</a>
743
                        [% END %]
754
                        [% END %]
744
                    </span>
755
                    </span>
745
                    [% mv.javascript | $raw %]
756
                    [% mv.javascript | $raw %]
Lines 769-776 function Changefwk() { Link Here
769
                    </a>
780
                    </a>
770
                [% END %]
781
                [% END %]
771
                </span>
782
                </span>
772
                
783
784
                [% IF ( subfield_loo.history ) %]
785
                <span class="subfield_history">
786
                    <a href="#" class="buttonHistory" onclick="HistoryToggle('[% mv.id %]_history'); return false;">
787
                        <img src="[% interface %]/[% theme %]/img/icon-history.png" alt="History" title="Display history for this field" />
788
                    </a>
789
                </span>
790
791
                <table class="history_table" id="[%- mv.id -%]_history" name="[%- mv.name -%]_history">
792
                    <tr><th>Value</th><th>Date</th><th>User</th></tr>
793
                    [% FOREACH hist IN subfield_loo.history %]
794
						<tr><td><a href="#" onclick="HistoryRollback('[% mv.id %]', '[% hist.data %]'); return false;">[% hist.data %]</a></td>
795
                        <td>[% hist.datetime %]</td>
796
                        <td>[% hist.title %] [% hist.firstname %] [% hist.surname %]</td></tr>
797
                    [% END %]
798
                </table>
799
                [% END %]
773
            </div>
800
            </div>
801
774
            <!-- End of the line -->
802
            <!-- End of the line -->
775
        [% END %]
803
        [% END %]
776
804
(-)a/koha-tmpl/intranet-tmpl/prog/js/cataloging.js (+10 lines)
Lines 55-60 function openAuth(tagsubfieldid,authtype,source) { Link Here
55
    newin=window.open("../authorities/auth_finder.pl?source="+source+"&authtypecode="+authtype+"&index="+tagsubfieldid+"&value_mainstr="+encodeURI(mainmainstring)+"&value_main="+encodeURI(mainstring), "_blank",'width=700,height=550,toolbar=false,scrollbars=yes');
55
    newin=window.open("../authorities/auth_finder.pl?source="+source+"&authtypecode="+authtype+"&index="+tagsubfieldid+"&value_mainstr="+encodeURI(mainmainstring)+"&value_main="+encodeURI(mainstring), "_blank",'width=700,height=550,toolbar=false,scrollbars=yes');
56
}
56
}
57
57
58
function HistoryToggle (tagid)
59
{
60
	$('#' + tagid).toggle();
61
}
62
63
function HistoryRollback (tagid, value)
64
{
65
	$('#' + tagid).val(value);
66
}
67
58
function ExpandField(index) {
68
function ExpandField(index) {
59
    var original = document.getElementById(index); //original <div>
69
    var original = document.getElementById(index); //original <div>
60
    var divs = original.getElementsByTagName('div');
70
    var divs = original.getElementsByTagName('div');
(-)a/t/db_dependent/BiblioHistory.t (-1 / +256 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
use Modern::Perl;
4
use t::lib::TestBuilder;
5
use t::lib::Mocks;
6
use Test::More tests => 6;
7
use POSIX qw(strftime);
8
9
use MARC::Record;
10
use MARC::Field;
11
use JSON qw(encode_json);
12
13
use Koha::MetadataRecord::History;
14
15
BEGIN {
16
    use_ok('C4::Biblio');
17
}
18
19
our $dbh = C4::Context->dbh;
20
$dbh->{AutoCommit} = 0;
21
$dbh->{RaiseError} = 0;
22
23
$dbh->do(q|DELETE FROM issues|);
24
$dbh->do(q|DELETE FROM items|);
25
$dbh->do(q|DELETE FROM borrowers|);
26
$dbh->do(q|DELETE FROM branches|);
27
$dbh->do(q|DELETE FROM categories|);
28
$dbh->do(q|DELETE FROM biblioitems|);
29
30
my $builder = t::lib::TestBuilder->new();
31
32
my $branch = $builder->build(
33
    {
34
        source => 'Branch',
35
    }
36
);
37
38
my $category = $builder->build(
39
    {
40
        source => 'Category',
41
    }
42
);
43
44
my $patron1 = $builder->build(
45
    {
46
        source => 'Borrower',
47
        value  => {
48
            title        => 'Mr/Mrs',
49
            firstname    => 'First name',
50
            surname      => 'Surname',
51
            categorycode => $category->{categorycode},
52
            branchcode   => $branch->{branchcode},
53
        },
54
    }
55
);
56
57
my $patron2 = $builder->build(
58
    {
59
        source => 'Borrower',
60
        value  => {
61
            title        => 'Mr/Mrs 2',
62
            firstname    => 'First name 2',
63
            surname      => 'Surname 2',
64
            categorycode => $category->{categorycode},
65
            branchcode   => $branch->{branchcode},
66
        },
67
    }
68
);
69
70
my $biblio = $builder->build(
71
    {
72
        source => 'Biblio',
73
        value  => {
74
            branchcode => $branch->{branchcode},
75
        },
76
    }
77
);
78
79
C4::Context->_new_userenv('DUMMY_SESSION_ID');
80
C4::Context->set_userenv( $patron1->{borrowernumber},
81
    $patron1->{userid}, 'usercnum', $patron1->{firstname}, $patron1->{surname},
82
    $branch->{branchcode}, 'My library', 0 );
83
84
# Item with no pre-existing history backlog.
85
my $record1 = MARC::Record->new();
86
my $field1  = MARC::Field->new(
87
    245, '1', '0',
88
    'a' => 'First Title',
89
    'c' => 'Some dude'
90
);
91
$record1->append_fields($field1);
92
93
my $record2 = MARC::Record->new();
94
my $field2  = MARC::Field->new(
95
    245, '1', '0',
96
    'a' => 'Second Title',
97
    'c' => 'Former dude'
98
);
99
$record2->append_fields($field2);
100
101
my $biblioitem1 = $builder->build(
102
    {
103
        source => 'Biblioitem',
104
        value  => {
105
            marcxml => $record1->as_xml(),
106
            history => undef,
107
        }
108
    }
109
);
110
111
# items{tagname}{subfield}{timestamp}{borrowernumber|timestamp|marcxml}
112
my $items = [
113
    {
114
        borrowernumber => $patron1->{borrowernumber},
115
        datetime       => '2012-08-12 12:00:10',
116
        marcxml        => $record1->as_xml(),
117
    },
118
    {
119
        borrowernumber => $patron2->{borrowernumber},
120
        datetime       => '2012-08-12 12:00:10',
121
        marcxml        => $record2->as_xml(),
122
    }
123
];
124
125
# Item with previous history entries.
126
my $biblioitem2 = $builder->build(
127
    {
128
        source => 'Biblioitem',
129
        value  => {
130
            marcxml => $record2->as_xml(),
131
            history => encode_json($items),
132
        }
133
    }
134
);
135
136
my $expected_history = {
137
    245 => {
138
        a => [
139
            {
140
                borrowernumber => $patron1->{borrowernumber},
141
                data           => 'First Title',
142
                datetime       => '2012-08-12 12:00:10'
143
            },
144
            {
145
                borrowernumber => $patron2->{borrowernumber},
146
                data           => 'Second Title',
147
                datetime       => '2012-08-12 12:00:10'
148
            },
149
        ],
150
        c => [
151
            {
152
                borrowernumber => $patron1->{borrowernumber},
153
                data           => 'Some dude',
154
                datetime       => '2012-08-12 12:00:10'
155
            },
156
            {
157
                borrowernumber => $patron2->{borrowernumber},
158
                data           => 'Former dude',
159
                datetime       => '2012-08-12 12:00:10'
160
            },
161
        ],
162
    },
163
};
164
165
my $expected_users = {
166
    $patron1->{borrowernumber} => undef,
167
    $patron2->{borrowernumber} => undef,
168
};
169
170
# Tests the decoding and reindexing of the data, without any borrower
171
# information implanted into the history itself.
172
173
my ( $history, $users ) = C4::Biblio::HistoryParse( encode_json($items) );
174
175
is_deeply( $history, $expected_history, 'HistoryParse: Output' );
176
is_deeply( $users,   $expected_users,   'HistoryParse: Users' );
177
178
# Tests the extraction of full borrower details based on the borrowernumber.
179
my $expected_details = {
180
    $patron1->{borrowernumber} => {
181
        firstname => $patron1->{firstname},
182
        surname   => $patron1->{surname},
183
        title     => $patron1->{title},
184
    },
185
    $patron2->{borrowernumber} => {
186
        firstname => $patron2->{firstname},
187
        surname   => $patron2->{surname},
188
        title     => $patron2->{title},
189
    }
190
};
191
192
my $details = HistoryBorrowerDetails($expected_users);
193
is_deeply( $details, $expected_details, 'HistoryBorrowerDetails: Output' );
194
195
{
196
    my $expected_mapped_history = {
197
        245 => {
198
            a => [
199
                {
200
                    borrowernumber => $patron1->{borrowernumber},
201
                    data           => 'First Title',
202
                    firstname      => $patron1->{firstname},
203
                    surname        => $patron1->{surname},
204
                    title          => $patron1->{title},
205
                    datetime       => '2012-08-12 12:00:10'
206
                },
207
                {
208
                    borrowernumber => $patron2->{borrowernumber},
209
                    data           => 'Second Title',
210
                    firstname      => $patron2->{firstname},
211
                    surname        => $patron2->{surname},
212
                    title          => $patron2->{title},
213
                    datetime       => '2012-08-12 12:00:10'
214
                },
215
            ],
216
            c => [
217
                {
218
                    borrowernumber => $patron1->{borrowernumber},
219
                    data           => 'Some dude',
220
                    firstname      => $patron1->{firstname},
221
                    surname        => $patron1->{surname},
222
                    title          => $patron1->{title},
223
                    datetime       => '2012-08-12 12:00:10'
224
                },
225
                {
226
                    borrowernumber => $patron2->{borrowernumber},
227
                    data           => 'Former dude',
228
                    firstname      => $patron2->{firstname},
229
                    surname        => $patron2->{surname},
230
                    title          => $patron2->{title},
231
                    datetime       => '2012-08-12 12:00:10'
232
                },
233
            ]
234
        }
235
    };
236
237
    $history = C4::Biblio::HistoryMapUsers( $expected_history, $details );
238
239
    is_deeply( $history, $expected_mapped_history, 'HistoryMapUsers: Output' );
240
241
  #my $record;
242
  #( $record, $history ) = GetMarcBiblioHistory( $biblioitem2->{biblionumber} );
243
244
    #is_deeply( $record, $record2 );
245
    #is_deeply( $history, $expected_mapped_history );
246
}
247
248
# The C4::Context will provide the expected user borrowernumber.
249
my $record = HistoryRecordNew($record1);
250
251
my $expected_record = {
252
    borrowernumber => C4::Context->userenv()->{number},
253
    marcxml        => $record1->as_xml_record(),
254
    datetime       => POSIX::strftime( '%Y-%m-%d %T', localtime ),
255
};
256
is_deeply( $record, $expected_record );

Return to bug 14367