Skip to content

Commit

Permalink
fix: corrected form data notification (#91)
Browse files Browse the repository at this point in the history
* fix: corrected form data notification email

* fix: corrected variable names

* fix: corrected values and helper

* chore: docs

* fix: reset
  • Loading branch information
NiclasNorin authored May 22, 2024
1 parent c6976e1 commit 6dd8cb7
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 12 deletions.
14 changes: 3 additions & 11 deletions source/php/Entity/PostType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace ModularityFormBuilder\Entity;

use ModularityFormBuilder\Helper\NestedFields;

class PostType
{
public $postTypeSlug;
Expand Down Expand Up @@ -446,17 +448,7 @@ public static function gatherFormData($post)
$formId
);

$nestedIndataArray = array();
if (is_array($indata)) {
foreach ($indata as $key => $value) {
$pattern = '/^id-\d+-/';
$key = preg_replace($pattern, "", sanitize_title($key), 1);
$nestedIndataArray[] = [
'key' => $key,
'value' => $value
];
}
}
$nestedIndataArray = NestedFields::createNestedArrayFromFieldData($indata);

foreach ($fields['form_fields'] as $key => $field) {
// Skip layout fields
Expand Down
33 changes: 33 additions & 0 deletions source/php/Helper/NestedFields.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace ModularityFormBuilder\Helper;

class NestedFields
{
/**
* Creates a nested array from field data.
*
* This function takes an array of field data and converts it into a nested array format.
* Each element in the input array is transformed into an associative array with 'key' and 'value' keys.
* The 'key' is obtained by removing the prefix 'id-' and sanitizing the original key.
*
* @param array $fieldData The input array of field data.
* @return array The nested array created from the field data.
*/
public static function createNestedArrayFromFieldData($fieldData)
{
$nestedDataArray = [];
if (is_array($fieldData)) {
foreach ($fieldData as $key => $value) {
$pattern = '/^id-\d+-/';
$key = preg_replace($pattern, "", sanitize_title($key), 1);
$nestedDataArray[] = [
'key' => $key,
'value' => $value
];
}
}

return $nestedDataArray;
}
}
49 changes: 48 additions & 1 deletion source/php/Submission.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace ModularityFormBuilder;

use ModularityFormBuilder\Helper\NestedFields;

/**
* Class Submission
* @package ModularityFormBuilder
Expand Down Expand Up @@ -466,6 +468,7 @@ public static function getSubmissionData($submissionId): array
if (!$formId) {
return array();
}

$fields = get_fields($formId);
$fields = $fields['form_fields'];
if (get_option('options_mod_form_crypt')) {
Expand All @@ -482,6 +485,9 @@ public static function getSubmissionData($submissionId): array
'custom_content',
'collapse'
);

$nestedDataArray = NestedFields::createNestedArrayFromFieldData($data);

foreach ($fields as $field) {
if ($field['acf_fc_layout'] === 'sender') {
// Merge default and custom labels
Expand All @@ -494,14 +500,55 @@ public static function getSubmissionData($submissionId): array
} elseif (in_array($field['acf_fc_layout'], $excludedFields)) {
continue;
} else {
$formdata[$field['label']] = $data[sanitize_title($field['label'])] ?? '';
$formdata[$field['label'] . self::increaseKeyValue($formdata, $field['label'])] = self::findMatchingNestedIndataArrayValue($nestedDataArray, sanitize_title($field['label'])) ?? ((!empty($data[sanitize_title($field['label'])])) ? $data[sanitize_title($field['label'])] : '');
}
}

$formdata['modularity-form-history'] = $data['modularity-form-history'] ?? '';
$formdata['modularity-form-url'] = $data['modularity-form-url'] ?? '';
return $formdata;
}

/**
* Increases the key value for a given form data array.
*
* @param array $formdata The form data array.
* @param string $key The key to increase the value for.
* @return string The increased key value.
*/
private static function increaseKeyValue($formdata, $key)
{
$i = 0;
if (array_key_exists($key, $formdata)) {
$i = 1;
while (array_key_exists($key . '-' . $i, $formdata)) {
$i++;
}
}

return ($i > 0) ? '-' . $i : '';
}

/**
* Finds and returns the value associated with a given key in a nested indata array.
* Handles multiple items with the same label/key
*
* @param array $nestedIndataArray The nested indata array to search in.
* @param string $key The key to search for.
* @return mixed The value associated with the given key, or an empty string if the key is not found.
*/
private static function findMatchingNestedIndataArrayValue(&$nestedIndataArray, $key)
{
foreach ($nestedIndataArray as $index => $item) {
if ($item['key'] == $key) {
unset($nestedIndataArray[$index]);
return $item['value'];
}
}

return '';
}

/**
* Notify users about new submission
* @param string $email
Expand Down

0 comments on commit 6dd8cb7

Please sign in to comment.