You are here

hds.module in Heartbeat 6.4

Heartbeat displays module.

File

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

// by Stalski (Jochen Stals) - Menhir - www.menhir.be

/**
 * @file
 * Heartbeat displays module.
 */

/**
 * Implementation of hook_init().
 */
function hds_init() {
  drupal_add_css(drupal_get_path('module', 'hds') . '/hds_regions.css');
}

/**
 * Implementation of hook_theme().
 */
function hds_theme() {

  // Formatter theming functions.
  return array(
    'hds_created_time_ago' => array(
      'arguments' => array(
        'heartbeatactivity' => NULL,
      ),
    ),
    'hds_created_time_interval' => array(
      'arguments' => array(
        'heartbeatactivity' => NULL,
      ),
    ),
    'hds_created_time_small' => array(
      'arguments' => array(
        'heartbeatactivity' => NULL,
      ),
    ),
    'hds_created_time_medium' => array(
      'arguments' => array(
        'heartbeatactivity' => NULL,
      ),
    ),
    'hds_created_time_large' => array(
      'arguments' => array(
        'heartbeatactivity' => NULL,
      ),
    ),
  );
}

/**
 * Implementation of hook_registry_alter().
 */
function hds_theme_registry_alter(&$registry) {
  $path = drupal_get_path('module', 'hds');
  array_unshift($registry['heartbeat_message_row']['theme paths'], $path);
}

/**
 * Implementation of hook_heartbeat_view().
 */
function hds_heartbeat_view(&$messages, HeartbeatAccess $stream) {
  foreach ($messages as $key => $message) {

    // Override the build mode if a different is specified
    if (isset($message->template->variables['build_mode'])) {
      $messages[$key]->build_mode = $message->template->variables['build_mode'];
    }

    // Override the build mode with the one set on stream level.
    // But only if the stream build mode is prior to message type level settings.
    if (isset($stream->stream->variables['build_mode']) && $stream->stream->variables['build_mode'] != 'none') {
      $messages[$key]->build_mode = $stream->stream->variables['build_mode'];
    }
    ds_build_fields_and_regions($messages[$key], 'hds');
  }
}

/**
 * Render the heartbeat_activity object through the DS views plugin.
 *
 * @param array $vars The variables to manipulate.
 * @param string $build_mode The build mode to use on this object.
 */
function ds_views_row_heartbeat_activity(&$vars, $build_mode) {
  $vars['row']->build_mode = $build_mode;
  $vars['object'] = theme('heartbeat_message_row', $vars['row']);
}

/**
 * Implementation of hook_preprocess_heartbeat_message_row().
 */
function hds_preprocess_heartbeat_message_row(&$vars) {

  // DS expects an array. Make sure that it contains the exact
  // fields DS expects. The __get/__set will end up in the
  // variables container of object HeartbeatActivity.
  // Here it is put back in the root of the hash to let DS play with it.
  // Another approach would be to wrap the object in another object with
  // a composition as vd does. I chose the magic setter manner since this
  // could be used by other modules.
  $message = (array) $vars['message'];
  $message += $message['variables'];

  // Break all the rendering if needed.
  if (!$vars['message']->render_by_ds) {
    $vars['template_files'][] = 'heartbeat-message-row';
    return;
  }

  // Add template suggestions only if the build mode is not excluded.
  $vars['template_files'][] = 'hds';
  $vars['template_files'][] = 'hds-' . $message['build_mode'];
  $vars['template_files'][] = 'hds-' . $message['build_mode'] . '-' . $message['message_id'];
  $vars['content'] = ds_render_content($vars['message'], 'hds', $message);
}

/**
 * Implementation of hook_ds_api().
 */
function hds_ds_api() {
  return array(
    'title' => 'Heartbeat displays',
    'module' => 'hds',
    'object' => 'heartbeatactivity',
    'views_base' => 'heartbeat_activity',
    'types' => 'hds_get_types',
  );
}

/**
 * Return heartbeat types.
 */
function hds_get_types() {
  $types = array();
  $row = new stdClass();
  $row->name = 'heartbeatactivity';
  $row->type = 'heartbeatactivity';
  $types['heartbeatactivity'] = $row;
  return $types;
}

/**
 * Implementation of hook_ds_fields().
 */
function hds_ds_fields($type_name, $build_mode, $extra) {
  $fields = array(
    'ds_avatar' => array(
      'title' => 'Actor avatar',
      'properties' => array(
        'formatters' => array(
          'ds_eval_code' => t('Default'),
        ),
        'code' => '<?php print theme("user_picture", $object->actor); ?>',
      ),
      'type' => DS_FIELD_TYPE_CODE,
      'status' => DS_FIELD_STATUS_STATIC,
    ),
    'ds_message' => array(
      'title' => 'Message',
      'properties' => array(
        'formatters' => array(
          'ds_eval_code' => t('Default'),
        ),
        'code' => '<?php print $object->message; ?>',
      ),
      'type' => DS_FIELD_TYPE_CODE,
      'status' => DS_FIELD_STATUS_STATIC,
    ),
    'ds_created' => array(
      'title' => 'Created time',
      'properties' => array(
        'formatters' => array(
          'hds_created_time_ago' => t('Time ago'),
          'hds_created_time_interval' => t('Time interval, granularity 2'),
          'hds_created_time_small' => t('Date format small'),
          'hds_created_time_medium' => t('Date format medium'),
          'hds_created_time_large' => t('Date format large'),
        ),
      ),
      'type' => DS_FIELD_TYPE_THEME,
      'status' => DS_FIELD_STATUS_STATIC,
    ),
    'ds_widgets' => array(
      'title' => 'Widget attachments',
      'properties' => array(
        'formatters' => array(
          'ds_eval_code' => t('Default'),
        ),
        'code' => '<?php print _theme_heartbeat_widgets($object); ?>',
      ),
      'type' => DS_FIELD_TYPE_CODE,
      'status' => DS_FIELD_STATUS_STATIC,
    ),
    'ds_buttons' => array(
      'title' => 'Buttons',
      'properties' => array(
        'formatters' => array(
          'ds_eval_code' => t('Default'),
        ),
        'code' => '<?php print theme("heartbeat_buttons", $object); ?>',
      ),
      'type' => DS_FIELD_TYPE_CODE,
      'status' => DS_FIELD_STATUS_STATIC,
    ),
  );
  return array(
    'hds' => $fields,
  );
}

/**
 * Implementation of hook_content_build_modes().
 */
function hds_content_build_modes() {
  $build_modes = array(
    'hds' => array(
      'title' => t('Heartbeat displays'),
      'build modes' => array(
        'full' => array(
          'title' => t('Full message'),
          'module' => 'hds',
          'weight' => -1,
        ),
        'message' => array(
          'title' => t('Message'),
          'module' => 'hds',
          'weight' => -1,
        ),
      ),
    ),
  );
  return $build_modes;
}

/**
 * Implementation of hook_form_alter().
 */
function hds_form_alter(&$form, $form_state, $form_id) {
  if (in_array($form_id, array(
    'heartbeat_messages_add',
    'heartbeat_messages_edit',
  ))) {

    // Add a build mode on template base
    $hds_build_modes = hds_content_build_modes();
    $options = ds_get_build_modes('hds');
    $form['data']['build_mode'] = array(
      '#type' => 'select',
      '#options' => drupal_map_assoc(array_keys($options)),
      '#default_value' => isset($form_state['message']->variables['build_mode']) ? $form_state['message']->variables['build_mode'] : 'full',
      '#title' => t('Display suite build mode'),
    );
  }
  if ($form_id == 'heartbeat_activity_stream_configure') {

    // Option when you want to use build modes on message type level
    $options = array(
      'none' => t('None, Use build mode of message templates.'),
    );
    $options += ds_get_build_modes('hds');
    $form['settings']['build_mode'] = array(
      '#type' => 'select',
      '#options' => drupal_map_assoc(array_keys($options)),
      '#default_value' => isset($form_state['stream']['build_mode']) ? $form_state['stream']['build_mode'] : 'full',
      '#title' => t('Display suite build mode'),
    );
  }
}

/**
 * Formatter for the time ago created display
 */
function theme_hds_created_time_ago($heartbeatactivity) {
  return _theme_time_ago($heartbeatactivity['object']->timestamp);
}

/**
 * Formatter for the time ago created display
 */
function theme_hds_created_time_small($heartbeatactivity) {
  return '<span class="heartbeat_times">' . format_date($_SERVER['REQUEST_TIME'] - $heartbeatactivity['object']->timestamp, 'small') . '</span>';
}

/**
 * Formatter for the time ago created display
 */
function theme_hds_created_time_medium($heartbeatactivity) {
  return '<span class="heartbeat_times">' . format_date($_SERVER['REQUEST_TIME'] - $heartbeatactivity['object']->timestamp, 'medium') . '</span>';
}

/**
 * Formatter for the time ago created display
 */
function theme_hds_created_time_large($heartbeatactivity) {
  return '<span class="heartbeat_times">' . format_date($_SERVER['REQUEST_TIME'] - $heartbeatactivity['object']->timestamp, 'large') . '</span>';
}

/**
 * Formatter for the time ago created display
 */
function theme_hds_created_time_interval($heartbeatactivity) {
  return '<span class="heartbeat_times">' . format_interval($_SERVER['REQUEST_TIME'] - $heartbeatactivity['object']->timestamp, 2) . ' ' . t('ago') . '</span>';
}

Functions

Namesort descending Description
ds_views_row_heartbeat_activity Render the heartbeat_activity object through the DS views plugin.
hds_content_build_modes Implementation of hook_content_build_modes().
hds_ds_api Implementation of hook_ds_api().
hds_ds_fields Implementation of hook_ds_fields().
hds_form_alter Implementation of hook_form_alter().
hds_get_types Return heartbeat types.
hds_heartbeat_view Implementation of hook_heartbeat_view().
hds_init Implementation of hook_init().
hds_preprocess_heartbeat_message_row Implementation of hook_preprocess_heartbeat_message_row().
hds_theme Implementation of hook_theme().
hds_theme_registry_alter Implementation of hook_registry_alter().
theme_hds_created_time_ago Formatter for the time ago created display
theme_hds_created_time_interval Formatter for the time ago created display
theme_hds_created_time_large Formatter for the time ago created display
theme_hds_created_time_medium Formatter for the time ago created display
theme_hds_created_time_small Formatter for the time ago created display