diff --git a/classes/helpers/FrmFieldsHelper.php b/classes/helpers/FrmFieldsHelper.php index ccec14eb64..e9d94b7048 100644 --- a/classes/helpers/FrmFieldsHelper.php +++ b/classes/helpers/FrmFieldsHelper.php @@ -43,15 +43,7 @@ public static function setup_new_vars( $type = '', $form_id = '' ) { } // Increase the field order of submit field and fields in the same row. - $last_row_field_ids = FrmAppHelper::get_post_param( 'last_row_field_ids', array() ); - if ( $last_row_field_ids ) { - foreach ( $last_row_field_ids as $index => $last_row_field_id ) { - FrmField::update( - $last_row_field_id, - array( 'field_order' => $field_count + $index + 2 ) - ); - } - } + FrmSubmitHelper::update_last_row_fields_order_when_adding_field( $field_count ); return $values; } diff --git a/classes/helpers/FrmSubmitHelper.php b/classes/helpers/FrmSubmitHelper.php index 300ee4a1b5..bb510b93ed 100644 --- a/classes/helpers/FrmSubmitHelper.php +++ b/classes/helpers/FrmSubmitHelper.php @@ -29,6 +29,13 @@ class FrmSubmitHelper { */ const DEFAULT_ORDER = 9999; + /** + * Track the new order of last row fields after a new field is added. + * + * @var array Keys are field IDs, values are the order. + */ + private static $last_row_fields_order = array(); + /** * Gets submit field object. * @@ -192,4 +199,51 @@ public static function only_contains_submit_field( $fields ) { } return $submit_field; } + + /** + * Updates fields in the last row when new field is added. + * + * @since x.x + * + * @param int $field_count The current field count. + */ + public static function update_last_row_fields_order_when_adding_field( $field_count ) { + $last_row_field_ids = FrmAppHelper::get_post_param( 'last_row_field_ids', array() ); + if ( ! is_array( $last_row_field_ids ) || empty( $last_row_field_ids ) ) { + return; + } + foreach ( $last_row_field_ids as $index => $last_row_field_id ) { + $last_row_field_id = absint( $last_row_field_id ); + if ( ! $last_row_field_id ) { + continue; + } + // Plus 2 here because the new field has that plus 1. + $new_order = $field_count + $index + 2; + $updated = FrmField::update( + $last_row_field_id, + array( 'field_order' => $new_order ) + ); + if ( false !== $updated ) { + self::$last_row_fields_order[ $last_row_field_id ] = $new_order; + } + } + } + + /** + * Prints the hidden input that contains the last row fields order to be processed in JS after adding new field. + * + * @since x.x + */ + public static function print_last_row_fields_order_input() { + if ( ! self::$last_row_fields_order ) { + return; + } + + printf( + '', + esc_attr( wp_json_encode( self::$last_row_fields_order ) ) + ); + + self::$last_row_fields_order = array(); + } } diff --git a/classes/views/frm-forms/add_field.php b/classes/views/frm-forms/add_field.php index bbdcc7d658..d0ed3454de 100644 --- a/classes/views/frm-forms/add_field.php +++ b/classes/views/frm-forms/add_field.php @@ -12,7 +12,11 @@
Imported '+r.data.name+"
",e.find(".status").prepend(n),e.find(".status").show(),O.importQueue=jQuery.grep(O.importQueue,(function(e){return e!=t})),O.imported++,0===O.importQueue.length?(e.find(".process-count").hide(),e.find(".forms-completed").text(O.imported),e.find(".process-completed").show()):(e.find(".form-current").text(O.imported+1),Ji(e)))}))}function Xi(e){e.preventDefault();var t=!1,r=jQuery('input[name="frm_export_forms[]"]');jQuery('input[name="frm_export_forms[]"]:checked').val()||(r.closest(".frm-table-box").addClass("frm_blank_field"),t="stop");var n=jQuery('input[name="type[]"]');if(jQuery('input[name="type[]"]:checked').val()||"checkbox"!==n.attr("type")||(n.closest("p").addClass("frm_blank_field"),t="stop"),"stop"===t)return!1;e.stopPropagation(),this.submit()}function Yi(){var e=jQuery(this).closest(".frm_blank_field");if(void 0!==e){var t=this.name;("type[]"===t&&jQuery('input[name="type[]"]:checked').val()||"frm_export_forms[]"===t&&jQuery(this).val())&&e.removeClass("frm_blank_field")}}function Zi(){null!==jQuery(this).val().match(/\.csv$/i)?jQuery(".show_csv").fadeIn():jQuery(".show_csv").fadeOut()}function eo(){var e=document.querySelector('select[name="format"]');return e?e.value:""}function to(e){var t,r,n=e.target.value;no(n),ro.call(e.target),t=n,r=document.getElementById("frm-export-select-all"),"csv"===t?(r.checked=!1,r.disabled=!0):r.disabled=!1}function ro(){var e=jQuery(this),t=e.find(":selected"),r=t.data("support"),n=r.indexOf("|");jQuery('input[name="type[]"]').each((function(){this.checked=!1,r.indexOf(this.value)>=0?(this.disabled=!1,-1===n&&(this.checked=!0)):this.disabled=!0})),"csv"===e.val()?(jQuery(".csv_opts").show(),jQuery(".xml_opts").hide()):(jQuery(".csv_opts").hide(),jQuery(".xml_opts").show());var i=t.data("count"),o=jQuery('input[name="frm_export_forms[]"]');"single"===i?(o.prop("multiple",!1),o.prop("checked",!1)):(o.prop("multiple",!0),o.prop("disabled",!1)),e.trigger("change")}function no(e){if(""!==e){var t=document.querySelectorAll(".frm-is-repeater");t.length&&("csv"===e?t.forEach((function(e){e.classList.remove("frm_hidden")})):t.forEach((function(e){e.classList.add("frm_hidden")})),bo.call(document.querySelector(".frm-auto-search")))}}function io(){var e=jQuery("select[name=format]").find(":selected").data("count"),t=jQuery('input[name="frm_export_forms[]"]');"single"===e&&this.checked?(t.prop("disabled",!0),this.removeAttribute("disabled")):t.prop("disabled",!1)}function oo(){jQuery(".frm_multiselect").hide().each(frmDom.bootstrap.multiselect.init)}function ao(e){e.preventDefault(),co(this,"frm_multiple_addons")}function lo(e){e.preventDefault(),co(this,"frm_activate_addon")}function so(e){e.preventDefault(),co(this,"frm_install_addon")}function co(e,t){r(1105).toggleAddonState(e,t)}function fo(){mo()}function uo(e){!function(e,t,r){var n=jQuery("#frm_leave_email_error");n.removeClass("frm_hidden").attr("frm-error",r),jQuery("#frm_leave_email").one("keyup",(function(){n.addClass("frm_hidden")}))}(0,0,e)}function mo(){var e=document.getElementById("frmapi-email-form");jQuery.ajax({dataType:"json",url:e.getAttribute("data-url"),success:function(t){var r=t.renderedHtml;r=r.replace(/]*(formidableforms.css|action=frmpro_css)[^>]*>/gi,""),e.innerHTML=r}})}function _o(e){frmDom.autocomplete.initSelectionAutocomplete(e)}function po(e){var t=this.parentNode.parentNode,r=t.elements.type.value;e.preventDefault(),this.classList.add("frm_loading_button"),ho(t,r,this)}function go(e){var t=this.elements.type.value,r=this.querySelector("button");e.preventDefault(),r.classList.add("frm_loading_button"),ho(this,t,r)}function ho(e,t,r){var n=function(e){var t,r,n={},i=e.elements;for(r=0;r'+e.message+"
Imported '+r.data.name+"
",e.find(".status").prepend(n),e.find(".status").show(),O.importQueue=jQuery.grep(O.importQueue,function(e){return e!=t}),O.imported++,0===O.importQueue.length?(e.find(".process-count").hide(),e.find(".forms-completed").text(O.imported),e.find(".process-completed").show()):(e.find(".form-current").text(O.imported+1),Zi(e)))})}function eo(e){e.preventDefault();var t=!1,r=jQuery('input[name="frm_export_forms[]"]');jQuery('input[name="frm_export_forms[]"]:checked').val()||(r.closest(".frm-table-box").addClass("frm_blank_field"),t="stop");var n=jQuery('input[name="type[]"]');if(jQuery('input[name="type[]"]:checked').val()||"checkbox"!==n.attr("type")||(n.closest("p").addClass("frm_blank_field"),t="stop"),"stop"===t)return!1;e.stopPropagation(),this.submit()}function to(){var e=jQuery(this).closest(".frm_blank_field");if(void 0!==e){var t=this.name;("type[]"===t&&jQuery('input[name="type[]"]:checked').val()||"frm_export_forms[]"===t&&jQuery(this).val())&&e.removeClass("frm_blank_field")}}function ro(){null!==jQuery(this).val().match(/\.csv$/i)?jQuery(".show_csv").fadeIn():jQuery(".show_csv").fadeOut()}function no(){var e=document.querySelector('select[name="format"]');return e?e.value:""}function io(e){var t,r,n=e.target.value;ao(n),oo.call(e.target),t=n,r=document.getElementById("frm-export-select-all"),"csv"===t?(r.checked=!1,r.disabled=!0):r.disabled=!1}function oo(){var e=jQuery(this),t=e.find(":selected"),r=t.data("support"),n=r.indexOf("|");jQuery('input[name="type[]"]').each(function(){this.checked=!1,r.indexOf(this.value)>=0?(this.disabled=!1,-1===n&&(this.checked=!0)):this.disabled=!0}),"csv"===e.val()?(jQuery(".csv_opts").show(),jQuery(".xml_opts").hide()):(jQuery(".csv_opts").hide(),jQuery(".xml_opts").show());var i=t.data("count"),o=jQuery('input[name="frm_export_forms[]"]');"single"===i?(o.prop("multiple",!1),o.prop("checked",!1)):(o.prop("multiple",!0),o.prop("disabled",!1)),e.trigger("change")}function ao(e){if(""!==e){var t=document.querySelectorAll(".frm-is-repeater");t.length&&("csv"===e?t.forEach(function(e){e.classList.remove("frm_hidden")}):t.forEach(function(e){e.classList.add("frm_hidden")}),xo.call(document.querySelector(".frm-auto-search")))}}function lo(){var e=jQuery("select[name=format]").find(":selected").data("count"),t=jQuery('input[name="frm_export_forms[]"]');"single"===e&&this.checked?(t.prop("disabled",!0),this.removeAttribute("disabled")):t.prop("disabled",!1)}function so(){jQuery(".frm_multiselect").hide().each(frmDom.bootstrap.multiselect.init)}function co(e){e.preventDefault(),mo(this,"frm_multiple_addons")}function fo(e){e.preventDefault(),mo(this,"frm_activate_addon")}function uo(e){e.preventDefault(),mo(this,"frm_install_addon")}function mo(e,t){r(105).toggleAddonState(e,t)}function _o(){go()}function po(e){!function(e,t,r){var n=jQuery("#frm_leave_email_error");n.removeClass("frm_hidden").attr("frm-error",r),jQuery("#frm_leave_email").one("keyup",function(){n.addClass("frm_hidden")})}(0,0,e)}function go(){var e=document.getElementById("frmapi-email-form");jQuery.ajax({dataType:"json",url:e.getAttribute("data-url"),success:function(t){var r=t.renderedHtml;r=r.replace(/]*(formidableforms.css|action=frmpro_css)[^>]*>/gi,""),e.innerHTML=r}})}function ho(e){frmDom.autocomplete.initSelectionAutocomplete(e)}function yo(e){var t=this.parentNode.parentNode,r=t.elements.type.value;e.preventDefault(),this.classList.add("frm_loading_button"),bo(t,r,this)}function vo(e){var t=this.elements.type.value,r=this.querySelector("button");e.preventDefault(),r.classList.add("frm_loading_button"),bo(this,t,r)}function bo(e,t,r){var n=function(e){var t,r,n={},i=e.elements;for(r=0;r'+t.message+"
'+t.message+"