You are here

heartbeat_views.module in Heartbeat 6.4

Same filename and directory in other branches
  1. 6.3 views/heartbeat_views.module

File

views/heartbeat_views.module
View source
<?php

// $id: $

/**
 * Implementation of hook_views_api().
 *
 * This hook will tell views2 that user activity is available
 */
function heartbeat_views_views_api() {
  return array(
    'api' => 2,
    'path' => drupal_get_path('module', 'heartbeat_views'),
  );
}

/**
 * Implementation of hook_views_bulk_operations_object_info()
 *
 * The array returned for each object type contains:
 *  'type' => the object type name, should be the same as 'type' field in actions.
 *  'base_table' => the Views 2 table name corresponding to that object type, should be the same as the $view->base_table attribute.
 *  'load' => a function($id) that returns the corresponding object.
 *  'title' => an attribute on the object that returns a human-friendly identifier of the object.
 *  'access' (optional) => a function($op, $node, $account = NULL) that behaves like node_access().
 *
 */
function heartbeat_views_views_bulk_operations_object_info() {
  return array(
    'heartbeat_activity' => array(
      'type' => 'heartbeat_activity',
      'base_table' => 'heartbeat_activity',
      'load' => 'heartbeat_load_message_instance',
      'title' => 'message',
      'oid' => 'uaid',
      'access' => '_heartbeat_message_has_access',
    ),
  );
}

/**
 * Implementation of hook_action_info().
 */
function heartbeat_views_action_info() {
  return array(
    'heartbeat_activity_delete_action' => array(
      'description' => t('Delete activity'),
      'type' => 'heartbeat_activity',
      'configurable' => FALSE,
      'hooks' => array(
        'heartbeat_activity' => array(
          'delete',
        ),
      ),
    ),
  );
}

/**
 * Views bulk operations callback to delete multiple activity.
 */
function heartbeat_activity_delete_action($heartbeatactivity, $context = array()) {
  if (_heartbeat_message_delete_access($heartbeatactivity->uaid)) {
    _heartbeat_activity_delete($heartbeatactivity->uaid);
  }
}

/**
 * Implementation of hook_views_pre_view().
 */
function heartbeat_views_views_pre_view(&$view, &$display_id = 0, &$args = NULL) {
  if ($view->base_table == 'heartbeat_activity' && !empty($view->result)) {
    _heartbeat_render_view($view);
    $view->rendered = TRUE;
  }
}

/**
 * Implementation of hook_views_pre_render().
 */
function heartbeat_views_views_pre_render(&$view) {
  if ($view->base_table == 'heartbeat_activity' && !empty($view->result) && empty($view->rendered)) {
    _heartbeat_render_view($view);
    $view->rendered = TRUE;
  }
}

/**
 * Implementation of hook_views_plugins().
 */
function heartbeat_views_views_plugins() {
  return array(
    'module' => 'views',
    // This just tells our themes are elsewhere.
    'row' => array(
      'heartbeat_rss' => array(
        'title' => t('Heartbeat'),
        'help' => t('Display the heartbeat with standard view.'),
        'handler' => 'views_plugin_row_heartbeat_rss',
        'path' => drupal_get_path('module', 'heartbeat_views'),
        'theme' => 'views_view_row_rss',
        'base' => array(
          'heartbeat_activity',
        ),
        // only works with 'node' as base.
        'uses options' => TRUE,
        'type' => 'feed',
        'help topic' => 'style-heartbeat-rss',
      ),
    ),
  );
}

/**
 * Helper function to (re-)render the messages from views.
 */
function _heartbeat_render_view(&$view) {
  $name = $view->name . '_' . $view->current_display;

  // Prepare info object
  $stream = isset($view->filter['access_type']->stream) ? $view->filter['access_type']->stream : new stdClass();
  if ($view->style_plugin
    ->uses_fields()) {
    $field_handlers = array();
    foreach ($view->field as $fieldname => $handler) {
      $fieldalias = isset($handler->field_alias) && $handler->field_alias != 'unknown' ? $handler->field_alias : $handler->real_field;
      $field_handlers[$fieldname] = $fieldalias;
    }
  }
  $results = array();
  foreach ($view->result as $row) {

    // unset message types that where denied in stream access configuration
    if (!isset($stream->messages_denied[$row->message_id])) {
      $template = new HeartbeatMessageTemplate($row->hid, $row->message_id, $row->message_orig, $row->message_concat_orig, $row->concat_args);
      $template->perms = $row->perms;
      $template->custom = $row->custom;
      $template->description = $row->description;
      $template
        ->set_variables($row->variables_orig);
      $template
        ->set_attachments($row->attachments);
      $template
        ->set_roles(isset($template->concat_args['roles']) ? $template->concat_args['roles'] : array());
      $heartbeatactivity = new HeartbeatActivity($row, $template);
      $results[$row->uaid]->count = 1;
      if (!empty($field_handlers)) {
        foreach ($field_handlers as $fieldname => $fieldalias) {
          if ($fieldname == 'counter') {
            continue;
          }
          $heartbeatactivity->{$fieldalias} = $row->{$fieldalias};
        }
      }
      $uses_ds = $view->style_plugin
        ->uses_row_plugin() && $view->style_plugin->row_plugin->definition['module'] == 'ds';
      if ($uses_ds) {
        $heartbeatactivity->content['message'] = $heartbeatactivity->message;
        $heartbeatactivity->content['widgets'] = _theme_heartbeat_widgets($heartbeatactivity);
        $heartbeatactivity->content['time_info'] = theme('heartbeat_time_ago', $heartbeatactivity);
        $heartbeatactivity->content['buttons'] = theme('heartbeat_buttons', $heartbeatactivity);
      }
      $results[$row->uaid] = $heartbeatactivity;
    }
  }
  if (isset($view->pager['items_per_page'])) {
    $stream->limit_sql = $view->pager['items_per_page'];
  }
  $result = _heartbeat_group_views_messages($name, $results, $stream);
  $view->result = $result;
}

/**
 * Helper function to group and merge message built by views.
 */
function _heartbeat_group_views_messages($name, $messages, $stream) {
  heartbeat_include('HeartbeatParser');
  $heartbeat = HeartbeatParser::instantiate($name);
  if (!empty($stream->grouping_seconds)) {
    $timespan = $stream->grouping_seconds;
  }
  else {
    $timespan = variable_get('heartbeat_activity_grouping_seconds', 7200);
  }
  $heartbeat
    ->set_timespan_gap($timespan);
  $heartbeat
    ->build_sets($messages);
  $heartbeat
    ->merge_sets();
  $messages = $heartbeat
    ->get_messages();
  $num_total_messages = count($messages);

  // From here we know the number of messages actualy loaded (and allowed)
  $messages = array_values(array_slice($messages, 0, $stream->limit_sql));

  // Give contributes modules the last chance to hook into the messages
  if (empty($stream->name)) {
    heartbeat_include('heartbeatstream', 'heartbeat');
    heartbeat_include('publicheartbeat', 'heartbeat');
    $heartbeatAccess = new PublicHeartbeat(new HeartbeatStream(heartbeat_stream_load('publicheartbeat')));
  }
  else {
    $class = $stream->name;
    heartbeat_include($stream->name, $stream->module);
    $heartbeatAccess = new $class($stream);
  }

  // Let other module load additions.
  $hook = 'heartbeat_load';
  foreach (module_implements($hook) as $module) {
    $function = $module . '_' . $hook;

    // $messages should be implemented by reference!!!
    $function($messages, $heartbeatAccess);
  }

  // Let other modules retheme or completely rebuild messages.
  $hook = 'heartbeat_theme_alter';
  foreach (module_implements($hook) as $module) {
    $function = $module . '_' . $hook;
    $result = $function($messages, $heartbeatAccess);
  }

  // let other modules hook into the theming of the components.
  $hook = 'heartbeat_view';
  foreach (module_implements($hook) as $module) {
    $function = $module . '_' . $hook;

    // $messages should be implemented by reference!!!
    $function($messages, $heartbeatAccess);
  }
  return $messages;
}

Functions

Namesort descending Description
heartbeat_activity_delete_action Views bulk operations callback to delete multiple activity.
heartbeat_views_action_info Implementation of hook_action_info().
heartbeat_views_views_api Implementation of hook_views_api().
heartbeat_views_views_bulk_operations_object_info Implementation of hook_views_bulk_operations_object_info()
heartbeat_views_views_plugins Implementation of hook_views_plugins().
heartbeat_views_views_pre_render Implementation of hook_views_pre_render().
heartbeat_views_views_pre_view Implementation of hook_views_pre_view().
_heartbeat_group_views_messages Helper function to group and merge message built by views.
_heartbeat_render_view Helper function to (re-)render the messages from views.