| 1 |
<?php
|
| 2 |
// $Id: block_assign.module,v 1.1 2008/02/04 17:05:11 dllh Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
*/
|
| 7 |
|
| 8 |
/**
|
| 9 |
* Implementation of hook_help().
|
| 10 |
*/
|
| 11 |
/**
|
| 12 |
* Display help and module information
|
| 13 |
* @param section which section of the site we're displaying help
|
| 14 |
* @return help text for section
|
| 15 |
*/
|
| 16 |
function block_assign_help($section='') {
|
| 17 |
|
| 18 |
$output = '';
|
| 19 |
|
| 20 |
switch ($section = '') {
|
| 21 |
case "admin/help#block_assign":
|
| 22 |
$output = '<p>'. t("") .'</p>';
|
| 23 |
break;
|
| 24 |
case "admin/modules/#description":
|
| 25 |
$output = '<p>'. t('block_assign help') .'</p>';
|
| 26 |
break;
|
| 27 |
}
|
| 28 |
|
| 29 |
return $output;
|
| 30 |
}
|
| 31 |
|
| 32 |
function block_assign_block_admin_submit($form_id, $form_values) {
|
| 33 |
if (function_exists('block_assign_show')) {
|
| 34 |
$sql = 'UPDATE blocks SET visibility = 2, pages = CONCAT("<", "?", "php ", "if (function_exists(\'block_assign_show\')) { return block_assign_show(\'", module, "\',\'", delta, "\'); }", "?", ">") WHERE status=1 AND theme=\'%s\'';
|
| 35 |
db_query($sql, variable_get('theme_default', 'garland'));
|
| 36 |
}
|
| 37 |
}
|
| 38 |
|
| 39 |
function block_assign_form_alter($form_id, &$form) {
|
| 40 |
if ($form_id == 'block_admin_display') {
|
| 41 |
$form['#submit']['block_assign_block_admin_submit'] = array();
|
| 42 |
}
|
| 43 |
elseif ($form_id == 'block_admin_configure') {
|
| 44 |
$module = arg(4);
|
| 45 |
$delta = arg(5);
|
| 46 |
$form['page_vis_settings']['visibility']['#disabled'] = TRUE;
|
| 47 |
$form['page_vis_settings']['pages']['#disabled'] = TRUE;
|
| 48 |
$form['page_vis_settings']['#description'] = t('Page specific visibility is being handled by your <strong>block_assign_show()</strong> function.');
|
| 49 |
$form['#submit']['block_assign_block_admin_submit'] = array();
|
| 50 |
}
|
| 51 |
}
|
| 52 |
|
| 53 |
|
| 54 |
function block_assign_menu($may_cache) {
|
| 55 |
$items[] = array(
|
| 56 |
'path' => 'admin/settings/block_assign',
|
| 57 |
'title' => t('Block Assign Settings'),
|
| 58 |
'callback' => 'block_assign_admin',
|
| 59 |
/*'callback' => 'drupal_get_form',
|
| 60 |
'callback arguments' => 'block_assign_admin',*/
|
| 61 |
'access' => user_access('access administration pages'),
|
| 62 |
'type' => MENU_NORMAL_ITEM,
|
| 63 |
);
|
| 64 |
|
| 65 |
$arg = intval(arg(3));
|
| 66 |
if ($arg > 0) {
|
| 67 |
$items[] = array(
|
| 68 |
'path' => 'admin/settings/block_assign/'. $arg,
|
| 69 |
'title' => t('Edit Block Assignment'),
|
| 70 |
'callback' => 'block_assign_admin',
|
| 71 |
'access' => user_access('access administration pages')
|
| 72 |
);
|
| 73 |
}
|
| 74 |
|
| 75 |
$items[] = array(
|
| 76 |
'path' => 'admin/build/block/visibility',
|
| 77 |
'title' => t('Page Visibility'),
|
| 78 |
'callback' => 'block_assign_admin',
|
| 79 |
'access' => user_access('access administration pages'),
|
| 80 |
'type' => MENU_LOCAL_TASK,
|
| 81 |
);
|
| 82 |
return $items;
|
| 83 |
}
|
| 84 |
|
| 85 |
function block_assign_edit_form() {
|
| 86 |
$id = intval(arg(3));
|
| 87 |
$form = array();
|
| 88 |
|
| 89 |
if ($id > 0) {
|
| 90 |
$block = db_fetch_object(db_query('SELECT * FROM {block_assign} WHERE id = %d', $id));
|
| 91 |
$blocks = module_invoke($block->module, 'block');
|
| 92 |
$title = array_key_exists($block->box_id, $blocks) ? $blocks[$block->box_id]['info'] : t('Unknown');
|
| 93 |
|
| 94 |
$types = array('node' => t('Node ID'), 'uri' => t('URI Regex'));
|
| 95 |
if (function_exists('globalnode_nodeapi')) {
|
| 96 |
$types['type'] = t('Node Type');
|
| 97 |
}
|
| 98 |
|
| 99 |
$form['#base'] = 'block_assign_edit_form';
|
| 100 |
$form['block_assign_id'] = array(
|
| 101 |
'#type' => 'hidden',
|
| 102 |
'#value' => $id
|
| 103 |
);
|
| 104 |
$form['block_assign_title'] = array(
|
| 105 |
'#id' => 'block_assign_title',
|
| 106 |
'#type' => 'item',
|
| 107 |
'#value' => '<strong>'. t('Title') .': </strong>'. $title
|
| 108 |
);
|
| 109 |
$form['block_assign_type'] = array(
|
| 110 |
'#id' => 'block_assign_type',
|
| 111 |
'#type' => 'select',
|
| 112 |
'#options' => $types,
|
| 113 |
'#value' => $block->condition_type
|
| 114 |
);
|
| 115 |
$form['block_assign_value'] = array(
|
| 116 |
'#id' => 'block_assign_value',
|
| 117 |
'#type' => 'textfield',
|
| 118 |
'#default_value' => stripslashes($block->condition_value)
|
| 119 |
);
|
| 120 |
$form['submit'] = array(
|
| 121 |
'#id' => 'submit',
|
| 122 |
'#type' => 'submit',
|
| 123 |
'#value' => t('Save')
|
| 124 |
);
|
| 125 |
$form['delete'] = array(
|
| 126 |
'#id' => 'delete',
|
| 127 |
'#type' => 'submit',
|
| 128 |
'#value' => t('Delete'),
|
| 129 |
'#attributes' => array('onclick' => 'return confirm("Are you sure you want to delete?")')
|
| 130 |
);
|
| 131 |
}
|
| 132 |
return $form;
|
| 133 |
}
|
| 134 |
|
| 135 |
function block_assign_new_form() {
|
| 136 |
$form = array();
|
| 137 |
$form['#base'] = 'block_assign_new_form';
|
| 138 |
$types = array('node' => t('Node ID'), 'uri' => t('URI Regex'));
|
| 139 |
if (function_exists('globalnode_nodeapi')) {
|
| 140 |
$types['type'] = t('Node Type');
|
| 141 |
}
|
| 142 |
|
| 143 |
$form['new'] = array(
|
| 144 |
'#type' => 'fieldset',
|
| 145 |
'#title' => t('Add New Block Association'),
|
| 146 |
);
|
| 147 |
$form['new']['top'] = array(
|
| 148 |
'#type' => 'item',
|
| 149 |
'#value' => ' ',
|
| 150 |
'#prefix' => '<table>',
|
| 151 |
'#weight' => -100
|
| 152 |
);
|
| 153 |
|
| 154 |
$form['new']['instructions'] = array(
|
| 155 |
'#type' => 'item',
|
| 156 |
'#weight' => -90,
|
| 157 |
'#value' => t('First, select the block(s) you would like to add. Then add values in the fields below for any search criteria you wish to set. Leave blank to set nothing.')
|
| 158 |
);
|
| 159 |
|
| 160 |
$instructions = array(
|
| 161 |
'type' => t('Enter a node type (e.g. "blog") if you wish to create a block association for the selected blocks.'),
|
| 162 |
'node' => t('Enter a node id (e.g. "3") if you wish to create a block association for that node.'),
|
| 163 |
'uri' => t('Enter a regular expression (e.g. "^\/blog") to be searched against the request uri. Omit starting and ending slashes and be sure to escape characters that typically must be escaped.')
|
| 164 |
);
|
| 165 |
|
| 166 |
//SELECT bid, info FROM {boxes} ORDER BY info
|
| 167 |
$blocks = array();
|
| 168 |
$result = db_query('SELECT module, delta FROM {blocks} WHERE status = 1');
|
| 169 |
while ($row = db_fetch_object($result)) {
|
| 170 |
$bl = module_invoke($row->module, 'block');
|
| 171 |
$blocks[$row->module .':'. $row->delta] = $bl[$row->delta]['info'];
|
| 172 |
}
|
| 173 |
asort($blocks);
|
| 174 |
$form['new']['block_assign_blocks'] = array(
|
| 175 |
'#id' => 'block_assign_blocks',
|
| 176 |
'#type' => 'select',
|
| 177 |
'#title' => t('Block'),
|
| 178 |
'#options' => $blocks,
|
| 179 |
'#weight' => -8,
|
| 180 |
'#multiple' => TRUE,
|
| 181 |
'#prefix' => '<tr><td colspan="2">',
|
| 182 |
'#suffix' => '</td></tr>'
|
| 183 |
);
|
| 184 |
|
| 185 |
foreach ($types as $k => $v) {
|
| 186 |
$form['new']['block_assign_type_'. $k] = array(
|
| 187 |
'#type' => 'item',
|
| 188 |
'#value' => '<strong>'. $v .'</strong>',
|
| 189 |
'#prefix' => '<tr><td valign="top">',
|
| 190 |
'#suffix' => '</td>',
|
| 191 |
//'#weight' => $weight
|
| 192 |
);
|
| 193 |
$form['new']['block_assign_value_'. $k] = array(
|
| 194 |
'#id' => 'block_assign_value_'. $k,
|
| 195 |
'#type' => 'textfield',
|
| 196 |
'#title' => t('Value'),
|
| 197 |
//'#weight' => $weight,
|
| 198 |
//'#description' => t('Enter the value the module should search for. If "Node ID" is selected above, this is an integer. If "URI Regex" is selected, this is a regular expression (minus slashes). If "Node Type" is available and selected, this should be a node type.'),
|
| 199 |
'#description' => $instructions[$k],
|
| 200 |
'#prefix' => '<td valign="top">',
|
| 201 |
'#suffix' => '</td></tr>'
|
| 202 |
);
|
| 203 |
}
|
| 204 |
|
| 205 |
$form['new']['block_add_new'] = array(
|
| 206 |
'#id' => 'block_add_new',
|
| 207 |
'#type' => 'submit',
|
| 208 |
'#value' => t('Add New'),
|
| 209 |
'#prefix' => '<tr><td colspan="2">',
|
| 210 |
'#suffix' => '</td></tr>'
|
| 211 |
);
|
| 212 |
|
| 213 |
$form['new']['bottom'] = array(
|
| 214 |
'#type' => 'item',
|
| 215 |
'#value' => ' ',
|
| 216 |
'#suffix' => '</table>',
|
| 217 |
'#weight' => 100
|
| 218 |
);
|
| 219 |
|
| 220 |
return $form;
|
| 221 |
}
|
| 222 |
|
| 223 |
function block_assign_admin() {
|
| 224 |
if ($_POST) {
|
| 225 |
switch ($_POST['op']) {
|
| 226 |
case 'Add New':
|
| 227 |
$types = array('type', 'node', 'uri');
|
| 228 |
if (sizeof($_POST['block_assign_blocks']) > 0) {
|
| 229 |
foreach ($_POST['block_assign_blocks'] as $block) {
|
| 230 |
list($module, $delta) = split(":", $block);
|
| 231 |
foreach ($types as $type) {
|
| 232 |
if ($_POST['block_assign_value_'. $type] != '') {
|
| 233 |
db_query('INSERT INTO {block_assign} (module, condition_type, condition_value, box_id) VALUES ("%s", "%s", "%s", "%s")', $module, $type, $_POST['block_assign_value_'. $type], $delta);
|
| 234 |
}
|
| 235 |
}
|
| 236 |
}
|
| 237 |
drupal_set_message(t('New Block Assign associations added.'));
|
| 238 |
}
|
| 239 |
else{
|
| 240 |
drupal_set_message(t('No blocks were selected.'));
|
| 241 |
}
|
| 242 |
break;
|
| 243 |
case 'Save':
|
| 244 |
db_query('UPDATE {block_assign} SET condition_type = "%s", condition_value = "%s" WHERE id = %d', $_POST['block_assign_type'], $_POST['block_assign_value'], intval($_POST['block_assign_id']));
|
| 245 |
drupal_set_message(t('Block edits completed.'));
|
| 246 |
break;
|
| 247 |
case 'Delete':
|
| 248 |
db_query('DELETE FROM {block_assign} WHERE id = %d', intval($_POST['block_assign_id']));
|
| 249 |
drupal_set_message(t('Block assignment deleted.'));
|
| 250 |
break;
|
| 251 |
}
|
| 252 |
drupal_goto('admin/settings/block_assign');
|
| 253 |
}
|
| 254 |
else{
|
| 255 |
if (intval(arg(3)) > 0) {
|
| 256 |
$output = drupal_get_form('block_assign_edit_form');
|
| 257 |
}
|
| 258 |
else{
|
| 259 |
$output = drupal_get_form('block_assign_new_form');
|
| 260 |
|
| 261 |
$result = db_query('SELECT ba.id, b.module, b.delta, ba.condition_type type, ba.condition_value value, MD5(CONCAT(ba.condition_type, ba.condition_value)) AS sep FROM {blocks} b, {block_assign} ba WHERE b.status = 1 AND b.module = ba.module AND b.delta = ba.box_id ORDER BY type, value');
|
| 262 |
|
| 263 |
$data = array();
|
| 264 |
$blocks = array();
|
| 265 |
$titles = array();
|
| 266 |
$sep = '';
|
| 267 |
while ($row = db_fetch_object($result)) {
|
| 268 |
$blocks[] = $row;
|
| 269 |
$info = module_invoke($row->module, 'block');
|
| 270 |
foreach ($info as $k => $v) {
|
| 271 |
$titles[$row->module .'_'. $k] = $v['info'];
|
| 272 |
}
|
| 273 |
}
|
| 274 |
|
| 275 |
foreach ($blocks as $row) {
|
| 276 |
if ($sep != $row->sep) {
|
| 277 |
$data[] = array('<strong>Type '. $row->type .' for value '. $row->value .'</strong>', '', '', '');
|
| 278 |
$sep = $row->sep;
|
| 279 |
}
|
| 280 |
$data[] = array(
|
| 281 |
$titles[$row->module .'_'. $row->delta],
|
| 282 |
$row->type,
|
| 283 |
$row->value,
|
| 284 |
'<a href="/admin/settings/block_assign/'. intval($row->id) .'">'. t('edit') .'</a>',
|
| 285 |
);
|
| 286 |
}
|
| 287 |
$headers = array(t('Block Name'), t('Type'), t('Value'), t('Edit'));
|
| 288 |
$output .= theme('table', $headers, $data);
|
| 289 |
}
|
| 290 |
return $output;
|
| 291 |
}
|
| 292 |
}
|
| 293 |
|
| 294 |
function block_assign_show($module, $delta) {
|
| 295 |
$conditions = array();
|
| 296 |
if (!array_key_exists('block_assign_onditions', $_SESSION)) {
|
| 297 |
$result = db_query('select b.module, ba.condition_type, ba.condition_value, b.delta AS bid from {block_assign} ba, {blocks} b WHERE ba.module = b.module AND ba.box_id = b.delta AND ba.module = "%s" AND ba.box_id = "%s"', $module, $delta);
|
| 298 |
while ($item = db_fetch_object($result)) {
|
| 299 |
$conditions[] = $item;
|
| 300 |
}
|
| 301 |
$_SESSION['block_assign_conditions'] = $conditions;
|
| 302 |
}
|
| 303 |
else{
|
| 304 |
$conditions = $_SESSION['block_assign_conditions'];
|
| 305 |
}
|
| 306 |
|
| 307 |
|
| 308 |
foreach ($conditions as $condition) {
|
| 309 |
switch ($condition->condition_type) {
|
| 310 |
case 'node':
|
| 311 |
preg_match('/^node\/(\d*)$/', $_GET['q'], $matches);
|
| 312 |
if ($matches[1] && $condition->condition_value == $matches[1]) {
|
| 313 |
error_log('node matches '. $matches[1]);
|
| 314 |
print('<pre>'. print_r($types, 1) .'</pre>');
|
| 315 |
return true;
|
| 316 |
}
|
| 317 |
break;
|
| 318 |
case 'type':
|
| 319 |
if (isset($GLOBALS['globalnode']) && isset($GLOBALS['globalnode']->type) && $GLOBALS['globalnode']->type == $condition->condition_value) {
|
| 320 |
error_log('type matches '. $condition->condition_value);
|
| 321 |
print('<pre>'. print_r($GLOBALS['globalnode'], 1) .'</pre>');
|
| 322 |
return true;
|
| 323 |
}
|
| 324 |
break;
|
| 325 |
case 'uri':
|
| 326 |
if (preg_match('/'. $condition->condition_value .'/', request_uri())) {
|
| 327 |
return true;
|
| 328 |
}
|
| 329 |
break;
|
| 330 |
}
|
| 331 |
}
|
| 332 |
|
| 333 |
return false;
|
| 334 |
}
|
| 335 |
|