You are here

mailchimp_automations.module in Mailchimp 7.5

Same filename and directory in other branches
  1. 7.4 modules/mailchimp_automations/mailchimp_automations.module

Module file for mailchimp_automations.

File

modules/mailchimp_automations/mailchimp_automations.module
View source
<?php

/**
 * @file
 * Module file for mailchimp_automations.
 */

/**
 * Implements hook_entity_info().
 */
function mailchimp_automations_entity_info() {
  $return = array(
    'mailchimp_automations_entity' => array(
      'label' => t('MailChimp Automations Entity'),
      'plural label' => t('MailChimp Automations Entities'),
      'controller class' => 'EntityAPIControllerExportable',
      'entity class' => 'MailchimpAutomationsEntity',
      'base table' => 'mailchimp_automations_entity',
      'uri callback' => 'entity_class_uri',
      'fieldable' => FALSE,
      'exportable' => TRUE,
      'module' => 'mailchimp_automations',
      'entity keys' => array(
        'id' => 'mailchimp_automations_entity_id',
        'name' => 'name',
        'label' => 'label',
      ),
      // Enable the entity API's admin UI.
      'admin ui' => array(
        'path' => 'admin/config/services/mailchimp/automations',
        'file' => 'includes/mailchimp_automations.admin.inc',
        'controller class' => 'MailChimpAutomationUIController',
      ),
      'label callback' => 'mailchimp_automations_entity_info_label',
      'access callback' => 'mailchimp_automations_entity_access',
    ),
  );
  return $return;
}

/**
 * Entity label callback.
 */
function mailchimp_automations_entity_info_label($entity, $entity_type) {
  return empty($entity) ? 'New MailChimp Automation' : $entity->label;
}

/**
 * Access callback for mailchimp_automations_entity.
 */
function mailchimp_automations_entity_access() {
  return mailchimp_apikey_ready_access('administer mailchimp automations');
}

/**
 * Implements hook_entity_insert().
 */
function mailchimp_automations_entity_insert($entity, $type) {
  $wrapper = entity_metadata_wrapper($type, $entity);
  $bundle = $wrapper
    ->getBundle();
  if ($automation_entity = mailchimp_automations_entity_automation($type, $bundle)) {
    mailchimp_automations_trigger_workflow($automation_entity, $wrapper);
  }
}

/**
 * Access callback for activity menu items.
 */
function mailchimp_automations_access(MailchimpAutomationsEntity $mailchimp_automations_entity) {
  if (user_access('access mailchimp automations')) {
    return TRUE;
  }
  return FALSE;
}

/**
 * Loads a single or multiple instances of MailchimpAutomationsEntity.
 *
 * @param int $id
 *   Optional name of the MailchimpAutomationsEntity instance to load.
 *
 * @return mixed
 *   Array of MailchimpAutomationsEntity instances or one instance if $name is set.
 */
function mailchimp_automations_load_entities($id = NULL) {
  $types = entity_load_multiple_by_name('mailchimp_automations_entity', isset($id) ? array(
    $id,
  ) : FALSE);
  return isset($id) ? reset($types) : $types;
}

/**
 * Wrapper for MailchimpAutomations->getAutomations().
 *
 * @return array
 *   The workflow automations for the active MailChimp API account.
 */
function mailchimp_automations_get_automations() {
  $mc_auto = mailchimp_get_api_object('MailchimpAutomations');
  $results = $mc_auto
    ->getAutomations();
  return $results->automations;
}

/**
 * Wrapper for MailchimpAutomations->getWorkflow().
 *
 * @param string $workflow_id
 *   The MailChimp workflow_id.
 *
 * @return mixed
 *   Array where the $key is workflow_id and the $value is the title or null.
 */
function mailchimp_automations_get_automation($workflow_id) {
  $mc_auto = mailchimp_get_api_object('MailchimpAutomations');
  $workflow = $mc_auto
    ->getWorkflow($workflow_id);
  $title = $workflow->settings->title;
  if (!empty($title)) {
    return array(
      $workflow->id => $title,
    );
  }
  return NULL;
}

/**
 * Wrapper for MailchimpAutomations->getWorkflowEmails().
 *
 * @param string $workflow_id
 *   The MailChimp workflow_id.
 *
 * @return array
 *   An array of email workflows associated with this automation.
 */
function mailchimp_automations_get_emails_for_workflow($workflow_id) {
  $emails = array();
  $mc_auto = mailchimp_get_api_object('MailchimpAutomations');
  $results = $mc_auto
    ->getWorkflowEmails($workflow_id);
  $email_results = $results->emails;
  foreach ($email_results as $email) {
    $title = $email->settings->title;
    if (!empty($title)) {
      $emails[$email->id] = $title;
    }
  }
  return $emails;
}

/**
 * Triggers a workflow automation via the MailChimp API.
 *
 * @param object $automation_entity
 *   The MailchimpAutomationsEntity object from the database.
 * @param EntityMetadataWrapper $wrapped_entity
 *   The wrapped entity that triggered the workflow automation.
 */
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);
  }
}

/**
 * Queries to see if there is an existing automation entity
 *
 * @param string $type
 *   The entity type name
 * @param string $bundle
 *   The Drupal bundle for the entity
 *
 * @return mixed
 *   The mailchimp_automations_entity or null.
 */
function mailchimp_automations_entity_automation($type, $bundle) {
  $query = new EntityFieldQuery();
  $query
    ->entityCondition('entity_type', 'mailchimp_automations_entity')
    ->propertyCondition('entity_type', $type)
    ->propertyCondition('bundle', $bundle)
    ->propertyCondition('status', 1);
  $result = $query
    ->execute();
  if ($result) {
    $entity_array = entity_load('mailchimp_automations_entity', array_keys($result['mailchimp_automations_entity']));
    return reset($entity_array);
  }
  return NULL;
}

/**
 * Implements hook_permission().
 */
function mailchimp_automations_permission() {
  $return = array();
  $return['administer mailchimp automations'] = array(
    'title' => t('Administer MailChimp automation entities'),
    'description' => t('Add, Delete, and Configure MailChimp Automation entity settings.'),
  );
  return $return;
}

Functions

Namesort descending Description
mailchimp_automations_access Access callback for activity menu items.
mailchimp_automations_entity_access Access callback for mailchimp_automations_entity.
mailchimp_automations_entity_automation Queries to see if there is an existing automation entity
mailchimp_automations_entity_info Implements hook_entity_info().
mailchimp_automations_entity_info_label Entity label callback.
mailchimp_automations_entity_insert Implements hook_entity_insert().
mailchimp_automations_get_automation Wrapper for MailchimpAutomations->getWorkflow().
mailchimp_automations_get_automations Wrapper for MailchimpAutomations->getAutomations().
mailchimp_automations_get_emails_for_workflow Wrapper for MailchimpAutomations->getWorkflowEmails().
mailchimp_automations_load_entities Loads a single or multiple instances of MailchimpAutomationsEntity.
mailchimp_automations_permission Implements hook_permission().
mailchimp_automations_trigger_workflow Triggers a workflow automation via the MailChimp API.