| 1 |
<?php
|
| 2 |
// $Id: about_this_node.module,v 1.3 2008/04/16 14:57:29 toddnienkerk Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Displays a block containing a node's information.
|
| 7 |
*/
|
| 8 |
|
| 9 |
|
| 10 |
/**
|
| 11 |
* Implementation of hook_menu()
|
| 12 |
*/
|
| 13 |
function about_this_node_menu() {
|
| 14 |
$items = array();
|
| 15 |
|
| 16 |
$items['admin/content/aboutthisnode'] = array(
|
| 17 |
'title' => t('About this node'),
|
| 18 |
'page callback' => 'drupal_get_form',
|
| 19 |
'page arguments' => array('about_this_node_admin_settings'),
|
| 20 |
'type' => MENU_NORMAL_ITEM,
|
| 21 |
'access arguments' => array('administer about this node'),
|
| 22 |
);
|
| 23 |
|
| 24 |
return $items;
|
| 25 |
}
|
| 26 |
|
| 27 |
|
| 28 |
/**
|
| 29 |
* Implementation of hook_perm()
|
| 30 |
*/
|
| 31 |
function about_this_node_perm() {
|
| 32 |
return array('administer about this node', 'view about this node block');
|
| 33 |
}
|
| 34 |
|
| 35 |
|
| 36 |
/**
|
| 37 |
* Implementation of hook_theme()
|
| 38 |
*/
|
| 39 |
function about_this_node_theme() {
|
| 40 |
return array(
|
| 41 |
'about_this_node_node' => array(
|
| 42 |
'arguments' => array('form' => NULL),
|
| 43 |
),
|
| 44 |
);
|
| 45 |
}
|
| 46 |
|
| 47 |
|
| 48 |
/**
|
| 49 |
* Implementation of hook_admin_settings()
|
| 50 |
*/
|
| 51 |
function about_this_node_admin_settings() {
|
| 52 |
$form['about_this_node_nodetypes'] = array(
|
| 53 |
'#type' => 'checkboxes',
|
| 54 |
'#title' => t('View information about these node types'),
|
| 55 |
'#options' => node_get_types('names'),
|
| 56 |
'#default_value' => variable_get('about_this_node_nodetypes', array('story')),
|
| 57 |
'#description' => t('The about this node block will appear when viewing these node types.'),
|
| 58 |
);
|
| 59 |
|
| 60 |
return system_settings_form($form);
|
| 61 |
}
|
| 62 |
|
| 63 |
|
| 64 |
/**
|
| 65 |
* Implementation of hook_block()
|
| 66 |
*/
|
| 67 |
function about_this_node_block($op = 'list', $delta = 0, $edit = array()) {
|
| 68 |
switch ($op) {
|
| 69 |
|
| 70 |
// Displays block name in admin/build/blocks
|
| 71 |
case 'list':
|
| 72 |
$blocks[0]['info'] = t('About This Node');
|
| 73 |
return $blocks;
|
| 74 |
|
| 75 |
// Generates block output
|
| 76 |
case 'view':
|
| 77 |
if (user_access('view about this node block')) {
|
| 78 |
$block['subject'] = t('About This Node');
|
| 79 |
$block['content'] = about_this_node_block_contents();
|
| 80 |
return $block;
|
| 81 |
}
|
| 82 |
}
|
| 83 |
}
|
| 84 |
|
| 85 |
|
| 86 |
/**
|
| 87 |
* Prepares the block and routes it to the appropriate theme function
|
| 88 |
*/
|
| 89 |
function about_this_node_block_contents() {
|
| 90 |
$output = '';
|
| 91 |
$id = arg(1);
|
| 92 |
$node_info = array();
|
| 93 |
|
| 94 |
if (is_numeric($id)) {
|
| 95 |
$pagetype = arg(0);
|
| 96 |
|
| 97 |
switch ($pagetype) {
|
| 98 |
|
| 99 |
// This version of about_this_node only displays a block for node page types (i.e., not user pages, etc.)
|
| 100 |
case 'node':
|
| 101 |
$node = node_load($id);
|
| 102 |
$allowed_nodetypes = variable_get('about_this_node_nodetypes', array('story'));
|
| 103 |
|
| 104 |
// Conditional will return 0 (FALSE) if it isn't an allowed node type
|
| 105 |
if ($allowed_nodetypes[$node->type]) {
|
| 106 |
$output .= theme('about_this_node_node', about_this_node_get_info($node), $node);
|
| 107 |
}
|
| 108 |
break;
|
| 109 |
|
| 110 |
}
|
| 111 |
}
|
| 112 |
|
| 113 |
return $output;
|
| 114 |
}
|
| 115 |
|
| 116 |
|
| 117 |
/**
|
| 118 |
* Gather information about the node and return it as a keyed array
|
| 119 |
*/
|
| 120 |
function about_this_node_get_info($node) {
|
| 121 |
$node_info = array();
|
| 122 |
|
| 123 |
// Node ID
|
| 124 |
$node_info['node_id'] = array(
|
| 125 |
'label' => 'Node ID (NID):',
|
| 126 |
'value' => $node->nid,
|
| 127 |
);
|
| 128 |
|
| 129 |
// Node type
|
| 130 |
$node_info['node_type'] = array(
|
| 131 |
'label' => 'Node type:',
|
| 132 |
'value' => node_get_types('name', $node),
|
| 133 |
);
|
| 134 |
|
| 135 |
// Gather author info
|
| 136 |
$author_uid = $node->uid;
|
| 137 |
$author = '';
|
| 138 |
$author_output = '';
|
| 139 |
if ($author_uid == '0') { // Check if author is anonymous
|
| 140 |
$author_output = variable_get('anonymous', 'Anonymous');
|
| 141 |
}
|
| 142 |
else {
|
| 143 |
$author = user_load(array('uid' => $author_uid));
|
| 144 |
$author_output = l($author->name, 'user/' . $author_uid);
|
| 145 |
}
|
| 146 |
|
| 147 |
// Author name/time
|
| 148 |
$node_info['created'] = array(
|
| 149 |
'label' => 'Created:',
|
| 150 |
'value' => array(
|
| 151 |
'created_on' => array(
|
| 152 |
'label' => 'on',
|
| 153 |
'value' => $node->created,
|
| 154 |
),
|
| 155 |
'created_by' => array(
|
| 156 |
'label' => 'by',
|
| 157 |
'value' => $author_output,
|
| 158 |
),
|
| 159 |
),
|
| 160 |
);
|
| 161 |
|
| 162 |
// Gather last updated author info
|
| 163 |
$update_author_uid = db_result(db_query('SELECT nr.uid FROM {node_revisions} nr INNER JOIN {node} n ON n.vid = nr.vid WHERE n.nid = %d', $node->nid));
|
| 164 |
$update_author = '';
|
| 165 |
$update_author_output = '';
|
| 166 |
if ($update_author_uid == '0') { // Check if author is anonymous
|
| 167 |
$update_author_output = variable_get('anonymous', 'Anonymous');
|
| 168 |
}
|
| 169 |
else {
|
| 170 |
$update_author = user_load(array('uid' => $update_author_uid));
|
| 171 |
$update_author_output = l($update_author->name, 'user/' . $update_author_uid);
|
| 172 |
}
|
| 173 |
|
| 174 |
// Last updated author name/time
|
| 175 |
$node_info['updated'] = array(
|
| 176 |
'label' => 'Last updated:',
|
| 177 |
);
|
| 178 |
|
| 179 |
// Check if node has never been updated
|
| 180 |
if ($node->created == $node->changed) {
|
| 181 |
$node_info['updated']['value'] = 'Never';
|
| 182 |
}
|
| 183 |
else {
|
| 184 |
$node_info['updated']['value'] = array(
|
| 185 |
'updated_on' => array(
|
| 186 |
'label' => 'on',
|
| 187 |
'value' => $node->changed,
|
| 188 |
),
|
| 189 |
'updated_by' => array(
|
| 190 |
'label' => 'by',
|
| 191 |
'value' => $update_author_output,
|
| 192 |
),
|
| 193 |
);
|
| 194 |
}
|
| 195 |
|
| 196 |
// Published status
|
| 197 |
$node_info['published'] = array(
|
| 198 |
'label' => 'Published:',
|
| 199 |
);
|
| 200 |
if ($node->status == 1) {
|
| 201 |
$node_info['published']['value'] = 'Yes';
|
| 202 |
}
|
| 203 |
else {
|
| 204 |
$node_info['published']['value'] = 'No';
|
| 205 |
}
|
| 206 |
|
| 207 |
// Promoted to front page status
|
| 208 |
$node_info['promoted'] = array(
|
| 209 |
'label' => 'Promoted:',
|
| 210 |
);
|
| 211 |
if ($node->promote == 1) {
|
| 212 |
$node_info['promoted']['value'] = 'Yes';
|
| 213 |
}
|
| 214 |
else {
|
| 215 |
$node_info['promoted']['value'] = 'No';
|
| 216 |
}
|
| 217 |
|
| 218 |
// Sticky status
|
| 219 |
$node_info['stickied'] = array(
|
| 220 |
'label' => 'Stickied:',
|
| 221 |
);
|
| 222 |
if ($node->sticky == 1) {
|
| 223 |
$node_info['stickied']['value'] = 'Yes';
|
| 224 |
}
|
| 225 |
else {
|
| 226 |
$node_info['stickied']['value'] = 'No';
|
| 227 |
}
|
| 228 |
|
| 229 |
// Commenting status
|
| 230 |
$node_info['commenting'] = array(
|
| 231 |
'label' => 'Commenting:',
|
| 232 |
);
|
| 233 |
if (module_exists('comment')) {
|
| 234 |
if ($node->comment == 0) {
|
| 235 |
$node_info['commenting']['value'] = 'Disabled';
|
| 236 |
}
|
| 237 |
else if ($node->comment == 1) {
|
| 238 |
$node_info['commenting']['value'] = 'Read only';
|
| 239 |
}
|
| 240 |
else {
|
| 241 |
$node_info['commenting']['value'] = 'Read/Write';
|
| 242 |
}
|
| 243 |
}
|
| 244 |
else {
|
| 245 |
$node_info['commenting']['value'] = 'Module not enabled';
|
| 246 |
}
|
| 247 |
|
| 248 |
return $node_info;
|
| 249 |
}
|
| 250 |
|
| 251 |
|
| 252 |
/**
|
| 253 |
* Prepare keyed array for theming as an item-list
|
| 254 |
*/
|
| 255 |
function about_this_node_prepare_info($node_info, $date_format = 'small') {
|
| 256 |
$node_info_prepped = array();
|
| 257 |
|
| 258 |
// Format some dates
|
| 259 |
if (isset($node_info['created']['value']['created_on']['value'])) {
|
| 260 |
$node_info['created']['value']['created_on']['value'] = format_date($node_info['created']['value']['created_on']['value'], $date_format);
|
| 261 |
}
|
| 262 |
if (isset($node_info['updated']['value']['updated_on']['value'])) {
|
| 263 |
$node_info['updated']['value']['updated_on']['value'] = format_date($node_info['updated']['value']['updated_on']['value'], $date_format);
|
| 264 |
}
|
| 265 |
|
| 266 |
foreach ($node_info as $key => $item) {
|
| 267 |
$node_info_prepped[$key] = array('data' => '<span class="aboutthisnode-label">' . $item['label'] . '</span> ');
|
| 268 |
|
| 269 |
// If this item has children
|
| 270 |
if (is_array($item['value'])) {
|
| 271 |
$node_info_prepped[$key]['children'] = about_this_node_prepare_info($item['value']);
|
| 272 |
}
|
| 273 |
else {
|
| 274 |
$node_info_prepped[$key]['data'] .= $item['value'];
|
| 275 |
}
|
| 276 |
}
|
| 277 |
|
| 278 |
return $node_info_prepped;
|
| 279 |
}
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
/**
|
| 284 |
* Theme function that build the block's output for nodes
|
| 285 |
*/
|
| 286 |
function theme_about_this_node_node($node_info, $node) {
|
| 287 |
return theme('item_list', about_this_node_prepare_info($node_info, 'small'));
|
| 288 |
}
|