Skip to content
Snippets Groups Projects
Commit beda9ad9 authored by Christian Wach's avatar Christian Wach :soccer:
Browse files

First pass at Conditional Email action

parent 658b6565
No related branches found
No related tags found
No related merge requests found
...@@ -114,11 +114,11 @@ class CFAFA_ACFE { ...@@ -114,11 +114,11 @@ class CFAFA_ACFE {
// Include class files. // Include class files.
include CFAFA_PATH . 'includes/form-actions/cfafa-form-action-base.php'; include CFAFA_PATH . 'includes/form-actions/cfafa-form-action-base.php';
include CFAFA_PATH . 'includes/form-actions/cfafa-form-action-redirect.php'; include CFAFA_PATH . 'includes/form-actions/cfafa-form-action-redirect.php';
//include CFAFA_PATH . 'includes/form-actions/cfafa-form-action-email.php'; include CFAFA_PATH . 'includes/form-actions/cfafa-form-action-email.php';
// Instantiate the Form Actions. // Instantiate the Form Actions.
new CFAFA_Form_Action_Redirect( $this ); new CFAFA_Form_Action_Redirect( $this );
//new CFAFA_Form_Action_Email( $this ); new CFAFA_Form_Action_Email( $this );
// Maybe add WooCommerce Product Action. // Maybe add WooCommerce Product Action.
if ( function_exists( 'WC' ) ) { if ( function_exists( 'WC' ) ) {
......
...@@ -73,7 +73,7 @@ class CFAFA_Form_Action_Base { ...@@ -73,7 +73,7 @@ class CFAFA_Form_Action_Base {
public function __construct() { public function __construct() {
// Callback for the "acfe/form/load/..." hook. // Callback for the "acfe/form/load/..." hook.
add_filter( 'acfe/form/load/' . $this->action_name, [ $this, 'load' ], 10, 3 ); add_filter( 'acfe/form/load/' . $this->action_name, [ $this, 'load' ], 10, 3 );
// Callback for the "acfe/form/make/..." hook. // Callback for the "acfe/form/make/..." hook.
add_action( 'acfe/form/make/' . $this->action_name, [ $this, 'make' ], 10, 3 ); add_action( 'acfe/form/make/' . $this->action_name, [ $this, 'make' ], 10, 3 );
...@@ -235,10 +235,14 @@ class CFAFA_Form_Action_Base { ...@@ -235,10 +235,14 @@ class CFAFA_Form_Action_Base {
// Build Mapping Tab. // Build Mapping Tab.
$mapping_tab_fields = $this->tab_mapping_add(); $mapping_tab_fields = $this->tab_mapping_add();
// Build Attachments Tab.
$attachments_tab_fields = $this->tab_attachments_add();
// Combine Sub-Fields. // Combine Sub-Fields.
$sub_fields = array_merge( $sub_fields = array_merge(
$action_tab_fields, $action_tab_fields,
$mapping_tab_fields $mapping_tab_fields,
$attachments_tab_fields
); );
/** /**
...@@ -362,14 +366,20 @@ class CFAFA_Form_Action_Base { ...@@ -362,14 +366,20 @@ class CFAFA_Form_Action_Base {
* *
* @since 0.1 * @since 0.1
* *
* @param string $label The label for this section.
* @return array $fields The array of Fields for this section. * @return array $fields The array of Fields for this section.
*/ */
public function tab_mapping_header() { public function tab_mapping_header( $label = '' ) {
// Set a default label.
if ( empty( $label ) ) {
$label = __( 'Mapping', 'conditional-form-actions-for-acfe' );
}
// "Mapping" Tab wrapper. // "Mapping" Tab wrapper.
$mapping_tab = [ [ $mapping_tab = [ [
'key' => $this->field_key . 'tab_load', 'key' => $this->field_key . 'tab_load',
'label' => __( 'Mapping', 'conditional-form-actions-for-acfe' ), 'label' => $label,
'name' => '', 'name' => '',
'type' => 'tab', 'type' => 'tab',
'instructions' => '', 'instructions' => '',
...@@ -396,6 +406,57 @@ class CFAFA_Form_Action_Base { ...@@ -396,6 +406,57 @@ class CFAFA_Form_Action_Base {
} }
/**
* Defines the "Attachments" Tab.
*
* @since 0.1
*
* @return array $fields The array of Fields for this section.
*/
public function tab_attachments_add() {
$fields = [];
return $fields;
}
/**
* Defines the "Attachments" Tab Header.
*
* @since 0.1
*
* @return array $fields The array of Fields for this section.
*/
public function tab_attachments_header() {
// "Attachments" Tab wrapper.
$attachments_tab = [ [
'key' => $this->field_key . 'tab_attachments',
'label' => __( 'Attachments', 'civicrm-wp-profile-sync' ),
'name' => '',
'type' => 'tab',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => [
'width' => '',
'class' => '',
'id' => '',
'data-no-preference' => true,
],
'acfe_permissions' => '',
'placement' => 'top',
'endpoint' => 0,
] ];
// Combine Fields.
$fields = array_merge(
$attachments_tab
);
// --<
return $fields;
}
/** /**
* Gets the array that defines a "Map Field" for the "Mapping" Tab. * Gets the array that defines a "Map Field" for the "Mapping" Tab.
* *
...@@ -460,7 +521,24 @@ class CFAFA_Form_Action_Base { ...@@ -460,7 +521,24 @@ class CFAFA_Form_Action_Base {
$helpers = acf_get_instance( 'acfe_dynamic_forms_helpers' ); $helpers = acf_get_instance( 'acfe_dynamic_forms_helpers' );
// Populate mapping Fields. // Populate mapping Fields.
add_filter( 'acf/prepare_field/name=' . $this->field_name . 'map_' . $code, [ $helpers, 'map_fields_deep_no_custom' ] ); add_filter( 'acf/prepare_field/name=' . $this->field_name . 'map_' . $code, [ $helpers, 'map_fields_deep_no_custom' ] );
}
/**
* Adds filters that configure a named Field when loaded.
*
* @since 0.1
*
* @param string $name The unique name for the Field.
*/
public function mapping_field_filter_deep( $name ) {
// Grab reference to ACFE Helper object.
$helpers = acf_get_instance( 'acfe_dynamic_forms_helpers' );
// Populate named Field.
add_filter( 'acf/prepare_field/name=' . $name, [ $helpers, 'map_fields_deep' ] );
} }
......
...@@ -98,14 +98,15 @@ class CFAFA_Form_Action_Email extends CFAFA_Form_Action_Base { ...@@ -98,14 +98,15 @@ class CFAFA_Form_Action_Email extends CFAFA_Form_Action_Base {
// Declare the mapped Email Fields with translatable titles. // Declare the mapped Email Fields with translatable titles.
$this->mapped_email_fields = [ $this->mapped_email_fields = [
'subject' => __( 'Subject', 'conditional-form-actions-for-acfe' ),
'from_name' => __( 'From Name', 'conditional-form-actions-for-acfe' ), 'from_name' => __( 'From Name', 'conditional-form-actions-for-acfe' ),
'from_email' => __( 'From Email', 'conditional-form-actions-for-acfe' ), 'from_email' => __( 'From Email', 'conditional-form-actions-for-acfe' ),
'alternative_receiver_address' => __( 'Alternative Receiver Address', 'conditional-form-actions-for-acfe' ), 'reply_to_name' => __( 'Reply To Name', 'conditional-form-actions-for-acfe' ),
'cc' => __( 'Carbon Copy', 'conditional-form-actions-for-acfe' ), 'reply_to_email' => __( 'Reply To Email', 'conditional-form-actions-for-acfe' ),
'bcc' => __( 'Blind Carbon Copy', 'conditional-form-actions-for-acfe' ), 'to_name' => __( 'To Name', 'conditional-form-actions-for-acfe' ),
//'extra_data' => __( 'Extra Data', 'conditional-form-actions-for-acfe' ), 'to_email' => __( 'To Email', 'conditional-form-actions-for-acfe' ),
//'from_email_option' => __( 'From Email Option', 'conditional-form-actions-for-acfe' ), 'cc' => __( 'Cc', 'conditional-form-actions-for-acfe' ),
'bcc' => __( 'Bcc', 'conditional-form-actions-for-acfe' ),
'subject' => __( 'Subject', 'conditional-form-actions-for-acfe' ),
]; ];
// Populate mapping Fields. // Populate mapping Fields.
...@@ -113,45 +114,11 @@ class CFAFA_Form_Action_Email extends CFAFA_Form_Action_Base { ...@@ -113,45 +114,11 @@ class CFAFA_Form_Action_Email extends CFAFA_Form_Action_Base {
$this->mapping_field_filters_add( $name ); $this->mapping_field_filters_add( $name );
} }
// Declare the Email Contact Fields with translatable titles.
$this->contact_fields = [
'contact_id' => __( 'Recipient CiviCRM Contact', 'conditional-form-actions-for-acfe' ),
];
// Handle Contact Fields.
foreach ( $this->contact_fields as $name => $title ) {
// Populate mapping Fields.
$this->mapping_field_filters_add( $name );
// Add Contact Action Reference Field to ACF Model.
$this->js_model_contact_reference_field_add( $this->field_name . 'ref_' . $name );
// Pre-load with "Generic" values.
//$filter = 'acf/prepare_field/name=' . $this->field_name . 'map_' . $name;
//add_filter( $filter, [ $this, 'prepare_choices' ], 5 );
}
// Email Conditional Field. // Email Conditional Field.
$this->mapping_field_filters_add( 'email_conditional' ); $this->mapping_field_filters_add( 'email_conditional' );
} // Map "File" Field.
$this->mapping_field_filter_deep( $this->field_name . 'file' );
/**
* Pre-load mapping Fields with "Generic" choices.
*
* Not used but leaving this here for future use.
*
* @since 0.1
*
* @param array $field The existing array of Field data.
* @param array $field The modified array of Field data.
*/
public function prepare_choices( $field ) {
// --<
return $field;
} }
...@@ -198,8 +165,27 @@ class CFAFA_Form_Action_Email extends CFAFA_Form_Action_Base { ...@@ -198,8 +165,27 @@ class CFAFA_Form_Action_Email extends CFAFA_Form_Action_Base {
// Populate Email data array. // Populate Email data array.
$email = $this->form_email_data( $form, $current_post_id, $action ); $email = $this->form_email_data( $form, $current_post_id, $action );
// Send the Email with the data from the Form. // Populate Attachments data array.
$email = $this->form_email_save( $email ); $attachments = $this->form_attachments_data( $form, $current_post_id, $action );
// Check Conditional.
if ( $this->form_conditional_check( $email ) ) {
// Build the arguments.
$args = $this->form_build_args( $email, $attachments, $form, $current_post_id, $action );
// Skip when the args have been overridden.
if ( ! empty( $args ) ) {
// Send the Email with the built arguments.
$email = $this->form_email_send( $args, $email );
// Maybe remove Dynamic Attachments.
$this->form_attachments_delete( $attachments );
}
}
// Save the results of this Action for later use. // Save the results of this Action for later use.
$this->make_action_save( $action, $email ); $this->make_action_save( $action, $email );
...@@ -215,151 +201,8 @@ class CFAFA_Form_Action_Email extends CFAFA_Form_Action_Base { ...@@ -215,151 +201,8 @@ class CFAFA_Form_Action_Email extends CFAFA_Form_Action_Base {
*/ */
public function tab_action_append() { public function tab_action_append() {
// Define Template Field.
$template_field = [
'key' => $this->field_key . 'template',
'label' => __( 'Message Template', 'conditional-form-actions-for-acfe' ),
'name' => $this->field_name . 'template',
'type' => 'select',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => [
'width' => '',
'class' => '',
'id' => '',
'data-instruction-placement' => 'field',
],
'acfe_permissions' => '',
'default_value' => '',
'placeholder' => '',
'allow_null' => 0,
'multiple' => 0,
'ui' => 0,
'return_format' => 'value',
'choices' => $this->civicrm->email->template_options_get(),
];
// Define "Disable Smarty" Field.
$smarty_field = [
'key' => $this->field_key . 'disable_smarty',
'label' => __( 'Disable Smarty', 'conditional-form-actions-for-acfe' ),
'name' => $this->field_name . 'disable_smarty',
'type' => 'true_false',
'instructions' => __( 'Disable Smarty. Normal CiviMail tokens are still supported. By default Smarty is enabled if configured by CIVICRM_MAIL_SMARTY.', 'conditional-form-actions-for-acfe' ),
'required' => 0,
'conditional_logic' => 0,
'wrapper' => [
'width' => '',
'class' => '',
'id' => '',
'data-instruction-placement' => 'field',
],
'acfe_permissions' => '',
'message' => '',
'default_value' => 0,
'ui' => 1,
'ui_on_text' => '',
'ui_off_text' => '',
];
// Define "Create Activity" Field.
$create_activity_field = [
'key' => $this->field_key . 'create_activity',
'label' => __( 'Create Activity', 'conditional-form-actions-for-acfe' ),
'name' => $this->field_name . 'create_activity',
'type' => 'true_false',
'instructions' => __( 'Usually an Email Activity is created when an Email is sent.', 'conditional-form-actions-for-acfe' ),
'required' => 0,
'conditional_logic' => 0,
'wrapper' => [
'width' => '',
'class' => '',
'id' => '',
'data-instruction-placement' => 'field',
],
'acfe_permissions' => '',
'message' => '',
'default_value' => 1,
'ui' => 1,
'ui_on_text' => '',
'ui_off_text' => '',
];
// Define "Activity Details" Field.
$activity_details_field = [
'key' => $this->field_key . 'activity_details',
'label' => __( 'Activity Details', 'conditional-form-actions-for-acfe' ),
'name' => $this->field_name . 'activity_details',
'type' => 'select',
'instructions' => '',
'required' => 0,
'wrapper' => [
'width' => '',
'class' => '',
'id' => '',
'data-instruction-placement' => 'field',
],
'acfe_permissions' => '',
'default_value' => 'html,text',
'placeholder' => '',
'allow_null' => 0,
'multiple' => 0,
'ui' => 0,
'return_format' => 'value',
'choices' => [
'html,text' => __( 'HTML and Text versions of the body', 'conditional-form-actions-for-acfe' ),
'tplName' => __( 'Just the name of the message template', 'conditional-form-actions-for-acfe' ),
'html' => __( 'Just the HTML version of the body', 'conditional-form-actions-for-acfe' ),
'text' => __( 'Just the text version of the body', 'conditional-form-actions-for-acfe' ),
],
'conditional_logic' => [
[
[
'field' => $this->field_key . 'create_activity',
'operator' => '==',
'value' => 1,
],
],
],
];
// Init Fields. // Init Fields.
$fields = [ $fields = [];
$template_field,
$smarty_field,
$create_activity_field,
$activity_details_field,
];
// Add Case Field if the CiviCase component is active.
$case_active = $this->civicrm->is_component_enabled( 'CiviCase' );
if ( $case_active ) {
$fields[] = [
'key' => $this->field_key . 'email_case_id',
'label' => __( 'Case', 'conditional-form-actions-for-acfe' ),
'name' => $this->field_name . 'email_case_id',
'type' => 'cfafa_acfe_case_action_ref',
'instructions' => __( 'Select a Case Action in this Form.', 'conditional-form-actions-for-acfe' ),
'required' => 0,
'wrapper' => [
'width' => '',
'class' => '',
'id' => '',
'data-instruction-placement' => 'field',
],
'acfe_permissions' => '',
'default_value' => '',
'placeholder' => __( 'None', 'conditional-form-actions-for-acfe' ),
'allow_null' => 1,
'multiple' => 0,
'ui' => 0,
'return_format' => 'value',
'choices' => [],
];
}
// Add Conditional Field. // Add Conditional Field.
$code = 'email_conditional'; $code = 'email_conditional';
...@@ -385,10 +228,8 @@ class CFAFA_Form_Action_Email extends CFAFA_Form_Action_Base { ...@@ -385,10 +228,8 @@ class CFAFA_Form_Action_Email extends CFAFA_Form_Action_Base {
public function tab_mapping_add() { public function tab_mapping_add() {
// Get Tab Header. // Get Tab Header.
$mapping_tab_header = $this->tab_mapping_header(); $label = __( 'Email', 'conditional-form-actions-for-acfe' );
$mapping_tab_header = $this->tab_mapping_header( $label );
// Build Contacts Accordion.
$mapping_contacts_accordion = $this->tab_mapping_accordion_contacts_add();
// Build Email Details Accordion. // Build Email Details Accordion.
$mapping_email_accordion = $this->tab_mapping_accordion_email_add(); $mapping_email_accordion = $this->tab_mapping_accordion_email_add();
...@@ -396,7 +237,6 @@ class CFAFA_Form_Action_Email extends CFAFA_Form_Action_Base { ...@@ -396,7 +237,6 @@ class CFAFA_Form_Action_Email extends CFAFA_Form_Action_Base {
// Combine Sub-Fields. // Combine Sub-Fields.
$fields = array_merge( $fields = array_merge(
$mapping_tab_header, $mapping_tab_header,
$mapping_contacts_accordion,
$mapping_email_accordion $mapping_email_accordion
); );
...@@ -406,171 +246,48 @@ class CFAFA_Form_Action_Email extends CFAFA_Form_Action_Base { ...@@ -406,171 +246,48 @@ class CFAFA_Form_Action_Email extends CFAFA_Form_Action_Base {
} }
/** /**
* Defines the Fields in the "Contacts" Accordion. * Defines the Fields in the "Email Fields" Accordion.
* *
* @since 0.1 * @since 0.1
* *
* @return array $fields The array of Fields for this section. * @return array $fields The array of Fields for this section.
*/ */
public function tab_mapping_accordion_contacts_add() { public function tab_mapping_accordion_email_add() {
// Init return. // Init return.
$fields = []; $fields = [];
// "Recipient Contact Reference" Accordion wrapper open. // Add "Mapping" Fields.
$fields[] = [ foreach ( $this->mapped_email_fields as $name => $title ) {
'key' => $this->field_key . 'mapping_accordion_contacts_open', $fields[] = $this->mapping_field_get( $name, $title );
'label' => __( 'Recipient Contact Reference', 'conditional-form-actions-for-acfe' ),
'name' => '',
'type' => 'accordion',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => [
'width' => '',
'class' => '',
'id' => '',
],
'acfe_permissions' => '',
'open' => 0,
'multi_expand' => 1,
'endpoint' => 0,
];
// Add Contact Reference Fields.
foreach ( $this->contact_fields as $name => $title ) {
// Bundle them into a container group.
$contact_group_field = [
'key' => $this->field_key . 'contact_group_' . $name,
'label' => $title,
'name' => $this->field_name . 'contact_group_' . $name,
'type' => 'group',
'instructions' => sprintf( __( 'Use one Field to identify the %s.', 'conditional-form-actions-for-acfe' ), $title ),
'wrapper' => [
'width' => '',
'class' => '',
'id' => '',
],
'required' => 0,
'layout' => 'block',
];
// Define Contact Action Reference Field.
$contact_group_field['sub_fields'][] = [
'key' => $this->field_key . 'ref_' . $name,
'label' => __( 'CiviCRM Contact Action', 'conditional-form-actions-for-acfe' ),
'name' => $this->field_name . 'ref_' . $name,
'type' => 'cfafa_acfe_contact_action_ref',
'instructions' => __( 'Select a Contact Action in this Form.', 'conditional-form-actions-for-acfe' ),
'required' => 0,
'wrapper' => [
'width' => '',
'class' => '',
'id' => '',
],
'acfe_permissions' => '',
'default_value' => '',
'placeholder' => __( 'None', 'conditional-form-actions-for-acfe' ),
'allow_null' => 0,
'multiple' => 0,
'ui' => 0,
'return_format' => 'value',
'choices' => [],
'conditional_logic' => [
[
[
'field' => $this->field_key . 'map_' . $name,
'operator' => '==empty',
],
[
'field' => $this->field_key . 'cid_' . $name,
'operator' => '==empty',
],
],
],
];
// Define Contact ID Field.
$cid_field = [
'key' => $this->field_key . 'cid_' . $name,
'label' => __( 'CiviCRM Contact ID', 'conditional-form-actions-for-acfe' ),
'name' => $this->field_name . 'cid_' . $name,
'type' => 'civicrm_contact',
'instructions' => __( 'Select a CiviCRM Contact ID from the database.', 'conditional-form-actions-for-acfe' ),
'required' => 0,
'wrapper' => [
'width' => '',
'class' => '',
'id' => '',
],
'acfe_permissions' => '',
'default_value' => '',
'placeholder' => __( 'None', 'conditional-form-actions-for-acfe' ),
'allow_null' => 0,
'multiple' => 0,
'ui' => 0,
'return_format' => 'value',
'choices' => [],
'conditional_logic' => [
[
[
'field' => $this->field_key . 'ref_' . $name,
'operator' => '==empty',
],
[
'field' => $this->field_key . 'map_' . $name,
'operator' => '==empty',
],
],
],
];
// Add Contact ID Field.
$contact_group_field['sub_fields'][] = $cid_field;
// Define Custom Contact Reference Field.
$title = sprintf( __( 'Custom Contact Reference', 'conditional-form-actions-for-acfe' ), $title );
$mapping_field = $this->mapping_field_get( $name, $title );
$mapping_field['instructions'] = __( 'Define a custom Contact Reference.', 'conditional-form-actions-for-acfe' );
$mapping_field['conditional_logic'] = [
[
[
'field' => $this->field_key . 'ref_' . $name,
'operator' => '==empty',
],
[
'field' => $this->field_key . 'cid_' . $name,
'operator' => '==empty',
],
],
];
// Add Custom Contact Reference Field.
$contact_group_field['sub_fields'][] = $mapping_field;
// Add Contact Reference Group.
$fields[] = $contact_group_field;
} }
// "Recipient Contact Reference" Accordion wrapper close. // Add Content Field.
$fields[] = [ $fields[] = [
'key' => $this->field_key . 'mapping_accordion_contacts_close', 'key' => $this->field_key . 'content',
'label' => __( 'Recipient Contact Reference', 'conditional-form-actions-for-acfe' ), 'label' => __( 'Content', 'conditional-form-actions-for-acfe' ),
'name' => '', 'name' => $this->field_name . 'content',
'type' => 'accordion', 'type' => 'wysiwyg',
'instructions' => '', 'instructions' => sprintf(
__( 'Fields values may be included using %1$s{field:field_key}%2$s %1$s{field:title}%2$s. All fields may be included using %1$s{fields}%2$s.%3$sSee "Cheatsheet" tab for advanced usage.', 'conditional-form-actions-for-acfe' ),
'<code>',
'</code>',
'<br />'
),
'required' => 0, 'required' => 0,
'conditional_logic' => 0, 'conditional_logic' => 0,
'wrapper' => [ 'wrapper' => [
'width' => '', 'width' => '',
'class' => '', 'class' => '',
'id' => '', 'id' => '',
'data-instruction-placement' => 'field'
], ],
'acfe_permissions' => '', 'acfe_permissions' => '',
'open' => 0, 'default_value' => '',
'multi_expand' => 1, 'tabs' => 'all',
'toolbar' => 'full',
'media_upload' => 1,
'delay' => 0,
'endpoint' => 1, 'endpoint' => 1,
]; ];
...@@ -580,23 +297,23 @@ class CFAFA_Form_Action_Email extends CFAFA_Form_Action_Base { ...@@ -580,23 +297,23 @@ class CFAFA_Form_Action_Email extends CFAFA_Form_Action_Base {
} }
/** /**
* Defines the Fields in the "Email Fields" Accordion. * Defines the "Attachments" Tab.
* *
* @since 0.1 * @since 0.1
* *
* @return array $fields The array of Fields for this section. * @return array $fields The array of Fields for this section.
*/ */
public function tab_mapping_accordion_email_add() { public function tab_attachments_add() {
// Init return. // Init tab array.
$fields = []; $attachments_tab = [];
// "Email Fields" Accordion wrapper open. // Add "Dynamic Files" Repeater.
$fields[] = [ $attachments_tab[] = [
'key' => $this->field_key . 'mapping_accordion_email_open', 'key' => $this->field_key . 'files',
'label' => __( 'Email Fields', 'conditional-form-actions-for-acfe' ), 'label' => __( 'Dynamic Files', 'conditional-form-actions-for-acfe' ),
'name' => '', 'name' => $this->field_name . 'files',
'type' => 'accordion', 'type' => 'repeater',
'instructions' => '', 'instructions' => '',
'required' => 0, 'required' => 0,
'conditional_logic' => 0, 'conditional_logic' => 0,
...@@ -606,22 +323,66 @@ class CFAFA_Form_Action_Email extends CFAFA_Form_Action_Base { ...@@ -606,22 +323,66 @@ class CFAFA_Form_Action_Email extends CFAFA_Form_Action_Base {
'id' => '', 'id' => '',
], ],
'acfe_permissions' => '', 'acfe_permissions' => '',
'open' => 0, 'acfe_repeater_stylised_button' => 0,
'multi_expand' => 1, 'collapsed' => '',
'endpoint' => 0, 'min' => 0,
'max' => 0,
'layout' => 'table',
'button_label' => __( 'Add File', 'conditional-form-actions-for-acfe' ),
'sub_fields' => [
[
'key' => $this->field_key . 'file',
'label' => __( 'File', 'conditional-form-actions-for-acfe' ),
'name' => $this->field_name . 'file',
'type' => 'select',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => [
'width' => '',
'class' => '',
'id' => '',
],
'acfe_permissions' => '',
'choices' => [],
'default_value' => [],
'allow_null' => 0,
'multiple' => 0,
'ui' => 1,
'return_format' => 'value',
'ajax' => 0,
'placeholder' => '',
'search_placeholder' => __( 'Enter a custom value or template tag. (See "Cheatsheet" tab)', 'conditional-form-actions-for-acfe' ),
'allow_custom' => 1,
],
[
'key' => $this->field_key . 'file_delete',
'label' => __( 'Delete File', 'conditional-form-actions-for-acfe' ),
'name' => $this->field_name . 'file_delete',
'type' => 'true_false',
'instructions' => '',
'required' => 0,
'wrapper' => [
'width' => '',
'class' => '',
'id' => '',
],
'acfe_permissions' => '',
'message' => __( 'Delete once submitted', 'conditional-form-actions-for-acfe' ),
'default_value' => 0,
'ui' => 1,
'ui_on_text' => '',
'ui_off_text' => '',
],
],
]; ];
// Add "Mapping" Fields. // Add "Static Files" Repeater.
foreach ( $this->mapped_email_fields as $name => $title ) { $attachments_tab[] = [
$fields[] = $this->mapping_field_get( $name, $title ); 'key' => $this->field_key . 'files_static',
} 'label' => __( 'Static Files', 'conditional-form-actions-for-acfe' ),
'name' => $this->field_name . 'files_static',
// "Email Fields" Accordion wrapper close. 'type' => 'repeater',
$fields[] = [
'key' => $this->field_key . 'mapping_accordion_email_close',
'label' => __( 'Email Fields', 'conditional-form-actions-for-acfe' ),
'name' => '',
'type' => 'accordion',
'instructions' => '', 'instructions' => '',
'required' => 0, 'required' => 0,
'conditional_logic' => 0, 'conditional_logic' => 0,
...@@ -631,11 +392,41 @@ class CFAFA_Form_Action_Email extends CFAFA_Form_Action_Base { ...@@ -631,11 +392,41 @@ class CFAFA_Form_Action_Email extends CFAFA_Form_Action_Base {
'id' => '', 'id' => '',
], ],
'acfe_permissions' => '', 'acfe_permissions' => '',
'open' => 0, 'acfe_repeater_stylised_button' => 0,
'multi_expand' => 1, 'collapsed' => '',
'endpoint' => 1, 'min' => 0,
'max' => 0,
'layout' => 'table',
'button_label' => __( 'Add File', 'conditional-form-actions-for-acfe' ),
'sub_fields' => [
[
'key' => $this->field_key . 'file_static',
'label' => __( 'File', 'conditional-form-actions-for-acfe' ),
'name' => $this->field_name . 'file_static',
'type' => 'file',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => [
'width' => '',
'class' => '',
'id' => '',
],
'acfe_permissions' => '',
'return_format' => 'id',
],
],
]; ];
// Get Tab Header.
$attachments_tab_header = $this->tab_attachments_header();
// Combine Sub-Fields.
$fields = array_merge(
$attachments_tab_header,
$attachments_tab
);
// --< // --<
return $fields; return $fields;
...@@ -653,29 +444,17 @@ class CFAFA_Form_Action_Email extends CFAFA_Form_Action_Base { ...@@ -653,29 +444,17 @@ class CFAFA_Form_Action_Email extends CFAFA_Form_Action_Base {
*/ */
public function form_email_data( $form, $current_post_id, $action ) { public function form_email_data( $form, $current_post_id, $action ) {
// Init data array. // Build Fields array.
$data = []; $fields = [];
foreach ( $this->mapped_email_fields as $name => $title ) {
// Get Field data. $fields[ $name ] = get_sub_field( $this->field_key . 'map_' . $name );
$data['from'] = get_sub_field( $this->field_key . 'from' ); }
$data['from'] = acfe_form_map_field_value( $from, $current_post_id, $form );
$data['reply_to'] = get_sub_field( $this->field_key . 'reply_to' );
$data['reply_to'] = acfe_form_map_field_value( $reply_to, $current_post_id, $form );
$data['to'] = get_sub_field($this->field_key . 'to' );
$data['to'] = acfe_form_map_field_value( $to, $current_post_id, $form );
$data['cc'] = get_sub_field( $this->field_key . 'cc' );
$data['cc'] = acfe_form_map_field_value( $cc, $current_post_id, $form );
$data['bcc'] = get_sub_field( $this->field_key . 'bcc' );
$data['bcc'] = acfe_form_map_field_value( $bcc, $current_post_id, $form );
$data['subject'] = get_sub_field( $this->field_key . 'subject' ); // Populate data array with values of mapped Fields.
$data['subject'] = acfe_form_map_field_value( $subject, $current_post_id, $form ); $data = acfe_form_map_vs_fields( $fields, $fields, $current_post_id, $form );
$data['content'] = get_sub_field( $this->field_key . 'content' ); // Add Email Content.
$content = get_sub_field( $this->field_key . 'content' );
$data['content'] = acfe_form_map_field_value( $content, $current_post_id, $form ); $data['content'] = acfe_form_map_field_value( $content, $current_post_id, $form );
// Get Email Conditional Reference. // Get Email Conditional Reference.
...@@ -694,121 +473,304 @@ class CFAFA_Form_Action_Email extends CFAFA_Form_Action_Base { ...@@ -694,121 +473,304 @@ class CFAFA_Form_Action_Email extends CFAFA_Form_Action_Base {
} }
/** /**
* Sends the Conditional Email given data from mapped Fields. * Builds Attachment data array from mapped Fields.
* *
* @since 0.1 * @since 0.1
* *
* @param array $email_data The array of Email data. * @param array $form The array of Form data.
* @return array|bool $email The Email data array, or false on failure. * @param integer $current_post_id The ID of the Post from which the Form has been submitted.
* @param string $action The customised name of the action.
* @return array $data The array of Attachment data.
*/ */
public function form_email_save( $email_data ) { public function form_attachments_data( $form, $current_post_id, $action ) {
// Init return. // Init return array.
$email = false; $data = [
'attachments' => [],
'delete' => [],
];
// Process Dynamic Attachments.
if ( have_rows( $this->field_name . 'files' ) ) {
while ( have_rows( $this->field_name . 'files' ) ) {
the_row();
// Find the ID of the File.
$file_field_key = get_sub_field( $this->field_name . 'file' );
$file_id = acfe_form_map_field_value( $file_field_key, $current_post_id, $form );
// Configure the File Field to return an array.
$field = acf_get_field( $file_field_key );
$field['return_format'] = 'array';
// Build an array of data for the Dynamic Attachments.
$files = acf_format_value( $file_id, 0, $field );
$files = acf_get_array( $files );
if ( acf_maybe_get( $files, 'ID' ) ) {
$files = [ $files ];
}
$file_delete = get_sub_field( $this->field_name . 'file_delete' );
foreach ( $files as $file ) {
// Skip if there's no File ID to process.
if ( ! acf_maybe_get( $file, 'ID' ) ) {
continue;
}
// Add to Attachments array.
$data['attachments'][] = get_attached_file( $file['ID'] );
// Maybe add to delete array.
if ( $file_delete ) {
$data['delete'][] = $file['ID'];
}
}
// Skip if the Email Conditional Reference Field has a value.
if ( ! empty( $email_data['email_conditional_ref'] ) ) {
// And the Email Conditional Field has no value.
if ( empty( $email_data['email_conditional'] ) ) {
return $email;
} }
} }
// Add Custom Field data if present. // Process Static Attachments.
if ( ! empty ( $custom_data ) ) { if ( have_rows( $this->field_name . 'files_static' ) ) {
$email_data += $custom_data; while ( have_rows( $this->field_name . 'files_static' ) ) {
} the_row();
// The File reference is the Sub-field content.
$file = get_sub_field( $this->field_name . 'file_static' );
// Unset Email Conditionals. // Just add to Attachments array.
if ( isset( $email_data['email_conditional'] ) ) { $data['attachments'][] = get_attached_file( $file );
unset( $email_data['email_conditional'] );
}
} }
if ( isset( $email_data['email_conditional_ref'] ) ) {
unset( $email_data['email_conditional_ref'] ); // --<
return $data;
}
/**
* Builds the arguments array.
*
* @since 0.1
*
* @param array $email_data The array of Email data.
* @param array $attachments_data The array of Attachments data.
* @param array $form The array of Form data.
* @param integer $current_post_id The ID of the Post from which the Form has been submitted.
* @param string $action The customised name of the action.
* @return array|bool $args The arguments array, or false on failure.
*/
public function form_build_args( $email_data, $attachments_data, $form, $current_post_id, $action ) {
// Build "From" and "Reply To" params.
$from = $this->form_email_item_build( $email_data['from_name'], $email_data['from_email'] );
$reply_to = $this->form_email_item_build( $email_data['reply_to_name'], $email_data['reply_to_email'] );
// Maybe use "From" when "Reply To" is empty.
if ( empty( $reply_to ) ) {
$reply_to = $from;
} }
// Strip out empty Fields. // Build "To" param.
$email_data = $this->form_data_prepare( $email_data ); $to = $this->form_email_item_build( $email_data['to_name'], $email_data['to_email'] );
// Sanity checks. // Sanity checks.
if ( empty( $email_data['contact_id'] ) || empty( $email_data['template_id'] ) ) { if ( empty( $from ) || empty( $to ) ) {
return $email; return false;
} }
// Send the Email. // Build Email headers.
$result = $this->civicrm->email->email_send( $email_data ); $headers[] = 'From: ' . $from;
if ( ! empty( $reply_to ) ) {
$headers[] = 'Reply-To: ' . $reply_to;
}
if ( ! empty( $email_data['cc'] ) ) {
$headers[] = 'Cc: ' . $email_data['cc'];
}
if ( ! empty( $email_data['bcc'] ) ) {
$headers[] = 'Bcc: ' . $email_data['bcc'];
}
// Bail on failure. $headers[] = 'Content-Type: text/html';
if ( $result === false ) { $headers[] = 'charset=UTF-8';
return $email;
// Finally, build args array.
$args = [
'from' => $from,
'to' => $to,
'reply_to' => $reply_to,
'cc' => $email_data['cc'],
'bcc' => $email_data['bcc'],
'subject' => $email_data['subject'],
'content' => $email_data['content'],
'headers' => $headers,
'attachments' => $attachments_data['attachments'],
];
// Get the Form name.
$form_name = acf_maybe_get( $form, 'name' );
/**
* Allow others to filter the Email arguments.
*
* Returning false for any of these filters will skip sending the Email.
*
* @since 0.1
*
* @param bool $args The array of arguments.
* @param array $form The array of Form data.
* @param integer $current_post_id The ID of the Post from which the Form has been submitted.
* @param string $action The customised name of the Form Action.
*/
$filter = 'acfe/form/submit/' . $this->action_name . '/email_args';
$args = apply_filters( $filter, $args, $form, $current_post_id, $action );
$args = apply_filters( $filter . '/form=' . $form_name, $args, $form, $current_post_id, $action );
if ( ! empty( $action ) ) {
$args = apply_filters( $filter . '/action=' . $action, $args, $form, $current_post_id, $action );
} }
// --< // --<
return $result; return $args;
} }
/** /**
* Finds the linked Contact ID when it has been mapped. * Checks the Conditional Email Field given data from mapped Fields.
* *
* @since 0.1 * @since 0.1
* *
* @param string $action_name The name of the referenced Form Action. * @param array $email_data The array of Email data.
* @return integer|bool $contact_id The numeric ID of the Contact, or false if not found. * @return bool $continue True if the Action should continue or false to skip.
*/ */
public function form_contact_id_get_mapped( $action_name ) { public function form_conditional_check( $email_data ) {
// Init return. // Approve by default.
$contact_id = false; $continue = true;
// We need an Action Name. // Skip if the Email Conditional Reference Field has a value.
if ( empty( $action_name ) ) { if ( ! empty( $email_data['email_conditional_ref'] ) ) {
return $contact_id; // And the Email Conditional Field has no value.
if ( empty( $email_data['email_conditional'] ) ) {
return false;
}
} }
// Get the Contact data for that Action. // --<
$related_contact = acfe_form_get_action( $action_name, 'contact' ); return $continue;
if ( empty( $related_contact['id'] ) ) {
return $contact_id; }
/**
* Sends the Email given a set of arguments.
*
* @since 0.1
*
* @param array $args The array of Email arguments.
* @param array $email_data The array of Email data.
* @return array|bool $args The array of Email arguments, or false on failure.
*/
public function form_email_send( $args, $email_data ) {
// Define rules for checking Email headers.
$rules = [
[
'args_key' => 'from',
'value_old' => $this->form_email_item_build( $email_data['from_name'], $email_data['from_email'] ),
'header_key' => 'From:',
],
[
'args_key' => 'reply_to',
'value_old' => $this->form_email_item_build( $email_data['reply_to_name'], $email_data['reply_to_email'] ),
'header_key' => 'Reply-To:',
],
[
'args_key' => 'cc',
'value_old' => $email_data['cc'],
'header_key' => 'Cc:',
],
[
'args_key' => 'bcc',
'value_old' => $email_data['bcc'],
'header_key' => 'Bcc:',
],
];
// Check if the Email headers have been changed.
foreach ( $rules as $rule ) {
$check = acf_maybe_get( $args, $rule['args_key'] );
if ( ! empty( $check ) && $check !== $rule['value_old'] ) {
foreach ( $args['headers'] as &$header ) {
if ( stripos( $header, $rule['header_key'] ) !== 0 ) {
continue;
}
// Repair Email header.
$header = $rule['header_key'] . ' ' . $check;
break;
}
}
} }
// Assign return. // Send the Email.
$contact_id = (int) $related_contact['id']; $result = wp_mail( $args['to'], $args['subject'], $args['content'], $args['headers'], $args['attachments'] );
// Bail on failure.
if ( $result === false ) {
return false;
}
// --< // --<
return $contact_id; return $args;
} }
/** /**
* Finds the linked Case ID when it has been mapped. * Builds an Email item according to the specification.
*
* For example "First Last <foo@bar.com>".
* *
* @since 0.1 * @since 0.1
* *
* @param string $action_name The name of the referenced Form Action. * @param str $name The name.
* @return integer|bool $case_id The numeric ID of the Case, or false if not found. * @param str $email The email.
* @return str $item The built item.
*/ */
public function form_case_id_get_mapped( $action_name ) { public function form_email_item_build( $name, $email ) {
// Init return. // Init return.
$case_id = false; $item = '';
// We need an Action Name. // Parse item from params.
if ( empty( $action_name ) ) { if ( ! empty( $name ) && ! empty( $email ) ) {
return $case_id; $item = sprintf( '%1$s <%2$s>', $name, $email );
} elseif ( ! empty( $email ) ) {
$item = $email;
} }
// Get the Case ID for that Action. // --<
$related_case_id = acfe_form_get_action( $action_name, 'id' ); return $item;
if ( empty( $related_case_id ) ) {
return $case_id;
}
// Assign return. }
$case_id = (int) $related_case_id;
// --< /**
return $case_id; * Deletes the Dynamic Attachments.
*
* @since 0.1
*
* @param array $attachments_data The array of Attachments data.
*/
public function form_attachments_delete( $attachments_data ) {
// Bail if there are none.
if ( ! empty( $attachments_data['delete'] ) ) {
return;
}
// Delete all the Dynamic Attachments.
foreach ( $attachments_data['delete'] as $file_id ) {
wp_delete_attachment( $file_id, true );
}
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment