You are here

block_refresh.module in Block Refresh 6

File

block_refresh.module
View source
<?php

define('BLOCK_REFRESH_DEFAULT_AUTOMATIC', FALSE);

// autorefresh disabled by default
define('BLOCK_REFRESH_DEFAULT_MANUAL', FALSE);

// manual refresh disabled by default
define('BLOCK_REFRESH_DEFAULT_AUTOMATIC_TIMER', 120);

// default refreshes every two minutes, if enabled

/**
 * Implementation of hook_init()
 * Calls the jquery to refresh blocks automatically, but only if the blocks exist on the current page and are enabled
 */
function block_refresh_init() {

  // If the contrib module jQ is not installed...
  if (!module_invoke('jq', 'add', 'block_refresh')) {
    drupal_add_js(drupal_get_path('module', 'block_refresh') . '/js/block_refresh.js');
    drupal_add_css(drupal_get_path('module', 'block_refresh') . '/css/block_refresh.css');
  }
}

/**
 *  Implementation of hook_perm()
 *  Add permission for accessing auto/manually refreshed block content
 */
function block_refresh_perm() {
  return array(
    'access block refresh content',
  );
}

/**
 * Implementation of hook_help().
 */
function block_refresh_help($path, $arg) {
  switch ($path) {
    case 'admin/help#block_refresh':
      $block_refresh_help = '<div class="form-item">';
      $block_refresh_help .= t("Ensure that you have !configured for user roles. Adding a permission to %access will allow a block, when configured, to be refreshed automatically and/or manually.", array(
        '%access' => 'access block refresh content',
        '!configured' => l(t('configured permissions'), 'admin/user/access', array(), NULL, 'module-block_refresh'),
      ));
      $block_refresh_help .= '</div><div class="form-item">';
      $block_refresh_help .= t("You will also need to set the appropriate settings for each block that you wish to automatically and/or manually refresh by clicking on the appropriate %configure link(s) on the !admin.", array(
        '%configure' => t('configure'),
        '!admin' => l(t('blocks administration page'), 'admin/build/block'),
      ));
      $block_refresh_help .= '</div>';
      return $block_refresh_help;
  }
}

/**
 * Implementation of hook_form_FORM_ID_alter()
 * Add a 'Block Refresh' settings fieldset to the block admin form
 */
function block_refresh_form_block_admin_configure_alter(&$form, $form_state) {
  $settings = variable_get('block_refresh_settings', array());
  $form['block_refresh'] = array(
    '#type' => 'fieldset',
    '#title' => t('Block refresh settings'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#weight' => -1,
  );
  $form['block_refresh']['block_refresh_enable'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable block to be refreshed automatically'),
    '#description' => t('If checked, then the content of this block will be refresh automatically every x seconds defined below.'),
    '#default_value' => isset($settings['block-' . $form['module']['#value'] . '-' . $form['delta']['#value']]['enabled']) ? $settings['block-' . $form['module']['#value'] . '-' . $form['delta']['#value']]['enabled'] : variable_get('block_refresh_default_automatic', BLOCK_REFRESH_DEFAULT_AUTOMATIC),
  );
  $form['block_refresh']['block_refresh_timer'] = array(
    '#type' => 'textfield',
    '#title' => t('Block refresh timer'),
    '#description' => t('If this block is set to be refreshed automatically (checkbox above is checked), enter the number of <strong>seconds</strong> between each refresh.'),
    //Assuming that Block Autorefresh is enabled, then the content of this block will refresh itself periodically, every number of seconds equal to this setting. If this block is grouped to refresh with other blocks, then this setting will be ignored, instaed using the global setting of @seconds.', array('@seconds' => format_plural(variable_get('block_refresh_default_automatic_timer', BLOCK_REFRESH_DEFAULT_AUTOMATIC_TIMER), '1 second', '@count seconds'))),
    '#default_value' => $settings['block-' . $form['module']['#value'] . '-' . $form['delta']['#value']]['timer'] ? $settings['block-' . $form['module']['#value'] . '-' . $form['delta']['#value']]['timer'] : variable_get('block_refresh_autorefresh_default_timer', BLOCK_REFRESH_DEFAULT_AUTOMATIC_TIMER),
  );
  $form['block_refresh']['block_refresh_manual'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable block to be refreshed manually'),
    '#description' => t('If checked, then the content of this block may be refreshed manually by the user, by clicking on a provided (themeable) button in the block\'s subject header.'),
    '#default_value' => isset($settings['block-' . $form['module']['#value'] . '-' . $form['delta']['#value']]['manual']) ? $settings['block-' . $form['module']['#value'] . '-' . $form['delta']['#value']]['manual'] : variable_get('block_refresh_default_manual', BLOCK_REFRESH_DEFAULT_MANUAL),
  );

  /*
    $form['block_refresh']['block_refresh_group'] = array(
      '#type' => 'checkbox',
      '#title' => t('Group with other refreshing blocks'),
      '#description' => t('If checked, then this block will be refreshed with other blocks. If also set to autorefresh, then the timer setting will be ignored, instead using the global setting of @seconds.', array('@seconds' => format_plural(variable_get('block_refresh_default_automatic_timer', BLOCK_REFRESH_DEFAULT_AUTOMATIC_TIMER), '1 second', '@count seconds'))),
      '#default_value' => isset($settings['block-' . $form['module']['#value'] . '-' . $form['delta']['#value']]['group']) ? $settings['block-' . $form['module']['#value'] . '-' . $form['delta']['#value']]['group'] : variable_get('block_refresh_group_auto', FALSE),
    );
  */
  $form['#submit'][] = 'block_refresh_submit';
}

/**
 *  Submission handler for for block_refresh_menu()
 *  This handles the submission on the specific block configuration page
 */
function block_refresh_submit($form, &$form_state) {
  $settings = variable_get('block_refresh_settings', array());
  $settings['block-' . $form_state['values']['module'] . '-' . $form_state['values']['delta']]['enabled'] = $form_state['values']['block_refresh_enable'];
  $settings['block-' . $form_state['values']['module'] . '-' . $form_state['values']['delta']]['manual'] = $form_state['values']['block_refresh_manual'];
  $settings['block-' . $form_state['values']['module'] . '-' . $form_state['values']['delta']]['group'] = $form_state['values']['block_refresh_group'];
  $settings['block-' . $form_state['values']['module'] . '-' . $form_state['values']['delta']]['timer'] = $form_state['values']['block_refresh_timer'];
  $settings['block-' . $form_state['values']['module'] . '-' . $form_state['values']['delta']]['block'] = array(
    'block' => $form_state['values']['module'],
    'delta' => $form_state['values']['delta'],
  );
  variable_set('block_refresh_settings', $settings);
}

/**
 * Implemention of hook_menu()
 */
function block_refresh_menu() {
  $items = array();

  /*
    $items['admin/settings/block_refresh'] = array(
      'title' => t('Block refresh'),
      'description' => t('Settings for automatic and manual refreshing of configured blocks.'),
      'page callback' => 'drupal_get_form',
      'page arguments' => array('block_refresh_settings'),
      'access arguments' => array('administer site configuration'),
    );
  */

  // this will display the contents of a block, if it's configured with Block Refresh
  $items['block_refresh'] = array(
    'title' => t('Block refresh block content'),
    'page callback' => 'block_refresh_block_content',
    'access arguments' => array(
      'access block refresh content',
    ),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
 * Implementation of hook_footer().
 */
function block_refresh_footer($main = 0) {

  // don't bother to continue if the user can't refresh content anyway...
  if (!user_access('access block refresh content')) {
    return;
  }
  global $theme_key;
  $blocks = array();

  // Get an array of regions for the given theme
  $regions = system_region_list($theme_key);

  // Get all blocks that are in each region of the theme and put into $blocks array
  foreach ($regions as $region => $value) {
    $blocks = array_merge($blocks, block_list($region));
  }

  // Get the variable that holds all of the configured blocks information (this is an array)
  $settings = variable_get('block_refresh_settings', array());
  $js = '';

  // Test every block that is currently being displayed
  foreach ($settings as $block) {

    // If the block is configured for Block Refresh...
    if (isset($blocks[$block['block']['block'] . '_' . $block['block']['delta']])) {

      // I believe (though have not thoroughly tested) that this is an unnecessary test
      if ($block['enabled']) {

        // this is the block div css id and content class
        $div = "#block-{$block['block']['block']}-{$block['block']['delta']} .content";

        // we store seconds, but js expects milliseconds
        $timer = $block['timer'] * 1000;
        $base = base_path();

        // Set jQuery array to be set in page footer ... also checks to see whether clean URLs are enabled or not
        $js .= "  _block_refresh_data['{$div}'] = new block_refresh_data({$timer}, '{$base}" . (variable_get('clean_url', 0) == 0 ? "?q=" : "") . "block_refresh/{$block['block']['block']}/{$block['block']['delta']}');\n";
        $js .= "  block_refresh_timer('{$div}');";
      }

      // If the block is configured to be user-refreshed
      if ($block['manual']) {

        // this is the block div css id and content class
        $div = "block-{$block['block']['block']}-{$block['block']['delta']}";
        $base = base_path();
        $url = "{$base}block_refresh/{$block['block']['block']}/{$block['block']['delta']}";
        $content_url = "#{$div} .content";
        $js .= "  block_refresh_add_button('{$div}', '{$url}', '{$content_url}', '" . t('Refresh') . "');";
      }
    }
  }

  // If there is a block that is currently being displayed that is configured to work with Block Refresh...
  if (!empty($js)) {
    drupal_add_js("\$(document).ready(function() {\n{$js}\n})", 'inline', 'footer');
  }
}

/**
 *  Callback for admin/settings/block_refresh
 */
function block_refresh_settings() {
  $form = array();
  $form['settings'] = array(
    '#type' => 'fieldset',
    '#title' => t('Global settings'),
    '#collapsible' => FALSE,
  );

  /*
    $form['settings']['block_refresh_group_auto'] = array(
      '#type' => 'checkbox',
      '#title' => t('Group block refreshes'),
      '#description' => t('If checked, then all grouped block refresh enabled blocks will be refreshed together, whether manually or using the timer settings below. To group blocks together, check the appropriate block on that block\'s configuration settings page.'),
      '#default_value' => variable_get('block_refresh_group_auto', FALSE),
    );
  */
  $form['settings']['block_refresh_default_automatic_timer'] = array(
    '#type' => 'textfield',
    '#title' => t('Default block refresh timer'),
    '#description' => t('Enter the time, <strong>in seconds</strong>, that blocks will refresh when set to automatic and are set to use the default timer.'),
    '#default_value' => variable_get('block_refresh_default_automatic_timer', BLOCK_REFRESH_DEFAULT_AUTOMATIC_TIMER),
  );
  return system_settings_form($form);
}

/**
 *  page callback for /block_refresh/[module]/[delta]
 *  displays the block content, without any other page information
 */
function block_refresh_block_content($block = NULL, $delta = NULL) {
  if (!isset($block) || !isset($delta) || $block != 'block_refresh' && !module_hook($block, 'block')) {
    drupal_not_found();
  }
  if ($block == 'block_refresh' && $delta == 'all') {
    $query = isset($_GET['blocks']) ? $_GET['blocks'] : '';
    $pairs = explode(',', $query);
    foreach ($pairs as $pair) {
      list($module, $delta) = explode('|', $pair);
      $block = module_invoke($module, 'block', 'view', $delta);
      $output .= '<div id="block-refresh-data-' . $module . '-' . $delta . '">' . $block['content'] . '</div>';
    }
    if ($output) {
      print '<div id="block-refresh-data-all" class="block-refresh-hidden">' . $output . '</div>';
    }
    exit;
  }
  $settings = variable_get('block_refresh_settings', array());
  if (!$settings['block-' . $block . '-' . $delta]['enabled'] && !$settings['block-' . $block . '-' . $delta]['manual']) {
    drupal_not_found();
  }
  $block = module_invoke($block, 'block', 'view', $delta);
  print $block['content'];
  exit;
}

/**
 * Implementation of hook_jq()
 * Allows optional integration with the jQ module.
 */
function block_refresh_jq($op, $plugin = NULL, $args = array(), $already_loaded = NULL) {
  switch ($op) {
    case 'info':
      $info = array();
      $info['block_refresh'] = array(
        'name' => t('Block Refresh'),
        'description' => t('Block Refresh allows an administrator to set up any block to automatically refresh its content every x seconds. Uses jQuery. Configure on individual blocks.'),
        'url' => 'http://drupal.org/project/block_refresh',
        'version' => t('1.2'),
        'files' => array(
          'js' => array(
            drupal_get_path('module', 'block_refresh') . '/js/block_refresh.js',
          ),
          'css' => array(
            drupal_get_path('module', 'block_refresh') . '/css/block_refresh.css',
          ),
        ),
      );
      return $info;
  }
}

Functions

Namesort descending Description
block_refresh_block_content page callback for /block_refresh/[module]/[delta] displays the block content, without any other page information
block_refresh_footer Implementation of hook_footer().
block_refresh_form_block_admin_configure_alter Implementation of hook_form_FORM_ID_alter() Add a 'Block Refresh' settings fieldset to the block admin form
block_refresh_help Implementation of hook_help().
block_refresh_init Implementation of hook_init() Calls the jquery to refresh blocks automatically, but only if the blocks exist on the current page and are enabled
block_refresh_jq Implementation of hook_jq() Allows optional integration with the jQ module.
block_refresh_menu Implemention of hook_menu()
block_refresh_perm Implementation of hook_perm() Add permission for accessing auto/manually refreshed block content
block_refresh_settings Callback for admin/settings/block_refresh
block_refresh_submit Submission handler for for block_refresh_menu() This handles the submission on the specific block configuration page

Constants