| 1 |
<?php
|
| 2 |
// $Id: headerimage.module,v 1.23 2009/05/18 15:35:48 sutharsan Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file headerimage.module
|
| 6 |
* Conditionally display an node in a block.
|
| 7 |
*
|
| 8 |
* WHISH LIST:
|
| 9 |
* create CCK node type at install (see: http://drupal.org/node/82908) (maybe: http://drupal.org/node/101742)
|
| 10 |
*/
|
| 11 |
|
| 12 |
/**
|
| 13 |
* Implementation of hook_menu()
|
| 14 |
*/
|
| 15 |
function headerimage_menu() {
|
| 16 |
$items['admin/settings/headerimage'] = array(
|
| 17 |
'title' => t('Header Image'),
|
| 18 |
'description' => t('Control the Header Image.'),
|
| 19 |
'page callback' => 'drupal_get_form',
|
| 20 |
'page arguments' => array('headerimage_settings_block_add'),
|
| 21 |
'access arguments' => array('administer header image'),
|
| 22 |
'type' => MENU_NORMAL_ITEM,
|
| 23 |
'file' => 'headerimage.admin.inc',
|
| 24 |
);
|
| 25 |
$items['admin/settings/headerimage/list'] = array(
|
| 26 |
'title' => t('List'),
|
| 27 |
'page arguments' => array('headerimage_settings_block_add'),
|
| 28 |
'type' => MENU_DEFAULT_LOCAL_TASK,
|
| 29 |
'weight' => -10,
|
| 30 |
);
|
| 31 |
$items['admin/settings/headerimage/settings'] = array(
|
| 32 |
'title' => t('Settings'),
|
| 33 |
'page arguments' => array('headerimage_settings_form'),
|
| 34 |
'access arguments' => array('administer header image'),
|
| 35 |
'type' => MENU_LOCAL_TASK,
|
| 36 |
'weight' => 10,
|
| 37 |
);
|
| 38 |
|
| 39 |
$items['admin/settings/headerimage/edit/%'] = array(
|
| 40 |
'title' => t('Edit Header Image block'),
|
| 41 |
'page callback' => 'drupal_get_form',
|
| 42 |
'page arguments' => array('headerimage_settings_block_edit', 4),
|
| 43 |
'access arguments' => array('administer header image'),
|
| 44 |
'type' => MENU_CALLBACK,
|
| 45 |
);
|
| 46 |
$items['admin/settings/headerimage/delete/%'] = array(
|
| 47 |
'title' => t('Delete Header Image block'),
|
| 48 |
'page callback' => 'drupal_get_form',
|
| 49 |
'page arguments' => array('headerimage_block_confirm_delete', 4),
|
| 50 |
'access arguments' => array('administer header image'),
|
| 51 |
'type' => MENU_CALLBACK,
|
| 52 |
'file' => 'headerimage.admin.inc',
|
| 53 |
);
|
| 54 |
|
| 55 |
return $items;
|
| 56 |
}
|
| 57 |
|
| 58 |
/**
|
| 59 |
* Edit block data form
|
| 60 |
*/
|
| 61 |
function headerimage_settings_block_edit(&$form_state, $delta) {
|
| 62 |
$blocks = headerimage_get_blocks();
|
| 63 |
$form = array();
|
| 64 |
|
| 65 |
$form['delta'] = array(
|
| 66 |
'#type' => 'hidden',
|
| 67 |
'#value' => $delta,
|
| 68 |
);
|
| 69 |
$form['title'] = array(
|
| 70 |
'#type' => 'textfield',
|
| 71 |
'#title' => t('Block title'),
|
| 72 |
'#description' => t('The block name must be unique.'),
|
| 73 |
'#default_value' => $blocks[$delta],
|
| 74 |
'#required' => true,
|
| 75 |
);
|
| 76 |
$form['op'] = array(
|
| 77 |
'#type' => 'submit',
|
| 78 |
'#value' => t('Save'),
|
| 79 |
);
|
| 80 |
$form['#redirect'] = 'admin/settings/headerimage';
|
| 81 |
|
| 82 |
|
| 83 |
return $form;
|
| 84 |
}
|
| 85 |
|
| 86 |
function headerimage_settings_block_edit_validate($form, &$form_state) {
|
| 87 |
$blocks = headerimage_get_blocks();
|
| 88 |
|
| 89 |
// Remove current blockname to prevent false error
|
| 90 |
unset($blocks[$form_state['values']['delta']]);
|
| 91 |
|
| 92 |
if (!empty($blocks)) {
|
| 93 |
// Check if name is unique
|
| 94 |
if (in_array($form_state['values']['title'], $blocks)) {
|
| 95 |
form_set_error('', t('Header Image block %s already exists. Please use a different name.', array('%s' => $form_state['values']['title'])));
|
| 96 |
}
|
| 97 |
}
|
| 98 |
}
|
| 99 |
|
| 100 |
function headerimage_settings_block_edit_submit($form, &$form_state) {
|
| 101 |
db_query("UPDATE {headerimage_block} SET title = '%s' WHERE delta = %d",
|
| 102 |
$form_state['values']['title'], $form_state['values']['delta']);
|
| 103 |
|
| 104 |
drupal_set_message(t('Header Image block updated.'));
|
| 105 |
return 'admin/settings/headerimage';
|
| 106 |
}
|
| 107 |
|
| 108 |
/**
|
| 109 |
* Implementation of hook_perm().
|
| 110 |
*/
|
| 111 |
function headerimage_perm() {
|
| 112 |
return array('administer header image', 'maintain display conditions', 'view header image');
|
| 113 |
}
|
| 114 |
|
| 115 |
/**
|
| 116 |
* Implementation of hook_block().
|
| 117 |
*/
|
| 118 |
function headerimage_block($op = 'list', $delta = 0, $edit = array()) {
|
| 119 |
switch ($op) {
|
| 120 |
case 'list':
|
| 121 |
$headerimage_blocks = headerimage_get_blocks();
|
| 122 |
foreach ($headerimage_blocks as $key => $name) {
|
| 123 |
$blocks[$key]['info'] = check_plain($name);
|
| 124 |
}
|
| 125 |
return $blocks;
|
| 126 |
break;
|
| 127 |
case 'configure':
|
| 128 |
return headerimage_block_configure($delta);
|
| 129 |
case 'save':
|
| 130 |
variable_set('headerimage_block_'. $delta .'_random_fallback', $edit['random_fallback']);
|
| 131 |
break;
|
| 132 |
case 'view':
|
| 133 |
if (user_access('view header image')) {
|
| 134 |
// select node from nodes assigned to this block
|
| 135 |
$nid = headerimage_select_node($delta);
|
| 136 |
$teaser = variable_get('headerimage_teaser', true);
|
| 137 |
|
| 138 |
// prepare block output
|
| 139 |
if (!empty($nid)) {
|
| 140 |
$node = node_load($nid);
|
| 141 |
$node = node_prepare($node, $teaser); //use node teaser view
|
| 142 |
|
| 143 |
// mimic node_view
|
| 144 |
$node = node_build_content($node, $teaser, false);
|
| 145 |
$content = drupal_render($node->content);
|
| 146 |
if ($teaser) {
|
| 147 |
$node->teaser = $content;
|
| 148 |
unset($node->body);
|
| 149 |
}
|
| 150 |
else {
|
| 151 |
$node->body = $content;
|
| 152 |
unset($node->teaser);
|
| 153 |
}
|
| 154 |
node_invoke_nodeapi($node, 'alter', $teaser);
|
| 155 |
|
| 156 |
$block['content'] = theme('headerimage_block', $node, $teaser);
|
| 157 |
}
|
| 158 |
return $block;
|
| 159 |
}
|
| 160 |
break;
|
| 161 |
}
|
| 162 |
}
|
| 163 |
|
| 164 |
/**
|
| 165 |
* Select a node to be displayed in the block
|
| 166 |
*
|
| 167 |
* Node selection by (1)weight and (2)condition.
|
| 168 |
* If multiple conditions are present, any true condition will select the node.
|
| 169 |
* If no node is selected by the conditions and random fallback selection is
|
| 170 |
* enabled for the block, one of the available nodes will be selected at random.
|
| 171 |
*
|
| 172 |
* @param $block
|
| 173 |
* The headerimage block number ($delta)
|
| 174 |
*
|
| 175 |
* @return
|
| 176 |
* nid of the selected node
|
| 177 |
* null: no node selected
|
| 178 |
*/
|
| 179 |
function headerimage_select_node($block) {
|
| 180 |
$result = db_query("SELECT nid, conditions FROM {headerimage} WHERE block = %d ORDER BY weight, nid ASC", $block);
|
| 181 |
while ($header_image = db_fetch_object($result)) {
|
| 182 |
$conditions = unserialize($header_image->conditions);
|
| 183 |
$match = false;
|
| 184 |
// Store the nid in an array for the random selection fallback option.
|
| 185 |
$block_nids[] = $header_image->nid;
|
| 186 |
$selected_types = variable_get('headerimage_condition_types', array('nid' => 'nid'));
|
| 187 |
foreach ($conditions as $type => $condition) {
|
| 188 |
if (!empty($condition) && !empty($selected_types[$type])) {
|
| 189 |
switch ($type) {
|
| 190 |
case 'nid':
|
| 191 |
$match = headerimage_eval_nid($condition);
|
| 192 |
break;
|
| 193 |
case 'url':
|
| 194 |
$match = headerimage_eval_url($condition);
|
| 195 |
break;
|
| 196 |
case 'taxonomy':
|
| 197 |
$match = headerimage_eval_taxonomy($condition);
|
| 198 |
break;
|
| 199 |
case 'book':
|
| 200 |
$match = headerimage_eval_book($condition);
|
| 201 |
break;
|
| 202 |
case 'nodetype':
|
| 203 |
$match = headerimage_eval_nodetype($condition);
|
| 204 |
break;
|
| 205 |
case 'php':
|
| 206 |
$match = drupal_eval($condition);
|
| 207 |
break;
|
| 208 |
}
|
| 209 |
}
|
| 210 |
if ($match) break;
|
| 211 |
}
|
| 212 |
if ($match) break;
|
| 213 |
}
|
| 214 |
if ($match) {
|
| 215 |
return $header_image->nid;
|
| 216 |
} elseif (variable_get('headerimage_block_'. $block .'_random_fallback', 0) && count($block_nids)) {
|
| 217 |
return $block_nids[array_rand($block_nids)];
|
| 218 |
}
|
| 219 |
}
|
| 220 |
|
| 221 |
/**
|
| 222 |
* Evaluate nid condition
|
| 223 |
*
|
| 224 |
* Checks if current page is in list of nids
|
| 225 |
*
|
| 226 |
* @param $condition
|
| 227 |
* comma separated list of nids
|
| 228 |
*
|
| 229 |
* @return
|
| 230 |
* true: current page is in $condition
|
| 231 |
* false: if not
|
| 232 |
* null: current page is not a node
|
| 233 |
*/
|
| 234 |
function headerimage_eval_nid($condition) {
|
| 235 |
if (arg(0) == 'node' && is_numeric(arg(1))) {
|
| 236 |
$nids = explode(',', $condition);
|
| 237 |
$match = in_array(arg(1), $nids);
|
| 238 |
}
|
| 239 |
return $match;
|
| 240 |
}
|
| 241 |
|
| 242 |
/**
|
| 243 |
* Evaluate url condition
|
| 244 |
*
|
| 245 |
* Check if current page is in selected paths
|
| 246 |
*
|
| 247 |
* @param $condition
|
| 248 |
* text with paths or '<frontpage>'. May contain wild cards.
|
| 249 |
*
|
| 250 |
* @return
|
| 251 |
* true: current page matches one of the $condition paths;
|
| 252 |
* false: if not
|
| 253 |
*/
|
| 254 |
function headerimage_eval_url($condition) {
|
| 255 |
$path = drupal_get_path_alias($_GET['q']);
|
| 256 |
$regexp = '/^('. preg_replace(array('/(\r\n?|\n)/', '/\\\\\*/', '/(^|\|)\\\\<front\\\\>($|\|)/'), array('|', '.*', '\1'. preg_quote(variable_get('site_frontpage', 'node'), '/') .'\2'), preg_quote($condition, '/')) .')$/';
|
| 257 |
// Compare with the internal and path alias (if any).
|
| 258 |
$match = preg_match($regexp, $path);
|
| 259 |
if ($match) return true;
|
| 260 |
|
| 261 |
if ($path != $_GET['q']) {
|
| 262 |
$match = preg_match($regexp, $_GET['q']);
|
| 263 |
}
|
| 264 |
return $match;
|
| 265 |
}
|
| 266 |
|
| 267 |
/**
|
| 268 |
* Evaluate taxonomy condition
|
| 269 |
*
|
| 270 |
* Check if current page has selected taxonomy terms
|
| 271 |
*
|
| 272 |
* @param $condition
|
| 273 |
* array of taxonomy term tid's
|
| 274 |
*
|
| 275 |
* @return
|
| 276 |
* true: current page contains one of $condition's tags
|
| 277 |
* false: if not
|
| 278 |
* null: current page is not a node
|
| 279 |
*/
|
| 280 |
function headerimage_eval_taxonomy($condition) {
|
| 281 |
if (arg(0) == 'node' && is_numeric(arg(1))) {
|
| 282 |
if ($node = menu_get_object()) {
|
| 283 |
foreach (array_keys($node->taxonomy) as $key) {
|
| 284 |
if (isset($condition[$key])) {
|
| 285 |
return true;
|
| 286 |
}
|
| 287 |
}
|
| 288 |
return false;
|
| 289 |
}
|
| 290 |
}
|
| 291 |
return null;
|
| 292 |
}
|
| 293 |
|
| 294 |
/**
|
| 295 |
* Evaluate book condition
|
| 296 |
*
|
| 297 |
* Return true if current node is a page of the book(s) selected in the condition
|
| 298 |
*
|
| 299 |
* @param $condition
|
| 300 |
* array of book root nid's
|
| 301 |
*
|
| 302 |
* @return
|
| 303 |
* true: current node is a page in $condition's books
|
| 304 |
* false: if not
|
| 305 |
* null: current page is not a node
|
| 306 |
*/
|
| 307 |
function headerimage_eval_book($condition) {
|
| 308 |
if (arg(0) == 'node' && is_numeric(arg(1))) {
|
| 309 |
|
| 310 |
// check if current node is one of the condition pages (books)
|
| 311 |
$match = in_array(arg(1), $condition);
|
| 312 |
if ($match) return true;
|
| 313 |
|
| 314 |
// check if current page is part of book in the condition pages
|
| 315 |
$bid = db_result(db_query("SELECT bid FROM {book} WHERE nid = %d", arg(1)));
|
| 316 |
$match = in_array($bid, $condition);
|
| 317 |
}
|
| 318 |
return $match;
|
| 319 |
}
|
| 320 |
/**
|
| 321 |
* Evaluate node type condition
|
| 322 |
*
|
| 323 |
* Return true if type of current node is selected
|
| 324 |
*
|
| 325 |
* @param $condition
|
| 326 |
* array of selected node types
|
| 327 |
*
|
| 328 |
* @return
|
| 329 |
* true: current node type is selected
|
| 330 |
* false: if not
|
| 331 |
* null: current page is not a node
|
| 332 |
*/
|
| 333 |
function headerimage_eval_nodetype($condition) {
|
| 334 |
$match = FALSE;
|
| 335 |
if (arg(0) == 'node' && is_numeric(arg(1))) {
|
| 336 |
$node = menu_get_object();
|
| 337 |
$match = in_array($node->type, $condition);
|
| 338 |
}
|
| 339 |
|
| 340 |
return $match;
|
| 341 |
}
|
| 342 |
|
| 343 |
/**
|
| 344 |
* Implementation of hook_form_alter
|
| 345 |
*
|
| 346 |
* Add display conditions to header image node forms
|
| 347 |
*/
|
| 348 |
function headerimage_form_alter(&$form, &$form_state, $form_id) {
|
| 349 |
if (strpos($form_id, '_node_form') && isset($form['type']['#value']) && in_array($form['type']['#value'], variable_get('headerimage_node_type', array()), true) && user_access('maintain display conditions')) {
|
| 350 |
|
| 351 |
$form['headerimage'] = array(
|
| 352 |
'#type' => 'fieldset',
|
| 353 |
'#title' => t('Display conditions'),
|
| 354 |
'#description' => t('This node is displayed in a Header Image block when one of the conditions below are evaluated true.'),
|
| 355 |
'#collapsible' => true,
|
| 356 |
'#collapsed' => false,
|
| 357 |
'#weight' => -2,
|
| 358 |
);
|
| 359 |
$form['headerimage']['headerimage_block'] = array(
|
| 360 |
'#type' => 'select',
|
| 361 |
'#title' => t('Block name'),
|
| 362 |
'#description' => t('The block in which this node is displayed.'),
|
| 363 |
'#options' => headerimage_get_blocks(),
|
| 364 |
'#default_value' => $form['#node']->headerimage_block,
|
| 365 |
'#required' => true,
|
| 366 |
'#multiple' => false,
|
| 367 |
);
|
| 368 |
$form['headerimage']['headerimage_weight'] = array(
|
| 369 |
'#type' => 'weight',
|
| 370 |
'#title' => t('Condition weight'),
|
| 371 |
'#description' => t('Determines the order of in which the nodes are evaluated. The conditions of a node with a smaller weight will be evaluated first, those with a larger weight are evaluated later. A default image (the one displayed if the conditions of all other images fail) should have the largest weight: 10.'),
|
| 372 |
'#default_value' => $form['#node']->headerimage_weight,
|
| 373 |
'#delta' => 10,
|
| 374 |
);
|
| 375 |
|
| 376 |
$condition_types = variable_get(headerimage_condition_types, array('nid' => 'nid'));
|
| 377 |
if (!empty($condition_types)) {
|
| 378 |
foreach ($condition_types as $type) {
|
| 379 |
if ($type != '0') {
|
| 380 |
$all_types = headerimage_get_condition_types();
|
| 381 |
$title = t("@name condition", array('@name' => $all_types[$type]));
|
| 382 |
$name = 'headerimage_condition_'. $type;
|
| 383 |
switch ($type) {
|
| 384 |
case 'nid':
|
| 385 |
$form['headerimage'][$name] = array(
|
| 386 |
'#type' => 'textarea',
|
| 387 |
'#title' => $title,
|
| 388 |
'#description' => t("Enter node id's separated by comma's. Example: 1,3,5,7"),
|
| 389 |
'#default_value' => $form['#node']->$name,
|
| 390 |
'#rows' => 4,
|
| 391 |
);
|
| 392 |
break;
|
| 393 |
case 'url':
|
| 394 |
$form['headerimage'][$name] = array(
|
| 395 |
'#type' => 'textarea',
|
| 396 |
'#title' => $title,
|
| 397 |
'#description' => t("Enter one url per line as Drupal paths. The '*' character is a wildcard. Example paths are blog for the blog page and blog/* for every personal blog. %front is the front page.", array('%front' => '<front>')),
|
| 398 |
'#default_value' => $form['#node']->$name,
|
| 399 |
'#rows' => 4,
|
| 400 |
);
|
| 401 |
break;
|
| 402 |
case 'taxonomy':
|
| 403 |
$options = array();
|
| 404 |
if (module_exists('taxonomy')) {
|
| 405 |
$vocab = taxonomy_get_vocabularies();
|
| 406 |
if (!empty($vocab)) {
|
| 407 |
foreach ($vocab as $v) {
|
| 408 |
$taxonomy = taxonomy_get_tree($v->vid);
|
| 409 |
if (!empty($taxonomy)) {
|
| 410 |
foreach ($taxonomy as $tag) {
|
| 411 |
$options[check_plain($v->name)][$tag->tid] = check_plain($tag->name);
|
| 412 |
}
|
| 413 |
}
|
| 414 |
}
|
| 415 |
}
|
| 416 |
if (!empty($options)) {
|
| 417 |
$description = t("Select one or multiple tags.");
|
| 418 |
}
|
| 419 |
else {
|
| 420 |
drupal_set_message(t("No vocabulary or tags defined. Please create vocabulary and tags or remove taxonomy from the !settings.",
|
| 421 |
array('!settings' => l(t('Header Image settings'), 'admin/settings/headerimage/settings'))));
|
| 422 |
break;
|
| 423 |
}
|
| 424 |
$form['headerimage'][$name] = array(
|
| 425 |
'#type' => 'select',
|
| 426 |
'#title' => $title,
|
| 427 |
'#description' => $description,
|
| 428 |
'#default_value' => $form['#node']->$name,
|
| 429 |
'#options' => $options,
|
| 430 |
'#multiple' => true,
|
| 431 |
);
|
| 432 |
}
|
| 433 |
else {
|
| 434 |
drupal_set_message(t("The taxonomy module is not enabled. Please enable the module or remove it from the !settings.",
|
| 435 |
array('!settings' => l(t('Header Image settings'), 'admin/settings/headerimage/settings'))));
|
| 436 |
}
|
| 437 |
break;
|
| 438 |
case 'book':
|
| 439 |
if (module_exists('book')) {
|
| 440 |
$options = array();
|
| 441 |
$books = book_get_books();
|
| 442 |
if (count($books)) {
|
| 443 |
$description = t("Select one book or multiple books.");
|
| 444 |
foreach ($books as $key => $book) {
|
| 445 |
$options[$key] = $book['title'];
|
| 446 |
}
|
| 447 |
}
|
| 448 |
else {
|
| 449 |
$description = t("No books defined. Please create a book before using it as a condition.");
|
| 450 |
}
|
| 451 |
$form['headerimage'][$name] = array(
|
| 452 |
'#type' => 'select',
|
| 453 |
'#title' => $title,
|
| 454 |
'#description' => $description,
|
| 455 |
'#default_value' => $form['#node']->$name,
|
| 456 |
'#options' => $options,
|
| 457 |
'#multiple' => true,
|
| 458 |
);
|
| 459 |
}
|
| 460 |
else {
|
| 461 |
drupal_set_message(t("The book module is not enabled. Please enable the module or remove it from the !settings.",
|
| 462 |
array('!settings' => l(t('Header Image settings'), 'admin/settings/headerimage/settings'))));
|
| 463 |
}
|
| 464 |
break;
|
| 465 |
case 'nodetype':
|
| 466 |
$nodes = node_get_types();
|
| 467 |
foreach ($nodes as $node) {
|
| 468 |
$nodetype[$node->type] = check_plain($node->name);
|
| 469 |
}
|
| 470 |
$form['headerimage'][$name] = array(
|
| 471 |
'#type' => 'select',
|
| 472 |
'#title' => $title,
|
| 473 |
'#description' => t('Select one or multiple content types'),
|
| 474 |
'#default_value' => $form['#node']->$name,
|
| 475 |
'#options' => $nodetype,
|
| 476 |
'#multiple' => true,
|
| 477 |
);
|
| 478 |
break;
|
| 479 |
case 'php':
|
| 480 |
$form['headerimage'][$name] = array(
|
| 481 |
'#type' => 'textarea',
|
| 482 |
'#title' => $title,
|
| 483 |
'#description' => t("Enter PHP code between <?php ?> tags. Note that executing incorrect PHP-code can break your Drupal site."),
|
| 484 |
'#default_value' => $form['#node']->$name,
|
| 485 |
'#rows' => 4,
|
| 486 |
);
|
| 487 |
break;
|
| 488 |
}
|
| 489 |
}
|
| 490 |
}
|
| 491 |
}
|
| 492 |
}
|
| 493 |
}
|
| 494 |
|
| 495 |
/**
|
| 496 |
* Return a form with a list of Header Image nodes
|
| 497 |
*
|
| 498 |
* Nodes assigned to Header Image block $delta are listed with weight and
|
| 499 |
* edit link
|
| 500 |
*
|
| 501 |
* @param $delta
|
| 502 |
* Header Image block delta
|
| 503 |
*
|
| 504 |
* @return
|
| 505 |
* form array
|
| 506 |
*/
|
| 507 |
function headerimage_block_configure($delta) {
|
| 508 |
$result = db_query("SELECT title, weight, n.nid FROM {headerimage} hi JOIN {node} n ON n.nid = hi.nid AND hi.block = %d ORDER BY weight, nid ASC", $delta);
|
| 509 |
// Table with image set details
|
| 510 |
$rows = array();
|
| 511 |
while ($data = db_fetch_object($result)) {
|
| 512 |
$rows[] = array(
|
| 513 |
'node' => check_plain($data->title),
|
| 514 |
'weight' => $data->weight,
|
| 515 |
'edit' => l(t('Edit'), "node/$data->nid/edit"),
|
| 516 |
);
|
| 517 |
}
|
| 518 |
|
| 519 |
if (empty($rows)) {
|
| 520 |
$rows[] = array(array('data' => t('No Header Image nodes assigned to this block.'), 'colspan' => '3'));
|
| 521 |
}
|
| 522 |
$header = array(t('Node'), t('Weight'), array('data' => t('Operation'), 'colspan' => '1'));
|
| 523 |
$output = theme('table', $header, $rows, array('id' => 'imageblock'));
|
| 524 |
|
| 525 |
// Pack the tabel in form as markup text.
|
| 526 |
$form['headerimage']['description'] = array(
|
| 527 |
'#prefix' => '<p>',
|
| 528 |
'#value' => t('The table below contains the nodes which will be displayed in this block when there condition evaluates to true.'),
|
| 529 |
'#suffix' => '</p>',
|
| 530 |
);
|
| 531 |
|
| 532 |
$form['headerimage']['table'] = array(
|
| 533 |
'#prefix' => '<div>',
|
| 534 |
'#value' => $output,
|
| 535 |
'#suffix' => '</div>',
|
| 536 |
);
|
| 537 |
$form['headerimage']['random_fallback'] = array(
|
| 538 |
'#type' => 'checkbox',
|
| 539 |
'#title' => t('Enable a random fallback selection'),
|
| 540 |
'#default_value' => variable_get('headerimage_block_'. $delta .'_random_fallback', 0),
|
| 541 |
'#description' => t('If no node is selected by the conditions, select a random node from those assigned to this block.'),
|
| 542 |
);
|
| 543 |
return $form;
|
| 544 |
}
|
| 545 |
|
| 546 |
/**
|
| 547 |
* Implementation of hook_nodeapi
|
| 548 |
*/
|
| 549 |
function headerimage_nodeapi(&$node, $op) {
|
| 550 |
if (!empty($node->type) && in_array($node->type, variable_get('headerimage_node_type', array()), true)) {
|
| 551 |
switch ($op) {
|
| 552 |
case 'update':
|
| 553 |
db_query("DELETE from {headerimage} WHERE nid = %d", $node->nid);
|
| 554 |
case 'insert':
|
| 555 |
// Pack all conditions into one array
|
| 556 |
$conditions = variable_get(headerimage_condition_types, array('nid' => 'nid'));
|
| 557 |
if (!empty($conditions)) {
|
| 558 |
foreach ($conditions as $condition) {
|
| 559 |
if ($condition != '0') {
|
| 560 |
$name = 'headerimage_condition_'. $condition;
|
| 561 |
$conditions[$condition] = $node->$name;
|
| 562 |
}
|
| 563 |
}
|
| 564 |
}
|
| 565 |
db_query("INSERT INTO {headerimage} (nid, block, weight, conditions)
|
| 566 |
VALUES (%d, %d, %d, '%s')", $node->nid, $node->headerimage_block, $node->headerimage_weight, serialize($conditions));
|
| 567 |
break;
|
| 568 |
case 'prepare':
|
| 569 |
// Load data from database if node is edited
|
| 570 |
$result = db_fetch_object(db_query("SELECT * from {headerimage} where nid = %d", $node->nid));
|
| 571 |
$node->headerimage_block = $result->block;
|
| 572 |
$node->headerimage_weight = $result->weight;
|
| 573 |
$conditions = unserialize($result->conditions);
|
| 574 |
if (!empty($conditions)) {
|
| 575 |
foreach ($conditions as $condition => $value) {
|
| 576 |
$name = 'headerimage_condition_'. $condition;
|
| 577 |
$node->$name = $value;
|
| 578 |
}
|
| 579 |
}
|
| 580 |
break;
|
| 581 |
case 'delete':
|
| 582 |
db_query("DELETE from {headerimage} WHERE nid = %d", $node->nid);
|
| 583 |
break;
|
| 584 |
}
|
| 585 |
}
|
| 586 |
}
|
| 587 |
|
| 588 |
/**
|
| 589 |
* Implementation of hook_help
|
| 590 |
*/
|
| 591 |
function headerimage_help($path, $arg) {
|
| 592 |
switch ($path) {
|
| 593 |
case 'admin/help#headerimage':
|
| 594 |
// Determine the status of the installation
|
| 595 |
$type_is_set = count(variable_get('headerimage_node_type', array())) ? t('(DONE)') : t('(TO DO)');
|
| 596 |
$block_is_created = count(headerimage_get_blocks()) ? t('(DONE)') : t('(TO DO)');
|
| 597 |
foreach (headerimage_get_blocks() as $delta => $name) {
|
| 598 |
$block_has_nodes = db_result(db_query("SELECT nid FROM {headerimage} WHERE block = %d", $delta));
|
| 599 |
if ($block_has_nodes) break;
|
| 600 |
}
|
| 601 |
$block_has_nodes = $block_has_nodes ? t('(DONE)') : t('(TO DO)');
|
| 602 |
foreach (headerimage_get_blocks() as $delta => $name) {
|
| 603 |
$block_in_region = db_result(db_query("SELECT region FROM {blocks} WHERE module = '%s' AND theme = '%s' AND delta = %d", 'headerimage', variable_get('theme_default', 'garland'), $delta));
|
| 604 |
if ($block_in_region) break;
|
| 605 |
}
|
| 606 |
$block_in_region = $block_in_region ? t('(DONE)') : t('(TO DO)');
|
| 607 |
|
| 608 |
$output = "<p>". t('Header Image allows you to to display an image on selected pages. It can display one image at the front page, a different one at FAQ pages and yet another at all other pages.') ."</p>\n";
|
| 609 |
$output .= "<p>". t('Visibility of each image, included in a node, can be determined by node ID, path, taxonomy, book, node type or custom PHP code. Header Image uses an arbitrary node type.') ."</p>\n";
|
| 610 |
$output .= "<p>". t('Multiple images (nodes) can be displayed in one block, with each image having its own conditions. Using a weight per node the order of selection can be controlled.') ."</p>\n";
|
| 611 |
$output .= "<h2>". t('Installation') ."</h2>\n";
|
| 612 |
$output .= "<p>". t('<ol>
|
| 613 |
<li>Set up <a href="!permissions">permissions</a>.</li>
|
| 614 |
<li>Optionally <a href="!create_node_type">create a node type</a> which will contain the header image.</li>
|
| 615 |
<li><a href="!set_node_type">Select a node type</a> to be used as Header Image. %status_type</li>
|
| 616 |
<li><a href="!create_header_image_block">Create a Header Image block</a>. %status_create</li>
|
| 617 |
<li>Create Header Image nodes. Select a Header Image block and set display conditions for each node. %status_assign</li>
|
| 618 |
<li><a href="!assign_header_image_block">Assign the Header Image block</a> to a region. %status_region</li>
|
| 619 |
</ol>', array(
|
| 620 |
'!permissions' => url('admin/user/permissions'),
|
| 621 |
'!create_node_type' => url('admin/content/types/add'),
|
| 622 |
'!set_node_type' => url('admin/settings/headerimage/settings'),
|
| 623 |
'%status_type' => $type_is_set,
|
| 624 |
'!create_header_image_block' => url('admin/settings/headerimage'),
|
| 625 |
'%status_create' => $block_is_created,
|
| 626 |
'!assign_header_image_block' => url('admin/build/block'),
|
| 627 |
'%status_assign' => $block_has_nodes,
|
| 628 |
'%status_region' => $block_in_region,
|
| 629 |
)
|
| 630 |
) ."</p>\n";
|
| 631 |
$output .= "<p>". t('For more information please read the <a href="@handbook">configuration and customization handbook</a> on Header Image.', array('@handbook' => 'http://drupal.org/node/201426/')) ."</p>\n";
|
| 632 |
return $output;
|
| 633 |
}
|
| 634 |
}
|
| 635 |
|
| 636 |
/**
|
| 637 |
* Return all condition types available
|
| 638 |
*
|
| 639 |
* @return
|
| 640 |
* array of condition types
|
| 641 |
*/
|
| 642 |
function headerimage_get_condition_types() {
|
| 643 |
return array(
|
| 644 |
'nid' => t('Node ID'),
|
| 645 |
'url' => t('URL'),
|
| 646 |
'taxonomy' => t('Taxonomy'),
|
| 647 |
'book' => t('Book'),
|
| 648 |
'nodetype' => t('Node type'),
|
| 649 |
'php' => t('PHP'),
|
| 650 |
);
|
| 651 |
}
|
| 652 |
|
| 653 |
/**
|
| 654 |
* Return all Header Image blocks
|
| 655 |
*
|
| 656 |
* @return
|
| 657 |
* array of Header Image blocks
|
| 658 |
*/
|
| 659 |
function headerimage_get_blocks() {
|
| 660 |
static $blocks;
|
| 661 |
|
| 662 |
if (!isset($blocks)) {
|
| 663 |
$blocks = array();
|
| 664 |
$result = db_query("SELECT * FROM {headerimage_block}");
|
| 665 |
while ($block = db_fetch_object($result)) {
|
| 666 |
if (!empty($block)) $blocks[$block->delta] = $block->title;
|
| 667 |
}
|
| 668 |
}
|
| 669 |
|
| 670 |
return $blocks;
|
| 671 |
}
|
| 672 |
|
| 673 |
/**
|
| 674 |
* Implementation of hook_theme()
|
| 675 |
*/
|
| 676 |
function headerimage_theme() {
|
| 677 |
return array(
|
| 678 |
'headerimage_block' => array(
|
| 679 |
'template' => 'headerimage-block',
|
| 680 |
'arguments' => array('node' => NULL, 'teaser' => NULL),
|
| 681 |
),
|
| 682 |
);
|
| 683 |
}
|
| 684 |
|
| 685 |
/**
|
| 686 |
* Process variables to format the headerimage block.
|
| 687 |
*
|
| 688 |
* $variables contains:
|
| 689 |
* - $node
|
| 690 |
* - $teaser: TRUE = display node as teaser; FALSE = display full node
|
| 691 |
*
|
| 692 |
* @see headerimage-block.tpl.php
|
| 693 |
*/
|
| 694 |
function template_preprocess_headerimage_block(&$variables) {
|
| 695 |
$node = $variables['node'];
|
| 696 |
$teaser = $variables['teaser'];
|
| 697 |
|
| 698 |
$variables['unpublished'] = !$node->status;
|
| 699 |
if ($teaser && isset($node->teaser)) {
|
| 700 |
$variables['content'] = $node->teaser;
|
| 701 |
}
|
| 702 |
else {
|
| 703 |
$variables['content'] = $node->body;
|
| 704 |
}
|
| 705 |
}
|