- var clone = clone_with_selected(original)
+
+function cloneItemBlock(index, unique_item_fields) {
+ var original;
+ if(index) {
+ original = $("#" + index); //original
+ }
+ var dont_copy_fields = new Array();
+ if(unique_item_fields) {
+ var dont_copy_fields = unique_item_fields.split(' ');
+ for(i in dont_copy_fields) {
+ dont_copy_fields[i] = "items." + dont_copy_fields[i];
+ }
+ }
+
var random = Math.floor(Math.random()*100000); // get a random itemid.
- // set the attribute for the new 'div' subfields
- clone.setAttribute('id',index + random);//set another id.
- var NumTabIndex;
- NumTabIndex = parseInt(original.getAttribute('tabindex'));
- if(isNaN(NumTabIndex)) NumTabIndex = 0;
- clone.setAttribute('tabindex',NumTabIndex+1);
- var CloneButtonPlus;
- var CloneButtonMinus;
- // try{
- var jclone = $(clone);
- CloneButtonPlus = $("a.addItem", jclone).get(0);
- CloneButtonPlus.setAttribute('onclick',"cloneItemBlock('" + index + random + "')");
- CloneButtonMinus = $("a.delItem", jclone).get(0);
- CloneButtonMinus.setAttribute('onclick',"deleteItemBlock('" + index + random + "')");
- CloneButtonMinus.setAttribute('style',"display:inline");
- // change itemids of the clone
- var elems = clone.getElementsByTagName('input');
- for( i = 0 ; elems[i] ; i++ )
- {
- if(elems[i].name.match(/^itemid/)) {
- elems[i].value = random;
+ var clone = $("
")
+ $.ajax({
+ url: "/cgi-bin/koha/services/itemrecorddisplay.pl",
+ dataType: 'html',
+ data: {
+ frameworkcode: 'ACQ'
+ },
+ success: function(data, textStatus, jqXHR) {
+ /* Create the item block */
+ $(clone).append(data);
+ /* Change all itemid fields value */
+ $(clone).find("input[name='itemid']").each(function(){
+ $(this).val(random);
+ });
+ /* Add buttons + and Clear */
+ var buttonPlus = '
Add';
+ var buttonClear = '
' + (window.MSG_ADDITEM_JS_CLEAR || 'Clear') + '';
+ $(clone).append(buttonPlus).append(buttonClear);
+ /* Copy values from the original block (input) */
+ $(original).find("input[name='field_value']").each(function(){
+ var kohafield = $(this).siblings("input[name='kohafield']").val();
+ if($(this).val() && dont_copy_fields.indexOf(kohafield) == -1) {
+ $(this).parent("div").attr("id").match(/^(subfield.)/);
+ var id = RegExp.$1;
+ var value = $(this).val();
+ $(clone).find("div[id^='"+id+"'] input[name='field_value']").val(value);
+ }
+ });
+ /* Copy values from the original block (select) */
+ $(original).find("select[name='field_value']").each(function(){
+ var kohafield = $(this).siblings("input[name='kohafield']").val();
+ if($(this).val() && dont_copy_fields.indexOf(kohafield) == -1) {
+ $(this).parent("div").attr("id").match(/^(subfield.)/);
+ var id = RegExp.$1;
+ var value = $(this).val();
+ $(clone).find("div[id^='"+id+"'] select[name='field_value']").val(value);
+ }
+ });
+
+ $("#outeritemblock").append(clone);
}
- }
- // }
- //catch(e){ // do nothig if ButtonPlus & CloneButtonPlus don't exist.
- //}
- // insert this line on the page
- original.parentNode.insertBefore(clone,original.nextSibling);
- var quantity = document.getElementById('quantity');
- quantity.setAttribute('value',parseFloat(quantity.getAttribute('value'))+1);
+ });
}
-function check_additem() {
- var barcodes = document.getElementsByName('barcode');
- var success = true;
- for(i=0;i
j) && (barcodes[i].value == barcodes[j].value) && barcodes[i].value !='') {
- barcodes[i].className='error';
- barcodes[j].className='error';
- success = false;
- }
- }
- }
- // TODO : Add AJAX function to test against barcodes already in the database, not just
- // duplicates within the form.
- return success;
+
+function clearItemBlock(node) {
+ var index = $(node).parent().attr('id');
+ var block = $("#"+index);
+ $(block).find("input[type='text']").each(function(){
+ $(this).val("");
+ });
+ $(block).find("select").each(function(){
+ $(this).find("option:first").attr("selected", true);
+ });
+}
+
+function check_additem(unique_item_fields) {
+ var success = true;
+ var data = new Object();
+ data['field'] = new Array();
+ data['value'] = new Array();
+ var array_fields = unique_item_fields.split(' ');
+ $(".error").empty(); // Clear error div
+
+ // Check if a value is duplicated in form
+ for ( field in array_fields ) {
+ var fieldname = array_fields[field];
+ if (fieldname == '') {
+ continue;
+ }
+ var values = new Array();
+ $("[name='kohafield'][value='items."+ fieldname +"']").each(function(){
+ var input = $(this).prevAll("input[name='field_value']")[0];
+ if($(input).val()) {
+ values.push($(input).val());
+ data['field'].push(fieldname);
+ data['value'].push($(input).val());
+ }
+ });
+
+ var sorted_arr = values.sort();
+ for (var i = 0; i < sorted_arr.length - 1; i += 1) {
+ if (sorted_arr[i + 1] == sorted_arr[i]) {
+ $(".error").append(
+ fieldname + " '" + sorted_arr[i] + "' "
+ + (window.MSG_ADDITEM_JS_IS_DUPLICATE || "is duplicated")
+ + "
");
+ success = false;
+ }
+ }
+ }
+
+ // If there is a duplication, we raise an error
+ if ( success == false ) {
+ $(".error").show();
+ return false;
+ }
+
+ $.ajax({
+ url: '/cgi-bin/koha/acqui/check_uniqueness.pl',
+ async: false,
+ dataType: 'json',
+ data: data,
+ success: function(data) {
+ for (field in data) {
+ success = false;
+ for (var i=0; i < data[field].length; i++) {
+ var value = data[field][i];
+ $(".error").append(
+ field + " '" + value + "' "
+ + (window.MSG_ADDITEM_JS_ALREADY_EXISTS_IN_DB
+ || "already exists in database")
+ + "
"
+ );
+ }
+ }
+ }
+ });
+
+ if ( success == false ) {
+ $(".error").show();
+ }
+ return success;
}
-function clone_with_selected (node) {
- var origin = node.getElementsByTagName("select");
- var tmp = node.cloneNode(true)
- var selectelem = tmp.getElementsByTagName("select");
- for (var i=0; i