View source
<?php
define('ACTIVITY_NOT_PUBLISHED', 0);
define('ACTIVITY_PUBLISHED', 1);
function activity_menu() {
$items['activity/%/delete'] = array(
'title' => 'Delete',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'activity_delete_confirm',
1,
),
'access callback' => 'activity_delete_access',
'access arguments' => array(
1,
),
'file' => 'activity.admin.inc',
'type' => MENU_CALLBACK,
);
$items['admin/activity/weight'] = array(
'title' => 'Fix Trigger weight',
'page callback' => 'activity_fix_trigger_weight',
'file' => 'activity.install',
'access arguments' => array(
'administer activity',
),
'type' => MENU_CALLBACK,
);
$items['admin/structure/activity'] = array(
'title' => 'Activity Templates',
'description' => 'Modify how your activity messages will look',
'page callback' => 'activity_admin_overview',
'access arguments' => array(
'administer activity',
),
'file' => 'activity.admin.inc',
);
$items['admin/structure/activity/list'] = array(
'title' => 'List',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items['admin/structure/activity/create'] = array(
'title' => 'Create',
'description' => 'Modify how your activity messages will look',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'activity_create_form',
),
'access arguments' => array(
'administer activity',
),
'file' => 'activity.admin.inc',
'type' => MENU_LOCAL_TASK,
'weight' => 2,
);
$items['admin/config/content/activity'] = array(
'title' => 'Activity',
'description' => 'Modify the settings for how activity behaves',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'activity_settings_form',
),
'access arguments' => array(
'administer activity',
),
'file' => 'activity.admin.inc',
'type' => MENU_NORMAL_ITEM,
);
$items['admin/config/content/activity/settings'] = array(
'title' => 'Settings',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items['admin/structure/activity/configure/%activity_handler'] = array(
'title' => 'Edit',
'description' => 'Modify how your activity messages will look',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'activity_configure_handler_form',
4,
),
'access arguments' => array(
'administer activity',
),
'file' => 'activity.admin.inc',
'type' => MENU_CALLBACK,
);
$items['admin/structure/activity/recreate/%activity_handler_batchable/%'] = array(
'title' => 'Recreate Messages',
'description' => 'Recreate messages for the provided action',
'page callback' => 'activity_admin_recreate',
'page arguments' => array(
4,
),
'access callback' => 'activity_batch_access',
'access arguments' => array(
4,
5,
),
'file' => 'activity.admin.inc',
'type' => MENU_CALLBACK,
);
$items['admin/structure/activity/delete/%actions'] = array(
'title' => 'Delete',
'description' => 'Remove an activity action and associated trigger assignment',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'activity_actions_delete_form',
4,
),
'access arguments' => array(
'administer activity',
),
'file' => 'activity.admin.inc',
'type' => MENU_CALLBACK,
);
$items['admin/structure/activity/regenerate/%activity_handler_batchable/%'] = array(
'title' => 'Create new Messages',
'description' => 'Delete messages for the provided action and create new ones',
'page callback' => 'activity_batch_regenerate',
'page arguments' => array(
4,
),
'access callback' => 'activity_batch_access',
'access arguments' => array(
4,
5,
),
'file' => 'activity.admin.inc',
);
return $items;
}
function activity_permission() {
return array(
'administer activity' => array(
'title' => t('Administer Activity'),
'description' => t('Perform administration tasks for Activity.'),
),
'delete own activity' => array(
'title' => t('Delete own Activity'),
'description' => t('Delete their own Activity from the site.'),
),
);
}
function activity_delete_access($aid) {
if (user_access('administer activity')) {
return TRUE;
}
if (user_access('delete own activity')) {
$uid = db_query("SELECT uid FROM {activity} WHERE aid = :aid", array(
':aid' => $aid,
))
->fetchField();
return $uid == $GLOBALS['user']->uid;
}
return FALSE;
}
function activity_handler_load($aid) {
$hook = db_query("SELECT hook FROM {trigger_assignments} WHERE aid = :aid", array(
":aid" => $aid,
))
->fetchField();
if (!empty($hook)) {
$action_record = actions_load($aid);
$action_record = drupal_unpack($action_record, 'parameters');
$handler = activity_load_handler($hook);
$handler->templates = $action_record->templates;
$handler->options = $action_record->options;
$handler->actions_id = $action_record->aid;
$handler->label = $action_record->label;
return $handler;
}
return FALSE;
}
function activity_handler_batchable_load($aid) {
$handler = activity_handler_load($aid);
if (!empty($handler) && $handler->batch) {
return $handler;
}
return FALSE;
}
function activity_batch_access($action, $token) {
return user_access('administer activity') && drupal_valid_token($token, $action->actions_id);
}
function activity_record($object, $context, $a1, $a2) {
if (isset($context['aid'])) {
$handler = activity_handler_load($context['aid']);
$eid = $handler
->determineEid($context);
if (isset($eid)) {
activity_save_activity($handler, $eid, $a1, $a2);
}
}
}
function activity_save_activity(ActivityActionHandler $handler, $eid, $a1, $a2) {
$objects = $handler
->loadObjects($eid);
drupal_alter('activity_objects', $objects, $handler->type);
$uid = $handler
->determineActor($objects);
$timestamp = $handler
->determineTimestamp($objects);
if ($handler
->valid($eid, $uid, $timestamp, $objects, $a1, $a2)) {
$messages = $handler
->tokenize($objects);
$nid = $handler
->determineNid($objects);
$argument1 = NULL;
$argument2 = NULL;
if (isset($a1)) {
$argument1 = serialize($a1);
}
if (isset($a2)) {
$argument2 = serialize($a2);
}
$txn = db_transaction();
$record = array(
'uid' => $uid,
'type' => $handler->type,
'nid' => $nid,
'eid' => $eid,
'created' => $timestamp,
'actions_id' => $handler->actions_id,
'argument1' => $argument1,
'argument2' => $argument2,
'status' => $handler
->isPublished($objects, $uid) ? ACTIVITY_PUBLISHED : ACTIVITY_NOT_PUBLISHED,
);
$record = (object) $record;
drupal_alter('activity_record', $record, $handler);
drupal_write_record('activity', $record);
drupal_alter('activity_messages', $messages, $handler->name, $objects);
activity_write_messages($messages, $record->aid);
foreach (activity_get_grants($record) as $realm => $values) {
foreach ($values as $value) {
$row = array(
'aid' => $record->aid,
'realm' => $realm,
'value' => $value,
);
drupal_write_record('activity_access', $row);
}
}
}
}
function activity_recreate_messages(array $records) {
foreach ($records as $record) {
$handler = activity_handler_load($record->actions_id);
if ($handler->batch) {
$amids = db_query("SELECT amid FROM {activity_targets} WHERE aid = :aid", array(
":aid" => $record->aid,
))
->fetchCol();
if (!empty($amids)) {
db_delete('activity_targets')
->condition('amid', $amids, 'IN')
->execute();
db_delete('activity_messages')
->condition('amid', $amids, 'IN')
->execute();
}
$objects = $handler
->loadObjects($record->eid);
drupal_alter('activity_objects', $objects, $handler->type);
$a1 = NULL;
$a2 = NULL;
if (isset($record->argument1)) {
$a1 = unserialize($record->argument1);
}
if (isset($record->argument2)) {
$a2 = unserialize($record->argument2);
}
if ($handler
->valid($record->eid, $record->uid, $record->created, $objects, $a1, $a2)) {
activity_write_messages($handler
->tokenize($objects), $record->aid);
}
else {
db_delete('activity')
->condition('aid', $record->aid)
->execute();
}
}
}
}
function activity_write_messages($messages, $aid) {
foreach ($messages as $language_id => $language_messages) {
foreach ($language_messages as $uid => $message) {
$message_record = new stdClass();
$message_record->message = $message;
drupal_write_record('activity_messages', $message_record);
$target_record = array(
'aid' => $aid,
'uid' => $uid,
'language' => $language_id,
'amid' => $message_record->amid,
);
drupal_write_record('activity_targets', $target_record);
}
}
}
function activity_update_status($activity_records) {
$updated_activity = array();
foreach ($activity_records as $record) {
$status = ACTIVITY_PUBLISHED;
$handler = activity_handler_load($record->actions_id);
$objects = $handler
->loadObjects($record->eid);
drupal_alter('activity_objects', $objects, $handler->type);
if (!$handler
->isPublished($objects, $record->uid)) {
$status = ACTIVITY_NOT_PUBLISHED;
}
$updated_activity[$status][] = $record->aid;
}
foreach ($updated_activity as $status => $aids) {
db_update('activity')
->fields(array(
'status' => $status,
))
->condition('aid', $aids, 'IN')
->execute();
}
}
function activity_views_api() {
return array(
'api' => '3.0-alpha1',
'path' => drupal_get_path('module', 'activity') . '/views',
);
}
function node_activity_api() {
return array(
'api' => '3.0-alpha1',
'realms' => array(
'node_author' => array(
'name' => 'Node Author',
),
),
'hooks' => array(
'node_insert' => array(
'batch' => TRUE,
'handler' => 'NodeActivityActionHandler',
'name' => 'Node Insert',
),
'node_view' => array(
'batch' => FALSE,
'handler' => 'NodeActivityActionHandler',
'name' => 'Node View',
),
'node_update' => array(
'batch' => FALSE,
'handler' => 'NodeActivityActionHandler',
'name' => 'Node Update',
),
),
'file' => drupal_get_path('module', 'activity') . '/modules/node.activity.inc',
);
}
function comment_activity_api() {
return array(
'api' => '3.0-alpha1',
'realms' => array(
'comment' => array(
'name' => 'Comment',
),
),
'hooks' => array(
'comment_insert' => array(
'batch' => TRUE,
'handler' => 'CommentActivityActionHandler',
'name' => 'Comment Insert',
),
),
'file' => drupal_get_path('module', 'activity') . '/modules/comment.activity.inc',
);
}
function user_activity_api() {
return array(
'api' => '3.0-alpha1',
'hooks' => array(
'user_insert' => array(
'batch' => TRUE,
'handler' => 'UserActivityActionHandler',
'name' => 'User Register',
),
'user_update' => array(
'batch' => FALSE,
'handler' => 'UserActivityActionHandler',
'name' => 'User Update',
),
'user_login' => array(
'batch' => FALSE,
'handler' => 'UserActivityActionHandler',
'name' => 'User Login',
),
'user_logout' => array(
'batch' => FALSE,
'handler' => 'UserActivityActionHandler',
'name' => 'User Logout',
),
),
);
}
function activity_delete($aids) {
$amids = array();
$count_activities = 0;
$count_messages = 0;
if (!empty($aids)) {
$amids = db_query("SELECT amid FROM {activity_targets} WHERE aid IN (:aids)", array(
':aids' => $aids,
))
->fetchCol();
}
if (!empty($amids)) {
db_delete('activity_targets')
->condition('amid', $amids, 'IN')
->execute();
$count_messages = db_delete('activity_messages')
->condition('amid', $amids, 'IN')
->execute();
}
if (!empty($aids)) {
$count_activities = db_delete('activity')
->condition('aid', $aids, 'IN')
->execute();
}
return array(
'activities' => $count_activities,
'messages' => $count_messages,
);
}
function activity_load_handler($hook) {
$hooks = activity_cache_get('hooks');
$handler = new $hooks[$hook]['handler']();
$handler->type = $hook;
$handler->batch = $hooks[$hook]['batch'];
return $handler;
}
function activity_enabled_languages() {
$languages = language_list('enabled');
return $languages[1];
}
function activity_get_grants($record) {
$record = (object) $record;
$grants = array();
$realms = activity_cache_get('realms');
foreach ($realms as $realm_id => $information) {
$module_grants = module_invoke($information['module'], 'activity_grants', $record);
foreach ($module_grants as $realm => $values) {
if (in_array($realm, array_keys($realms))) {
$grants[$realm] = $values;
}
}
}
drupal_alter('activity_access_records', $grants, $record);
return $grants;
}
function activity_form_options_hooks() {
$hooks = activity_cache_get('hooks');
$hook_options = array();
foreach ($hooks as $hook => $information) {
$hook_options[$hook] = t($information['name']);
}
return $hook_options;
}
function activity_cache_get($type) {
$api_information = activity_cache_api_load();
return $api_information[$type];
}
function activity_cache_api_load() {
$info_cache =& drupal_static(__FUNCTION__);
if (empty($info_cache)) {
$info_cache = array(
'realms' => array(),
'hooks' => array(),
'all_realms' => array(),
);
foreach (module_implements('activity_api') as $module) {
$module_result = module_invoke($module, 'activity_api');
if ($module_result['api'] == '3.0-alpha1') {
$module_result += array(
'realms' => array(),
'hooks' => array(),
);
foreach ($module_result['realms'] as $realm => $data) {
$module_result['realms'][$realm] += array(
'module' => $module,
);
}
$info_cache['hooks'] += $module_result['hooks'];
$info_cache['realms'] += $module_result['realms'];
$info_cache['all_realms'] += $module_result['realms'];
if (isset($module_result['file'])) {
require_once './' . $module_result['file'];
}
}
$enabled_realms = variable_get('activity_access_realms', array(
'node_author',
));
if (!empty($enabled_realms)) {
foreach (array_diff(array_keys($info_cache['realms']), $enabled_realms) as $key) {
unset($info_cache['realms'][$key]);
}
}
}
drupal_alter('activity_api', $info_cache);
}
return $info_cache;
}
function activity_user_update(&$edit, $account, $category) {
entity_load('user', array(), array(), TRUE);
$records = db_query("SELECT * FROM {activity} WHERE uid = :uid", array(
":uid" => $account->uid,
))
->fetchAll();
activity_recreate_messages($records);
activity_update_status($records);
}
function activity_user_delete($account) {
$aids = db_query("SELECT aid FROM {activity} WHERE uid = :uid", array(
":uid" => $account->uid,
))
->fetchCol();
activity_delete($aids);
}
function activity_node_update($node) {
entity_load('node', array(), array(), TRUE);
$records = db_query("SELECT * FROM {activity} WHERE nid = :nid", array(
":nid" => $node->nid,
))
->fetchAll();
activity_recreate_messages($records);
activity_update_status($records);
}
function activity_node_delete($node) {
$aids = db_query("SELECT aid FROM {activity} WHERE nid = :nid", array(
':nid' => $node->nid,
))
->fetchCol();
activity_delete($aids);
}
function activity_comment_update($comment) {
entity_load('comment', array(), array(), TRUE);
$records = db_query("SELECT * FROM {activity} WHERE type IN (:types) AND eid = :eid", array(
":types" => array(
'comment_insert',
),
":eid" => $comment->cid,
))
->fetchAll();
activity_recreate_messages($records);
activity_update_status($records);
}
function activity_comment_delete($comment) {
$aids = db_query("SELECT aid FROM {activity} WHERE type IN (:types) AND eid = :eid", array(
":types" => array(
'comment_insert',
),
":eid" => $comment->cid,
))
->fetchCol();
activity_delete($aids);
}
function activity_theme($existing, $type, $theme, $path) {
return array(
'activity_settings_actions_list' => array(
'variables' => array(
'results' => NULL,
),
),
'activity_token_help' => array(
'variables' => array(
'types' => NULL,
'prefix' => NULL,
'suffix' => NULL,
),
),
'activity_username' => array(
'variables' => array(
'account' => NULL,
),
),
);
}
function theme_activity_settings_actions_list($vars) {
$header = array(
t('Label'),
t('Hook'),
t('Operations'),
);
$hooks = activity_cache_get('hooks');
foreach ($vars['results'] as $result) {
$handler = activity_handler_load($result->aid);
$operations = array(
l(t('configure'), 'admin/structure/activity/configure/' . $result->aid),
l(t('delete'), 'admin/structure/activity/delete/' . $result->aid),
);
if ($handler->batch) {
$operations[] = l(t('update messages'), 'admin/structure/activity/recreate/' . $result->aid . '/' . drupal_get_token($result->aid));
$operations[] = l(t('regenerate messages'), 'admin/structure/activity/regenerate/' . $result->aid . '/' . drupal_get_token($result->aid));
}
$rows[] = array(
$result->label,
$hooks[$result->hook]['name'],
implode(' | ', $operations),
);
}
$output = theme('table', array(
'header' => $header,
'rows' => $rows,
));
return $output;
}
function theme_activity_username($variables) {
if (isset($variables['link_path'])) {
$output = l($variables['name'] . $variables['extra'], $variables['link_path'], $variables['link_options']);
}
else {
$output = '<span' . drupal_attributes($variables['attributes_array']) . '>' . $variables['name'] . $variables['extra'] . '</span>';
}
return $output;
}
function template_preprocess_activity_username(&$variables) {
$account = $variables['account'];
$variables['extra'] = '';
if (empty($account->uid)) {
$variables['uid'] = 0;
}
else {
$variables['uid'] = (int) $account->uid;
}
$name = $variables['name_raw'] = format_username($account);
if (drupal_strlen($name) > 20) {
$name = drupal_substr($name, 0, 15) . '...';
}
$variables['name'] = check_plain($name);
$variables['profile_access'] = TRUE;
$variables['link_attributes'] = array();
if ($variables['uid'] && $variables['profile_access']) {
$variables['link_attributes'] = array(
'title' => t('View user profile.'),
);
$variables['link_path'] = 'user/' . $variables['uid'];
}
elseif (!empty($account->homepage)) {
$variables['link_attributes'] = array(
'rel' => array(
'nofollow',
),
);
$variables['link_path'] = $account->homepage;
$variables['homepage'] = $account->homepage;
}
$variables['link_options']['html'] = TRUE;
$variables['attributes_array'] = array(
'class' => array(
'username',
),
);
}
function template_process_activity_username(&$variables) {
if (isset($variables['link_path'])) {
$variables['link_options']['attributes'] = array_merge_recursive($variables['link_attributes'], $variables['attributes_array']);
}
}