'dynamicload/js', 'title' => t('Dynamicload page'), 'access' => user_access('access content'), 'type' => MENU_CALLBACK, 'callback' => 'dynamicload_js', ); $items[] = array( 'path' => 'dynamicload/block', 'title' => t('Dynamicload block'), 'access' => user_access('access content'), 'type' => MENU_CALLBACK, 'callback' => 'dynamicload_fetch_block', ); } else { dynamicload_load(); } return $items; } /** * Load needed files. */ function dynamicload_load() { static $loaded = FALSE; if (!$loaded) { jstools_modules_includes('dynamicload'); $path = drupal_get_path('module', 'dynamicload'); module_invoke('jstools', 'add_js', array( $path . '/dynamicload.js', 'misc/progress.js', )); $loaded = TRUE; } } /** * Send settings to the browser. * * We do this in the footer because some of the settings are theme-dependent. Typically, * hook_footer will be called after the theme is initiated, so we have the correct theme * to load data for. */ function dynamicload_footer() { static $loaded = FALSE; if (!$loaded) { $settings = dynamicload_block_data(); foreach (module_invoke_all('dynamicload') as $setting) { $settings = array_merge($settings, $setting); } // Allow theme-based overrides. $theme_data = module_invoke('jstools', 'theme_data', 'dynamicload'); if (is_array($theme_data)) { foreach ($theme_data as $key => $selector) { if ($source = array_search($key, $settings)) { $settings[$source] = $selector; } // No theme support, unset. else { unset($settings[$source]); } } } drupal_add_js(array('dynamicload' => array('content' => $theme_data['content'] ? $theme_data['content'] : '#main', 'settings' => $settings)), 'setting'); $loaded = TRUE; } return ''; } /** * Implementation of hook_form_alter(). */ function dynamicload_form_alter($form_id, &$form) { if ($form_id == 'block_admin_configure') { $options = array('content' => t('Main content area')); $result = db_query('SELECT module, delta FROM {blocks} WHERE status = 1 AND NOT (module = "%s" AND delta = "%s")', $form['module']['#value'], $form['delta']['#value']); while ($block = db_fetch_object($result)) { $module_blocks = module_invoke($block->module, 'block', 'list'); $options['#block-' . $block->module . '-' . $block->delta] = t('!info block', array('!info' => $module_blocks[$block->delta]['info'])); } $settings = dynamicload_block_data($form['module']['#value'], $form['delta']['#value']); $form['#submit']['dynamicload_submit'] = array(); $form['dynamicload'] = array( '#type' => 'fieldset', '#title' => t('Dynamic loading'), '#collapsible' => true, '#weight' => -5 ); $form['dynamicload']['dynamicload_apply'] = array( '#type' => 'checkbox', '#title' => t('Apply dynamic loading'), '#description' => t('Check if you want links on this block to be loaded dynamically rather than through a regular page refresh. If you enable this functionality for a block, make sure that you thoroughly test the result, as not all pages will perform as expected when loaded dynamically.'), '#default_value' => $settings['apply'] ? 1 : 0 ); $form['dynamicload']['dynamicload_target'] = array( '#type' => 'select', '#title' => t('Target area'), '#default_value' => $settings['target'] ? $settings['target'] : '#main', '#options' => $options, '#description' => t('Select the page area for this block\'s links to load into. Only enabled blocks are available.'), ); $form['dynamicload']['dynamicload_refresh'] = array( '#type' => 'checkbox', '#title' => t('Automatically refresh this block'), '#default_value' => $settings['refresh'] ? 1 : 0 ); $form['dynamicload']['dynamicload_refresh_interval'] = array( '#type' => 'select', '#title' => t('Block refresh interval'), '#description' => t('Select the interval at which this block should refresh.'), '#default_value' => $settings['refresh_interval'] ? $settings['refresh_interval'] : 30000, '#options' => array(5000 => t('five seconds'), 10000 => t('ten seconds'), 30000 => t('thirty seconds'), 60000 => t('one minute'), 120000 => t('two minutes'), 180000 => t('three minutes'), 300000 => t('five minutes'), 600000 => t('ten minutes')), ); } } /** * Register settings. */ function dynamicload_submit($form_id, $form_values) { if (db_num_rows(db_query("SELECT * FROM {dynamicload_blocks} WHERE module = '%s' AND delta = %d", $form_values['module'], $form_values['delta']))) { db_query("UPDATE {dynamicload_blocks} SET refresh = %d, refresh_interval = %d, apply = %d, target = '%s' WHERE module = '%s' AND delta = %d", $form_values['dynamicload_refresh'], $form_values['dynamicload_refresh_interval'], $form_values['dynamicload_apply'], $form_values['dynamicload_target'], $form_values['module'], $form_values['delta']); drupal_set_message(t('Dynamic loading block data updated.')); } else { db_query("INSERT INTO {dynamicload_blocks} (module, delta, refresh, refresh_interval, apply, target) VALUES ('%s', %d, %d, %d, %d, '%s')", $form_values['module'], $form_values['delta'], $form_values['dynamicload_refresh'], $form_values['dynamicload_refresh_interval'], $form_values['dynamicload_apply'], $form_values['dynamicload_target']); drupal_set_message(t('Dynamic loading block data saved.')); } } /** * Load dynamicload settings for a block or all blocks. */ function dynamicload_block_data($module = NULL, $delta = NULL) { static $blocks = NULL; if ($blocks == NULL) { $blocks = array(); $result = db_query("SELECT * FROM {dynamicload_blocks}"); while ($block = db_fetch_array($result)) { foreach (array('refresh', 'apply') as $key) { $block[$key] = (bool) ($block[$key]); } $block['type'] = 'block'; $block['selector'] = '#block-'. $block['module'] .'-'. $block['delta']; $blocks[$block['module'] .'_'. $block['delta']] = $block; } } if ($module !== NULL && $delta !== NULL) { return $blocks[$module .'_'. $delta] ? $blocks[$module .'_'. $delta] : array(); } else { return $blocks; } } /** * List blocks available to the current user on the current page. */ function dynamicload_block_list() { global $theme_key; $blocks = array(); if ($theme_key) { $regions = system_region_list($theme_key); foreach (array_keys($regions) as $region) { $block_list = block_list($region); $blocks = array_merge($blocks, $block_list); } } return $blocks; } /** * Menu callback to return a rendered block in JSON format. */ function dynamicload_fetch_block($module, $delta) { global $theme_key; if (!isset($theme_key)) { init_theme(); } $region = db_result(db_query("SELECT region FROM {blocks} WHERE module = '%s' AND delta = %d AND theme = '%s'", $module, $delta, $theme_key)); $blocks = block_list($region); print drupal_to_js(array( 'result' => TRUE, 'content' => theme('block', $blocks[$module .'_'. $delta]), )); exit; } /** * Menu callback to return a rendered page in JSON format. */ function dynamicload_js() { // The path requested is the current q arg minus the 'dynamicload/js/'. if ($path = substr($_GET['q'], 15)) { $return = module_invoke('pagearray', 'page', $path); if ($return['status'] === FALSE) { switch ($return['value']) { case MENU_NOT_FOUND: case NULL: $result = array( 'result' => FALSE, 'message' => t('Item was not found.'), ); break; case MENU_ACCESS_DENIED: $result = array( 'result' => FALSE, 'message' => t('Access denied.'), ); break; case MENU_SITE_OFFLINE: $result = array( 'result' => FALSE, 'message' => t('Site offline.'), ); break; } } else { $extra = '