# FILE: fcn-ms_m.pl
# DESCRIPTION: Message Management (miscellaneous functions)
#-------------------------------------------------------------------------------
# DISCUS COPYRIGHT NOTICE
#
# Discus is copyright (c) 2002 by DiscusWare, LLC, all rights reserved.
# The use of Discus is governed by the Discus License Agreement which is
# available from the Discus WWW site at:
#    http://www.discusware.com/discus/license
#
# Pursuant to the Discus License Agreement, this copyright notice may not be
# removed or altered in any way.
#-------------------------------------------------------------------------------

use strict;
use vars qw($GLOBAL_OPTIONS $DCONF $PARAMS);

###
### reorder_messages
###
### Reorders messages on a page
###

sub reorder_messages {
	my ($arg) = @_;
	my $file = GetPage($arg->{topic}, $arg->{page}, { lock => 1 });
	@{$file->{messages}} = sort { $arg->{order}->{ $a->{number} } <=> $arg->{order}->{ $b->{number} } } @{$file->{messages}};
	$file->{general}->{subtopic_raw} = 1;
	$file->{general}->{messages_raw} = 0;
	SetPage($file, { unlock => 1 });
	my @morder = ();
	foreach my $l (@{$file->{messages}}) {
		push @morder, $l->{number};
	}
	dreq("fcn-tree");
	my $uh = { post_list => join(",",@morder), page => $arg->{page} };
	my $tree = read_tree($arg->{topic}, { no_unlock => 1 });
	$tree = tree_record_update($tree, undef, $uh);
	write_tree($arg->{topic}, $tree, { no_lock => 1 });
	topic_tree_to_main($tree) if $arg->{topic} == $arg->{page};
	return \@morder;
}

1;
