View source
<?php
function mailchimp_activity_entity_info() {
$return = array(
'mailchimp_activity_entity' => array(
'label' => t('MailChimp Activity Entity'),
'controller class' => 'EntityAPIControllerExportable',
'entity class' => 'MailchimpActivityEntity',
'base table' => 'mailchimp_activity_entity',
'uri callback' => 'entity_class_uri',
'fieldable' => FALSE,
'exportable' => TRUE,
'module' => 'mailchimp_activity',
'entity keys' => array(
'id' => 'mailchimp_activity_entity_id',
'name' => 'name',
'label' => 'label',
),
'admin ui' => array(
'path' => 'admin/config/services/mailchimp/activity',
'file' => 'mailchimp_activity.admin.inc',
'controller class' => 'MailChimpActivityUIController',
),
'label callback' => 'entity_class_label',
'access callback' => 'mailchimp_activity_entity_access',
),
);
return $return;
}
function mailchimp_activity_entity_access() {
return user_access('administer mailchimp activity');
}
function mailchimp_activity_menu() {
$items = array();
$mailchimp_activity_entities = mailchimp_activity_load_entities();
foreach ($mailchimp_activity_entities as $mailchimp_activity_entity) {
$arg = substr_count($mailchimp_activity_entity->entity_path, '/');
$items[$mailchimp_activity_entity->entity_path . '%entity_object/mailchimp_activity'] = array(
'title' => 'MailChimp Activity',
'load arguments' => array(
$mailchimp_activity_entity->entity_type,
),
'page callback' => 'mailchimp_activity_page',
'page arguments' => array(
$arg,
$mailchimp_activity_entity,
),
'access callback' => 'mailchimp_activity_access',
'access arguments' => array(
$mailchimp_activity_entity,
),
'type' => MENU_LOCAL_TASK,
);
}
return $items;
}
function mailchimp_activity_access(MailchimpActivityEntity $mailchimp_activity_entity) {
if ($mailchimp_activity_entity->enabled && user_access('access mailchimp activity')) {
return TRUE;
}
return FALSE;
}
function mailchimp_activity_load_entities($name = NULL) {
$types = entity_load_multiple_by_name('mailchimp_activity_entity', isset($name) ? array(
$name,
) : FALSE);
return isset($name) ? reset($types) : $types;
}
function mailchimp_activity_get_member_activity($list_id, $email, $reset = FALSE) {
$cache = $reset ? NULL : cache_get("mailchimp_activity_{$list_id}");
$memberactivity = array();
if ($cache && isset($cache->data[$email])) {
$memberactivity = $cache->data[$email];
}
else {
if ($q = mailchimp_get_api_object()) {
$result = $q
->listMemberActivity($list_id, array(
$email,
));
if ($result['success']) {
$memberactivity = reset($result['data']);
$newcache = array();
if ($cache) {
$newcache = $cache;
}
$newcache[$email] = $memberactivity;
cache_set('mailchimp_activity_' . $list_id, $newcache, 'cache', CACHE_TEMPORARY);
}
}
}
return $memberactivity;
}
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();
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();
$user_activity = mailchimp_activity_get_all_activity($email);
if (isset($user_activity['lists'])) {
foreach ($user_activity['lists'] 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',
),
),
);
foreach ($list_activity['activity'] as $activity) {
$rows[] = array(
array(
'data' => array(
'#type' => 'link',
'#title' => $user_activity['campaigns'][$activity['campaign_id']]['title'],
'#href' => $user_activity['campaigns'][$activity['campaign_id']]['archive_url'],
),
),
$activity['action'],
$activity['timestamp'],
);
}
}
}
$display['mailchimp_activity'] = array(
'#theme' => 'table',
'#header' => $header,
'#rows' => $rows,
);
return $display;
}
function mailchimp_activity_get_all_activity($email) {
$all_activity = array();
$listdata = mailchimp_get_lists_by_email($email);
if (isset($listdata['campaigns'])) {
$all_activity['campaigns'] = $listdata['campaigns'];
}
if (isset($listdata['lists'])) {
foreach ($listdata['lists'] as $list) {
$all_activity['lists'][$list['id']] = array(
'list' => $list,
'activity' => mailchimp_activity_get_member_activity($list['id'], $email),
);
}
}
return $all_activity;
}
function mailchimp_activity_permission() {
$return = array();
$return['access mailchimp activity'] = array(
'title' => t('Access MailChimp activity'),
'description' => t('View own MailChimp activity history.'),
);
$return['administer mailchimp activity'] = array(
'title' => t('Administer MailChimp activity entities'),
'description' => t('Add, Delete, and Configure MailChimp Activity entity settings.'),
);
return $return;
}