You are here

function mailchimp_automations_trigger_workflow in Mailchimp 7.4

Same name and namespace in other branches
  1. 7.5 modules/mailchimp_automations/mailchimp_automations.module \mailchimp_automations_trigger_workflow()

Triggers a workflow automation via the Mailchimp API.

Parameters

object $automation: The MailchimpAutomationsEntity object from the database.

EntityMetadataWrapper $wrapped_entity: The wrapped entity that triggered the workflow automation.

1 call to mailchimp_automations_trigger_workflow()
mailchimp_automations_entity_insert in modules/mailchimp_automations/mailchimp_automations.module
Implements hook_entity_insert().

File

modules/mailchimp_automations/mailchimp_automations.module, line 158
Module file for mailchimp_automations.

Code

function mailchimp_automations_trigger_workflow($automation_entity, $wrapped_entity) {
  $email_property_field = $automation_entity->email_property;
  $email = $wrapped_entity->{$email_property_field}
    ->value();
  if (!mailchimp_is_subscribed($automation_entity->list_id, $email)) {
    $merge_vars = NULL;
    drupal_alter('mailchimp_automations_mergevars', $merge_vars, $automation_entity, $wrapped_entity);

    // Skip mailchimp_subscribe to avoid cron if set
    $added = mailchimp_subscribe_process($automation_entity->list_id, $email, $merge_vars);
    if (empty($added) || isset($added->success) && $added->success === FALSE) {
      watchdog('mailchimp', 'An error occurred subscribing @email to list @list during a workflow @automation. The automation did not comlplete.', array(
        '@automation' => $automation_entity->label,
        '@email' => $email,
      ), WATCHDOG_ERROR);
    }
  }
  $mc_auto = mailchimp_get_api_object('MailchimpAutomations');
  try {
    $result = $mc_auto
      ->addWorkflowEmailSubscriber($automation_entity->workflow_id, $automation_entity->workflow_email_id, $email);
    if ($result) {
      module_invoke_all('mailchimp_automations_workflow_email_triggered', $automation_entity, $email, $wrapped_entity);
    }
  } catch (Exception $e) {
    watchdog('mailchimp', 'An error occurred triggering a workflow automation. Workflow: @automation, Email: @email. The automation did not successfully complete. "%message"', array(
      '@automation' => $automation_entity->label,
      '@email' => $email,
      '%message' => $e
        ->getMessage(),
    ), WATCHDOG_ERROR);
  }
}