You are here

views_autorefresh.rules.inc in Views Auto-Refresh 7

Same filename and directory in other branches
  1. 7.2 views_autorefresh.rules.inc

Rules integration for the views_autorefresh module.

File

views_autorefresh.rules.inc
View source
<?php

/**
 * @file
 * Rules integration for the views_autorefresh module.
 */

/**
 * Implements hook_rules_action_info().
 */
function views_autorefresh_rules_action_info() {
  $items = array();
  if (module_exists('nodejs')) {
    $items['views_autorefresh'] = array(
      'label' => t('Autorefresh a View through Node.js'),
      'group' => t('Views Autorefresh'),
      'parameter' => array(
        'views' => array(
          'type' => 'list<integer>',
          'label' => t('Views'),
          'description' => t('Select one or more views that should refreshed.'),
          'options list' => 'views_autorefresh_get_nodejs_views',
        ),
        'context' => array(
          'type' => '*',
          'label' => t('Context'),
          'description' => t('Additional data that can be passed to the message hook (optional).'),
          'optional' => TRUE,
        ),
      ),
      'base' => 'views_autorefresh_rules_process',
    );
  }
  return $items;
}

/**
 * Rules action function to process and send message to nodejs.
 */
function views_autorefresh_rules_process($views, $context) {
  views_autorefresh_nodejs_refresh($views, $context);
}

/**
 * Helper function to get all views.
 */
function views_autorefresh_get_nodejs_views() {
  $eligible_views = array();
  $all_views = views_get_enabled_views();
  if ($all_views) {
    foreach ($all_views as $view) {
      if ($view->display) {
        foreach ($view->display as $id => $display) {
          if (isset($display->display_options['header']['autorefresh'])) {

            // Show all displays except default and the secondary one
            // because we don't know which one needs to be notified.
            foreach ($view->display as $id2 => $display2) {
              if ($display2->display_plugin != 'default' && $id2 != $display->display_options['header']['autorefresh']['display']) {
                $eligible_views[$view->name . '-' . $id2] = $view->human_name . ' - ' . $display2->display_title;
              }
            }
            break;
          }
        }
      }
    }
  }
  natsort($eligible_views);
  return $eligible_views;
}

Functions

Namesort descending Description
views_autorefresh_get_nodejs_views Helper function to get all views.
views_autorefresh_rules_action_info Implements hook_rules_action_info().
views_autorefresh_rules_process Rules action function to process and send message to nodejs.