Skip to content

Commit

Permalink
Merge pull request #15 from ItinerisLtd/fix-nan-and-save-bugs
Browse files Browse the repository at this point in the history
Fix Bugs 05/04/2024
  • Loading branch information
Barny-Thorpe authored Apr 5, 2024
2 parents b8f7dfe + 5c87239 commit 88562ed
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 8 deletions.
21 changes: 18 additions & 3 deletions public/js/gf-giftaid-field-frontend.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,31 @@ function initGiftAid() {
if (!totalSelector) {
return;
}
window.gform.addAction('gform_input_change', function (elem) {
const donationValue = parseFloat(elem.value);
window.gform.addAction(
'gform_input_change',
function (elem) {
if (
!(elem instanceof HTMLInputElement) ||
!elem.closest(totalSelector) ||
!elem.value
) {
return;
}
const sanitizedValue = elem.value.replace(/[^0-9.]/g, '');
const donationValue = parseFloat(sanitizedValue);
const giftAidValue = donationValue * 1.25;
if (!donationValue || !giftAidValue) {
return;
}
const donationRounded = donationValue.toFixed(2);
const giftAidRounded = giftAidValue.toFixed(2);
if (!donationRounded || !giftAidRounded) {
return;
}
updateGiftAidDisplay(gravityForm, donationRounded, giftAidRounded);
}, 10);
},
10,
);
}

/**
Expand Down
38 changes: 33 additions & 5 deletions src/GfGiftAidField.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public static function run(): void
GFAddOn::register(AddOn::class);

add_action('gform_field_standard_settings', [static::class, 'addSelectedPriceFieldSetting'], 10, 2);
add_action('gform_editor_js', [static::class, 'addSelectedPriceFieldScript']);
}

/**
Expand All @@ -33,10 +34,15 @@ public static function addSelectedPriceFieldSetting(int $position, int $form_id)
if (25 !== $position || empty($form_id)) {
return;
}
$exists = GFAPI::form_id_exists($form_id);
if (empty($exists)) {
return;
}
$field_options = static::getFormFields($form_id);
if (empty($field_options)) {
return;
}

?>
<li class="selected_price_field_setting field_setting">
<label for="selected_price_field_dropdown" class="section_label">
Expand All @@ -48,6 +54,8 @@ public static function addSelectedPriceFieldSetting(int $position, int $form_id)
id="selected_price_field_dropdown"
onchange="SetFieldProperty('selectedPriceField', this.value);"
>
<option value="">--Please select a field--</option>

<?php foreach ($field_options as $field_id => $field_label) : ?>
<?php
if (empty($field_id) || empty($field_label)) {
Expand All @@ -62,17 +70,37 @@ public static function addSelectedPriceFieldSetting(int $position, int $form_id)
<?php
}

public static function addSelectedPriceFieldScript(): void
{
?>
<script type='text/javascript'>
//adding setting to fields of type "text"
fieldSettings.text += ', .selected_price_field_setting';
// Update the field settings when the field is loaded in the form editor
jQuery(document).on('gform_load_field_settings', function(event, field, form) {
if (!field || !field.type || 'gift_aid' !== field.type) {
return;
}
var savedValue = rgar(field, 'selectedPriceField');
if (!savedValue) {
return;
}
var selectedPriceDropdown = document.querySelector('#selected_price_field_dropdown');
if (!(selectedPriceDropdown instanceof HTMLSelectElement)) {
return;
}
selectedPriceDropdown.value = savedValue;
});
</script>
<?php
}

/**
* Get all the fields in a gravity form in the format:
* ['field class' => 'field_label', ...]
*/
public static function getFormFields(int $form_id): array
{
$exists = GFAPI::form_id_exists($form_id);
if (empty($exists)) {
return [];
}

$form = GFAPI::get_form($form_id);
$fields = $form['fields'] ?? [];
if (empty($fields) || !is_array($fields)) {
Expand Down

0 comments on commit 88562ed

Please sign in to comment.