/[drupal]/contributions/modules/block_refresh/block_refresh.module
ViewVC logotype

Contents of /contributions/modules/block_refresh/block_refresh.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.5 - (show annotations) (download) (as text)
Wed May 28 03:01:54 2008 UTC (18 months ago) by aaron
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-5, DRUPAL-6--1
Changes since 1.4: +48 -5 lines
File MIME type: text/x-php
grouping settings
1 <?php
2 // $Id: block_refresh.module,v 1.4 2008/02/28 21:29:47 aaron Exp $
3
4 define('BLOCK_REFRESH_AUTOREFRESH_DEFAULT_ENABLE', FALSE); // autorefresh disabled by default
5 define('BLOCK_REFRESH_AUTOREFRESH_DEFAULT_MANUAL', FALSE); // manual refresh disabled by default
6 define('BLOCK_REFRESH_AUTOREFRESH_DEFAULT_TIMER', 120); // default refreshes every two minutes, if enabled
7
8 /**
9 * Add a 'Block Refresh' settings fieldset to the block admin form
10 */
11 function block_refresh_form_alter($form_id, &$form) {
12 if ($form_id == 'block_admin_configure') {
13 $settings = variable_get('block_refresh_settings', array());
14 $form['#submit']['block_refresh_submit'] = array();
15 $form['block_refresh'] = array(
16 '#type' => 'fieldset',
17 '#title' => t('Block refresh settings'),
18 '#collapsible' => true,
19 '#weight' => -5
20 );
21 $form['block_refresh']['block_refresh_enable'] = array(
22 '#type' => 'checkbox',
23 '#title' => t('Enable block autorefresh'),
24 '#description' => t('If checked, then the content of this block may be autorefreshed automatically on a timer.'),
25 '#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_autorefresh_default_enable', BLOCK_REFRESH_AUTOREFRESH_DEFAULT_ENABLE),
26 );
27 $form['block_refresh']['block_refresh_manual'] = array(
28 '#type' => 'checkbox',
29 '#title' => t('Enable block manual refresh'),
30 '#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.'),
31 '#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_manual_refresh_default_enable', BLOCK_REFRESH_AUTOREFRESH_DEFAULT_MANUAL),
32 );
33 $form['block_refresh']['block_refresh_group'] = array(
34 '#type' => 'checkbox',
35 '#title' => t('Group with other refreshing blocks'),
36 '#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_group_auto_timer', BLOCK_REFRESH_AUTOREFRESH_DEFAULT_TIMER), '1 second', '@count seconds'))),
37 '#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', TRUE),
38 );
39 $form['block_refresh']['block_refresh_timer'] = array(
40 '#type' => 'textfield',
41 '#title' => t('Block refresh timer'),
42 '#description' => t('Assuming that Blox 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_group_auto_timer', BLOCK_REFRESH_AUTOREFRESH_DEFAULT_TIMER), '1 second', '@count seconds'))),
43 '#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_AUTOREFRESH_DEFAULT_TIMER),
44 );
45 }
46 }
47
48 /**
49 * handle submission of block refresh form on block settings page
50 */
51 function block_refresh_submit($form_id, $edit) {
52 $settings = variable_get('block_refresh_settings', array());
53 $settings['block-' . $edit['module'] . '-' . $edit['delta']]['enabled'] = $edit['block_refresh_enable'];
54 $settings['block-' . $edit['module'] . '-' . $edit['delta']]['manual'] = $edit['block_refresh_manual'];
55 $settings['block-' . $edit['module'] . '-' . $edit['delta']]['group'] = $edit['block_refresh_group'];
56 $settings['block-' . $edit['module'] . '-' . $edit['delta']]['timer'] = $edit['block_refresh_timer'];
57 $settings['block-' . $edit['module'] . '-' . $edit['delta']]['block'] = array('block' => $edit['module'], 'delta' => $edit['delta']);
58 variable_set('block_refresh_settings', $settings);
59 }
60
61 /**
62 * implements hook_menu
63 */
64 function block_refresh_menu($may_cache) {
65 $items = array();
66 if ($may_cache) {
67 $items[] = array(
68 'path' => 'admin/settings/block_refresh',
69 'title' => t('Block refresh'),
70 'description' => t('Settings for automatic and manual refreshing of configured blocks.'),
71 'callback' => 'drupal_get_form',
72 'callback arguments' => 'block_refresh_settings',
73 'access' => user_access('administer site configuration'),
74 );
75 // this will display the contents of a block, if it's enabled
76 $items[] = array(
77 'path' => 'block_refresh',
78 'type' => MENU_CALLBACK,
79 'access' => user_access('access block refresh content'),
80 'callback' => 'block_refresh_block_content',
81 'title' => t('Block refresh block content'),
82 );
83 }
84 return $items;
85 }
86
87 /**
88 * implements hook_perm -- add permission for accessing auto/manually refreshed block content
89 */
90 function block_refresh_perm() {
91 return array('access block refresh content');
92 }
93
94 /**
95 * callback for admin/settings/block_refresh
96 */
97 function block_refresh_settings() {
98 $form = array();
99
100 $form['settings'] = array(
101 '#type' => 'fieldset',
102 '#title' => t('Settings help'),
103 '#collapsible' => TRUE,
104 );
105 $form['settings']['configure'] = array(
106 '#type' => 'item',
107 '#value' => 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'))),
108 );
109 $form['settings']['settings'] = array(
110 '#type' => 'item',
111 '#value' => 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'))),
112 );
113
114 $form['global'] = array(
115 '#type' => 'fieldset',
116 '#title' => t('Global settings'),
117 '#collapsible' => TRUE,
118 );
119 $form['global']['block_refresh_group_auto'] = array(
120 '#type' => 'checkbox',
121 '#title' => t('Group block refreshes'),
122 '#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.'),
123 '#default_value' => variable_get('block_refresh_group_auto', TRUE),
124 );
125 $form['global']['block_refresh_group_auto_timer'] = array(
126 '#type' => 'textfield',
127 '#title' => t('Group block refresh timer'),
128 '#description' => t('Enter the time, in seconds, that grouped blocks will refresh, when grouped and set to automatic.'),
129 '#default_value' => variable_get('block_refresh_group_auto_timer', BLOCK_REFRESH_AUTOREFRESH_DEFAULT_TIMER),
130 );
131
132 return system_settings_form($form);
133 }
134
135 /**
136 * page callback for /block_refresh/[module]/[delta]
137 * displays the block content, without any other page information
138 */
139 function block_refresh_block_content($block = NULL, $delta = NULL) {
140 if (!isset($block) || !isset($delta) || ($block != 'block_refresh' && !module_implements($block, 'block'))) {
141 drupal_not_found();
142 }
143 if ($block == 'block_refresh' && $delta == 'all') {
144 $query = isset($_GET['blocks']) ? $_GET['blocks'] : '';;
145 $pairs = explode(',', $query);
146 foreach ($pairs as $pair) {
147 list($module, $delta) = explode('|', $pair);
148 $block = module_invoke($module, 'block', 'view', $delta);
149 $output .= '<div id="block-refresh-data-' . $module . '-' . $delta . '">' . $block['content'] . '</div>';
150 }
151 if ($output) {
152 print '<div id="block-refresh-data-all" class="block-refresh-hidden">' . $output . '</div>';
153 }
154 exit();
155 }
156 $settings = variable_get('block_refresh_settings', array());
157 if (!$settings['block-' . $block . '-' . $delta]['enabled'] && !$settings['block-' . $block . '-' . $delta]['manual']) {
158 drupal_not_found();
159 }
160 $block = module_invoke($block, 'block', 'view', $delta);
161 print $block['content'];
162 exit();
163 }
164
165 /**
166 * implements hook_footer
167 * calls the jquery to refresh blocks automatically, but only if the blocks exist on the current page and are enabled
168 */
169 function block_refresh_footer() {
170 // don't bother to continue if the user can't refresh content anyway...
171 if (!user_access('access block refresh content')) {
172 return;
173 }
174 global $theme_key;
175 $blocks = array();
176 $regions = system_region_list($theme_key);
177 foreach ($regions as $region => $value) {
178 $blocks = array_merge($blocks, block_list($region));
179 }
180 $settings = variable_get('block_refresh_settings', array());
181 foreach ($settings as $block) {
182 if (isset($blocks[$block['block']['block'] . '_' . $block['block']['delta']])) {
183 if ($block['enabled']) {
184 $js .= theme('block_refresh_js', $block);
185 }
186 if ($block['manual']) {
187 $js .= theme('block_manual_refresh_js', $block);
188 }
189 }
190 }
191 if ($js) {
192 if (!module_invoke('jq', 'add', 'block_refresh')) {
193 drupal_add_js(drupal_get_path('module', 'block_refresh') .'/js/block_refresh.js');
194 drupal_add_css(drupal_get_path('module', 'block_refresh') . '/css/block_refresh.css');
195 }
196 drupal_add_js(theme('block_refresh_js_wrapper', $js), 'inline');
197 }
198 }
199
200 /**
201 * call the js for an enabled block div.
202 * you might need to override $div if your blocks have different id's than the drupal standard.
203 * or you may wish to manually put this code in your block.tpl.php file for fine-tune control,
204 * although that would take some finagling to do properly.
205 */
206 function theme_block_refresh_js($block) {
207 // this is the block div css id and content class
208 $div = "#block-{$block['block']['block']}-{$block['block']['delta']} .content";
209
210 // we store seconds, but js expects milliseconds
211 $timer = $block['timer'] * 1000;
212
213 $base = base_path();
214 $js .= " _block_refresh_data['$div'] = new block_refresh_data({$timer}, '{$base}block_refresh/{$block['block']['block']}/{$block['block']['delta']}');\n";
215 $js .= " block_refresh_timer('$div');";
216 return $js;
217 }
218
219 /**
220 * Add a manual refresh button to the header...
221 */
222 function theme_block_manual_refresh_js($block) {
223 // this is the block div css id and content class
224 $div = "block-{$block['block']['block']}-{$block['block']['delta']}";
225
226 $base = base_path();
227 $url = "{$base}block_refresh/{$block['block']['block']}/{$block['block']['delta']}";
228 $content_url = "#$div .content";
229 $js .= " block_refresh_add_button('$div', '$url', '$content_url');";
230
231 return $js;
232 }
233
234 /**
235 * put the whole thing in a jquery ready call
236 */
237 function theme_block_refresh_js_wrapper($js) {
238 return "$(document).ready(function() {\n$js\n})";
239 }
240
241 /**
242 * Implements hook_jq
243 * Allows optional integration with the jQ module.
244 */
245 function block_refresh_jq($op, $plugin = NULL, $args = array(), $already_loaded = NULL) {
246 switch ($op) {
247 case 'info':
248 $info = array();
249 $info['block_refresh'] = array(
250 'name' => t('Block Refresh'),
251 '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.'),
252 'url' => 'http://drupal.org/project/block_refresh',
253 'version' => t('1.2'),
254 'files' => array(
255 'js' => array(
256 drupal_get_path('module', 'block_refresh') . '/js/block_refresh.js',
257 ),
258 'css' => array(
259 drupal_get_path('module', 'block_refresh') . '/css/block_refresh.css',
260 ),
261 ),
262 );
263 return $info;
264 }
265 }

  ViewVC Help
Powered by ViewVC 1.1.2