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

Contents of /contributions/modules/views_multiblock/views_multiblock.module

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


Revision 1.1 - (show annotations) (download) (as text)
Wed Jun 13 05:23:48 2007 UTC (2 years, 5 months ago) by morrissinger
Branch: MAIN
CVS Tags: DRUPAL-5--1-0, HEAD
Branch point for: DRUPAL-5
File MIME type: text/x-php
Initial commit of views_multiblock module. This module allows you to use one view to generate many different blocks.
1 <?php
2 // $Id$
3
4 /**
5 * @file
6 * Provides the ability to use one view in more than one block.
7 *
8 * Provides the ability to use one view in more than one block by
9 * allowing users to specify views arguments in the block configuration
10 * form.
11 */
12
13 /***********************************************************
14 * Drupal Hooks
15 */
16
17 /**
18 * Implementation of hook_menu().
19 */
20 function views_multiblock_menu($may_cache) {
21 $path = drupal_get_path('module', 'views_multiblock');
22 require_once("./$path/views_multiblock_views.inc");
23
24 drupal_add_css(drupal_get_path('module', 'views_multiblock') .'/views_multiblock.css');
25
26 $items = array();
27
28 if ($may_cache) {
29 $items[] = array('path' => 'views_multiblock/increment',
30 'access' => user_access('administer nodes'),
31 'callback' => 'views_multiblock_story_number',
32 'callback arguments' => array('direction' => 'increment'),
33 'type' => MENU_CALLBACK,
34 );
35 $items[] = array('path' => 'views_multiblock/decrement',
36 'access' => user_access('administer nodes'),
37 'callback' => 'views_multiblock_story_number',
38 'callback arguments' => array('direction' => 'decrement'),
39 'type' => MENU_CALLBACK,
40 );
41
42 $items[] = array(
43 'path' => 'admin/settings/views_multiblock',
44 'title' => t('Views_Multiblock'),
45 'description' => t('These settings control the configurable global options for views_multiblock.'),
46 'callback' => 'drupal_get_form',
47 'callback arguments' => array('views_multiblock_admin_settings'),
48 'access' => user_access('administer site configuration'),
49 'type' => MENU_NORMAL_ITEM, // optional
50 );
51
52 }
53
54 return $items;
55
56 }
57
58 /**
59 * Implementation of hook_block().
60 */
61 function views_multiblock_block($op = 'list', $delta = 0, $edit = array()) {
62 switch ($op) {
63 case 'list':
64 for($i = 0; $i < variable_get('views_multiblock_number', 5); $i++) {
65 $blocks[$i]['info'] = variable_get('views_multiblock_title_' . $i, t('Views Multiblock !d', array('!d' => $i))) . ' [vm]';
66 }
67 return $blocks;
68 case 'configure':
69 return _views_multiblock_configure_form($delta);
70 case 'save':
71 _views_multiblock_save_configuration($delta, $edit);
72 break;
73 case 'view':
74 //Figure out which view we are using, and load it.
75 $bypass = variable_get('views_multiblock_view_' . $delta, 0);
76
77 if($bypass != '0') {
78 $view_name = $bypass;
79 } else {
80 $view_name = variable_get('views_multiblock_view', 'views_multiblock');
81 }
82 $view = views_get_view($view_name);
83
84 //Use the view arguments to create a list of arguments to pass to the view
85 $view_arguments = $view->argument;
86 $view_argument_values = views_argument_api_get_processed('views_multiblock_' . $delta, NULL, $view_name);
87 $arguments = views_argument_api_get_processed('views_multiblock_' . $delta, NULL, $view_name, TRUE);
88
89 $GLOBALS['views_multiblock_current_arguments'] = $view_argument_values;
90
91 $syndicate = FALSE;
92 foreach($view_arguments as $argument) {
93 if($argument['type'] == 'node_feed') {
94 $syndicate = TRUE;
95 }
96 }
97
98 if (empty($view)) {
99 return;
100 }
101
102 $number_of_stories = variable_get('views_multiblock_items_' . $delta, 3);
103
104 //Pass the block delta to the view, for anything that wants it.
105 $view->block_delta = $delta;
106
107 //Get the queries used to make the view
108 $path = drupal_get_path('module', 'views');
109 require_once("./$path/views_query.inc");
110 $view_queries = _views_build_query($view, $arguments, array());
111
112 if(empty($view_queries['countquery'])) {
113 return;
114 }
115
116 $count = db_result(call_user_func_array('db_query', array_merge(array($view_queries['countquery']), $view_queries['args'])));
117
118 if($count == 0) {
119 return;
120 }
121
122 //Use the block view so that we have control over the number of stories outputted without using the pager.
123 $view_output = views_build_view('block', $view, $arguments, FALSE, $number_of_stories);
124
125 if(empty($view_output)) {
126 return;
127 }
128
129 //$view_output = theme_view($view_name, $number_of_stories, FALSE, 'block', $arguments);
130 unset($GLOBALS['views_multiblock_current_arguments']);
131
132 //Increment/Decrement Buttons
133 if(user_access('administer nodes')) {
134 $buttons = theme('views_multiblock_buttons', $delta, $count);
135 }
136
137 //Headers
138 $header = variable_get('views_multiblock_header_' . $delta, NULL);
139 if(!empty($header)) {
140 $header = theme('views_multiblock_header', $header);
141 }
142
143 //Feed Icon
144 $feed_icon = NULL;
145 if($syndicate) {
146 $url = views_get_url($view, $arguments) . '/feed';
147 //$feed_icon = theme_feed_icon($url);
148 $feed_icons = drupal_add_feed($url, views_get_title($view));
149 $feed_icon = $feed_icons[$url];
150 }
151
152 $block['content'] = $buttons . $header . $view_output . $feed_icon;
153
154
155 if($count > 0) {
156 return $block;
157 } else {
158 return;
159 }
160
161 }
162 }
163
164 function views_multiblock_views_pre_view(&$view, $items) {
165 $last_update = 0;
166 foreach($items as $item) {
167 if(isset($item->node_changed_changed) && $item->node_changed_changed > $last_update) {
168 $last_update = $item->node_changed_changed;
169 }
170 }
171 if($last_update > 0) {
172 return theme('views_multiblock_last_update', $view, $last_update);
173 }
174
175 }
176
177 /***********************************************************
178 * Menu Callbacks
179 */
180
181 /**
182 * Admin settings callback().
183 */
184 function views_multiblock_admin_settings() {
185 //Default number of views_multiblock
186 $form['views_multiblock_number'] = array('#title' => t('Number of blocks'),
187 '#description' => t('Select the number of blocks to use on this site'),
188 '#type' => 'select',
189 '#options' => array(0, 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40),
190 '#default_value' => variable_get('views_multiblock_number', 5),
191 );
192
193 //Get all views
194 $views = _views_multiblock_get_views();
195
196 //Select a view
197 $form['views_multiblock_view'] = array(
198 '#title' => t('View'),
199 '#type' => 'select',
200 '#description' => t('Select which view to use with views_multiblock.'),
201 '#options' => $views,
202 '#default_value' => variable_get('views_multiblock_view', 'views_multiblock'),
203 );
204
205 return system_settings_form($form);
206 }
207
208 /**
209 * Menu callback to change the nodes per block for a specific block.
210 *
211 * @param $direction
212 * Either 'increment' or 'decrement'.
213 * @param $delta
214 * A block delta.
215 */
216 function views_multiblock_story_number($direction, $delta) {
217 $current_setting = variable_get('views_multiblock_items_' . arg(2), FALSE);
218 if ($current_setting !== FALSE) {
219 if ($direction == 'increment') {
220 variable_set('views_multiblock_items_' . arg(2), ++$current_setting);
221 }
222 else {
223 variable_set('views_multiblock_items_' . arg(2), --$current_setting);
224 }
225 }
226 drupal_goto(referer_uri());
227 }
228
229
230 /***********************************************************
231 * Theme Functions
232 */
233
234 /**
235 * Themes a set of buttons to change nodes_per_block.
236 *
237 * @param $delta
238 * A block delta.
239 * @param $count
240 * The result of a views countquery for the specific block delta's view.
241 * @return
242 * A themed set of buttons.
243 */
244 function theme_views_multiblock_buttons($delta, $count) {
245 $stories_per_block = variable_get('views_multiblock_items_' . $delta, 0);
246
247 $path = drupal_get_path('module', 'views_multiblock');
248 global $base_url;
249
250 if($stories_per_block < $count) {
251 $plus = "$base_url/$path/plus.gif";
252 $plus = l('<img src="' . $plus . '" />', 'views_multiblock/increment/' . $delta, array(), NULL, NULL, FALSE, TRUE);
253 }
254
255 if($stories_per_block > 1) {
256 $minus = "$base_url/$path/minus.gif";
257 $minus = l('<img src="' . $minus . '" />', 'views_multiblock/decrement/' . $delta, array(), NULL, NULL, FALSE, TRUE);
258 }
259
260 $buttons = '<div style="float: right; clear: both; position: relative; right: -10px; padding: 5px; border: 1px solid #ddd; background-color: #eee; margin: 5px;">' . $stories_per_block . '/' . $count . ' ' . $minus . $plus . '</div>';
261
262 return $buttons;
263 }
264
265 /**
266 * Themes a views_multiblock block header.
267 *
268 * @param $header
269 * The header text.
270 * @return
271 * A themed block header.
272 */
273 function theme_views_multiblock_header($header) {
274 return '<div class="views_multiblock_block_header">' . $header . "</div>\n";
275 }
276
277 /**
278 * Themes a last update time in a block.
279 *
280 * @param $view
281 * A view.
282 * @param $last_update
283 * A timestamp that reflects the last update time.
284 * @return
285 * A themed last update time.
286 */
287 function theme_views_multiblock_last_update($view, $last_update) {
288 $delta = $view->block_delta;
289 $handler = variable_get('views_multiblock_update_time_' . $delta, 'none');
290 if($handler != 'none') {
291 return '<div class="views_multiblock_last_update">' . t('Last updated !d', array('!d' => format_date($last_update, $handler))) . "</div>\n";
292 }
293 }
294
295 /***********************************************************
296 * Helper Functions
297 */
298
299 /**
300 * Returns a block configuration form.
301 *
302 * The block configuration form includes any form elements that
303 * have been created by modules that implement the
304 * hook_views_multiblock_block hook.
305 *
306 * @param $delta
307 * A block delta.
308 * @return
309 * A form object for use on the block configuration page.
310 */
311 function _views_multiblock_configure_form($delta) {
312 if(check_plain($_POST['op']) == t('Update')) {
313 variable_set('views_multiblock_view_' . $delta, $form['bypass']['view_bypass']);
314 }
315
316 $form['#multistep'] = TRUE;
317 $form['#redirect'] = FALSE;
318 $form['#base'] = 'block_admin_configure';
319
320 //Figure out which view we are using, and load it.
321 $bypass = variable_get('views_multiblock_view_' . $delta, 0);
322
323 if($bypass === 0) {
324 $view_name = variable_get('views_multiblock_views', 'views_multiblock');
325 } else {
326 $view_name = $bypass;
327 }
328 $view = views_get_view($view_name);
329
330 $date_handler_options = array(
331 'none' => t('None'),
332 'small' => t('Small'),
333 'medium' => t('Medium'),
334 'large' => t('Large'),
335 );
336
337 $form['title'] = array(
338 '#type' => 'textfield',
339 '#title' => 'Title',
340 '#default_value' => variable_get('views_multiblock_title_' . $delta, NULL),
341 '#weight' => -20
342 );
343
344 $form['items'] = array(
345 '#type' => 'select',
346 '#title' => t('Number of items to display'),
347 '#default_value' => variable_get('views_multiblock_items_' . $delta, 2) - 1,
348 '#options' => array('1', '2', '3', '4', '5', '6', '7', '8', '9', '10'),
349 );
350 $form['update_time'] = array(
351 '#type' => 'select',
352 '#title' => t('Update time'),
353 '#description' => t('Select a method for showing the last-updated time of the block.'),
354 '#default_value' => variable_get('views_multiblock_update_time_' . $delta, NULL),
355 '#options' => $date_handler_options,
356 );
357 $form['header'] = array(
358 '#type' => 'textarea',
359 '#title' => 'Header',
360 '#default_value' => variable_get('views_multiblock_header_' . $delta, ''),
361 '#description' => t('Text for the header of the block.'),
362 );
363 $form['bypass'] = array(
364 '#type' => 'fieldset',
365 '#title' => t('View'),
366 '#collapsible' => TRUE,
367 '#collapsed' => FALSE,
368 );
369 $form['bypass']['view_bypass'] = array(
370 '#type' => 'select',
371 '#title' => 'View',
372 '#default_value' => variable_get('views_multiblock_view_' . $delta, -1),
373 '#description' => t('Select a view to use if you wish to bypass the default.'),
374 '#options' => array_merge(array('0' => t('--Default--')), _views_multiblock_get_views()),
375 );
376 $form['bypass']['redo_arg_form'] = array(
377 '#type' => 'button',
378 '#value' => t('Update'),
379 );
380
381 //Use the view arguments to create a list of configurable options for the block
382 $view_arguments = $view->argument;
383 $form['arguments'] = array(
384 '#title' => t('View arguments'),
385 '#type' => 'fieldset',
386 '#collapsible' => TRUE,
387 '#collapsed' => FALSE,
388 );
389
390 $views_argument_api_form = views_argument_api_build_argument_form($view_name);
391 $views_argument_api_default_values = views_argument_api_get('views_multiblock_' . $delta, NULL);
392
393 foreach($views_argument_api_form as $key => $element) {
394 $form['arguments'][$key] = $element;
395 $form['arguments'][$key]['#default_value'] = $views_argument_api_default_values[$key];
396 }
397
398 return $form;
399 }
400
401 /**
402 * Saves the block configuration for a specific delta.
403 *
404 * The block configuration form includes any form elements that
405 * have been created by modules that implement the
406 * hook_views_multiblock_block hook. These get saved by an additional
407 * call to that same hook.
408 *
409 * @param $delta
410 * A block delta.
411 * @param $edit
412 * An array, passed to hook_block as the $edit argument.
413 */
414 function _views_multiblock_save_configuration($delta, $edit = array()) {
415 variable_set('views_multiblock_title_' . $delta, $edit['title']);
416 variable_set('views_multiblock_items_' . $delta, $edit['items'] + 1);
417 variable_set('views_multiblock_header_' . $delta, $edit['header']);
418 variable_set('views_multiblock_view_' . $delta, $edit['view_bypass']);
419 variable_set('views_multiblock_update_time_' . $delta, $edit['update_time']);
420
421
422 //Figure out which view we are using, and load it.
423 $bypass = variable_get('views_multiblock_view_' . $delta, 0);
424
425 if($bypass === 0) {
426 $view_name = variable_get('views_multiblock_views', 'views_multiblock');
427 } else {
428 $view_name = $bypass;
429 }
430
431 //Save module-specific argument settings
432 views_argument_api_save_arguments('views_multiblock_' . $delta, $edit, $view_name);
433 }
434
435 /**
436 * Returns a list of views, including default views.
437 * @return
438 * An array of view names, keyed by view name.
439 */
440 function _views_multiblock_get_views() {
441 $views = array();
442 $qs = db_query("SELECT v.vid, v.name FROM {view_view} v");
443 while($obj = db_fetch_object($qs)) {
444 $views[$obj->name] = $obj->name;
445 }
446 $default_views = module_invoke_all('views_default_views');
447 foreach($default_views as $default_view) {
448 $views[$default_view->name] = $default_view->name;
449 }
450
451 return $views;
452 }

  ViewVC Help
Powered by ViewVC 1.1.2