From 84d05c61911ff2a92dfa699b30109276059af65a Mon Sep 17 00:00:00 2001
From: JediKev <kevin@enhancesoft.com>
Date: Thu, 14 Feb 2019 15:19:52 -0600
Subject: [PATCH] variable: Complete Thread ASC or DESC

This pull adds the ability to choose between ASC or DESC order for the
complete thread variable. If `complete_asc` is chosen the system will order
the thread in ASC order, if `complete_desc` is chosen the system will order
the thread in DESC order. This keeps backwards compatibility with just
`complete` as some people might forget to update this for a while.
---
 include/class.thread.php                      | 50 +++++++++++++++----
 .../client/templates/thread-export.tmpl.php   |  2 +-
 2 files changed, 41 insertions(+), 11 deletions(-)

diff --git a/include/class.thread.php b/include/class.thread.php
index f1c6ac988..075341c01 100644
--- a/include/class.thread.php
+++ b/include/class.thread.php
@@ -2989,7 +2989,7 @@ implements TemplateVariable {
     }
 
     function asVar() {
-        return $this->getVar('complete');
+        return new ThreadEntries($this);
     }
 
     function getVar($name) {
@@ -3013,21 +3013,14 @@ implements TemplateVariable {
 
             break;
         case 'complete':
-            $content = '';
-            $thread = $this;
-            ob_start();
-            include INCLUDE_DIR.'client/templates/thread-export.tmpl.php';
-            $content = ob_get_contents();
-            ob_end_clean();
-            return $content;
-
+            return $this->asVar();
             break;
         }
     }
 
     static function getVarScope() {
       return array(
-        'complete' => __('Thread Correspondence'),
+        'complete' =>array('class' => 'ThreadEntries', 'desc' => __('Thread Correspondence')),
         'original' => array('class' => 'MessageThreadEntry', 'desc' => __('Original Message')),
         'lastmessage' => array('class' => 'MessageThreadEntry', 'desc' => __('Last Message')),
       );
@@ -3047,6 +3040,43 @@ implements TemplateVariable {
     }
 }
 
+class ThreadEntries {
+    var $thread;
+
+    function __construct($thread) {
+        $this->thread = $thread;
+    }
+
+    function asVar() {
+        return $this->getVar();
+    }
+
+    function getVar($name='') {
+
+        $order = '';
+        switch ($name) {
+        case 'reversed':
+            $order = '-';
+        default:
+            $content = '';
+            $thread = $this->thread;
+            ob_start();
+            include INCLUDE_DIR.'client/templates/thread-export.tmpl.php';
+            $content = ob_get_contents();
+            ob_end_clean();
+            return $content;
+            break;
+        }
+    }
+
+    static function getVarScope() {
+      return array(
+        'reversed' => sprintf('%s %s', __('Thread Correspondence'),
+            __('in reversed order')),
+      );
+    }
+}
+
 // Ticket thread class
 class TicketThread extends ObjectThread {
     static function create($ticket=false) {
diff --git a/include/client/templates/thread-export.tmpl.php b/include/client/templates/thread-export.tmpl.php
index 5914a88da..2ed2288d8 100644
--- a/include/client/templates/thread-export.tmpl.php
+++ b/include/client/templates/thread-export.tmpl.php
@@ -12,7 +12,7 @@ AttachmentFile::objects()->filter(array(
             ))->all();
 
 $entries = $thread->getEntries();
-$entries->filter(array('type__in' => array_keys($entryTypes)));
+$entries->filter(array('type__in' => array_keys($entryTypes)))->order_by("{$order}id");;
 ?>
 <style type="text/css">
     div {font-family: sans-serif;}
-- 
GitLab