| 1 |
<?php
|
| 2 |
// $Id: views_filterblock.module,v 1.5 2007/08/11 22:31:56 douggreen Exp $
|
| 3 |
|
| 4 |
/** @file
|
| 5 |
* A simple module that uses javascript to reposition the views placeholder
|
| 6 |
* in a block
|
| 7 |
*/
|
| 8 |
|
| 9 |
include_once(drupal_get_path('module', 'views') .'/views_cache.inc');
|
| 10 |
|
| 11 |
/**
|
| 12 |
* Implementation of hook_menu()
|
| 13 |
*/
|
| 14 |
function views_filterblock_menu($may_cache) {
|
| 15 |
$items['admin/settings/views_filterblock'] = array(
|
| 16 |
'title' => t('Views Filter Block'),
|
| 17 |
'description' => t('Move the views filter form to a block.'),
|
| 18 |
'page callback' => 'drupal_get_form',
|
| 19 |
'page arguments' => 'views_filterblock_settings',
|
| 20 |
'access arguments' => array('administer site configuration'),
|
| 21 |
'type' => MENU_NORMAL_ITEM, // optional
|
| 22 |
);
|
| 23 |
return $items;
|
| 24 |
}
|
| 25 |
|
| 26 |
function views_filterblock_settings() {
|
| 27 |
$form['views_filterblock_num'] = array(
|
| 28 |
'#type' => 'select',
|
| 29 |
'#title' => t('Number of Blocks'),
|
| 30 |
'#options' => drupal_map_assoc(range(1, 10)),
|
| 31 |
'#default_value' => variable_get('views_filterblock_num', 1),
|
| 32 |
'#description' => t('Select the number of blocks'),
|
| 33 |
);
|
| 34 |
return system_settings_form($form);
|
| 35 |
}
|
| 36 |
|
| 37 |
/**
|
| 38 |
* Implementation of hook_block().
|
| 39 |
*/
|
| 40 |
function views_filterblock_block($op = 'list', $delta = 0, $edit = array() ) {
|
| 41 |
if ($op == 'list') {
|
| 42 |
for ($i = 0; $i < variable_get('views_filterblock_num', 1); $i ++) {
|
| 43 |
$blocks[$i]['info'] = t('Views Filter Block.!i', array('!i' => $i));
|
| 44 |
}
|
| 45 |
return $blocks;
|
| 46 |
}
|
| 47 |
elseif ($op == 'configure') {
|
| 48 |
$options[''] = '-- Select One --';
|
| 49 |
$result = db_query('SELECT * FROM {view_view} WHERE page=1');
|
| 50 |
while ($view = db_fetch_object($result)) {
|
| 51 |
$options[$view->name] = $view->description ? $view->description : $view->name;
|
| 52 |
$used[$view->name] = true;
|
| 53 |
}
|
| 54 |
$default_views = _views_get_default_views();
|
| 55 |
foreach ($default_views as $name => $view) {
|
| 56 |
if ($view->page && !$used[$view->name]) {
|
| 57 |
$options[$view->name] = $view->description;
|
| 58 |
}
|
| 59 |
}
|
| 60 |
asort($options);
|
| 61 |
|
| 62 |
$var = 'views_filterblock_view';
|
| 63 |
$form[$var] = array(
|
| 64 |
'#type' => 'select',
|
| 65 |
'#title' => t('View'),
|
| 66 |
'#options' => $options,
|
| 67 |
'#default_value' => variable_get("$var.$delta", ''),
|
| 68 |
'#description' => t('Select a View whose filter you want to display in the block'),
|
| 69 |
);
|
| 70 |
return $form;
|
| 71 |
}
|
| 72 |
elseif ($op == 'save') {
|
| 73 |
$var = 'views_filterblock_view';
|
| 74 |
variable_set("$var.$delta", $edit[$var]);
|
| 75 |
}
|
| 76 |
elseif ($op == 'view') {
|
| 77 |
$block['content'] = _views_filterblock_block($delta);
|
| 78 |
return $block;
|
| 79 |
}
|
| 80 |
}
|
| 81 |
|
| 82 |
function _views_filterblock_block($delta) {
|
| 83 |
// TODO: this needs to be rewritten for Drupal 5.0
|
| 84 |
if ($name = variable_get("views_filterblock_view.$delta", '')) {
|
| 85 |
if ($view = views_get_view($name)) {
|
| 86 |
return drupal_get_form('views_filterblock', $view);
|
| 87 |
}
|
| 88 |
}
|
| 89 |
}
|
| 90 |
|
| 91 |
function views_filterblock($view) {
|
| 92 |
$form = views_filters($view);
|
| 93 |
$form['#action'] = url($view->url);
|
| 94 |
$form['#views_filterblock'] = true;
|
| 95 |
return $form;
|
| 96 |
}
|
| 97 |
|
| 98 |
function views_filterblock_theme() {
|
| 99 |
return array(
|
| 100 |
'views_filterblock' => array(
|
| 101 |
'arguments' => array('form'),
|
| 102 |
),
|
| 103 |
'views_filterblock_output' => array(
|
| 104 |
'arguments' => array('form'),
|
| 105 |
),
|
| 106 |
);
|
| 107 |
}
|
| 108 |
|
| 109 |
function theme_views_filterblock($form) {
|
| 110 |
$view = $form['view']['#value'];
|
| 111 |
|
| 112 |
// make the 'q' come first
|
| 113 |
$output = drupal_render($form['q']);
|
| 114 |
|
| 115 |
foreach ($view->exposed_filter as $count => $filter) {
|
| 116 |
$newform["fieldset$count"] = array(
|
| 117 |
'#type' => 'fieldset',
|
| 118 |
'#title' => $filter['label'],
|
| 119 |
'#collapsible' => true,
|
| 120 |
'#weight' => $count - 1000, // we'll never have this many filters
|
| 121 |
);
|
| 122 |
$newform["fieldset$count"]['#collapsed'] = TRUE;
|
| 123 |
}
|
| 124 |
|
| 125 |
foreach ($form as $field => $value) {
|
| 126 |
if (preg_match('/(op|filter)([0-9]+)/', $field, $match)) {
|
| 127 |
$curcount = $match[2];
|
| 128 |
$newform["fieldset$curcount"][$field] = $value;
|
| 129 |
|
| 130 |
if (!isset($newform["fieldset$curcount"]['#weight'])) {
|
| 131 |
$newform["fieldset$curcount"]['#weight'] = $value['#weight'];
|
| 132 |
}
|
| 133 |
}
|
| 134 |
else {
|
| 135 |
if ($field == 'submit' || drupal_substr($field, 0, 1) == '#') {
|
| 136 |
unset($curcount);
|
| 137 |
}
|
| 138 |
if (isset($curcount)) {
|
| 139 |
$newform["fieldset$curcount"][$field] = $value;
|
| 140 |
}
|
| 141 |
else {
|
| 142 |
$newform[$field] = $value;
|
| 143 |
}
|
| 144 |
}
|
| 145 |
}
|
| 146 |
|
| 147 |
foreach ($view->exposed_filter as $count => $filter) {
|
| 148 |
if ($filter['is_default']) {
|
| 149 |
$newform["fieldset$count"]['#collapsed'] = FALSE;
|
| 150 |
$open = TRUE;
|
| 151 |
}
|
| 152 |
else {
|
| 153 |
$value = $newform["fieldset$count"]["filter$count"]['#default_value'];
|
| 154 |
if (isset($value)) {
|
| 155 |
if (is_array($value)) {
|
| 156 |
foreach ($value as $key => $item) {
|
| 157 |
if ($item != '') {
|
| 158 |
$newform["fieldset$count"]['#collapsed'] = FALSE;
|
| 159 |
$open = TRUE;
|
| 160 |
}
|
| 161 |
}
|
| 162 |
}
|
| 163 |
elseif ($value != '') {
|
| 164 |
$newform["fieldset$count"]['#collapsed'] = FALSE;
|
| 165 |
$open = TRUE;
|
| 166 |
}
|
| 167 |
}
|
| 168 |
}
|
| 169 |
}
|
| 170 |
if (!$open) {
|
| 171 |
$newform["fieldset0"]['#collapsed'] = FALSE;
|
| 172 |
}
|
| 173 |
|
| 174 |
return theme('views_filterblock_output', $newform);
|
| 175 |
}
|
| 176 |
|
| 177 |
function theme_views_filterblock_output($form) {
|
| 178 |
return drupal_render($form);
|
| 179 |
}
|
| 180 |
|
| 181 |
/**
|
| 182 |
* Implementation of hook_form_alter().
|
| 183 |
*/
|
| 184 |
function views_filterblock_form_alter(&$form, $form_state, $form_id) {
|
| 185 |
if ($form_id == 'views_filters') {
|
| 186 |
/**
|
| 187 |
* Hide the form displayed as part of the view, when the
|
| 188 |
* filterform is displayed in a block
|
| 189 |
*/
|
| 190 |
$name = $form['view']['#value']->name;
|
| 191 |
if (!$form['#views_filterblock']) {
|
| 192 |
foreach (array('content', 'left', 'right', 'header', 'footer') as $region) {
|
| 193 |
foreach (block_list($region) as $block) {
|
| 194 |
if ($block->module == 'views_filterblock' && variable_get("views_filterblock_view.$block->delta", '') == $name) {
|
| 195 |
$form['#type'] = 'hidden';
|
| 196 |
return;
|
| 197 |
}
|
| 198 |
}
|
| 199 |
}
|
| 200 |
}
|
| 201 |
}
|
| 202 |
}
|