Skip to content

Commit

Permalink
make pdf translation settings available to client, fix issue with cal…
Browse files Browse the repository at this point in the history
…culator element not updating when user skips original field tag e.g. {hidden} and instead uses {hidden_2}+{hidden_3} etc.
  • Loading branch information
RensTillmann committed Sep 17, 2024
1 parent d7fcbf1 commit 4350477
Show file tree
Hide file tree
Showing 15 changed files with 220 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,11 @@
// @since 1.5.0 - first update the data fields attribute to make sure regex tags are replaced with according field names
//SUPER.init_calculator_update_fields_attribute(form, calculatorFields);
}else{
calculatorFields = form.querySelectorAll('.super-calculator-wrapper[data-fields*="{'+args.el.dataset.oname+'}"]');
// We can't use oname (original field name) here because someone might use {hidden_2}+{hidden_3}+etc. inside their math, which would cause the Calculator to not update at all.
// This could be avoided by using ({hidden}-{hidden})+({hidden_2}+{hidden_3}+etc.) as a work-around to this issue.
// Not sure how this might affect other use-cases but we will be using args.el.name instead from now on going forward and not oname (original field name)
// deprecated: calculatorFields = form.querySelectorAll('.super-calculator-wrapper[data-fields*="{'+args.el.dataset.oname+'}"]');
calculatorFields = form.querySelectorAll('.super-calculator-wrapper[data-fields*="{'+args.el.name+'}"]');
if(calculatorFields.length===0 && SUPER.calculatorFieldConnections[form.id][args.el.dataset.oname]){
var count = Object.keys(SUPER.calculatorFieldConnections[form.id][args.el.dataset.oname]).length;
if(count>0){
Expand Down
3 changes: 3 additions & 0 deletions src/add-ons/super-forms-calculator/super-forms-calculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ private function is_request($type){
*/
private function init_hooks() {

error_log('init_hooks calculator');

// Filters since 1.0.0
add_filter( 'super_shortcodes_after_form_elements_filter', array( $this, 'add_calculator_element' ), 10, 2 );

Expand Down Expand Up @@ -510,6 +512,7 @@ public static function calculator($x) {
* @since 1.0.0
*/
public static function add_calculator_element( $array, $attributes ) {
error_log('add_calculator_element()');

// Include the predefined arrays
require( SUPER_PLUGIN_DIR . '/includes/shortcodes/predefined-arrays.php' );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1333,10 +1333,9 @@ public static function before_sending_email( $x ) {
// If this is a paid signup form (when checkout is enabled)
$checkout = false;
// WooCommerce checkout
$wcs = null;
if(isset($settings['_woocommerce'])) $wcs = $settings['_woocommerce'];
if(isset($wcs) && $wcs['checkout']=='true'){
$checkout = SUPER_Common::conditionally_wc_checkout($data, $settings);
$wcs = SUPER_Common::get_form_woocommerce_settings($form_id);
if(!empty($wcs) && $wcs['checkout']=='true'){
$checkout = SUPER_Common::conditionally_wc_checkout($data, $wcs, $settings);
}
// PayPal checkout
if((isset($settings['paypal_checkout'])) && ($settings['paypal_checkout'] == 'true')){
Expand All @@ -1357,6 +1356,7 @@ public static function before_sending_email( $x ) {
}
}
// Stripe checkout
error_log('get_form_stripe_settings(2)');
$s = SUPER_Common::get_form_stripe_settings($form_id);
// Skip if Stripe checkout is not enabled
if(!empty($s) && $s['enabled']==='true'){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,7 @@ public static function signature($x) {
* @since 1.0.0
*/
public static function add_signature_element( $array, $attributes ) {
error_log('add_signature_element()');

// Include the predefined arrays
require( SUPER_PLUGIN_DIR . '/includes/shortcodes/predefined-arrays.php' );
Expand Down
3 changes: 2 additions & 1 deletion src/assets/css/backend/ui.css
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
font-weight: 500;
font-size: 14px;
}
.sfui-title .flag {
.sfui-title .flag,
.sfui-subline .flag {
margin: 0px 0px 0px 6px;
}
.sfui-setting .sfui-title>span {
Expand Down
51 changes: 27 additions & 24 deletions src/assets/js/backend/create-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -1232,6 +1232,28 @@
return SUPER.get_obj_value_by_key(obj[is[0]], is.slice(1), value);
}
};
SUPER.add_country_flags = function(i18n, flag, nodes){
if(flag){
for(var i=0; i<nodes.length; i++){
// Add the country flag next to the setting title to indicate translatable option
var node = nodes[i].closest('label').querySelector('.sfui-title');
if(node){
var cloneFlag = flag.cloneNode();
cloneFlag.title = 'Translation for ' + (i18n ? i18n : 'main language');
if (node.querySelector('.flag')) node.querySelector('.flag').remove();
node.appendChild(cloneFlag);
}else{
var node = nodes[i].closest('label').querySelector('.sfui-subline');
if(node){
var cloneFlag = flag.cloneNode();
cloneFlag.title = 'Translation for ' + (i18n ? i18n : 'main language');
if (node.querySelector('.flag')) node.querySelector('.flag').remove();
node.appendChild(cloneFlag);
}
}
}
}
};
SUPER.get_tab_settings = function(settings, slug, tab, data, returnData){
if(typeof returnData === 'undefined') returnData = false;
var nodes, i, i18n_data = null;
Expand All @@ -1244,18 +1266,7 @@
tab = document.querySelector('.super-tab-content.super-tab-' + slug);
}
nodes = tab.querySelectorAll('.sfui-i18n [name]');
if(flag){
for(i = 0; i<nodes.length; i++){
// Add the country flag next to the setting title to indicate translatable option
var title = nodes[i].closest('label').querySelector('.sfui-title');
if (title) {
var cloneFlag = flag.cloneNode();
cloneFlag.title = 'Translation for ' + (i18n ? i18n : 'main language');
if (title.querySelector('.flag')) title.querySelector('.flag').remove();
title.appendChild(cloneFlag);
}
}
}
SUPER.add_country_flags(i18n, flag, nodes);
if(SUPER.ui.i18n.translating) {
for (i = 0; i < nodes.length; i++) {
// Skip if already exists
Expand Down Expand Up @@ -1352,18 +1363,7 @@
var flag = document.querySelector(':scope .super-tabs > .super-tab-builder > .flag');
// Remember the original value for translatable settings
nodes = tab.querySelectorAll('.sfui-i18n [name]');
if (flag) {
for (i = 0; i < nodes.length; i++) {
// Add the country flag next to the setting title to indicate translatable option
var title = nodes[i].closest('label').querySelector('.sfui-title');
if (title) {
var cloneFlag = flag.cloneNode();
cloneFlag.title = 'Translation for ' + (i18n ? i18n : 'main language');
if (title.querySelector('.flag')) title.querySelector('.flag').remove();
title.appendChild(cloneFlag);
}
}
}
SUPER.add_country_flags(i18n, flag, nodes);
if(SUPER.ui.i18n.translating) {
for (i = 0; i < nodes.length; i++) {
// Skip if already exists
Expand Down Expand Up @@ -3615,6 +3615,9 @@
i18n: $i18n
},
success: function (data) {
debugger;
debugger;
debugger;
console.log(SUPER.ui.settings);
data = JSON.parse(data);
$('.super-preview-elements').html(data.elements);
Expand Down
32 changes: 32 additions & 0 deletions src/assets/js/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -469,15 +469,42 @@ function SUPERreCaptcha(){
}
});
};
SUPER.mergeTranslatedSettings = function(defaultSettings, translatedSettings) {
for (var key in translatedSettings) {
if (translatedSettings.hasOwnProperty(key)) {
// Check if the key exists in default settings
if (defaultSettings.hasOwnProperty(key)) {
// If the value is an object, we need to recursively merge
if (typeof translatedSettings[key] === 'object' && !Array.isArray(translatedSettings[key])) {
// Recursively merge objects
defaultSettings[key] = SUPER.mergeTranslatedSettings(defaultSettings[key], translatedSettings[key]);
} else {
// If not an object (or if it's an array), replace the value in default settings with the translated value
defaultSettings[key] = translatedSettings[key];
}
} else {
// If the key doesn't exist in default settings, add it from the translated settings
defaultSettings[key] = translatedSettings[key];
}
}
}
return defaultSettings;
};
SUPER.process_form_data = function(args){
args.generatePdf = false;
args.pdfSettings = null;
if( typeof SUPER.form_js !== 'undefined' &&
typeof SUPER.form_js[args.form_id] !== 'undefined' &&
typeof SUPER.form_js[args.form_id]._pdf !== 'undefined' &&
SUPER.form_js[args.form_id]._pdf.generate === "true" ) {
debugger;
debugger;
args.generatePdf = true;
args.pdfSettings = SUPER.form_js[args.form_id]._pdf;
if(args.pdfSettings['i18n'] && args.pdfSettings['i18n'][args.form0.dataset.i18n]){
var $translatedSettings = args.pdfSettings['i18n'][args.form0.dataset.i18n];
args.pdfSettings = SUPER.mergeTranslatedSettings(args.pdfSettings, $translatedSettings);
}
if(args.progressBar) args.progressBar.style.width = 0+'%';
if(args.pdfSettings.generatingText===''){
args.loadingOverlay.querySelector('.super-inner-text').innerHTML = '<span>'+super_common_i18n.loadingOverlay.generating_pdf+'</span>';
Expand All @@ -489,8 +516,13 @@ function SUPERreCaptcha(){
if( typeof SUPER.get_form_settings === 'function' &&
typeof SUPER.get_form_settings()._pdf !== 'undefined' &&
SUPER.get_form_settings()._pdf.generate === "true" ) {
debugger;
args.generatePdf = true;
args.pdfSettings = SUPER.get_form_settings()._pdf;
if(args.pdfSettings['i18n'] && args.pdfSettings['i18n'][args.form0.dataset.i18n]){
var $translatedSettings = args.pdfSettings['i18n'][args.form0.dataset.i18n];
args.pdfSettings = SUPER.mergeTranslatedSettings(args.pdfSettings, $translatedSettings);
}
if(args.progressBar) args.progressBar.style.width = 0+'%';
if(args.pdfSettings.generatingText===''){
args.loadingOverlay.querySelector('.super-inner-text').innerHTML = '<span>'+super_common_i18n.loadingOverlay.generating_pdf+'</span>';
Expand Down
5 changes: 4 additions & 1 deletion src/includes/class-ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ public static function language_switcher() {
);
// Check if languages are used
$verified = true;
$settings = SUPER_Common::get_form_settings($atts['id']);
$settings = SUPER_Common::get_form_settings($atts['id'], true);
if(empty($settings['i18n_switch'])) $settings['i18n_switch'] = 'false';
if($settings['i18n_switch']!=='true'){
// No need to do this, return message
Expand Down Expand Up @@ -947,6 +947,7 @@ public static function restore_backup() {
error_log('3.1: '.json_encode($s));
SUPER_Common::save_form_pdf_settings($s, $backup_id);

error_log('get_form_stripe_settings(3)');
$s = SUPER_Common::get_form_stripe_settings($backup_id);
SUPER_Common::save_form_stripe_settings($s, $backup_id);

Expand Down Expand Up @@ -3232,6 +3233,8 @@ public static function submit_form() {
$entry_id = absint($sfsi['entry_id']);
$list_id = absint($sfsi['list_id']);
$settings = $sfsi['settings'];
error_log('submit_form(1)::$settings');
error_log(json_encode($settings));
$response_data = $sfsi['response_data'];
do_action( 'super_before_processing_data', array('atts' => $sfsi) );
if( ( isset( $data ) ) && ( count( $data )>0 ) ) {
Expand Down
Loading

0 comments on commit 4350477

Please sign in to comment.