You are here

flagactivity.module in Activity 6

Same filename and directory in other branches
  1. 5.4 contrib/flagactivity/flagactivity.module

File

contrib/flagactivity/flagactivity.module
View source
<?php

/**
 * Activity definition file
 *
 * This defines what hooks activity module should use
 */
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]'),
      ),
    ),
  );
}

/** 
 * Implementation of hook_activityapi().
 */
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();
      }
    }
  }
}

/**
 * Token module integration. Defines available default tokens.
 */
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;
  }
}

/**
 * Token module integration. Defines available default token values.
 */
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;
  }
}

/**
 * Implementation of hook_flag()
 * @param $op
 *   flag operation
 * @param $flag
 *   flag object
 * @param $nid
 *   object id of flagged object
 * @param $user
 *   user object of user who flagged
 */
function flagactivity_flag($op, $flag, $content_id, $account) {

  // Check if both type and operation are
  // enabled for activity. If not then stop here
  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;
  }

  // Privacy setting check
  if (activity_user_privacy_optout($account)) {
    return FALSE;
  }

  // User hide activity permission check
  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);
}

Functions

Namesort descending Description
flagactivity_activityapi Implementation of hook_activityapi().
flagactivity_activity_info Activity definition file
flagactivity_flag Implementation of hook_flag()
flagactivity_token_list Token module integration. Defines available default tokens.
flagactivity_token_values Token module integration. Defines available default token values.