You are here

views_autorefresh.module in Views Hacks 6

File

views_autorefresh/views_autorefresh.module
View source
<?php

/**
 * Implementation of hook_views_api().
 */
function views_autorefresh_views_api() {
  return array(
    'api' => 2.0,
  );
}

/**
 * Implementation of hook_theme().
 */
function views_autorefresh_theme() {
  return array(
    'views_autorefresh' => array(
      'arguments' => array(
        'interval' => array(
          'default' => NULL,
        ),
        'ping' => array(
          'default' => NULL,
        ),
        'incremental' => array(
          'default' => NULL,
        ),
        'view' => array(
          'default' => NULL,
        ),
      ),
    ),
  );
}

/**
 * Theme function for 'views_autorefresh'.
 */
function theme_views_autorefresh($interval, $ping = NULL, $incremental = NULL, $view = NULL) {
  if (empty($view)) {
    $view = views_get_current_view();
  }

  // Signal the view that we're incrementally auto-refreshing.
  if (!empty($incremental)) {
    $view->autorefresh['incremental'] = $incremental;
  }
  if (!empty($ping)) {
    $view->autorefresh['ping'] = $ping;
  }

  // Add the JavaScript settings.
  drupal_add_js(drupal_get_path('module', 'views_autorefresh') . '/views_autorefresh.js');
  drupal_add_js(array(
    'views_autorefresh' => array(
      $view->name => array(
        'interval' => $interval,
        'ping' => $ping,
        'incremental' => $incremental,
        'timestamp' => __views_autorefresh_get_timestamp($view),
      ),
    ),
  ), 'setting');
  $query_string = drupal_query_string_encode($_REQUEST, array_merge(array(
    'q',
    'pass',
  ), array_keys($_COOKIE)));
  $query[] = $query_string;
  $link = l('', $_GET['q'], array(
    'query' => count($query) ? implode('&', $query) : NULL,
  ));

  // Signal modules to add their own plugins.
  module_invoke_all('views_autorefresh_plugins', $view);
  return '<div class="auto-refresh">' . $link . '</div>';
}

/**
 * Implementation of hook_ajax_data_alter().
 */
function views_autorefresh_ajax_data_alter(&$object, $type, $view) {
  if (!empty($view->autorefresh['incremental']) || !empty($view->autorefresh['ping'])) {
    $object->view_name = $view->name;
    $object->timestamp = __views_autorefresh_get_timestamp($view);
    if (!empty($view->autorefresh['incremental']) && $view->autorefresh['incremental']['view_base_path'] == $_REQUEST['view_base_path']) {

      // remove 'Drupal.Views.Ajax.ajaxViewResponse' from callbacks because we're not replacing the whole div.
      unset($object->__callbacks[array_search('Drupal.Views.Ajax.ajaxViewResponse', $object->__callbacks)]);
      array_unshift($object->__callbacks, 'Drupal.views_autorefresh.ajaxViewResponseUpdate');
    }
    else {
      $object->__callbacks[] = 'Drupal.views_autorefresh.ajaxViewResponseRefresh';
    }
  }
}

/**
 * Helper function to return view's "timestamp" - either real timestamp or max primary key in view rows.
 */
function __views_autorefresh_get_timestamp($view) {
  if (empty($view->autorefresh['ping']) || empty($view->autorefresh['ping']['use_base_field'])) {
    return time();

    // return standard timestamp
  }
  $base_field = $view->base_field;
  $max = 0;
  foreach ($view->result as $row) {
    if ($row->{$base_field} > $max) {
      $max = $row->{$base_field};
    }
  }
  return $max;
}

Functions

Namesort descending Description
theme_views_autorefresh Theme function for 'views_autorefresh'.
views_autorefresh_ajax_data_alter Implementation of hook_ajax_data_alter().
views_autorefresh_theme Implementation of hook_theme().
views_autorefresh_views_api Implementation of hook_views_api().
__views_autorefresh_get_timestamp Helper function to return view's "timestamp" - either real timestamp or max primary key in view rows.