diff --git a/admin_message.css b/admin_message.css index c080235..4df75fd 100644 --- a/admin_message.css +++ b/admin_message.css @@ -1,4 +1,5 @@ -.admin-message .close { +.admin-message-close { float: right; } + diff --git a/admin_message.info b/admin_message.info index 609e4b0..b7d1ba4 100644 --- a/admin_message.info +++ b/admin_message.info @@ -1,4 +1,5 @@ name = Admin message description = Display messages that can be individually closed by logged in users. -core = 6.x \ No newline at end of file +core = 7.x + diff --git a/admin_message.install b/admin_message.install index 98495ec..26c8766 100644 --- a/admin_message.install +++ b/admin_message.install @@ -9,24 +9,20 @@ */ function admin_message_schema() { $schema['admin_message'] = array( - 'description' => t('TODO'), 'fields' => array( 'nid' => array( - 'description' => t('TODO'), 'type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0, ), 'keep_new' => array( - 'description' => t('TODO'), 'type' => 'int', 'size' => 'tiny', 'not null' => TRUE, 'default' => 0, ), 'php_visibility' => array( - 'description' => t('TODO'), 'type' => 'text', 'size' => 'small', 'not null' => TRUE, @@ -35,24 +31,21 @@ function admin_message_schema() { 'primary key' => array('nid'), ); $schema['admin_message_close'] = array( - 'description' => t('TODO'), 'fields' => array( 'nid' => array( - 'description' => t('TODO'), 'type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0, ), 'uid' => array( - 'description' => t('TODO'), 'type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0, ), ), - 'primary key' => array('nid, uid'), + 'primary key' => array('nid', 'uid'), ); return $schema; } @@ -61,13 +54,14 @@ function admin_message_schema() { * Implementation of hook_install(). */ function admin_message_install() { - drupal_install_schema('admin_message'); - // Set to published and sticky as default for content type. variable_set('node_options_admin_message' , array('status', 'sticky')); // Disable comments for this content type. variable_set('comment_admin_message', '0'); + node_types_rebuild(); + $types = node_type_get_types(); + node_add_body_field($types['admin_message'], t('Message')); drupal_set_message(t('Admin message was installed. Enable the block "Admin messages" to display messages to users.')); } @@ -75,7 +69,6 @@ function admin_message_install() { * Implementation of hook_uninstall(). */ function admin_message_uninstall() { - drupal_uninstall_schema('admin_message'); } @@ -89,3 +82,4 @@ function admin_message_update_6001() { $results[] = update_sql('ALTER TABLE admin_message_close CHANGE COLUMN uid uid INT(10) UNSIGNED NOT NULL DEFAULT 0'); return $results; } + diff --git a/admin_message.js b/admin_message.js index fdf3164..36f753c 100644 --- a/admin_message.js +++ b/admin_message.js @@ -1,13 +1,13 @@ -// Global Killswitch -if (Drupal.jsEnabled) { +(function ($) { $(document).ready(function() { // Close - $(".admin-message a.admin-message-close").click(function() { + $(".admin-message a.admin-message-close").click(function(evt) { var href = $(this).attr("href"); $.get(href); $(this).parent().slideUp('fast'); - return false; + evt.preventDefault(); }); }); -} +})(jQuery); + diff --git a/admin_message.module b/admin_message.module index 0b6503a..663b543 100644 --- a/admin_message.module +++ b/admin_message.module @@ -12,52 +12,66 @@ define('ADMIN_MESSAGE_COOKIE_LIFETIME', 86400 * 365); * Implementation of hook_menu(). */ function admin_message_menu() { - $items = array(); + $items = array(); - $items['admin_message/close/%'] = array( + $items['admin_message/close/%'] = array( 'page callback' => 'admin_message_close', 'page arguments' => array(2), 'access arguments' => array('close admin messages'), 'type' => MENU_CALLBACK, ); - + return $items; } /** - * Implementation of hook_nodeapi(). + * Implements hook_node_load(). */ -function admin_message_nodeapi(&$node, $op, $teaser, $page) { - switch ($op) { - case 'load': - if ($node->type == 'admin_message') { - $object = db_fetch_object(db_query('SELECT keep_new FROM {admin_message} WHERE nid = %d', $node->nid)); - return array('admin_message_keep_new' => $object->keep_new); - } - break; - - case 'insert': - if ($node->type == 'admin_message') { - 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)); - } - break; - - case 'update': - if ($node->type == 'admin_message') { - db_query('DELETE FROM {admin_message} WHERE nid = %d', $node->nid); - 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)); - } - break; +function admin_message_node_load($nodes, $types) { + if (!in_array('admin_message', $types)) { + return; + } + foreach ($nodes as $node) { + if ($node->type == 'admin_message') { + $object = db_query('SELECT keep_new FROM {admin_message} WHERE nid = :nid', array(':nid' => $node->nid))->fetchObject(); + $node->admin_message_keep_new = $object->keep_new; + } + } +} - case 'delete': - if ($node->type == 'admin_message') { - db_query('DELETE FROM {admin_message} WHERE nid = %d', $node->nid); - db_query('DELETE FROM {admin_message_close} WHERE nid = %d', $node->nid); - } - break; +/** + * Implements hook_node_insert(). + */ +function admin_message_node_insert($node) { + if ($node->type == 'admin_message') { + db_merge('admin_message') + ->key(array('nid' => $node->nid)) + ->insertFields(array( + 'nid' => $node->nid, + 'keep_new' => empty($node->admin_message_keep_new) ? 0 : $node->admin_message_keep_new, + 'php_visibility' => empty($php_visibility) ? '' : trim($node->php_visibility), + )) + ->updateFields(array( + 'keep_new' => empty($node->admin_message_keep_new) ? 0 : $node->admin_message_keep_new, + 'php_visibility' => empty($php_visibility) ? '' : trim($node->php_visibility), + )) + ->execute(); + } +} - case 'view': - break; +/** + * Implements hook_node_update(). + */ +function admin_message_node_update($node) { + if ($node->type == 'admin_message') { + db_query('DELETE FROM {admin_message} WHERE nid = :nid', array(':nid' => $node->nid)); + db_query("INSERT INTO {admin_message} (nid, keep_new, php_visibility) VALUES (:nid, :keep_new, ':php')", + array( + ':nid' => $node->nid, + ':keep_new' => empty($node->admin_message_keep_new) ? 0 : $node->admin_message_keep_new, + ':php' => trim($node->php_visibility) + ) + ); } } @@ -98,8 +112,8 @@ function _admin_message_get_cookie($nid) { function admin_message_close($nid) { global $user; if ($user->uid) { - db_query('DELETE FROM {admin_message_close} WHERE nid = %d AND uid = %d', $nid, $user->uid); - db_query('INSERT INTO {admin_message_close} (nid, uid) VALUES (%d, %d)', $nid, $user->uid); + db_query('DELETE FROM {admin_message_close} WHERE nid = :nid AND uid = :uid', array(':nid' => $nid, ':uid' => $user->uid)); + db_query('INSERT INTO {admin_message_close} (nid, uid) VALUES (:nid, :uid)', array(':nid' => $nid, ':uid' => $user->uid)); } else { // For anonymous users we store the value in a cookie @@ -109,37 +123,33 @@ function admin_message_close($nid) { } /** - * Implementation of hook_user. + * Implementation of hook_user_delete. */ -function admin_message_user($op, &$edit, &$user) { - if ($op == 'delete') { - db_query('DELETE FROM {admin_message_close} WHERE uid = %d', $user->uid); - } +function admin_message_user_delete($account) { + db_query('DELETE FROM {admin_message_close} WHERE uid = :uid', array(':uid' => $user->uid)); } /** - * Implementation of hook_block(). - * - * op: admin_message - * + * Implements hook_block_info */ -function admin_message_block($op = 'list', $delta = 0) { - switch ($op) { - case 'list': - $blocks['admin_message']['info'] = t('Admin messages'); - return $blocks; - - case 'view': - switch ($delta) { - case 'admin_message': - if (user_access('access content')) { - $block['subject'] = ''; - $block['content'] = admin_message_list_messages(); - } - break; +function admin_message_block_info() { + $blocks['admin_message']['info'] = t('Admin messages'); + return $blocks; +} + +/** + * Implements hook_block_view(). + */ +function admin_message_block_view($delta = "") { + switch ($delta) { + case 'admin_message': + if (user_access('access content')) { + $block['subject'] = ''; + $block['content'] = admin_message_list_messages(); } - return $block; + break; } + return $block; } /** @@ -148,12 +158,21 @@ function admin_message_block($op = 'list', $delta = 0) { function admin_message_list_messages() { global $user; $output = ''; - $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")); + //$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")); + $q = db_select('node', 'n'); + $q->join('admin_message', 'a', 'n.nid = a.nid'); + $q->fields('n', array('nid', 'created')); + $q->fields('a', array('keep_new', 'php_visibility')); + $q->condition('n.sticky', 1, '='); + $q->condition('n.status', 1, '='); + $q->condition('n.type', 'admin_message', '='); + $q->orderBy('n.created', 'DESC'); + $result = $q->execute(); $messages = array(); - while ($item = db_fetch_object($result)) { + while ($item = $result->fetchObject()) { $php_visibility = empty($item->php_visibility) ? TRUE : drupal_eval($item->php_visibility); if ($user->uid) { - $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)); + $closed_by_user = db_query("SELECT a.nid FROM {admin_message_close} a WHERE a.nid = :nid AND a.uid = :uid", array(':nid' => $item->nid, ':uid' => $user->uid))->fetchObject(); } else { $closed_by_user = _admin_message_get_cookie($item->nid); @@ -175,8 +194,8 @@ function admin_message_list_messages() { } // Insert JavaScript and CSS if messages are displayed. - drupal_add_js(drupal_get_path('module', 'admin_message') .'/admin_message.js', 'module'); - drupal_add_css(drupal_get_path('module', 'admin_message') .'/admin_message.css', 'module'); + drupal_add_js(drupal_get_path('module', 'admin_message') . '/admin_message.js'); + drupal_add_css(drupal_get_path('module', 'admin_message') . '/admin_message.css'); } return $output; @@ -196,34 +215,43 @@ function admin_message_form_alter(&$form, &$form_state, $form_id) { // "Hijack" the sticky option. $sticky_field = $form['options']['sticky']; unset($form['options']['sticky']); - $form['admin_message']['sticky'] = $sticky_field; + $form['admin_message']['sticky'] = $sticky_field; $form['admin_message']['sticky']['#title'] = t('Show message (sticky)'); // Keep new. $form['admin_message']['admin_message_keep_new'] = array( '#type' => 'checkbox', '#title' => t('Always show this message to new users'), - '#default_value' => isset($form['#node']->admin_message_keep_new) ? $form['#node']->admin_message_keep_new : variable_get('admin_message_keep_new_'. $type, 0), - '#prefix' => '
true
(PHP-mode, experts only)'),
'#description' => t('Enter PHP code between %php. Note that executing incorrect PHP-code can break your Drupal site.', array('%php' => '')),
'#default_value' => $php_visibility_code,
- '#suffix' => '