diff --git a/source/php/Entity/PostType.php b/source/php/Entity/PostType.php index b7664dbb..3a897f99 100644 --- a/source/php/Entity/PostType.php +++ b/source/php/Entity/PostType.php @@ -2,6 +2,8 @@ namespace ModularityFormBuilder\Entity; +use ModularityFormBuilder\Helper\NestedFields; + class PostType { public $postTypeSlug; @@ -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 diff --git a/source/php/Helper/NestedFields.php b/source/php/Helper/NestedFields.php new file mode 100644 index 00000000..e535d1e7 --- /dev/null +++ b/source/php/Helper/NestedFields.php @@ -0,0 +1,33 @@ + $value) { + $pattern = '/^id-\d+-/'; + $key = preg_replace($pattern, "", sanitize_title($key), 1); + $nestedDataArray[] = [ + 'key' => $key, + 'value' => $value + ]; + } + } + + return $nestedDataArray; + } +} diff --git a/source/php/Submission.php b/source/php/Submission.php index 340ac1f5..9671079b 100644 --- a/source/php/Submission.php +++ b/source/php/Submission.php @@ -2,6 +2,8 @@ namespace ModularityFormBuilder; +use ModularityFormBuilder\Helper\NestedFields; + /** * Class Submission * @package ModularityFormBuilder @@ -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')) { @@ -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 @@ -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