 su_comments.css     |    5 ++
 su_comments.info    |    2 +-
 su_comments.install |   33 ++------------
 su_comments.module  |  114 ++++++++++++++++++++-------------------------------
 4 files changed, 55 insertions(+), 99 deletions(-)

diff --git a/su_comments.css b/su_comments.css
new file mode 100644
index 0000000..2a6248c
--- /dev/null
+++ b/su_comments.css
@@ -0,0 +1,5 @@
+
+span.su-comments-unpublished {
+  font-style: italic;
+}
+
diff --git a/su_comments.info b/su_comments.info
index 934fddc..b2fc835 100644
--- a/su_comments.info
+++ b/su_comments.info
@@ -1,6 +1,6 @@
 name = Show unpublished comments
 description = Show unpublished comments to the authors.
-core = 6.x
+core = 7.x
 dependencies[] = comment
 package = Comment
 
diff --git a/su_comments.install b/su_comments.install
index c95a91a..5100aff 100644
--- a/su_comments.install
+++ b/su_comments.install
@@ -1,36 +1,13 @@
 <?php
 
 /**
- * Implementation of hook_install().
+ * @file
+ * Schema definition for the su_comments.module.
  */
-function su_comments_install() {
-  // feedback:
-  drupal_set_message(t('Beginning installation of "Show unpublished comments" module.'));
-  drupal_install_schema('su_comments');
-  
-  // feedback:
-  drupal_set_message(t('The "Show unpublished comments" module tables created successfully.'));
-}
-
-function su_comments_schema() {
-  $schema['comments_sessions'] = array(
-    'fields' => array(
-      'cid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
-      'sid' => array('type' => 'varchar', 'length' => 32, 'not null' => TRUE, 'default' => ''),
-    ),
-    'primary key' => array('cid'),
-  );
 
-  return $schema;
-}
 /**
- * Implementation of hook_uninstall().
+ * Drop table {comments_sessions}.
  */
-function su_comments_uninstall() {
-  // feedback:
-  drupal_set_message(t('Beginning uninstallation of "Show unpublished comments" module.'));
-  drupal_uninstall_schema('su_comments');
-
-  // feedback:
-  drupal_set_message(t('The "Show unpublished comments" module was uninstalled successfully.'));
+function su_comments_update_7000() {
+  db_drop_table('comments_statistics');
 }
diff --git a/su_comments.module b/su_comments.module
index 05dc2d0..e2c1055 100644
--- a/su_comments.module
+++ b/su_comments.module
@@ -1,79 +1,53 @@
 <?php
 
-function su_comments_comment(&$a1, $op) {
-	/* there is no point in creating db entries
-	 * for users that can administer comments
-	 * because they can see them anyway */
-	if ($op == 'insert' && $a1['status'] && !user_access('administer comments')){
-		/* $a1['status'] == 1
-		 *  means that comment is set as unpublished
-		 *  so we should create apropriate entry in our table */
-		 $query = "INSERT INTO {comments_sessions} (cid, sid) VALUES (%d, '%s')";
-		 global $user;
-		 $sid = ($user->uid ? $user->sid : session_id());
-		 if ($sid) // we must make sure we have non empty session id!
-		 	db_query($query, $a1['cid'], $sid);
-	}
-	
-	/* here we remove comment from our table when comment get's published */
-	if (($op == 'update' && !$a1['status']) || $op == 'publish' || $op == 'delete'){
-	  $query = "DELETE FROM {comments_sessions} WHERE cid = %d";
-	  db_query($query, ($op == 'update' ? $a1['cid'] : $a1->cid));
+function su_comments_comment_insert($comment) {
+	// If this comment is not published and the user does not have the rights to
+  // see unpublished comments, store the cid in the session.
+	if ($comment->status == COMMENT_NOT_PUBLISHED && !user_access('administer comments')){
+     $_SESSION['su_comments'][] = $comment->cid;
 	}
 }
 
-function su_comments_nodeapi(&$node, $op, $a3 = NULL, $page = NULL) {
-	if ($op == 'view' && $page && !user_access('administer comments')){
-	  /* comment query (a little modified) and comment rendering code
-		 * is borrowed form comment.module - function comment_render($node, $cid);
-		 */
-    $query = "SELECT c.cid, c.pid, c.nid, c.subject, c.comment, c.format, c.timestamp, c.name, c.mail, c.homepage, u.uid, u.name AS registered_name, u.signature, u.picture, u.data, c.status FROM {comments} c INNER JOIN {users} u ON c.uid = u.uid INNER JOIN {comments_sessions} s ON c.cid = s.cid WHERE c.nid = %d AND s.sid = '%s'";
-    $query = db_rewrite_sql($query, 'c', 'cid');
-    global $user;
-    $result = db_query($query, $node->nid, ($user->uid ? $user->sid : session_id()));
-    $comments = array();
-		while($comment = db_fetch_object($result)){
-      $comment->name = $comment->uid ? $comment->registered_name : $comment->name;
-      $links = module_invoke_all('link', 'comment', $comment, 1);
-      /* we should remove 'reply' link as visiting it will throw
-			 * "The comment you are replying to does not exist."
-			 * error message. */
-			unset($links['comment_reply']);
-			
-      /* to notify users that their comments are in moderation queue
-			 * we add a fake link to the node links list */
-      $links[] = array('title' => '<span class="su-comments-unpublished">'.t('Your comment is awaiting moderation').'</span>', 'html' => true);
-      drupal_alter('link', $links, $node);
+/**
+ * Implements hook_query_TAG_alter().
+ */
+function su_comments_query_comment_filter_alter(SelectQueryInterface $query) {
+  // Only alter the query if the user does not have administer comments
+  // permission and has a session.
+  if (!user_access('administer comments') && !empty($_SESSION['su_comments'])) {
+    $conditions = &$query->conditions();
+    // Remove status condition.
+    foreach ($conditions as $key => $condition) {
+      if (is_array($condition) && $condition['field'] == 'c.status') {
+        unset($conditions[$key]);
+      }
+    }
 
-			/* finally we render the unpublished comment
-			 * unfortunately we have to put it at the top of all comments,
-			 * just below node content - this is all we can get from hook_nodeapi */
-      $comments[] = theme('comment_view', $comment, $node, $links);
-		}
-		
-		if (count($comments)){
-			$node->content['su_comments'] = array(
-				'#value' => implode("\n", $comments),
-				'#weight' => 100
-			);
-		}
-	}
+    // Add a new condition to only show published comments or those created
+    // by this session.
+    $or = db_or();
+    $or->condition('c.status', COMMENT_PUBLISHED);
+    $or->condition('c.cid', $_SESSION['su_comments']);
+    $query->condition($or);
+  }
 }
 
-function su_comments_user($op, &$edit, &$account, $category = NULL){
-	/* when user logs in the session id changes so we should update our comments_sessions table
-	 * this is one way process. When user logs out comments_sessions wont be updated
-	 * and user wont be able to see his/hers unpublished comments any more. */
-	static $old_sid = false;
-	if ($op = 'load'){
-		/* on user load we have still old session id
-		 * (which is regenerated later by the core)
-		 * but uid is already valid */
-		$old_sid = $account->sid;
-	} else if ($op = 'login' && $old_sid) {
-		/* on user login the session id has already been regenerated
-		 * so now we can update our comments table */
-		$query = "UPDATE {comments_sessions} SET sid = '%s' WHERE sid = '%s'";
-		db_query($query, $old_sid, $account->sid);
-	}
+/**
+ * Implements hook_comment_view_alter().
+ */
+function su_comments_comment_view_alter(&$build) {
+  // Only act on comments which are not published and if this user does not have
+  // administer comments permission.
+  if ($build['#comment']->status == COMMENT_NOT_PUBLISHED && !user_access('administer comments')) {
+    // Hide the reply link as trying to reply results in an error that there is
+    // no such comment.
+    unset($build['links']['comment']['#links']['comment-reply']);
+
+    $build['su_comments'] = array(
+      '#markup' => '<span class="su-comments-unpublished">' . t('Your comment is awaiting moderation.') . '</span>',
+      '#attached' => array(
+        'css' => array(drupal_get_path('module', 'su_comments') . '/su_comments.css'),
+      ),
+    );
+  }
 }
