| 1 |
<?php
|
| 2 |
// $Id: node_block.module,v 1.7 2006/10/26 20:55:10 suuch Exp $
|
| 3 |
|
| 4 |
function node_block_help($section) {
|
| 5 |
switch ($section) {
|
| 6 |
case 'admin/help#node_block':
|
| 7 |
return t('<p>Create any number of custom blocks and automatically<br>
|
| 8 |
associate them for display on a particular (node) page view. </p>
|
| 9 |
<p>Useful for granting non-admin users the ability to insert<br>
|
| 10 |
custom blocks.</p>
|
| 11 |
<p>Saves the trouble of going to configure the block manually<br>
|
| 12 |
in admin/block</p>
|
| 13 |
<p>Block display region is set in admin/settings/node_block.</p>
|
| 14 |
<p>On deleting the block association, the block itself is deleted from<br>
|
| 15 |
|
| 16 |
the block table.</p>
|
| 17 |
<p>No database tables are created by this module.</p>');
|
| 18 |
|
| 19 |
case 'admin/modules#description':
|
| 20 |
return t('Create any number of custom blocks and automatically
|
| 21 |
associate them for display on a particular (node) page view.');
|
| 22 |
|
| 23 |
}
|
| 24 |
}
|
| 25 |
|
| 26 |
/**
|
| 27 |
* hook_menu: Add a tab for easily placing blocks on a node
|
| 28 |
*/
|
| 29 |
|
| 30 |
function node_block_menu($may_cache)
|
| 31 |
{
|
| 32 |
if (!$may_cache) {
|
| 33 |
if (arg(0) == 'node' && is_numeric(arg(1)) && user_access(t('create info boxes'))) {
|
| 34 |
$node = node_load(array('nid' => arg(1)));
|
| 35 |
if ($node->nid && $node->type != 'image') {
|
| 36 |
$node_block_show_tab = variable_get('node_block_node_types', array());
|
| 37 |
if ((in_array($node->type, $node_block_show_tab))) {
|
| 38 |
$items[] = array(
|
| 39 |
'path' => 'node/' . arg(1) . '/blocks',
|
| 40 |
'title' => t('info boxes'),
|
| 41 |
'callback' => '_add_info_boxes',
|
| 42 |
'access' => user_access(t('create info boxes')),
|
| 43 |
'type' => MENU_LOCAL_TASK,
|
| 44 |
'weight' => 2,
|
| 45 |
);
|
| 46 |
}
|
| 47 |
}
|
| 48 |
}
|
| 49 |
}
|
| 50 |
|
| 51 |
return $items;
|
| 52 |
}
|
| 53 |
|
| 54 |
/**
|
| 55 |
* hook_perm: Allow node_block creation
|
| 56 |
* 'create info boxes' shows you the info box form without the admin options
|
| 57 |
* 'administer info boxes' shows you the extra administrative options
|
| 58 |
*/
|
| 59 |
|
| 60 |
function node_block_perm()
|
| 61 |
{
|
| 62 |
return array(t('create info boxes'), t('administer info boxes'));
|
| 63 |
}
|
| 64 |
|
| 65 |
|
| 66 |
/**
|
| 67 |
* hook_form_alter: Add option to include info boxes on this page
|
| 68 |
|
| 69 |
function node_block_form_alter($form_id, &$form)
|
| 70 |
{
|
| 71 |
$node_block_show_tab = variable_get('node_block_node_types', array());
|
| 72 |
$nodetype = substr($form_id, 0, strlen($form_id)-10 );
|
| 73 |
|
| 74 |
if(is_array($node_block_show_tab))
|
| 75 |
{
|
| 76 |
foreach($node_block_show_tab as $type=>$value)
|
| 77 |
{
|
| 78 |
if(strcmp($nodetype,$value)==0) //exactly identical
|
| 79 |
{
|
| 80 |
print_r($form);
|
| 81 |
//print $type;
|
| 82 |
}
|
| 83 |
}
|
| 84 |
}
|
| 85 |
|
| 86 |
if (array_search($nodetype, $node_block_show_tab)==$nodetype)
|
| 87 |
{
|
| 88 |
print_r($nodetype);
|
| 89 |
}
|
| 90 |
|
| 91 |
}
|
| 92 |
*/
|
| 93 |
/*
|
| 94 |
* create info boxes with drupal's built in block functions
|
| 95 |
*/
|
| 96 |
|
| 97 |
function _add_info_boxes()
|
| 98 |
{
|
| 99 |
$title = db_result(db_query("SELECT title FROM {node} WHERE nid=%d", arg(1)));
|
| 100 |
drupal_set_title($title. ' ('.t('info boxes').')');
|
| 101 |
$edit = $_POST['edit'];
|
| 102 |
$op = $_POST['op'];
|
| 103 |
$edit['visibility'] = 0; //default
|
| 104 |
$region = variable_get('node_block_region', 'right');
|
| 105 |
$path = drupal_get_path_alias('node/'.arg(1));
|
| 106 |
$delta = 0;
|
| 107 |
if($op==t('Save block')) //this is called before
|
| 108 |
{//update the block table
|
| 109 |
|
| 110 |
$delta = db_result(db_query("SELECT bid FROM {boxes} WHERE info = '%s'", $edit['info']));
|
| 111 |
db_query("UPDATE {blocks} SET visibility = %d, pages = '%s', custom = %d, region='%s' WHERE module = '%s' AND delta = '%s'",
|
| 112 |
$edit['visibility'], $path, 0, $region, 'block', $delta);
|
| 113 |
}
|
| 114 |
if(!is_numeric(arg(4)))
|
| 115 |
{
|
| 116 |
$output.= my_block_box_add();
|
| 117 |
}
|
| 118 |
else
|
| 119 |
{
|
| 120 |
$output.=l(t('Add new info box'), 'node/'.arg(1).'/blocks');
|
| 121 |
}
|
| 122 |
$output.= _associated_blocks($path);
|
| 123 |
return $output;
|
| 124 |
|
| 125 |
}
|
| 126 |
|
| 127 |
/**
|
| 128 |
* change the default form_id that Drupal's block_box_add sets
|
| 129 |
*/
|
| 130 |
function my_block_box_add()
|
| 131 |
{
|
| 132 |
$form['addblock'] = array(
|
| 133 |
'#type'=>'fieldset',
|
| 134 |
'#title'=>t('Add new info box'),
|
| 135 |
'#collapsible'=>TRUE,
|
| 136 |
'#collapsed'=>FALSE,
|
| 137 |
|
| 138 |
);
|
| 139 |
$form['addblock'] = array_merge(block_box_form(),$form['addblock']);
|
| 140 |
|
| 141 |
// Highlight regions on page to provide visual reference.
|
| 142 |
$block_regions = system_region_list(variable_get('theme_default', 'bluemarine'));
|
| 143 |
$form['addblock']['region'] = array
|
| 144 |
(
|
| 145 |
'#type'=> 'select',
|
| 146 |
'#title'=> t('Info Box Region'),
|
| 147 |
'#description'=>t('Select region for placing info boxes. The default region is currently highlighted.'),
|
| 148 |
'#options'=> $block_regions,
|
| 149 |
'#default_value'=> variable_get('node_block_default_region', 'right'),
|
| 150 |
|
| 151 |
);
|
| 152 |
$form['addblock']['submit'] = array('#type' => 'submit', '#value' => t('Save info box'));
|
| 153 |
|
| 154 |
return drupal_get_form('my_block_box_add', $form);
|
| 155 |
}
|
| 156 |
|
| 157 |
function my_block_box_add_submit($form_id, $form_values)
|
| 158 |
{
|
| 159 |
$path = drupal_get_path_alias('node/'.arg(1));
|
| 160 |
$region = $form_values['region'];
|
| 161 |
$form_values['visibility'] = 1; //show only on this page
|
| 162 |
|
| 163 |
if (!form_get_errors()) {
|
| 164 |
if (block_box_save($form_values)) {
|
| 165 |
drupal_set_message(t('The block has been created.'));
|
| 166 |
_block_rehash(); //update the blocks db table
|
| 167 |
//enable the block on this page
|
| 168 |
$delta = db_result(db_query("SELECT bid FROM {boxes} WHERE info = '%s'", $form_values['info']));
|
| 169 |
db_query("UPDATE {blocks} SET visibility = %d, pages = '%s', custom = %d, region='%s', status=%d WHERE module = '%s' AND delta = '%s'",
|
| 170 |
$form_values['visibility'], $path, 0, $region, 1, 'block', $delta);
|
| 171 |
return $path;
|
| 172 |
}
|
| 173 |
}
|
| 174 |
}
|
| 175 |
|
| 176 |
function my_block_box_add_validate($form_id, $form_values) {
|
| 177 |
if (empty($form_values['info']) || db_num_rows(db_query("SELECT info FROM {boxes} WHERE info = '%s'", $form_values['info']))) {
|
| 178 |
form_set_error('info', t('Please ensure that each block description is unique.'));
|
| 179 |
}
|
| 180 |
}
|
| 181 |
|
| 182 |
function node_block_settings()
|
| 183 |
{
|
| 184 |
$form['default'] = array
|
| 185 |
(
|
| 186 |
'#type'=>'fieldset',
|
| 187 |
'#title'=>t('Info Box Default Settings')
|
| 188 |
);
|
| 189 |
|
| 190 |
|
| 191 |
$form['default']['node_block_default_region'] = array
|
| 192 |
(
|
| 193 |
'#type'=> 'select',
|
| 194 |
'#title'=> t('Default Region'),
|
| 195 |
'#description'=>t('Default region for placing info boxes'),
|
| 196 |
'#options'=> system_region_list(variable_get('theme_default', 'bluemarine')),
|
| 197 |
'#default_value'=> variable_get('node_block_default_region', 'right'),
|
| 198 |
|
| 199 |
);
|
| 200 |
$form['node_block_nodes'] = array(
|
| 201 |
'#type' => 'fieldset',
|
| 202 |
'#title' => t('Content Type Info boxes Association'),
|
| 203 |
'#description' => t('.'),
|
| 204 |
'#collapsible' => TRUE,
|
| 205 |
'#collapsed' => TRUE
|
| 206 |
);
|
| 207 |
|
| 208 |
$form['node_block_nodes']['node_block_node_types'] = array(
|
| 209 |
'#type' => 'checkboxes',
|
| 210 |
'#title' => t('Display Info Boxes with Content Types'),
|
| 211 |
'#default_value' => variable_get('node_block_node_types', array()),
|
| 212 |
'#options' => node_get_types(),
|
| 213 |
'#description' => t('When a node content type has been checked, it will display associated info boxes.'),
|
| 214 |
'#required' => TRUE,
|
| 215 |
|
| 216 |
);
|
| 217 |
|
| 218 |
return $form;
|
| 219 |
}
|
| 220 |
|
| 221 |
/**
|
| 222 |
* Return the blocks associated with only this path
|
| 223 |
*/
|
| 224 |
function _associated_blocks($path)
|
| 225 |
{
|
| 226 |
$blocks = array();
|
| 227 |
$theme_key = variable_get('theme_default', 'bluemarine');
|
| 228 |
$throttle = module_exist('throttle');
|
| 229 |
$block_regions = system_region_list($theme_key);
|
| 230 |
$res = db_query("SELECT * FROM {blocks} INNER JOIN {boxes} ON bid=delta WHERE pages='%s' AND module='block'", $path);
|
| 231 |
while($row=db_fetch_object($res))
|
| 232 |
{
|
| 233 |
$old_blocks[$row->module][$row->delta] = $row;
|
| 234 |
$module = $row->module;
|
| 235 |
$delta = $row->delta;
|
| 236 |
if ($old_blocks[$row->module][$row->delta])
|
| 237 |
{
|
| 238 |
$block['delta'] = $delta;
|
| 239 |
$block['module'] = $old_blocks[$module][$delta]->module;
|
| 240 |
$block['info'] = $old_blocks[$module][$delta]->info;
|
| 241 |
$block['status'] = $old_blocks[$module][$delta]->status;
|
| 242 |
$block['weight'] = $old_blocks[$module][$delta]->weight;
|
| 243 |
$block['region'] = $old_blocks[$module][$delta]->region;
|
| 244 |
$block['visibility'] = $old_blocks[$module][$delta]->visibility;
|
| 245 |
$block['pages'] = $old_blocks[$module][$delta]->pages;
|
| 246 |
$block['custom'] = $old_blocks[$module][$delta]->custom;
|
| 247 |
$block['throttle'] = $old_blocks[$module][$delta]->throttle;
|
| 248 |
}
|
| 249 |
$blocks[$delta] = $block;
|
| 250 |
}
|
| 251 |
|
| 252 |
|
| 253 |
// Build form tree
|
| 254 |
//$form['#action'] = url($path);
|
| 255 |
$form['#tree'] = TRUE;
|
| 256 |
foreach ($blocks as $i => $block) {
|
| 257 |
$form[$i]['module'] = array('#type' => 'value', '#value' => $block['module']);
|
| 258 |
$form[$i]['delta'] = array('#type' => 'value', '#value' => $block['delta']);
|
| 259 |
$form[$i]['info'] = array('#value' => $block['info']);
|
| 260 |
$form[$i]['status'] = array('#type' => 'checkbox', '#default_value' => $block['status']);
|
| 261 |
$form[$i]['theme'] = array('#type' => 'hidden', '#value' => $theme_key);
|
| 262 |
$form[$i]['weight'] = array('#type' => 'weight', '#default_value' => $block['weight']);
|
| 263 |
$form[$i]['region'] = array('#type' => 'select',
|
| 264 |
'#default_value' => isset($block['region']) ? $block['region'] : system_default_region($theme_key),
|
| 265 |
'#options' => $block_regions,
|
| 266 |
);
|
| 267 |
|
| 268 |
if ($block['module'] == 'block') { //only those manually added
|
| 269 |
$form[$i]['configure'] = array('#value' => l(t('modify'), 'node/'.arg(1).'/blocks/configure/'. $block['delta']));
|
| 270 |
$form[$i]['delete'] = array('#value' => l(t('delete'), 'node/'.arg(1).'/blocks/delete/'. $block['delta']));
|
| 271 |
}
|
| 272 |
}
|
| 273 |
$form['submit'] = array('#type' => 'submit', '#value' => t('Save blocks'));
|
| 274 |
|
| 275 |
//$theform['node_block_admin_display'] = array('#type'=>'fieldset', '#title'=>t('info boxes'));
|
| 276 |
|
| 277 |
$htmlform = drupal_get_form('my_block_admin', $form, 'block_admin_display');
|
| 278 |
|
| 279 |
//block editing or deletion
|
| 280 |
$action = arg(3);
|
| 281 |
$bid = arg(4);
|
| 282 |
if($bid)
|
| 283 |
{
|
| 284 |
$configform['bid'] = array('#type'=>'value', '#value'=>$bid);
|
| 285 |
$configform['nid'] = array('#type'=>'value', '#value'=>arg(1));
|
| 286 |
switch($action)
|
| 287 |
{
|
| 288 |
case 'configure':
|
| 289 |
if ($settings = module_invoke($module, 'block', 'configure', $bid)) {
|
| 290 |
$configform['block_settings'] = array(
|
| 291 |
'#type' => 'fieldset',
|
| 292 |
'#title' => t('Modify info box'),
|
| 293 |
'#collapsible' => true,
|
| 294 |
);
|
| 295 |
|
| 296 |
foreach ($settings as $k => $v) {
|
| 297 |
$configform['block_settings'][$k] = $v;
|
| 298 |
}
|
| 299 |
$configform['block_settings']['submit'] = array(
|
| 300 |
'#type' => 'submit',
|
| 301 |
'#value' => t('Save info box'),
|
| 302 |
);
|
| 303 |
}
|
| 304 |
$configformhtml = drupal_get_form('_my_block_config', $configform);
|
| 305 |
break;
|
| 306 |
case 'delete':
|
| 307 |
$box = block_box_get($bid);
|
| 308 |
$message = t('Are you sure you want to delete the info box %name?', array('%name' => theme('placeholder', $box['info'])));
|
| 309 |
$deleteform['block_settings'] = array(
|
| 310 |
'#type' => 'fieldset',
|
| 311 |
'#title' => t('Delete info box'),
|
| 312 |
'#collapsible' => true,
|
| 313 |
'#description'=> $message.confirm_form('_my_block_delete', $configform, $message, $path.'/blocks', '', t('Delete'), t('Cancel')),
|
| 314 |
);
|
| 315 |
|
| 316 |
$configformhtml = form_render($deleteform);
|
| 317 |
break;
|
| 318 |
}
|
| 319 |
}
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
$form['associatedblocks'] = array(
|
| 324 |
'#type'=>'fieldset',
|
| 325 |
'#title'=>t('Associated info boxes'),
|
| 326 |
'#collapsible'=>TRUE,
|
| 327 |
// '#collapsed'=>TRUE,
|
| 328 |
'#description'=>$htmlform.$configformhtml,
|
| 329 |
);
|
| 330 |
return form_render($form['associatedblocks']) ;
|
| 331 |
}
|
| 332 |
|
| 333 |
/**
|
| 334 |
* Process form generated by _associated_blocks()
|
| 335 |
*/
|
| 336 |
function my_block_admin_submit($form_id, $form_values)
|
| 337 |
{
|
| 338 |
foreach ($form_values as $block) {
|
| 339 |
db_query("UPDATE {blocks} SET status = %d, weight = %d, region = '%s', throttle = %d WHERE module = '%s' AND delta = '%s' AND theme = '%s'", $block['status'], $block['weight'], $block['region'], $block['throttle'], $block['module'], $block['delta'], $block['theme']);
|
| 340 |
}
|
| 341 |
drupal_set_message(t('The info box settings have been updated.'));
|
| 342 |
cache_clear_all();
|
| 343 |
}
|
| 344 |
|
| 345 |
function _my_block_config_submit($form_id, $form_values)
|
| 346 |
{
|
| 347 |
db_query("UPDATE {boxes} SET title = '%s', body = '%s', info = '%s', format = %d WHERE bid = %d", $form_values['title'], $form_values['body'], $form_values['info'], $form_values['format'], $form_values['bid']);
|
| 348 |
drupal_set_message(t('The info box settings have been updated.'));
|
| 349 |
cache_clear_all();
|
| 350 |
return 'node/'.$form_values['nid'].'';
|
| 351 |
}
|
| 352 |
|
| 353 |
function _my_block_delete_submit($form_id, $form_values)
|
| 354 |
{
|
| 355 |
db_query('DELETE FROM {boxes} WHERE bid = %d', $form_values['bid']);
|
| 356 |
drupal_set_message(t('The info box %name has been removed.', array('%name' => theme('placeholder', $form_values['info']))));
|
| 357 |
_block_rehash();
|
| 358 |
cache_clear_all();
|
| 359 |
return 'node/'.$form_values['nid'].'';
|
| 360 |
}
|
| 361 |
|
| 362 |
|
| 363 |
/**
|
| 364 |
* hook_form_alter
|
| 365 |
* Change the info box addition form to set certain fields by default
|
| 366 |
* for users without 'administer info boxes' permissions
|
| 367 |
*/
|
| 368 |
|
| 369 |
function node_block_form_alter($form_id, &$form)
|
| 370 |
{
|
| 371 |
}
|
| 372 |
?>
|