Gravity Perks // GF Nested Forms // Auto-attach File Uploads to Parent Form Notifications
<?php
/**
* Gravity Perks // GF Nested Forms // Auto-attach File Uploads to Parent Form Notifications
* http://gravitywiz.com/documentation/gravity-forms-nested-forms/
*/
add_filter( 'gform_notification', function( $notification, $form, $entry ) {
if( ! is_callable( 'gp_nested_forms' ) ) {
return $notification;
}
$nested_form_fields = GFCommon::get_fields_by_type( $form, array( 'form' ) );
if( empty( $nested_form_fields ) ) {
return $notification;
}
$parent_entry = new GPNF_Entry( $entry );
$attachments = array();
foreach( $nested_form_fields as $nested_form_field ) {
$child_entries = $parent_entry->get_child_entries( $nested_form_field->id );
if( empty( $child_entries ) ) {
continue;
}
$child_form = GFAPI::get_form( $nested_form_field->gpnfForm );
$upload_fields = GFCommon::get_fields_by_type( $child_form, array( 'fileupload' ) );
$upload_root = GFFormsModel::get_upload_root();
foreach( $child_entries as $child_entry ) {
foreach ( $upload_fields as $upload_field ) {
// Get field value.
$attachment_urls = rgar( $child_entry, $upload_field->id );
// If field value is empty, skip.
if ( empty( $attachment_urls ) ) {
continue;
}
// Convert to array.
$attachment_urls = $upload_field->multipleFiles ? json_decode( stripslashes( $attachment_urls ), true ) : array( $attachment_urls );
// Loop through attachment URLs; replace URL with path and add to attachments.
foreach ( $attachment_urls as $attachment_url ) {
$attachment_url = preg_replace( '|^(.*?)/gravity_forms/|', $upload_root, $attachment_url );
$attachments[] = $attachment_url;
}
}
}
$notification['attachments'] = array_merge( rgar( $notification, 'attachments', array() ), $attachments );
return $notification;
}
}, 10, 3 );