You are here

function mailchimp_activity_page in Mailchimp 7.4

Same name and namespace in other branches
  1. 7.5 modules/mailchimp_activity/mailchimp_activity.module \mailchimp_activity_page()
  2. 7.2 modules/mailchimp_activity/mailchimp_activity.module \mailchimp_activity_page()
  3. 7.3 modules/mailchimp_activity/mailchimp_activity.module \mailchimp_activity_page()

Content for the mailchimp log tab on entity instance pages.

Parameters

object $entity: The entity this page is being generated for.

MailchimpActivityEntity $mailchimp_activity_entity: The MailChimpActivityEntity instance for this page.

Return value

array Render array for the activity page.

1 string reference to 'mailchimp_activity_page'
mailchimp_activity_menu in modules/mailchimp_activity/mailchimp_activity.module
Implements hook_menu().

File

modules/mailchimp_activity/mailchimp_activity.module, line 167
Module file for mailchimp_activity.

Code

function mailchimp_activity_page($entity, MailchimpActivityEntity $mailchimp_activity_entity) {
  $entity_wrapper = entity_metadata_wrapper($mailchimp_activity_entity->entity_type, $entity);
  $email_property = $mailchimp_activity_entity->email_property;
  $email_property_array = explode(':', $email_property);
  $parent = $entity_wrapper;
  foreach ($email_property_array as $drupal_field) {
    if ($parent instanceof EntityListWrapper) {
      $child_wrapper = $parent
        ->get(0)->{$drupal_field};
    }
    else {
      $child_wrapper = $parent->{$drupal_field};
    }
    $parent = $child_wrapper;
  }
  $email = $parent
    ->value();

  // Validate email address.
  if (!valid_email_address($email)) {
    return array(
      'error_notice' => array(
        '#markup' => t('%email does not contain a valid email address. Unable to lookup Mailchimp activity history without a valid email.', array(
          '%email' => $email_property,
        )),
      ),
    );
  }
  $header = array(
    t('Campaign'),
    t('Action'),
    t('Date & Time'),
  );
  $rows = array();

  // Loop through all activities, creating rows for each:
  $user_activity = mailchimp_activity_get_all_activity($email);

  /* @var \Mailchimp\MailchimpCampaigns $mc_campaigns */
  $mc_campaigns = mailchimp_get_api_object('MailchimpCampaigns');

  // TODO: Campaign limit is set high because all campaigns need to be
  // retrieved here. This function should be refactored to be more efficient.
  $result = $mc_campaigns
    ->getCampaigns(array(
    'count' => 500,
  ));
  $campaigns = $result->total_items > 0 ? $result->campaigns : array();
  $campaign_index = array();
  foreach ($campaigns as $campaign) {
    $campaign_index[$campaign->id] = $campaign;
  }
  if (!empty($user_activity)) {
    foreach ($user_activity as $list_activity) {
      $rows[] = array(
        'no_striping' => TRUE,
        'data' => array(
          array(
            'data' => '<strong>List: ' . $list_activity['list']->name . '</strong>',
            'colspan' => count($header),
            'style' => 'text-align:center',
          ),
        ),
      );
      if (!empty($list_activity['activity'])) {
        foreach ($list_activity['activity'] as $activity) {
          if (!empty($activity->campaign_id)) {
            $title = array(
              'data' => array(
                '#type' => 'link',
                '#title' => $campaign_index[$activity->campaign_id]->settings->title,
                '#href' => $campaign_index[$activity->campaign_id]->archive_url,
              ),
            );
          }
          else {
            $title = $activity->title;
          }
          $rows[] = array(
            $title,
            $activity->action,
            $activity->timestamp,
          );
        }
      }
      else {
        $rows[] = array(
          'no_striping' => TRUE,
          'data' => array(
            array(
              'data' => 'No activity yet.',
              'colspan' => count($header),
              'style' => 'text-align:center',
            ),
          ),
        );
      }
    }
  }
  $display['mailchimp_activity'] = array(
    '#theme' => 'table',
    '#header' => $header,
    '#rows' => $rows,
  );
  return $display;
}