Koha/koha-tmpl/intranet-tmpl/prog/js/merge-record.js
Owen Leonard dc393dc6be Bug 25320: Move translatable strings out of merge-record-strings.inc into merge-record.js
This patch eliminates the use of a separate include file containing
translatable strings in favor of embedding translatable strings in the
JavaScript itself.

To test, apply the patch and clear your browser cache if necessary.

 - Perform a catalog search in the staff interface.
 - Select two results to merge. Click Edit -> Merge records.
 - Click "Next" on the "Merging records" page.
 - On the "Source records" page click the second tab where all the
   checkboxes are unchecked.
   - Check the box for a tag which is non-repeatable, e.g. 245.
     - You should see a message, "The field is non-repeatable and
       already exists in the destination record. Therefore, you cannot add it."
   - Check the box for a subfield which is non-repeatable, e.g. 245$a.
     - You should see a message, "The subfield is non-repeatable and
       already exists in the destination record. Therefore, you cannot
       add it."

TESTING TRANSLATABILITY

 - Update a translation, e.g. fr-FR:

   > cd misc/translator
   > perl translate update fr-FR

 - Open the corresponding .po file for JavaScript strings, e.g.
   misc/translator/po/fr-FR-messages-js.po
 - Locate strings pulled from prog/js/merge-record.js for
   translation, e.g.:

   #: koha-tmpl/intranet-tmpl/prog/js/merge-record.js:72
   msgctxt "Bibliographic record"
   msgid ""
   "The field is non-repeatable and already exists in the destination
   record. "
   "Therefore, you cannot add it."
   msgstr ""

 - Edit the "msgstr" string however you want (it's just for testing).
 - Install the updated translation:

   > perl translate install fr-FR

 - Switch to your newly translated language in the staff client and
   repeat the test plan above. The translated string should appear.

Signed-off-by: David Nind <david@davidnind.com>

Signed-off-by: Katrin Fischer <katrin.fischer.83@web.de>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
2020-09-29 14:28:19 +02:00

184 lines
5.2 KiB
JavaScript

/*
* Merging 2 source records into a destination record
*/
function build_target_record($sources) {
var target_record = {};
$sources.find('.record input[type="checkbox"].fieldpick:checked').each(function() {
var $checkbox = $(this);
var $li = $checkbox.parent();
var field = $checkbox.parent().find("span.field").text();
if (!(field in target_record)) {
target_record[field] = [];
}
target_record[field].push({
'id' : $li.attr('id'),
'tag' : field,
'subfields' : []
});
});
$sources.find('.record input[type="checkbox"].subfieldpick:checked').each(function() {
var $checkbox = $(this);
var $li = $checkbox.parent();
var $field_li = $li.parents('li');
var field = $field_li.find('span.field').text();
var subfield = $li.find('span.subfield').text();
var target_field;
if (field in target_record) {
for (i in target_record[field]) {
if (target_record[field][i].id == $field_li.attr('id')) {
target_field = target_record[field][i];
}
}
if (!target_field) {
target_field = target_record[field][0];
}
}
if (target_field) {
target_field.subfields.push({
'id' : $li.attr('id'),
'code' : subfield
});
} else {
$field_li.find('input.fieldpick').prop('checked', true);
target_record[field] = [{
'id' : $field_li.attr('id'),
'tag' : field,
'subfields' : [{
'id' : $li.attr('id'),
'code' : subfield
}]
}];
}
});
return target_record;
}
function field_can_be_added($sources, $li) {
target_record = build_target_record($sources);
var tag = $li.find('span.field').text();
var repeatable = true;
if (tag in tagslib) {
repeatable = (tagslib[tag].repeatable != 0) ? true : false;
}
if ((!repeatable) && (tag in target_record)) {
alert( __p("Bibliographic record", "The field is non-repeatable and already exists in the destination record. Therefore, you cannot add it.") );
return false;
}
return true;
}
function subfield_can_be_added($sources, $li) {
target_record = build_target_record($sources);
var $field_li = $li.parents('li');
var tag = $field_li.find('span.field').text();
var code = $li.find('span.subfield').text();
var repeatable = true;
if (tag in tagslib && code in tagslib[tag]) {
repeatable = (tagslib[tag][code].repeatable != 0) ? true : false;
}
if (!repeatable) {
var target_field;
if (tag in target_record) {
for (i in target_record[tag]) {
if (target_record[tag][i].id == $field_li.attr('id')) {
target_field = target_record[tag][i];
}
}
if (!target_field) {
target_field = target_record[tag][0];
}
}
if (target_field) {
for (i in target_field.subfields) {
var subfield = target_field.subfields[i];
if (code == subfield.code) {
alert( __p("Bibliographic record", "The subfield is non-repeatable and already exists in the destination record. Therefore, you cannot add it.") );
return false;
}
}
}
}
return true;
}
function rebuild_target($sources, $target) {
target_record = build_target_record($sources);
$target.empty();
var keys = $.map(target_record, function(elem, idx) { return idx }).sort();
for (k in keys) {
var tag = keys[k];
var fields = target_record[tag];
for (i in fields) {
var field = fields[i];
if (parseInt(tag) < 10) {
var $field_clone = $('#' + field.id).clone();
$field_clone.find('.fieldpick').remove();
$target.append($field_clone);
} else if (field.subfields.length > 0) {
var $field_clone = $('#' + field.id).clone();
$field_clone.find('ul').empty();
$field_clone.find('.fieldpick').remove();
$target.append($field_clone);
for (j in field.subfields) {
var subfield = field.subfields[j];
var $subfield_clone = $('#' + subfield.id).clone();
$subfield_clone.find('.subfieldpick').remove();
$field_clone.find('ul').append($subfield_clone);
}
} else {
$('#' + field.id).find('input.fieldpick').prop('checked', false);
}
}
}
}
/*
* Add actions on field and subfields checkboxes
*/
$(document).ready(function(){
// When a field is checked / unchecked
$('input.fieldpick').click(function() {
var ischecked = this.checked;
if (ischecked) {
$(this).prop('checked', false);
if (!field_can_be_added($('#tabs'), $(this).parent())) {
return false;
}
$(this).prop('checked', true);
}
// (un)check all subfields
$(this).parent().find("input.subfieldpick").each(function() {
this.checked = ischecked;
});
rebuild_target($('#tabs'), $('#resultul'));
});
// When a field or subfield is checked / unchecked
$("input.subfieldpick").click(function() {
var ischecked = this.checked;
if (ischecked) {
$(this).prop('checked', false);
if (!subfield_can_be_added($('#tabs'), $(this).parent())) {
return false;
}
$(this).prop('checked', true);
}
rebuild_target($('#tabs'), $('#resultul'));
});
});