From 270da89ba4933a29ce59edf5f558f4e5415a4684 Mon Sep 17 00:00:00 2001
From: Tadej <tadej@artnetik.si>
Date: Mon, 21 Mar 2011 14:01:21 +0100
Subject: [PATCH 1/2] Issue #191499 by paranojik: Allow newest-first comment ordering for flat comments.

---
 modules/comment/comment.module |   35 ++++++++++++++++++++++++++++++-----
 1 files changed, 30 insertions(+), 5 deletions(-)

diff --git a/modules/comment/comment.module b/modules/comment/comment.module
index 9ab70c5..99c9ca1 100644
--- a/modules/comment/comment.module
+++ b/modules/comment/comment.module
@@ -30,6 +30,16 @@ define('COMMENT_MODE_FLAT', 0);
 define('COMMENT_MODE_THREADED', 1);
 
 /**
+ * Comments are ordered by date - oldest first.
+ */
+define('COMMENT_ORDER_OLDEST_FIRST', 0);
+
+/**
+ * Comments are ordered by date - newest first.
+ */
+define('COMMENT_ORDER_NEWEST_FIRST', 1);
+
+/**
  * Anonymous posters cannot enter their contact information.
  */
 define('COMMENT_ANONYMOUS_MAYNOT_CONTACT', 0);
@@ -342,6 +352,7 @@ function comment_node_type_delete($info) {
     'comment',
     'comment_default_mode',
     'comment_default_per_page',
+    'comment_default_order',
     'comment_anonymous',
     'comment_subject_field',
     'comment_preview',
@@ -532,14 +543,16 @@ function comment_get_recent($number = 10) {
 function comment_new_page_count($num_comments, $new_replies, $node) {
   $mode = variable_get('comment_default_mode_' . $node->type, COMMENT_MODE_THREADED);
   $comments_per_page = variable_get('comment_default_per_page_' . $node->type, 50);
+  $order = variable_get('comment_default_order_' . $node->type, COMMENT_ORDER_OLDEST_FIRST);
   $pagenum = NULL;
   $flat = $mode == COMMENT_MODE_FLAT ? TRUE : FALSE;
-  if ($num_comments <= $comments_per_page) {
-    // Only one page of comments.
+  if ($num_comments <= $comments_per_page || ($flat && $order == COMMENT_ORDER_NEWEST_FIRST)) {
+    // Only one page of comments or flat forum and newest first.
+    // First new comment will always be on first page.
     $pageno = 0;
   }
   elseif ($flat) {
-    // Flat comments.
+    // Flat comments and oldest first.
     $count = $num_comments - $new_replies;
     $pageno =  $count / $comments_per_page;
   }
@@ -815,6 +828,7 @@ function comment_node_page_additions($node) {
  * to consider the trailing "/" so we use a substring only.
  */
 function comment_get_thread($node, $mode, $comments_per_page) {
+  $order = variable_get('comment_default_order_' . $node->type, COMMENT_ORDER_OLDEST_FIRST);
   $query = db_select('comment', 'c')->extend('PagerDefault');
   $query->addField('c', 'cid');
   $query
@@ -837,7 +851,12 @@ function comment_get_thread($node, $mode, $comments_per_page) {
     $count_query->condition('c.status', COMMENT_PUBLISHED);
   }
   if ($mode === COMMENT_MODE_FLAT) {
-    $query->orderBy('c.cid', 'ASC');
+    if ($order == COMMENT_ORDER_NEWEST_FIRST) {
+      $query->orderBy('c.cid', 'DESC');
+    }
+    else {
+      $query->orderBy('c.cid', 'ASC');
+    }
   }
   else {
     // See comment above. Analysis reveals that this doesn't cost too
@@ -1759,6 +1778,7 @@ function comment_num_new($nid, $timestamp = 0) {
 function comment_get_display_ordinal($cid, $node_type) {
   // Count how many comments (c1) are before $cid (c2) in display order. This is
   // the 0-based display ordinal.
+  $order = variable_get('comment_default_order_' . $node_type, COMMENT_ORDER_OLDEST_FIRST);
   $query = db_select('comment', 'c1');
   $query->innerJoin('comment', 'c2', 'c2.nid = c1.nid');
   $query->addExpression('COUNT(*)', 'count');
@@ -1772,7 +1792,12 @@ function comment_get_display_ordinal($cid, $node_type) {
     // For flat comments, cid is used for ordering comments due to
     // unpredicatable behavior with timestamp, so we make the same assumption
     // here.
-    $query->condition('c1.cid', $cid, '<');
+    if ($order == COMMENT_ORDER_NEWEST_FIRST) {
+      $query->condition('c1.cid', $cid, '>=');
+    }
+    else {
+      $query->condition('c1.cid', $cid, '<');
+    }
   }
   else {
     // For threaded comments, the c.thread column is used for ordering. We can
-- 
1.7.0.2.msysgit.0


From 48d700e4270e22ca5344c268b118adbeeaf32df0 Mon Sep 17 00:00:00 2001
From: Tadej <tadej@artnetik.si>
Date: Mon, 21 Mar 2011 14:57:57 +0100
Subject: [PATCH 2/2] Issue #191499 by paranojik: Allow newest-first comment ordering for flat comments.

---
 modules/comment/comment.module |   14 ++++++++++++++
 1 files changed, 14 insertions(+), 0 deletions(-)

diff --git a/modules/comment/comment.module b/modules/comment/comment.module
index 99c9ca1..155a9d6 100644
--- a/modules/comment/comment.module
+++ b/modules/comment/comment.module
@@ -1161,6 +1161,20 @@ function comment_form_node_type_form_alter(&$form, $form_state) {
       '#default_value' => variable_get('comment_default_mode_' . $form['#node_type']->type, COMMENT_MODE_THREADED),
       '#description' => t('Show comment replies in a threaded list.'),
     );
+    $form['comment']['comment_default_order'] = array(
+      '#type' => 'select',
+      '#title' => t('Display order'),
+      '#default_value' => variable_get('comment_default_order_' . $form['#node_type']->type, COMMENT_ORDER_NEWEST_FIRST),
+      '#options' =>  array(
+          COMMENT_ORDER_NEWEST_FIRST => t('Newest first'),
+          COMMENT_ORDER_OLDEST_FIRST => t('Oldest first'),
+      ),
+      '#states' => array(
+        'visible' => array(
+          ':input[name="comment_default_mode"]' => array('checked' => FALSE),
+        ),
+      ),
+    );
     $form['comment']['comment_default_per_page'] = array(
       '#type' => 'select',
       '#title' => t('Comments per page'),
-- 
1.7.0.2.msysgit.0

