You are here

heartbeat_rules.module in Heartbeat 7

Rules integration. Event/trigger as well as the heartbeat actions.

File

modules/heartbeat_rules/heartbeat_rules.module
View source
<?php

/**
 * @file
 * Rules integration. Event/trigger as well as the heartbeat actions.
 */

/**
 * Implementation of hook_menu().
 */
function heartbeat_rules_menu() {
  $items = array();
  $items['heartbeat/heartbeat_activity_rules_default/js'] = array(
    'title' => 'Callback to supply extra action variables as arguments',
    'type' => MENU_CALLBACK,
    'page callback' => 'heartbeat_rules_action_message_id_js',
    'access arguments' => array(
      'admin heartbeat templates',
    ),
  );
  return $items;
}

/**
 * Implements hook_token_info().
 */
function heartbeat_token_info() {
  $type = array(
    'name' => t('Heartbeat activity'),
    'description' => t('Tokens related to individual heartbeat activity messages.'),
    'needs-data' => 'heartbeat_activity',
  );

  // Core tokens for nodes.
  $node['link'] = array(
    'name' => t("Content name with a link to it"),
    'description' => t('The node with a link to it.'),
  );

  // Core tokens for nodes.
  $user['link'] = array(
    'name' => t("Username with a link to it"),
    'description' => t('The user with a link to it.'),
  );

  // Heartbeat tokens.
  $heartbeat_activity['actor-link'] = array(
    'name' => t("Actor username with a link to it"),
    'description' => t('The acting user with a link to it.'),
  );
  return array(
    'types' => array(
      'heartbeat_activity' => $type,
    ),
    'tokens' => array(
      'node' => $node,
      'user' => $user,
      'heartbeat_activity' => $heartbeat_activity,
    ),
  );
}

/**
 * Implements hook_tokens().
 */
function heartbeat_tokens($type, $tokens, array $data = array(), array $options = array()) {
  $url_options = array(
    'absolute' => FALSE,
  );
  $url_options['alias'] = TRUE;
  if (isset($options['language'])) {
    $url_options['language'] = $options['language'];
    $language_code = $options['language']->language;
  }
  else {
    $language_code = NULL;
  }
  $sanitize = !empty($options['sanitize']);
  $replacements = array();

  // Node activity tokens.
  if ($type == 'node' && !empty($data['node'])) {
    $node = $data['node'];
    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'link':
          $replacements[$original] = l($node->title, 'node/' . $node->nid, $url_options);
          break;
      }
    }
  }

  // User tokens.
  if ($type == 'user' && !empty($data['user'])) {
    $account = $data['user'];
    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'link':
          $url_options['attributes']['class'] = array(
            'user-link',
          );
          $name = !empty($account->realname) ? $account->realname : $account->name;
          $replacements[$original] = l(format_username($account), 'user/' . $account->uid, $url_options);
          break;
      }
    }
  }

  // Heartbeat activity tokens.
  if ($type == 'heartbeat_activity' && !empty($data['heartbeat_activity'])) {
    $heartbeat_activity = $data['heartbeat_activity'];
    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'actor-link':
          $url_options['attributes']['class'] = array(
            'user-link',
          );
          $name = !empty($heartbeat_activity->actor->realname) ? $heartbeat_activity->actor->realname : $heartbeat_activity->actor->name;
          $replacements[$original] = l(format_username($heartbeat_activity->actor), 'user/' . $heartbeat_activity->actor->uid, $url_options);
          break;
      }
    }
  }
  return $replacements;
}

/**
 * Rules action to log activity.
 */
function rules_action_heartbeat_activity_log() {

  // I did not find yet how to receive dynamic variables and catch them wisely.

  //$entity_id, $uid, $entity_id_target, $uid_target, $message_id, $variables
  $args = func_get_args();
  array_pop($args);
  array_pop($args);
  $rulesState = array_pop($args);

  // Prepare the arguments we need.
  $arguments = $rulesState->currentArguments;
  unset($arguments['settings'], $arguments['state']);

  // Fix the message id and load the template.
  $info = $arguments['message_id']
    ->info();
  $message_id = $info['default value'];
  $variables = array(
    'variables' => array(),
  );
  ctools_include('export');
  $template = heartbeat_message_template_load($message_id);
  foreach ($arguments as $key => $value) {
    if ($value instanceof EntityValueWrapper) {
      $val = $value
        ->value();
    }
    else {
      $val = $value;
    }
    if (isset($template->variables[$key])) {
      $variables['variables'][$key] = $val;
    }
    else {
      $variables[$key] = $val;
    }
    if (in_array($key, array(
      'nid',
      'uid',
      'nid_target',
      'uid_target',
      'cid',
      'in_group',
    ))) {
      $variables[$key] = (int) $variables[$key];
    }
  }
  $variables['access'] = _heartbeat_activity_get_access($variables['uid'], $template);
  $variables['message_id'] = $message_id;

  // Break logging if no actor is specified.
  if (!is_numeric($variables['uid'])) {
    return;
  }
  heartbeat_log($variables, $variables['variables']);
}

/**
 * eof().
 */

Functions

Namesort descending Description
heartbeat_rules_menu Implementation of hook_menu().
heartbeat_tokens Implements hook_tokens().
heartbeat_token_info Implements hook_token_info().
rules_action_heartbeat_activity_log Rules action to log activity.