View source
<?php
define('BLOCK_REFRESH_DEFAULT_AUTOMATIC', FALSE);
define('BLOCK_REFRESH_DEFAULT_MANUAL', FALSE);
define('BLOCK_REFRESH_DEFAULT_AUTOMATIC_TIMER', 120);
function block_refresh_init() {
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');
}
}
function block_refresh_perm() {
return array(
'access block refresh content',
);
}
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;
}
}
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.'),
'#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['#submit'][] = 'block_refresh_submit';
}
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);
}
function block_refresh_menu() {
$items = array();
$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;
}
function block_refresh_footer($main = 0) {
if (!user_access('access block refresh content')) {
return;
}
global $theme_key;
$blocks = array();
$regions = system_region_list($theme_key);
foreach ($regions as $region => $value) {
$blocks = array_merge($blocks, block_list($region));
}
$settings = variable_get('block_refresh_settings', array());
$js = '';
foreach ($settings as $block) {
if (isset($blocks[$block['block']['block'] . '_' . $block['block']['delta']])) {
if ($block['enabled']) {
$div = "#block-{$block['block']['block']}-{$block['block']['delta']} .content";
$timer = $block['timer'] * 1000;
$base = base_path();
$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 ($block['manual']) {
$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 (!empty($js)) {
drupal_add_js("\$(document).ready(function() {\n{$js}\n})", 'inline', 'footer');
}
}
function block_refresh_settings() {
$form = array();
$form['settings'] = array(
'#type' => 'fieldset',
'#title' => t('Global settings'),
'#collapsible' => 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);
}
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;
}
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;
}
}