| 1 |
<?php
|
| 2 |
// $Id: promote_blocks.module,v 1.4 2008/01/31 20:31:14 emackn Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Promote blocks module, Allows a node type to be marked as eligible to be promoted
|
| 7 |
* to the front page as a block.
|
| 8 |
*/
|
| 9 |
|
| 10 |
/**
|
| 11 |
* Implementation of hook_help().
|
| 12 |
*/
|
| 13 |
function promote_blocks_help($section = '') {
|
| 14 |
switch ($section) {
|
| 15 |
case "admin/modules#description":
|
| 16 |
return "select a content type to promote as a block";
|
| 17 |
case 'admin/settings/promote-blocks':
|
| 18 |
return t("<p>Set the Content Type and default region that the promoted blocks will land in.</p><p>You will
|
| 19 |
have to ". l("configure the blocks", "admin/build/block" ) ." after promoting the nodes</p>");
|
| 20 |
case "admin/help#promote_blocks":
|
| 21 |
// Return a line-break version of the module README
|
| 22 |
return filter_filter('process', 2, NULL, file_get_contents( dirname(__FILE__) ."/README.txt") );
|
| 23 |
}
|
| 24 |
|
| 25 |
return '';
|
| 26 |
}
|
| 27 |
|
| 28 |
/**
|
| 29 |
* Implementation of hook_menu().
|
| 30 |
*/
|
| 31 |
function promote_blocks_menu($may_cache) {
|
| 32 |
$items = array();
|
| 33 |
if ($may_cache) {
|
| 34 |
$items[] = array(
|
| 35 |
'path' => 'admin/settings/promote-blocks',
|
| 36 |
'title' => t("Promote Blocks"),
|
| 37 |
'description' => t("Set default region and content type for promotion"),
|
| 38 |
'callback' => 'drupal_get_form',
|
| 39 |
'callback arguments' => array('promote_blocks_admin_settings'),
|
| 40 |
'access' => user_access('administer site configuration'),
|
| 41 |
);
|
| 42 |
}
|
| 43 |
return $items;
|
| 44 |
}
|
| 45 |
|
| 46 |
/**
|
| 47 |
* Create form for admin settings page
|
| 48 |
* @ingroup forms
|
| 49 |
*/
|
| 50 |
function promote_blocks_admin_settings() {
|
| 51 |
$form['promote_blocks_region'] = array(
|
| 52 |
'#type' => 'select',
|
| 53 |
'#title' => t('Promote Blocks Region'),
|
| 54 |
'#default_value' => variable_get('promote_blocks_region', 'content'),
|
| 55 |
'#description' => t('Specifies the default display region of promoted articles.'),
|
| 56 |
'#options' => theme('regions')
|
| 57 |
);
|
| 58 |
|
| 59 |
$types = node_get_types();
|
| 60 |
$keys = array_keys($types);
|
| 61 |
|
| 62 |
$form['promote_blocks_node_type'] = array(
|
| 63 |
'#type' => 'select',
|
| 64 |
'#title' => t("Node Type"),
|
| 65 |
'#default_value' => variable_get('promote_blocks_node_type', 'Not Selected'),
|
| 66 |
'#description' => t('Specify the node type that will be promoted.'),
|
| 67 |
'#options' => ($types ? array_combine($keys, $keys) : array("No Types Found") ),
|
| 68 |
);
|
| 69 |
return system_settings_form($form);
|
| 70 |
}
|
| 71 |
|
| 72 |
|
| 73 |
/**
|
| 74 |
* Implementation of hook_block().
|
| 75 |
*/
|
| 76 |
function promote_blocks_block($op = 'list', $delta = 0, $edit = array()) {
|
| 77 |
if ($op == "list") {
|
| 78 |
$items = _get_promote_items();
|
| 79 |
$blocks = array();
|
| 80 |
foreach ($items as $item) {
|
| 81 |
$blocks['promote_block-'. $item->nid] = array
|
| 82 |
(
|
| 83 |
'info' => $item->title,
|
| 84 |
'status' => 1,
|
| 85 |
'region' => variable_get('promote_blocks_region', 'content')
|
| 86 |
);
|
| 87 |
}
|
| 88 |
return $blocks;
|
| 89 |
}
|
| 90 |
else if ($op == 'view') {
|
| 91 |
$block = _get_promote_block($delta);
|
| 92 |
return $block;
|
| 93 |
}
|
| 94 |
}
|
| 95 |
|
| 96 |
/**
|
| 97 |
* Implementation of hook_nodeapi().
|
| 98 |
* operate on the nodes specified by the admin settings
|
| 99 |
*/
|
| 100 |
function promote_blocks_nodeapi(&$node, $op, $teaser, $page) {
|
| 101 |
$node_type = variable_get('promote_blocks_node_type', 'xxxxx');
|
| 102 |
if ($node->type == $node_type) {
|
| 103 |
switch ($op) {
|
| 104 |
case 'insert':
|
| 105 |
_update_node_promote_block($node->nid, $node->promote_block);
|
| 106 |
_block_rehash();
|
| 107 |
break;
|
| 108 |
|
| 109 |
case 'update':
|
| 110 |
case 'delete':
|
| 111 |
_update_node_promote_block($node->nid, $node->promote_block);
|
| 112 |
_block_rehash();
|
| 113 |
break;
|
| 114 |
|
| 115 |
case 'prepare' :
|
| 116 |
$node->promote_block = _is_node_promote_block($node->nid);
|
| 117 |
print("prepare $node->promote_block");
|
| 118 |
break;
|
| 119 |
}
|
| 120 |
}
|
| 121 |
}
|
| 122 |
|
| 123 |
/**
|
| 124 |
* Format the promoted block
|
| 125 |
*
|
| 126 |
* @ingroup themeable
|
| 127 |
*/
|
| 128 |
function theme_promote_block($node) {
|
| 129 |
$block['subject'] = l($node->category, "articles/$node->category");
|
| 130 |
|
| 131 |
$content .= "<div class='promote_block_content $extra_class'>";
|
| 132 |
$content .= l($node->title, "node/$node->nid") .'<br/>';
|
| 133 |
$content .= $node->teaser .' '. l(t('Read More'), "node/$node->nid");
|
| 134 |
$content .= '</div>';
|
| 135 |
$block['content'] = $content;
|
| 136 |
|
| 137 |
return $block;
|
| 138 |
}
|
| 139 |
|
| 140 |
/**
|
| 141 |
* Implementation of hook_form_alter().
|
| 142 |
*
|
| 143 |
* Add our "promote to block" checkbox to the node form.
|
| 144 |
* @ingroup forms
|
| 145 |
*/
|
| 146 |
function promote_blocks_form_alter($form_id, &$form) {
|
| 147 |
$node = $form['#node'];
|
| 148 |
|
| 149 |
$form_to_look_for = variable_get('promote_blocks_node_type', 'xxxNOT_SETxxx') ."_node_form";
|
| 150 |
if ($form_id == $form_to_look_for) {
|
| 151 |
$form['options']['promote_block'] = array(
|
| 152 |
'#type' => 'checkbox',
|
| 153 |
'#title' => t('Promoted to block on frontpage'),
|
| 154 |
'#default_value' => variable_get($node->promote_block, 0)
|
| 155 |
);
|
| 156 |
|
| 157 |
}
|
| 158 |
}
|
| 159 |
|
| 160 |
/**
|
| 161 |
* Update promote block table
|
| 162 |
*
|
| 163 |
*/
|
| 164 |
function _update_node_promote_block($nid, $promoted) {
|
| 165 |
if (_is_node_promote_block($nid) && !$promoted) {
|
| 166 |
$query = db_query("DELETE FROM {node_field_promote_block} WHERE nid = %d", $nid);
|
| 167 |
}
|
| 168 |
else if (!_is_node_promote_block($nid) && $promoted) {
|
| 169 |
$query = db_query("INSERT INTO {node_field_promote_block} SET nid = %d", $nid);
|
| 170 |
}
|
| 171 |
else{
|
| 172 |
// no action needed
|
| 173 |
}
|
| 174 |
}
|
| 175 |
|
| 176 |
/**
|
| 177 |
* Check if the node is promoted to a block
|
| 178 |
*
|
| 179 |
*/
|
| 180 |
function _is_node_promote_block($nid) {
|
| 181 |
$query = db_query("select count(*) from {node_field_promote_block} where nid = %d", $nid);
|
| 182 |
// 1:TRUE 0:FALSE
|
| 183 |
return (db_result($query) ? 1 : 0);
|
| 184 |
}
|
| 185 |
|
| 186 |
/**
|
| 187 |
* List all nodes that are promoted to the front page.
|
| 188 |
*/
|
| 189 |
function _get_promote_items() {
|
| 190 |
$sql .= 'select n.* from {node} n ';
|
| 191 |
$sql .= 'inner join {node_field_promote_block} pb on n.nid = pb.nid ';
|
| 192 |
$sql .= "where n.status = 1 and n.type = '%s'";
|
| 193 |
$result = db_query($sql, variable_get('promote_blocks_node_type', 'xxx'));
|
| 194 |
|
| 195 |
$items = array();
|
| 196 |
while ($node = db_fetch_object($result)) {
|
| 197 |
$items[] = $node;
|
| 198 |
}
|
| 199 |
|
| 200 |
return $items;
|
| 201 |
}
|
| 202 |
|
| 203 |
/**
|
| 204 |
* Get the block html
|
| 205 |
* @params $delta The block id
|
| 206 |
*/
|
| 207 |
function _get_promote_block($delta) {
|
| 208 |
$nid = substr($delta, 14);
|
| 209 |
$node = node_load($nid);
|
| 210 |
$block = theme('promote_block', $node);
|
| 211 |
return $block;
|
| 212 |
}
|
| 213 |
|
| 214 |
/**
|
| 215 |
* Implementation of hook_views_tables()
|
| 216 |
* Will allow views to filter on if a node has been block promoted.
|
| 217 |
*/
|
| 218 |
function promote_blocks_views_tables() {
|
| 219 |
$tables['node_field_promote_block'] = array(
|
| 220 |
'notafield' => TRUE,
|
| 221 |
'name' => 'node_field_promote_block',
|
| 222 |
'join' => array(
|
| 223 |
'left' => array(
|
| 224 |
"table" => "node",
|
| 225 |
"field" => "nid"
|
| 226 |
),
|
| 227 |
'right' => array(
|
| 228 |
"field" => "nid"
|
| 229 |
),
|
| 230 |
),
|
| 231 |
'filters' => array(
|
| 232 |
'promote' => array(
|
| 233 |
'name' => "Node: Promoted to Block",
|
| 234 |
'operator' => "views_handler_operator_eqneq",
|
| 235 |
'list' => array(0 => "False", 1 => "True"),
|
| 236 |
'help' => t('help text'),
|
| 237 |
),
|
| 238 |
),
|
| 239 |
);
|
| 240 |
return $tables;
|
| 241 |
}
|
| 242 |
|
| 243 |
/**
|
| 244 |
* Fix for PHP4. array_combine() function is not available until PHP5
|
| 245 |
* Taken from http://us3.php.net/manual/en/function.array-combine.php#76933
|
| 246 |
*/
|
| 247 |
if (!function_exists('array_combine')) {
|
| 248 |
function array_combine($keys, $values) {
|
| 249 |
$keys = array_values($keys);
|
| 250 |
$values = array_values($values);
|
| 251 |
$result = array();
|
| 252 |
foreach ( $keys as $index => $key ) {
|
| 253 |
$result[$key] = $values[$index];
|
| 254 |
}
|
| 255 |
return $result;
|
| 256 |
}
|
| 257 |
}
|