headerimage.module in Header image 5
Same filename and directory in other branches
headerimage.module Conditionally display an node in a block.
WHISH LIST: create CCK node type at install
File
headerimage.moduleView source
<?php
/**
* @file headerimage.module
* Conditionally display an node in a block.
*
* WHISH LIST:
* create CCK node type at install
*/
/**
* Implementation of hook_menu()
*/
function headerimage_menu($may_cache) {
$admin_access = user_access('administer Header Image');
$items = array();
if ($may_cache) {
$items[] = array(
'title' => t('Header Image'),
'description' => t('Control the Header Image.'),
'callback' => 'drupal_get_form',
'callback arguments' => array(
'headerimage_settings_block_add',
),
'path' => 'admin/settings/headerimage',
'access' => $admin_access,
'type' => MENU_NORMAL_ITEM,
);
$items[] = array(
'title' => t('List'),
'path' => 'admin/settings/headerimage/list',
'callback' => 'drupal_get_form',
'callback arguments' => array(
'headerimage_settings_block_add',
),
'access' => $admin_access,
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items[] = array(
'title' => t('Settings'),
'path' => 'admin/settings/headerimage/settings',
'callback' => 'drupal_get_form',
'callback arguments' => array(
'headerimage_settings_form',
),
'access' => $admin_access,
'type' => MENU_LOCAL_TASK,
'weight' => 10,
);
}
else {
$block = arg(4);
$items[] = array(
'title' => t('Edit'),
'path' => 'admin/settings/headerimage/edit/' . $block,
'callback' => 'drupal_get_form',
'callback arguments' => array(
'headerimage_settings_block_edit',
$block,
),
'access' => $admin_access,
'type' => MENU_CALLBACK,
);
$items[] = array(
'title' => t('Delete'),
'path' => 'admin/settings/headerimage/delete/' . $block,
'callback' => 'drupal_get_form',
'callback arguments' => array(
'headerimage_block_confirm_delete',
$block,
),
'access' => $admin_access,
'type' => MENU_CALLBACK,
);
}
return $items;
}
/**
* Overview of all image blocks
*/
function headerimage_settings_block_add() {
$blocks = headerimage_get_blocks();
// Table of image sets
$rows = array();
foreach ($blocks as $delta => $block) {
$rows[] = array(
'name' => check_plain($block),
'edit' => l(t('Edit'), "admin/settings/headerimage/edit/{$delta}"),
'delete' => l(t('Delete'), "admin/settings/headerimage/delete/{$delta}"),
'block' => l(t('Configure block'), "admin/build/block/configure/headerimage/{$delta}"),
);
}
if (empty($rows)) {
$rows[] = array(
array(
'data' => t('No Header Image blocks available.'),
'colspan' => '4',
),
);
}
$header = array(
t('Name'),
array(
'data' => t('Operation'),
'colspan' => '3',
),
);
$output = theme('table', $header, $rows, array(
'id' => 'imageblock',
));
$form = array();
$form['list'] = array(
'#type' => 'fieldset',
'#title' => t('Header Image blocks'),
);
$form['list']['table'] = array(
'#prefix' => '<div>',
'#value' => $output,
'#suffix' => '</div>',
);
$form['block'] = array(
'#type' => 'fieldset',
'#title' => t('Add Header Image block'),
);
$form['block']['title'] = array(
'#type' => 'textfield',
'#title' => t('Block title'),
'#description' => t('A block with this same name will be created. Header Image nodes assigned to this block will be displayed in it.'),
'#default_value' => '',
'#required' => true,
);
$form['block']['op'] = array(
'#type' => 'submit',
'#value' => t('Add block'),
);
return $form;
}
function headerimage_settings_block_add_validate($form_id, $form_values) {
$blocks = headerimage_get_blocks();
if (!empty($blocks)) {
// Check if name is unique
if (in_array($form_values['title'], $blocks)) {
form_set_error('', t('Header Image block %s already exists. Please use a different name.', array(
'%s' => $form_values['title'],
)));
}
}
}
function headerimage_settings_block_add_submit($form_id, $form_values) {
db_query("INSERT INTO {headerimage_block} (title) VALUES ('%s')", $form_values['title']);
drupal_set_message(t('Header Image block %s added.', array(
'%s' => $form_values['title'],
)));
}
/**
* Header Image settings form
*/
function headerimage_settings_form() {
$form = array();
$nodes = node_get_types();
foreach ($nodes as $node) {
$nodetype[$node->type] = check_plain($node->name);
}
$form['headerimage_node_type'] = array(
'#type' => 'checkboxes',
'#title' => t('Node type'),
'#description' => t('The node type(s) that can be used with Header Image module.'),
'#default_value' => variable_get('headerimage_node_type', array()),
'#options' => $nodetype,
'#multiple' => true,
'#required' => true,
);
$form['headerimage_condition_types'] = array(
'#type' => 'checkboxes',
'#title' => t('Condition types'),
'#description' => t('Types of conditions by which nodes can be selected to be displayed.'),
'#default_value' => variable_get('headerimage_condition_types', array(
'nid',
)),
'#options' => headerimage_get_condition_types(),
'#required' => true,
);
$form['headerimage_teaser'] = array(
'#type' => 'checkbox',
'#title' => t('Teaser'),
'#description' => t('Display the Header Image node as teaser or full node.'),
'#default_value' => variable_get('headerimage_teaser', true),
);
return system_settings_form($form);
}
/**
* Edit block data form
*/
function headerimage_settings_block_edit($delta) {
$blocks = headerimage_get_blocks();
$form = array();
$form['delta'] = array(
'#type' => 'hidden',
'#value' => $delta,
);
$form['title'] = array(
'#type' => 'textfield',
'#title' => t('Block title'),
'#description' => t('The block name must be unique.'),
'#default_value' => $blocks[$delta],
'#required' => true,
);
$form['op'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
function headerimage_settings_block_edit_validate($form_id, $form_values) {
$blocks = headerimage_get_blocks();
// Remove current blockname to prevent false error
unset($blocks[$form_values['delta']]);
if (!empty($blocks)) {
// Check if name is unique
if (in_array($form_values['title'], $blocks)) {
form_set_error('', t('Header Image block %s already exists. Please use a different name.', array(
'%s' => $form_values['title'],
)));
}
}
}
function headerimage_settings_block_edit_submit($form_id, $form_values) {
db_query("UPDATE {headerimage_block} SET title = '%s' WHERE delta = %d", $form_values['title'], $form_values['delta']);
drupal_set_message(t('Header Image block updated.'));
return 'admin/settings/headerimage';
}
/**
* Delete block form
*/
function headerimage_block_confirm_delete($delta) {
$blocks = headerimage_get_blocks();
$form['delta'] = array(
'#type' => 'value',
'#value' => $delta,
);
return confirm_form($form, t('Are you sure you want to delete the block %title?', array(
'%title' => $blocks[$delta],
)), 'admin/settings/headerimage', t('All Header Image assignments to this block will be deleted, the nodes will not be deleted. This action cannot be undone.'), t('Delete'), t('Cancel'));
}
function headerimage_block_confirm_delete_submit($form_id, $form_values) {
db_query("DELETE from {headerimage_block} WHERE delta = %d", $form_values['delta']);
db_query("DELETE from {headerimage} WHERE block = %d", $form_values['delta']);
drupal_set_message(t('Header Image block deleted'));
return 'admin/settings/headerimage';
}
/**
* Implementation of hook_perm().
*/
function headerimage_perm() {
return array(
'administer Header Image',
'maintain display conditions',
'view header image',
);
}
/**
* Implementation of hook_block().
*/
function headerimage_block($op = 'list', $delta = 0, $edit = array()) {
switch ($op) {
case 'list':
$headerimage_blocks = headerimage_get_blocks();
foreach ($headerimage_blocks as $key => $name) {
$blocks[$key]['info'] = check_plain($name);
}
return $blocks;
break;
case 'configure':
return headerimage_block_configure($delta);
case 'save':
variable_set('headerimage_block_' . $delta . '_random_fallback', $edit['random_fallback']);
break;
case 'view':
if (user_access('view header image')) {
// select node from nodes assigned to this block
$nid = headerimage_select_node($delta);
$teaser = variable_get('headerimage_teaser', true);
// prepare block output
if (!empty($nid)) {
$node = node_load($nid);
$node = node_prepare($node, $teaser);
//use node teaser view
// mimic node_view
$node = node_build_content($node, $teaser, false);
$content = drupal_render($node->content);
if ($teaser) {
$node->teaser = $content;
unset($node->body);
}
else {
$node->body = $content;
unset($node->teaser);
}
node_invoke_nodeapi($node, 'alter', $teaser, $page);
$block['content'] = theme('headerimage_block', $node, $teaser);
}
return $block;
}
break;
}
}
/**
* Select a node to be displayed in the block
*
* Node selection by (1)weight and (2)condition.
* If multiple conditions are present, any true condition will select the node.
* If no node is selected by the conditions and random fallback selection is
* enabled for the block, one of the available nodes will be selected at random.
*
* @param $block
* The headerimage block number ($delta)
*
* @return
* nid of the selected node
* null: no node selected
*/
function headerimage_select_node($block) {
$result = db_query("SELECT nid, conditions FROM {headerimage} WHERE block = %d ORDER BY weight, nid ASC", $block);
while ($header_image = db_fetch_object($result)) {
$conditions = unserialize($header_image->conditions);
$match = false;
// Store the nid in an array for the random selection fallback option.
$block_nids[] = $header_image->nid;
$selected_types = variable_get(headerimage_condition_types, array(
'nid' => 'nid',
));
foreach ($conditions as $type => $condition) {
if (!empty($condition) && !empty($selected_types[$type])) {
switch ($type) {
case 'nid':
$match = headerimage_eval_nid($condition);
break;
case 'url':
$match = headerimage_eval_url($condition);
break;
case 'taxonomy':
$match = headerimage_eval_taxonomy($condition);
break;
case 'book':
$match = headerimage_eval_book($condition);
break;
case 'nodetype':
$match = headerimage_eval_nodetype($condition);
break;
case 'php':
$match = drupal_eval($condition);
break;
}
}
if ($match) {
break;
}
}
if ($match) {
break;
}
}
if ($match) {
return $header_image->nid;
}
elseif (variable_get('headerimage_block_' . $block . '_random_fallback', 0) && count($block_nids)) {
return $block_nids[array_rand($block_nids)];
}
}
/**
* Evaluate nid condition
*
* Checks if current page is in list of nids
*
* @param $condition
* comma separated list of nids
*
* @return
* true: current page is in $condition
* false: if not
* null: current page is not a node
*/
function headerimage_eval_nid($condition) {
if (arg(0) == 'node' && is_numeric(arg(1))) {
$nids = explode(',', $condition);
$match = in_array(arg(1), $nids);
}
return $match;
}
/**
* Evaluate url condition
*
* Check if current page is in selected paths
*
* @param $condition
* text with paths or '<frontpage>'. May contain wild cards.
*
* @return
* true: current page matches one of the $condition paths;
* false: if not
*/
function headerimage_eval_url($condition) {
$path = drupal_get_path_alias($_GET['q']);
$regexp = '/^(' . preg_replace(array(
'/(\\r\\n?|\\n)/',
'/\\\\\\*/',
'/(^|\\|)\\\\<front\\\\>($|\\|)/',
), array(
'|',
'.*',
'\\1' . preg_quote(variable_get('site_frontpage', 'node'), '/') . '\\2',
), preg_quote($condition, '/')) . ')$/';
// Compare with the internal and path alias (if any).
$match = preg_match($regexp, $path);
if ($match) {
return true;
}
if ($path != $_GET['q']) {
$match = preg_match($regexp, $_GET['q']);
}
return $match;
}
/**
* Evaluate taxonomy condition
*
* Check if current page has selected taxonomy terms
*
* @param $condition
* array of taxonomy term tid's
*
* @return
* true: current page contains one of $condition's tags
* false: if not
* null: current page is not a node
*/
function headerimage_eval_taxonomy($condition) {
if (arg(0) == 'node' && is_numeric(arg(1))) {
$node = node_load(arg(1));
foreach (array_keys($node->taxonomy) as $key) {
if (isset($condition[$key])) {
return true;
}
}
return false;
}
return null;
}
/**
* Evaluate book condition
*
* Return true if current node is a page of the book(s) selected in the condition
*
* @param $condition
* array of book root nid's
*
* @return
* true: current node is a page in $condition's books
* false: if not
* null: current page is not a node
*/
function headerimage_eval_book($condition) {
if (arg(0) == 'node' && is_numeric(arg(1))) {
// check if current node is one of the condition pages (book roots)
$match = in_array(arg(1), $condition);
if ($match) {
return true;
}
// check if root of current page is one of the condition pages
$book = book_location(node_load(arg(1)));
$match = in_array($book[0]->nid, $condition);
// $book[0] is book root
}
return $match;
}
/**
* Evaluate node type condition
*
* Return true if type of current node is selected
*
* @param $condition
* array of selected node types
*
* @return
* true: current node type is selected
* false: if not
* null: current page is not a node
*/
function headerimage_eval_nodetype($condition) {
if (arg(0) == 'node' && is_numeric(arg(1))) {
$node = node_load(arg(1));
$match = in_array($node->type, $condition);
}
return $match;
}
/**
* Implementation of hook_form_alter
*/
function headerimage_form_alter($form_id, &$form) {
if (user_access('maintain display conditions') && in_array(str_replace('_node_form', '', $form['form_id']['#value']), variable_get('headerimage_node_type', array()), true)) {
$form['headerimage'] = array(
'#type' => 'fieldset',
'#title' => t('Display conditions'),
'#description' => t('This node is displayed in a Header Image block when one of the conditions below are evaluated true.'),
'#collapsible' => true,
'#collapsed' => false,
'#weight' => -2,
);
$form['headerimage']['headerimage_block'] = array(
'#type' => 'select',
'#title' => t('Block name'),
'#description' => t('The block in which this node is displayed.'),
'#options' => headerimage_get_blocks(),
'#default_value' => $form['#node']->headerimage_block,
'#required' => true,
'#multiple' => false,
);
$form['headerimage']['headerimage_weight'] = array(
'#type' => 'weight',
'#title' => t('Condition weight'),
'#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.'),
'#default_value' => $form['#node']->headerimage_weight,
'#delta' => 10,
);
$condition_types = variable_get(headerimage_condition_types, array(
'nid' => 'nid',
));
if (!empty($condition_types)) {
foreach ($condition_types as $type) {
if ($type != '0') {
$all_types = headerimage_get_condition_types();
$title = t("@name condition", array(
'@name' => $all_types[$type],
));
$name = 'headerimage_condition_' . $type;
switch ($type) {
case 'nid':
$form['headerimage'][$name] = array(
'#type' => 'textarea',
'#title' => $title,
'#description' => t("Enter node id's separated by comma's."),
'#default_value' => $form['#node']->{$name},
'#rows' => 4,
);
break;
case 'url':
$form['headerimage'][$name] = array(
'#type' => 'textarea',
'#title' => $title,
'#description' => t("Enter one page 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>',
)),
'#default_value' => $form['#node']->{$name},
'#rows' => 4,
);
break;
case 'taxonomy':
$options = array();
if (module_exists('taxonomy')) {
$vocab = taxonomy_get_vocabularies();
if (!empty($vocab)) {
foreach ($vocab as $v) {
$taxonomy = taxonomy_get_tree($v->vid);
if (!empty($taxonomy)) {
foreach ($taxonomy as $tag) {
$options[$tag->tid] = check_plain($tag->name);
}
}
}
}
if (!empty($options)) {
$description = t("One tag or multiple tags can be selected.");
}
else {
drupal_set_message(t("No vocabulary or tags defined. Please create vocabulary and tags or remove taxonomy from the !settings.", array(
'!settings' => l(t('Header Image settings'), 'admin/settings/headerimage/settings'),
)));
break;
}
$form['headerimage'][$name] = array(
'#type' => 'select',
'#title' => $title,
'#description' => $description,
'#default_value' => $form['#node']->{$name},
'#options' => $options,
'#multiple' => true,
);
}
else {
drupal_set_message(t("The taxonomy module is not enabled. Please enable the module or remove it from the !settings.", array(
'!settings' => l(t('Header Image settings'), 'admin/settings/headerimage/settings'),
)));
}
break;
case 'book':
if (module_exists('book')) {
$result = db_query(db_rewrite_sql('SELECT n.nid, n.title FROM {node} n INNER JOIN {book} b ON n.vid = b.vid WHERE n.status = 1 AND b.parent = 0 ORDER BY b.weight, n.title'));
$books = array();
while ($data = db_fetch_object($result)) {
$books[$data->nid] = check_plain($data->title);
}
if (count($books)) {
$description = t("One book or multiple books can be selected.");
}
else {
$description = t("No books defined. Please create a book before using it as a condition.");
}
$form['headerimage'][$name] = array(
'#type' => 'select',
'#title' => $title,
'#description' => $description,
'#default_value' => $form['#node']->{$name},
'#options' => $books,
'#multiple' => true,
);
}
else {
drupal_set_message(t("The book module is not enabled. Please enable the module or remove it from the !settings.", array(
'!settings' => l(t('Header Image settings'), 'admin/settings/headerimage/settings'),
)));
}
break;
case 'nodetype':
$nodes = node_get_types();
foreach ($nodes as $node) {
$nodetype[$node->type] = check_plain($node->name);
}
$form['headerimage'][$name] = array(
'#type' => 'select',
'#title' => $title,
'#description' => t('Select one or multiple node types'),
'#default_value' => $form['#node']->{$name},
'#options' => $nodetype,
'#multiple' => true,
);
break;
case 'php':
$form['headerimage'][$name] = array(
'#type' => 'textarea',
'#title' => $title,
'#description' => t("Enter PHP code between <?php ?> tags. Note that executing incorrect PHP-code can break your Drupal site."),
'#default_value' => $form['#node']->{$name},
'#rows' => 4,
);
break;
}
}
}
}
}
}
/**
* Return a form with a list of Header Image nodes
*
* Nodes assigned to Header Image block $delta are listed with weight and
* edit link
*
* @param $delta
* Header Image block delta
*
* @return
* form array
*/
function headerimage_block_configure($delta) {
$result = db_query("SELECT title, weight, n.nid FROM {headerimage} hi LEFT JOIN {node} n ON n.nid = hi.nid WHERE hi.block = %d ORDER BY weight, nid ASC", $delta);
// Table with image set details
$rows = array();
while ($data = db_fetch_object($result)) {
$rows[] = array(
'node' => check_plain($data->title),
'weight' => $data->weight,
'edit' => l(t('Edit'), "node/{$data->nid}/edit"),
);
}
if (empty($rows)) {
$rows[] = array(
array(
'data' => t('No Header Image nodes assigned to this block.'),
'colspan' => '3',
),
);
}
$header = array(
t('Node'),
t('Weight'),
array(
'data' => t('Operation'),
'colspan' => '1',
),
);
$output = theme('table', $header, $rows, array(
'id' => 'imageblock',
));
// Pack the tabel in form as markup text.
$form['headerimage']['description'] = array(
'#prefix' => '<p>',
'#value' => t('The table below contains the nodes which will be displayed in this block when there condition evaluates to true.'),
'#suffix' => '</p>',
);
$form['headerimage']['table'] = array(
'#prefix' => '<div>',
'#value' => $output,
'#suffix' => '</div>',
);
$form['headerimage']['random_fallback'] = array(
'#type' => 'checkbox',
'#title' => t('Enable a random fallback selection'),
'#default_value' => variable_get('headerimage_block_' . $delta . '_random_fallback', 0),
'#description' => t('If no node is selected by the conditions, select a random node from those assigned to this block.'),
);
return $form;
}
/**
* Implementation of hook_nodeapi
*/
function headerimage_nodeapi(&$node, $op) {
if (!empty($node->type) && in_array($node->type, variable_get('headerimage_node_type', array()), true)) {
switch ($op) {
case 'update':
db_query("DELETE from {headerimage} WHERE nid = %d", $node->nid);
case 'insert':
// Pack all conditions into one array
$conditions = variable_get(headerimage_condition_types, array(
'nid' => 'nid',
));
if (!empty($conditions)) {
foreach ($conditions as $condition) {
if ($condition != '0') {
$name = 'headerimage_condition_' . $condition;
$conditions[$condition] = $node->{$name};
}
}
}
db_query("INSERT INTO {headerimage} (nid, block, weight, conditions)\n VALUES (%d, %d, %d, '%s')", $node->nid, $node->headerimage_block, $node->headerimage_weight, serialize($conditions));
break;
case 'prepare':
// Load data from database if node is edited
$result = db_fetch_object(db_query("SELECT * from {headerimage} where nid = %d", $node->nid));
$node->headerimage_block = $result->block;
$node->headerimage_weight = $result->weight;
$conditions = unserialize($result->conditions);
if (!empty($conditions)) {
foreach ($conditions as $condition => $value) {
$name = 'headerimage_condition_' . $condition;
$node->{$name} = $value;
}
}
break;
case 'delete':
db_query("DELETE from {headerimage} WHERE nid = %d", $node->nid);
break;
}
}
}
/**
* Implementation of hook_help
*/
function headerimage_help($section) {
switch ($section) {
case 'admin/help#headerimage':
$output = "<p>" . t('Header Image module allows you to to display an image on selected pages. It can display one image on the front page, a different one on the FAQ pages and yet an other on the about us all remaining pages.') . "</p>\n";
$output .= "<p>" . t('Visibility of each image, included in a node, can be based on node id, path, taxonomy, book, node type or PHP code. Header Image uses an arbitrary node type.') . "</p>\n";
$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";
$output .= "<p>" . t('For more information please read the configuration and customization handbook <a href="@headerimage">Header Image page</a>.', array(
'@headerimage' => 'http://drupal.org/node/201426/',
)) . "</p>\n";
return $output;
}
}
/**
* Return all condition types available
*
* @return
* array of condition types
*/
function headerimage_get_condition_types() {
return array(
'nid' => t('Node ID'),
'url' => t('URL'),
'taxonomy' => t('Taxonomy'),
'book' => t('Book'),
'nodetype' => t('Node type'),
'php' => t('PHP'),
);
}
/**
* Return all Header Image blocks
*
* @return
* array of Header Image blocks
*/
function headerimage_get_blocks() {
static $blocks;
if (!isset($blocks)) {
$blocks = array();
$result = db_query("SELECT * FROM {headerimage_block}");
while ($block = db_fetch_object($result)) {
if (!empty($block)) {
$blocks[$block->delta] = $block->title;
}
}
}
return $blocks;
}
/**
* Default Header Image block theme
*
* @param $node
* rendered array of node in teaser view
*/
function theme_headerimage_block($node, $teaser = true) {
if (!$node->status) {
$output = '<div class="node-unpublished">';
}
if ($teaser && $node->teaser) {
$output .= $node->teaser;
}
else {
$output .= $node->body;
}
if (!$node->status) {
$output .= '</div>';
}
return $output;
}