diff --git a/include/class.staff.php b/include/class.staff.php index 8eb4aab3996fb43e95798f8c5de09d8a09489a21..e3273fae5c5e0b3715b90905944caa0327377818 100644 --- a/include/class.staff.php +++ b/include/class.staff.php @@ -86,7 +86,8 @@ implements AuthenticatedUser, EmailContact, TemplateVariable { // Defaults array( 'default_from_name' => '', - 'datetime_format' => '', + 'datetime_format' => '', + 'thread_view_order' => '', )); $this->_config = $_config->getInfo(); } @@ -675,6 +676,7 @@ implements AuthenticatedUser, EmailContact, TemplateVariable { $_config->updateAll(array( 'datetime_format' => $vars['datetime_format'], 'default_from_name' => $vars['default_from_name'], + 'thread_view_order' => $vars['thread_view_order'], ) ); $this->_config = $_config->getInfo(); diff --git a/include/class.thread.php b/include/class.thread.php index 595b9671b7ef4828593d7ebdf59737d877d8b11b..6abb8a8df4f3a058247a55f7fb610539cd37cc4f 100644 --- a/include/class.thread.php +++ b/include/class.thread.php @@ -244,6 +244,9 @@ class Thread extends VerySimpleModel { if ($type && is_array($type)) $entries->filter(array('type__in' => $type)); + if ($options['sort'] && !strcasecmp($options['sort'], 'DESC')) + $entries->order_by('-id'); + // Precache all the attachments on this thread AttachmentFile::objects()->filter(array( 'attachments__thread_entry__thread__id' => $this->id diff --git a/include/staff/profile.inc.php b/include/staff/profile.inc.php index c3fd062b8cdd2d1f03eb3207a56dc75a85620485..e71a1d1e0a10383b297b54e5cb4cc8c262654956 100644 --- a/include/staff/profile.inc.php +++ b/include/staff/profile.inc.php @@ -223,7 +223,29 @@ if ($avatar->isChangeable()) { ?> <div class="error"><?php echo $errors['default_from_name']; ?></div> </td> </tr> - + <tr> + <td><?php echo __('Thread View Order');?>: + <div class="faded"><?php echo __('The order of thread entries');?></div> + </td> + <td> + <select name="thread_view_order"> + <?php + $options=array( + 'desc' => __('Descending'), + 'asc' => __('Ascending'), + '' => '— '.__('System Default').' —', + ); + foreach($options as $k=>$v) { + echo sprintf('<option value="%s" %s>%s</option>', + $k + ,($staff->thread_view_order == $k) ? 'selected="selected"' : '' + ,$v); + } + ?> + </select> + <div class="error"><?php echo $errors['thread_view_order']; ?></div> + </td> + </tr> <tr> <td><?php echo __('Default Signature');?>: <div class="faded"><?php echo __('This can be selected when replying to a thread');?></div> diff --git a/include/staff/templates/task-view.tmpl.php b/include/staff/templates/task-view.tmpl.php index 47acc7a7cab3cb527026fcd8705953e737f42a01..a2a95ff4ebd01632f320c40a97fa89d4fbfac89e 100644 --- a/include/staff/templates/task-view.tmpl.php +++ b/include/staff/templates/task-view.tmpl.php @@ -311,7 +311,8 @@ if (!$ticket) { ?> $task->getThread()->render(array('M', 'R', 'N'), array( 'mode' => Thread::MODE_STAFF, - 'container' => 'taskThread' + 'container' => 'taskThread', + 'sort' => $thisstaff->thread_view_order ) ); ?> diff --git a/include/staff/templates/thread-entries.tmpl.php b/include/staff/templates/thread-entries.tmpl.php index a3f3bdfc725a80b0c636933286c3b50b63d03e01..cabd7560b722df841904cfda1398f87bd93eb0ab 100644 --- a/include/staff/templates/thread-entries.tmpl.php +++ b/include/staff/templates/thread-entries.tmpl.php @@ -1,5 +1,15 @@ <?php -$events = $events->order_by('id'); + +$sort = 'id'; +if ($options['sort'] && !strcasecmp($options['sort'], 'DESC')) + $sort = '-id'; + +$cmp = function ($a, $b) use ($sort) { + return ($sort == 'id') + ? ($a < $b) : $a > $b; +}; + +$events = $events->order_by($sort); $events = $events->getIterator(); $events->rewind(); $event = $events->current(); @@ -33,7 +43,7 @@ foreach (Attachment::objects()->filter(array( // changes in dates between thread items. foreach ($entries as $entry) { // Emit all events prior to this entry - while ($event && $event->timestamp < $entry->created) { + while ($event && $cmp($event->timestamp, $entry->created)) { $event->render(ThreadEvent::MODE_STAFF); $events->next(); $event = $events->current(); @@ -79,6 +89,7 @@ foreach (Attachment::objects()->filter(array( $('#'+container).data('imageUrls', <?php echo JsonDataEncoder::encode($urls); ?>); // Trigger thread processing. if ($.thread) - $.thread.onLoad(container); + $.thread.onLoad(container, + {autoScroll: <?php echo $sort == 'id' ? 'true' : 'false'; ?>}); }); </script> diff --git a/include/staff/ticket-view.inc.php b/include/staff/ticket-view.inc.php index 16e22b0168cb1b3cdaa7d7e4c42bee11f501186b..ca3ba69cdbdc3092cd8ded141dd72130f7015ecd 100644 --- a/include/staff/ticket-view.inc.php +++ b/include/staff/ticket-view.inc.php @@ -497,8 +497,10 @@ $tcount = $ticket->getThreadEntries($types)->count(); $ticket->getThread()->render( array('M', 'R', 'N'), array( - 'html-id' => 'ticketThread', - 'mode' => Thread::MODE_STAFF) + 'html-id' => 'ticketThread', + 'mode' => Thread::MODE_STAFF, + 'sort' => $thisstaff->thread_view_order + ) ); ?> <div class="clear"></div>