View source
<?php
function flagactivity_activity_info() {
$types = flag_get_flags();
foreach ($types as $type) {
$token_types[$type->name] = $type->title;
}
return array(
'ops' => array(
'flag' => t('Flag'),
'unflag' => t('Unflag'),
),
'types' => $token_types,
'roles' => array(
'author' => array(
'#name' => t('Author'),
'#description' => t('The person who flagged the node.'),
'#default' => t('[author] [operation]ged the [content-type] [content-link]'),
),
'all' => array(
'#name' => t('All'),
'#description' => t('The general public.'),
'#default' => t('[author-all] [operation]ged the [content-type] [content-link]'),
),
),
);
}
function flagactivity_activityapi(&$activity, $op) {
if ($op == 'load') {
if ($activity['data']['module'] == 'flagactivity' && ($data['content-type'] == 'node' || $data['content-type'] == 'comment')) {
if (!node_access('view', node_load($activity['data']['node-id']))) {
$activity = array();
}
}
}
}
function flagactivity_token_list($type = 'all') {
if ($type == 'flagactivity') {
$tokens['flagactivity'] = array(
'content-type' => t('The content type that was flagged (node, comment or user)'),
'content-id' => t('Id of the content that was flagged'),
'content-title' => t('Title of the content that was flagged (node or comment)'),
'content-link' => t('Link to the content that was flagged'),
'node-type' => t('The type of the node that was flagged'),
);
return $tokens;
}
}
function flagactivity_token_values($type, $data = NULL, $options = array()) {
static $nodes, $comments;
if ($type == 'flagactivity' && !empty($data)) {
switch ($data['content-type']) {
case 'node':
if (!isset($nodes[$data['content-id']])) {
$nodes[$data['content-id']] = db_fetch_object(db_query('SELECT title, type FROM {node} WHERE nid = %d', $data['content-id']));
}
$node = $nodes[$data['content-id']];
$data['node-type'] = theme('activity_node_type', $node->type);
$data['content-title'] = check_plain($node->title);
$data['content-type'] = $data['node-type'];
$data['content-link'] = l($data['content-title'], 'node/' . $data['content-id']);
break;
case 'comment':
if (!isset($coments[$data['content-id']])) {
$comments[$data['content-id']] = db_fetch_object(db_query('SELECT n.title, n.type, n.nid, c.subject FROM {node} n INNER JOIN {comments} c ON n.nid = c.nid WHERE c.cid = %d', $data['content-id']));
}
$comment = $comments[$data['content-id']];
$data['node-type'] = theme('activity_node_type', $comment->type);
$data['content-title'] = $comment->subject ? check_plain($comment->subject) : check_plain($comment->title);
$data['content-link'] = l($data['content-title'], 'node/' . $comment->nid, array(
'fragment' => 'comment-' . $data['content-id'],
));
break;
case 'user':
if (!isset($authors[$data['content-id']])) {
$authors[$data['content-id']] = activity_user_load($data['content-id']);
}
$user = $authors[$data['content-id']];
$data['content-link'] = theme('activity_username', $user);
break;
}
return $data;
}
}
function flagactivity_flag($op, $flag, $content_id, $account) {
if (!in_array($flag->name, variable_get('flagactivity_token_types', array(
$flag->name,
)), TRUE) || !in_array($op, variable_get('flagactivity_op_types', array(
$op,
)), TRUE)) {
return FALSE;
}
if (activity_user_privacy_optout($account)) {
return FALSE;
}
if (user_access('hide activity', $user)) {
return FALSE;
}
$data = array(
'operation' => $op,
'content-id' => $content_id,
'content-type' => $flag->content_type,
);
$target_users_roles = array(
ACTIVITY_ALL => 'all',
$account->uid => 'author',
);
activity_insert($account->uid, 'flagactivity', $flag->name, $op, $data, $target_users_roles);
}