Koha/koha-tmpl/intranet-tmpl/prog/en/js/merge-record.js
Fridolyn SOMERS 7a5eac3a51 Bug 10648 - In records merge greatest field can not be added
When merging 2 records (/cgi-bin/koha/cataloguing/merge.pl), the destination record is build using the fields and subfields checked in source records.
When a field is checked, the javascript code searches in destination record a field with a greater tag number to insert new field before.
When the new field tag number is greater than all existing field tag numbers, the field is not added.

This patch corrects this by adding at end if no greater field tag number exists. Also adds a sort of fields by tag number because all mergo code is based on this.

Test plan :
- Add to a framework a repeatable field with the greater non existing tag number. For example 998.
- Edit 2 records with this framework and add them a value in this tag.
- Put those records is a list
- Go to this list and check the two records
- Click on "Merge selected"
- Click on next
- Go to second source record
- Click on the greatest tag number. for example 998.
=> The field is added at the end of destination record

Signed-off-by: Nick Clemens <nick@quecheelibrary.org>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@gmail.com>
Works as described, no regressions.
2014-08-05 20:17:17 -03:00

145 lines
4.9 KiB
JavaScript

/*
* Merging 2 source records into a destination record
*/
/**
* Check or uncheck a field or subfield in a source record
* @param pField the checkbox clicked
*/
function toggleField(pField) {
// Getting the key of the clicked checkbox
var ckid = $(pField).attr("id");
var tab = ckid.split('_');
var source = tab[1]; // From which record the click came from
var key = tab[2];
var type = $(pField).attr("class");
// Getting field/subfield
var field;
var subfield;
if (type == "subfieldpick") {
field = $(pField).parent().parent().parent().find("span.field").text();
subfield = $(pField).parent().find("span.subfield").text();
} else {
field = $(pField).parent().find("span.field").text();
}
// If the field has just been checked
if (pField.checked) {
// We check for repeatability
var canbeadded = true;
if (type == "subfieldpick") {
var repeatable = 1;
var alreadyexists = 0;
if (tagslib[field] && tagslib[field][subfield]) {
// Note : we can't use the dot notation here (tagslib.021) because the key is a number
repeatable = tagslib[field][subfield].repeatable;
// TODO : Checking for subfields
}
} else {
if (tagslib[field]) {
repeatable = tagslib[field].repeatable;
alreadyexists = $("#resultul span.field:contains(" + field + ")");
if (repeatable == 0 && alreadyexists.length != 0) {
canbeadded = false;
}
}
}
// If the field is not repeatable, we check if it already exists in the result table
if (canbeadded == false) {
alert(MSG_MERGEREC_ALREADY_EXISTS);
pField.checked = 0;
} else {
// Cloning the field or subfield we picked
var clone = $(pField).parent().clone();
// Removing the checkboxes from it
$(clone).find("input.subfieldpick, input.fieldpick").each(function() {
$(this).remove();
});
// If we are a subfield
if (type == "subfieldpick") {
// then we need to find who is our parent field...
fieldkey = $(pField).parent().parent().parent().attr("id");
// Find where to add the subfield
// First, check if the field is not already in the destination record
if ($("#resultul li#" + fieldkey).length > 0) {
// If so, we add our field to it
$("#resultul li#" + fieldkey + " ul").append(clone);
} else {
// If not, we add the subfield to the first matching field
var where = 0;
$("#resultul li span.field").each(function() {
if ($(this).text() == field) {
where = this;
return false; // break each()
}
});
// If there is no matching field in the destination record
if (where == 0) {
// TODO:
// We select the whole field and removing non-selected subfields, instead of...
// Alerting the user
alert(MSG_MERGEREC_SUBFIELD.format(field));
pField.checked = false;
} else {
$(where).nextAll("ul").append(clone);
}
}
} else {
// If we are a field
var where = 0;
// Find a greater field to add before
$("#resultul li span.field").each(function() {
if ($(this).text() > field) {
where = this;
return false; // break each()
}
});
if (where) {
$(where).parent().before(clone);
} else {
// No greater field, add to the end
$("#resultul").append(clone);
}
}
}
} else {
// Else, we remove it from the results tab
$("ul#resultul li#k" + key).remove();
}
}
/*
* Add actions on field and subfields checkboxes
*/
$(document).ready(function(){
// When a field is checked / unchecked
$('input.fieldpick').click(function() {
toggleField(this);
// (un)check all subfields
var ischecked = this.checked;
$(this).parent().find("input.subfieldpick").each(function() {
this.checked = ischecked;
});
});
// When a field or subfield is checked / unchecked
$("input.subfieldpick").click(function() {
toggleField(this);
});
});