Process Group Repeats Programmatically
-
I am trying to access my form entries with php and have troubles into creating an object that reflects users data entries. I don’t understand the logic which the child fields of a repeatable group follow. some seem to have suffix “-\d” some seem to count up. So in my child there are ids like ‘text-2-2’ and upload-2 in the second iteration. How can I summarize these?
Are there any helper functions that return a workable object/array from the fields? I havent dug too deep into the code, but couldnt understand the enumeration logics.
Following code examples from the web I have accomplished the label/field_id mapping. Yet for child-elements of groups it doesnt seem to work. I’ve played a bit regex and parent keys to bring things together…
add_shortcode('forminator_entries', __NAMESPACE__ . '\\forminator_entries');
function forminator_entries()
{
$form_id = 63983;
// Get the form details
$form = \Forminator_API::get_form($form_id);
$entries = \Forminator_API::get_form_entries($form->id);
$field_labels = array();
$submissions = array();
// Get fields labels with hierarchy
if (!is_null($form)) {
if (is_array($form->fields)) {
foreach ($form->fields as $field) {
/** @var Forminator_Form_Field_Model $field */
if (!empty($field->parent_group)) {
// Handle fields within groups
if (!isset($field_labels[$field->parent_group])) {
$field_labels[$field->parent_group] = array();
}
// Attempt to assign the field label
$field_labels[$field->slug] = [
'label' => $field->get_label_for_entry(),
'parent' => $field->parent_group,
];
} else {
// Handle top-level fields
$field_labels[$field->slug] = [
'label' => $field->get_label_for_entry(),
'parent' => null,
];
}
}
}
}
// Set limit
$limit = count($entries);
// Collect entries
for ($i = 0; $i < $limit; $i++) {
/** @var Forminator_Form_Entry_Model $entry */
$entry = $entries[$i];
$entry_data = array(
'id' => $entry->entry_id,
'time' => $entry->time_created,
'fields' => array(),
);
foreach ($entry->meta_data as $field_id => $meta) {
$cleanField = preg_replace('/-\d+(-\d+)$/', '$1', $field_id);
if (isset($field_labels[$cleanField]['label'])) {
$label = $field_labels[$cleanField]['label'];
$parent = $field_labels[$cleanField]['parent'];
if ($parent) {
$pattern = '/-\d+-(\d+)/';
if (preg_match($pattern, $field_id, $matches)) {
$grouped_count = $matches[1];
} else {
$grouped_count = 0;
}
$entry_data[$parent][$grouped_count][$label] = $meta['value'];
do_action('qm/debug',[$field_id,$cleanField,$grouped_count,$parent,$label ] );
} else {
$entry_data[$label] = $meta['value'];
}
} else {
do_action('qm/debug','doesnotexist:' .$cleanField);
}
}
// TODO foreach group
// Check if the custom post type with meta 'spenden_id' exists
$existing_post = get_posts(array(
'post_type' => 'moebelspende',
'meta_key' => 'spenden_id',
'meta_value' => $entry->entry_id,
'posts_per_page' => 1,
));
// If no existing post, create a new one
if (empty($existing_post)) {
$post_id = wp_insert_post(array(
'post_title' => 'Entry ID ' . $entry->entry_id,
'post_type' => 'moebelspende',
'post_status' => 'publish',
));
} else {
$post_id = $existing_post[0]->ID;
}
$post_data = array(
'ID' => $post_id,
//DEBUG
//'post_content' => '<h2>Fields</h2><pre>' . print_r($entry_data,true) . '</pre>' . '<h2>Labels</h2><pre>' . print_r($field_labels,true) . '</pre>',
'meta_input' => array(
'spenden_id' => $entry->entry_id,
'time_created' => $entry->time_created,
)
);
foreach ($entry_data as $field_key => $field_value) {
$post_data['meta_input'][$field_key] = $field_value;
}
// DEBUG
//wp_update_post($post_data);
$submissions[] = $entry_data;
}
$test = '<h2>Raw</h2><pre>' . print_r($submissions, true) . '</pre>';
return $test;
}Any help is kindly appreciated!
- You must be logged in to reply to this topic.