| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Module to display messages that can be individually closed by logged in users.
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Implementation of hook_menu().
|
| 11 |
*/
|
| 12 |
function admin_message_menu() {
|
| 13 |
$items = array();
|
| 14 |
|
| 15 |
$items['admin_message/close/%'] = array(
|
| 16 |
'page callback' => 'admin_message_close',
|
| 17 |
'page arguments' => array(2),
|
| 18 |
'access arguments' => array('close admin messages'),
|
| 19 |
'type' => MENU_CALLBACK,
|
| 20 |
);
|
| 21 |
|
| 22 |
return $items;
|
| 23 |
}
|
| 24 |
|
| 25 |
/**
|
| 26 |
* Implementation of hook_nodeapi().
|
| 27 |
*/
|
| 28 |
function admin_message_nodeapi(&$node, $op, $teaser, $page) {
|
| 29 |
switch ($op) {
|
| 30 |
case 'load':
|
| 31 |
if ($node->type == 'admin_message') {
|
| 32 |
$object = db_fetch_object(db_query('SELECT keep_new FROM {admin_message} WHERE nid = %d', $node->nid));
|
| 33 |
return array('admin_message_keep_new' => $object->keep_new);
|
| 34 |
}
|
| 35 |
break;
|
| 36 |
|
| 37 |
case 'insert':
|
| 38 |
if ($node->type == 'admin_message') {
|
| 39 |
db_query("INSERT INTO {admin_message} (nid, keep_new, php_visibility) VALUES (%d, %d, '%s')", $node->nid, $node->admin_message_keep_new, trim($node->php_visibility));
|
| 40 |
}
|
| 41 |
break;
|
| 42 |
|
| 43 |
case 'update':
|
| 44 |
if ($node->type == 'admin_message') {
|
| 45 |
db_query('DELETE FROM {admin_message} WHERE nid = %d', $node->nid);
|
| 46 |
db_query("INSERT INTO {admin_message} (nid, keep_new, php_visibility) VALUES (%d, %d, '%s')", $node->nid, $node->admin_message_keep_new, trim($node->php_visibility));
|
| 47 |
}
|
| 48 |
break;
|
| 49 |
|
| 50 |
case 'delete':
|
| 51 |
if ($node->type == 'admin_message') {
|
| 52 |
db_query('DELETE FROM {admin_message} WHERE nid = %d', $node->nid);
|
| 53 |
db_query('DELETE FROM {admin_message_close} WHERE nid = %d', $node->nid);
|
| 54 |
}
|
| 55 |
break;
|
| 56 |
|
| 57 |
case 'view':
|
| 58 |
break;
|
| 59 |
}
|
| 60 |
}
|
| 61 |
|
| 62 |
function admin_message_close($nid) {
|
| 63 |
global $user;
|
| 64 |
db_query('DELETE FROM {admin_message_close} WHERE nid = %d AND uid = %d', $nid, $user->uid);
|
| 65 |
db_query('INSERT INTO {admin_message_close} (nid, uid) VALUES (%d, %d)', $nid, $user->uid);
|
| 66 |
drupal_goto();
|
| 67 |
}
|
| 68 |
|
| 69 |
/**
|
| 70 |
* Implementation of hook_user.
|
| 71 |
*/
|
| 72 |
function admin_message_user($op, &$edit, &$user) {
|
| 73 |
if ($op == 'delete') {
|
| 74 |
db_query('DELETE FROM {admin_message_close} WHERE uid = %d', $user->uid);
|
| 75 |
}
|
| 76 |
}
|
| 77 |
|
| 78 |
/**
|
| 79 |
* Implementation of hook_block().
|
| 80 |
*
|
| 81 |
* op: admin_message
|
| 82 |
*
|
| 83 |
*/
|
| 84 |
function admin_message_block($op = 'list', $delta = 0) {
|
| 85 |
switch ($op) {
|
| 86 |
case 'list':
|
| 87 |
$blocks['admin_message']['info'] = t('Admin messages');
|
| 88 |
return $blocks;
|
| 89 |
|
| 90 |
case 'view':
|
| 91 |
switch ($delta) {
|
| 92 |
case 'admin_message':
|
| 93 |
$block['subject'] = '';
|
| 94 |
$block['content'] = admin_message_list_messages();
|
| 95 |
break;
|
| 96 |
}
|
| 97 |
return $block;
|
| 98 |
}
|
| 99 |
}
|
| 100 |
|
| 101 |
/**
|
| 102 |
* Generates a themed list of admin messages which is passed to hook_block().
|
| 103 |
*/
|
| 104 |
function admin_message_list_messages() {
|
| 105 |
global $user;
|
| 106 |
$output = '';
|
| 107 |
$result = db_query(db_rewrite_sql("SELECT n.nid, n.created, a.keep_new, a.php_visibility FROM {node} n LEFT JOIN {admin_message} a ON n.nid = a.nid WHERE n.sticky = 1 AND n.status = 1 AND n.type = 'admin_message' ORDER BY n.created DESC"));
|
| 108 |
$messages = FALSE;
|
| 109 |
while ($item = db_fetch_object($result)) {
|
| 110 |
$php_visibility = empty($item->php_visibility) ? TRUE : drupal_eval($item->php_visibility);
|
| 111 |
$closed_by_user = db_fetch_object(db_query("SELECT a.nid FROM {admin_message_close} a WHERE a.nid = %d AND a.uid = %d", $item->nid, $user->uid));
|
| 112 |
|
| 113 |
if (empty($closed_by_user) && ($item->created >= $user->created || $item->keep_new) && $php_visibility) {
|
| 114 |
$node = node_build_content(node_load($item->nid));
|
| 115 |
$output .= theme('admin_message_message', $node);
|
| 116 |
}
|
| 117 |
|
| 118 |
$messages = TRUE;
|
| 119 |
}
|
| 120 |
|
| 121 |
if ($messages) {
|
| 122 |
// Insert JavaScript and CSS if messages are displayed.
|
| 123 |
drupal_add_js(drupal_get_path('module', 'admin_message') .'/admin_message.js', 'module');
|
| 124 |
drupal_add_css(drupal_get_path('module', 'admin_message') .'/admin_message.css', 'module');
|
| 125 |
}
|
| 126 |
return $output;
|
| 127 |
}
|
| 128 |
|
| 129 |
/**
|
| 130 |
* Implementation of hook_form_alter().
|
| 131 |
*/
|
| 132 |
function admin_message_form_alter(&$form, &$form_state, $form_id) {
|
| 133 |
if ('admin_message_node_form' == $form_id) {
|
| 134 |
$form['admin_message'] = array(
|
| 135 |
'#type' => 'fieldset',
|
| 136 |
'#title' => t('Message visibility'),
|
| 137 |
'#collapsible' => TRUE,
|
| 138 |
'#collapsed' => FALSE,
|
| 139 |
);
|
| 140 |
// "Hijack" the sticky option.
|
| 141 |
$sticky_field = $form['options']['sticky'];
|
| 142 |
unset($form['options']['sticky']);
|
| 143 |
$form['admin_message']['sticky'] = $sticky_field;
|
| 144 |
$form['admin_message']['sticky']['#title'] = t('Show message (sticky)');
|
| 145 |
|
| 146 |
// Keep new.
|
| 147 |
$form['admin_message']['admin_message_keep_new'] = array(
|
| 148 |
'#type' => 'checkbox',
|
| 149 |
'#title' => t('Always show this message to new users'),
|
| 150 |
'#default_value' => isset($form['#node']->admin_message_keep_new) ? $form['#node']->admin_message_keep_new : variable_get('admin_message_keep_new_'. $type, 0),
|
| 151 |
'#prefix' => '<div id="admin-message-toggle">',
|
| 152 |
);
|
| 153 |
|
| 154 |
// PHP visibility form.
|
| 155 |
$result = db_fetch_object(db_query('SELECT php_visibility FROM {admin_message} WHERE nid = %d', $form['nid']['#value']));
|
| 156 |
$php_visibility_code = $result->php_visibility;
|
| 157 |
if (user_access('use PHP for block visibility')) {
|
| 158 |
$form['admin_message']['php_visibility'] = array(
|
| 159 |
'#type' => 'textarea',
|
| 160 |
'#title' => t('Show if the following PHP code returns <code>true</code> (PHP-mode, experts only)'),
|
| 161 |
'#description' => t('Enter PHP code between %php. Note that executing incorrect PHP-code can break your Drupal site.', array('%php' => '<?php ?>')),
|
| 162 |
'#default_value' => $php_visibility_code,
|
| 163 |
'#suffix' => '</div>',
|
| 164 |
);
|
| 165 |
}
|
| 166 |
else {
|
| 167 |
$form['admin_message']['admin_message_keep_new']['#suffix'] = '</div>';
|
| 168 |
}
|
| 169 |
|
| 170 |
drupal_add_js(drupal_get_path('module', 'admin_message') .'/admin_message_form.js', 'module');
|
| 171 |
}
|
| 172 |
}
|
| 173 |
|
| 174 |
function admin_message_theme() {
|
| 175 |
return array(
|
| 176 |
'admin_message_message' => array(
|
| 177 |
'arguments' => array('node' => NULL),
|
| 178 |
'template' => 'admin-message-node',
|
| 179 |
),
|
| 180 |
);
|
| 181 |
}
|