View source
<?php
define('BLOCK_REFRESH_DEFAULT_AUTOMATIC', FALSE);
define('BLOCK_REFRESH_DEFAULT_MANUAL', FALSE);
define('BLOCK_REFRESH_DEFAULT_AUTOMATIC_TIMER', 120);
define('BLOCK_REFRESH_ACCESS_CONTENT_PERMISSION', 'access block refresh content');
function block_refresh_init() {
if (user_access(BLOCK_REFRESH_ACCESS_CONTENT_PERMISSION)) {
drupal_add_js(drupal_get_path('module', 'block_refresh') . '/js/block_refresh.js', array(
'scope' => 'footer',
));
drupal_add_css(drupal_get_path('module', 'block_refresh') . '/css/block_refresh.css');
drupal_add_js(array(
'block_refresh' => array(
'settings' => variable_get('block_refresh_settings', array()),
'args' => arg(),
'query' => block_refresh_get_query_as_string(),
),
), 'setting');
}
}
function block_refresh_permission() {
return array(
BLOCK_REFRESH_ACCESS_CONTENT_PERMISSION => array(
'title' => t('Access block refresh content'),
'description' => t('Selected roles will see content refreshed in enabled blocks.'),
),
);
}
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' => BLOCK_REFRESH_ACCESS_CONTENT_PERMISSION,
'!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) {
$block_id = drupal_html_id('block-' . $form['module']['#value'] . '-' . $form['delta']['#value']);
$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_auto'] = 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_id]['auto']) ? $settings[$block_id]['auto'] : 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' => isset($settings[$block_id]['timer']) ? $settings[$block_id]['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_id]['manual']) ? $settings[$block_id]['manual'] : variable_get('block_refresh_default_manual', BLOCK_REFRESH_DEFAULT_MANUAL),
);
$form['#submit'][] = 'block_refresh_submit';
}
function block_refresh_submit($form, &$form_state) {
$block_id = drupal_html_id('block-' . $form_state['values']['module'] . '-' . $form_state['values']['delta']);
$settings = variable_get('block_refresh_settings', array());
if (!$form['block_refresh']['block_refresh_auto']['#checked'] && !$form['block_refresh']['block_refresh_manual']['#checked']) {
unset($settings[$block_id]);
}
else {
$settings[$block_id]['element'] = $block_id;
$settings[$block_id]['auto'] = $form_state['values']['block_refresh_auto'];
$settings[$block_id]['manual'] = $form_state['values']['block_refresh_manual'];
$settings[$block_id]['timer'] = $form_state['values']['block_refresh_timer'];
$settings[$block_id]['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' => 'Block refresh block content',
'page callback' => 'block_refresh_block_content',
'access arguments' => array(
BLOCK_REFRESH_ACCESS_CONTENT_PERMISSION,
),
'type' => MENU_CALLBACK,
);
return $items;
}
function block_refresh_block_content($module = NULL, $delta = NULL) {
if (!isset($module) || !isset($delta)) {
drupal_not_found();
}
$block_id = drupal_html_id('block-' . $module . '-' . $delta);
$settings = variable_get('block_refresh_settings', array());
if (!$settings[$block_id]['auto'] && !$settings[$block_id]['manual']) {
drupal_not_found();
}
$args = arg();
if (!empty($args[3]) && $args[0] == 'block_refresh' && $args[1] == $module && $args[2] == $delta) {
unset($args[0]);
unset($args[1]);
unset($args[2]);
$_GET['q'] = implode('/', $args);
}
else {
$_GET['q'] = '';
$args = arg();
}
$block = module_invoke($module, 'block_view', $delta);
print render($block['content']);
}
function block_refresh_get_query_as_string() {
$vars = $_GET;
if (!empty($vars['q'])) {
unset($vars['q']);
}
if (count($vars) > 0) {
$querystring = '?' . http_build_query($vars);
return $querystring;
}
else {
return '';
}
}