# FILE: fcn-pg_m.pl
# DESCRIPTION: Page 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);

###
### rename_subtopic
###
### Gives a subtopic a new name
###

sub rename_subtopic {
	my ($topic, $page, $new_name, $user, $file_in_1, $file_in_2, $tree_in, $tree_cat_in) = @_;
	error_message("Rename subtopic error", "Topic cannot equal page in renaming subtopic.", 0, 1) if $topic == $page;
	dreq("fcn-tree", "fcn-regn");
	my $tree = defined $tree_in ? $tree_in : read_tree($topic, { no_unlock => 1 });
	my $hb = tree_record_update($tree, $tree_cat_in, { page => $page, name => $new_name }, 1);
	my $cat = $hb->{cat}; $tree = $hb->{tree};
	write_tree($topic, $tree, { no_lock => 1 }) if ! defined $tree_in;
	my $parent = $cat->{parent}->{$page};
	my $file1 = defined $file_in_1 ? $file_in_1 : GetPage($topic, $parent, { lock => 1 });
	$file1->{general}->{subtopic_raw} = 0;
	$file1->{sublist} = expand_sublist($file1->{sublist}, $topic, $tree);
	SetPage($file1, { unlock => 1 }) if ! defined $file_in_1;
	my $file2 = defined $file_in_2 ? $file_in_2 : GetPage($topic, $page, { lock => 1 });
	$file2->{head}->{me_name} = $new_name;
	my $l = scalar(@{$file2->{head}->{levels}})-1;
	$file2->{head}->{levels}->[$l]->{level_name} = $new_name;
	SetPage($file2, { unlock => 1 }) if ! defined $file_in_2;
	my $inc = incremental_descendants($topic, $page, "RS", $tree, $cat);
	return unlink "$DCONF->{admin_dir}/backups/$inc->{tempfile}.TMP" if $inc->{count} == 0;
	my $chgfile = regeneration_create_change_process_file("regen_level_change", $l, $new_name);
	return regenerate_board({ esc => 1, hold => regeneration_create_hold({ HTTP_REFERER => "/$topic/$parent" }), topic => $topic, tempfile => $inc->{tempfile}, done => 0, total => $inc->{count}, action => "regen3", username => $user, changefile => $chgfile, operation => 5, description => 501 });
}

###
### reorder_subtopics
###
### Reorders subtopics & links on a page
###

sub reorder_subtopics {
	my ($arg) = @_;
	dreq("fcn-tree");
	my $file = GetPage($arg->{topic}, $arg->{page}, { lock => 1 });
	@{$file->{sublist}} = sort { $arg->{order}->{ $a->{number} } <=> $arg->{order}->{ $b->{number} } } @{$file->{sublist}};
	$file->{general}->{subtopic_raw} = 0;
	my $tree = read_tree($arg->{topic}, { no_unlock => 1 });
	my $order = $arg->{order};
	my $coderef = sub { $order->{$a->[0]->{page}} <=> $order->{$b->[0]->{page}} };
	$tree = (tree_subtopic_reorder($tree, undef, $arg->{page}, $coderef, 0))[0];
	$file->{sublist} = expand_sublist($file->{sublist}, $arg->{topic}, $tree);
	write_tree($arg->{topic}, $tree, { no_lock => 1 });
	SetPage($file, { unlock => 1 });
	return $file->{sublist};
}

###
### change_page_layout
###
### Changes the layout of a given page with these parameters:
###		topic =>			Topic number
###		page =>				Page number
###		param =>			New parameters (Announce,Sublist,About,Messages,Add)
### If you're changing multiple pages at once you might want to define:
###		tree_ref =>			Tree reference
###		cat_ref =>			Categorized tree reference
###

sub change_page_layout {
	my ($arg, $file_in) = @_;
	my $param = {};
	$param->{'topic'} = (defined($arg->{topic}) ? $arg->{'topic'} : error_message("Invalid Topic", "Invalid topic received for layout change!", 0, 1));
	$param->{'page'} = (defined($arg->{page}) ? $arg->{'page'} : error_message("Invalid Page", "Invalid page received for layout change!", 0, 1));
	$param->{'param'} = (defined($arg->{layout}) ? $arg->{'layout'} : "AnnounceSublistAboutMessages");
	my $file = defined $file_in ? $file_in : GetPage($param->{topic}, $param->{page}, { lock => 1} );
	my $m = $file->{head}->{param};
	$file->{head}->{param} = $param->{param};
	SetPage($file, { unlock => 1 }) if ! defined $file_in;
	dreq("fcn-tree");
	my $tree = defined $arg->{tree_ref} ? $arg->{tree_ref} : read_tree($param->{topic}, { no_unlock => 1 });
	my ($tree_out, $cat) = tree_record_update($tree, $arg->{cat_ref}, { topic => $param->{topic}, page => $param->{page}, param => $param->{param} });
	if ($DCONF->{pro} && $GLOBAL_OPTIONS->{sort_archive_method_on}) {
		if (($m =~ /Archive/ && $param->{param} !~ /Archive/) || ($m !~ /Archive/ && $param->{param} =~ /Archive/)) {
			if ($file->{head}->{parent} > 0 && $file->{head}->{parent} != $param->{page}) {
				dreq("posting");
				my $parent_arrays = {};
				foreach my $refr (@{ $tree_out }) {
					next if $refr->{page} == 0;
					push (@{ $parent_arrays->{ $refr->{parent} } }, $refr) if $refr->{level} > 0;
					push (@{ $parent_arrays->{ 0 } }, $refr) if $refr->{level} == 0;
				}
				@{$parent_arrays->{ $file->{head}->{parent} } } = sort sort_archive_method @{$parent_arrays->{ $file->{head}->{parent} } };
				$tree_out = buildtree($parent_arrays, 0);
				my $pg_in = GetPage($param->{topic}, $file->{head}->{parent}, { lock => 1 });
				$pg_in->{general}->{subtopic_raw} = 0;
				$pg_in->{sublist} = expand_sublist($pg_in->{sublist}, $param->{topic}, $tree_out);
				@{$pg_in->{sublist}} = sort sort_archive_method @{$pg_in->{sublist}};
				SetPage($pg_in, { unlock => 1 });		
			}
		}
	}
	if (! defined $arg->{tree_ref}) {
		write_tree($param->{topic}, $tree_out, { no_lock => 1 }) if defined $tree_out;
		topic_tree_to_main($tree_out, undef) if $param->{topic} == $param->{page};
	}
	return {
		file => $file,
		tree => $tree_out,
		cat => $cat,
	};
}

###
### change_page_properties
###
### Saves changes to link/page properties
###

sub change_page_properties {
	my ($topic, $page, $lnum, $newprops, $tree_in, $cat_in) = @_;	
	my $Z = GetPage($topic, $page, { lock => 1 });
	my @U = save_subtopic_properties($topic, $Z->{sublist}, { $lnum => $newprops }, $tree_in, $cat_in);
	$Z->{sublist} = $U[0];
	$Z->{general}->{subtopic_raw} = 0;
	SetPage($Z, { unlock => 1 });
	push @U, $Z;
	return @U;	
}

###
### save_subtopic_properties
###
### Saves changes to subtopic or link properties
###

sub save_subtopic_properties {
	my ($topic, $subtopics, $updates, $tree_in, $tree_cat_in) = @_;
	dreq("fcn-tree");
	my $tree = defined $tree_in ? $tree_in : read_tree($topic, { no_unlock => 1 });
	my $cat = defined $tree_cat_in ? $tree_cat_in : tree_categorize($tree);
	foreach my $S (@{$subtopics}) {
		my $num = $S->{number};
		next if $updates->{$num} == 0;
		my $U = $updates->{$num};
		my $tl = $tree->[$cat->{array_pos}->{$num}];
		$S->{descr} = $U->{descr} if defined $U->{descr};
		$tl->{icon} = $U->{icon} if $U->{icon};
		if (defined $U->{name}) {
			$tl->{name} = $U->{name};
			$S->{name} = $U->{name};
		}
		$tl->{property_emot} = $U->{emot} if $U->{emot};
		$tl->{props}->{emot} = $U->{emot} if $U->{emot};
		$tl->{props}->{icon} = $U->{icon} if $U->{icon};
		$tl->{properties} = undef;
		$tl->{url} = $U->{url} if $U->{url};
		if ($U->{target}) {
			$tl->{target} = $U->{target};
			$S->{target} = $U->{target};
		}
		if (! $tl->{islink}) {
			my $Q = GetPage($topic, $num, { no_unlock => 1 });
			my $j = generate_properties_hash($Q->{head}->{properties});
			$j->{emot} = $tl->{props}->{emot};
			$j->{icon} = $tl->{props}->{icon};
			$Q->{head}->{properties} = generate_properties($j);
			SetPage($Q, { no_lock => 1 });	
		}
	}
	$subtopics = expand_sublist($subtopics, $topic, $tree);
	write_tree($topic, $tree, { no_lock => 1 }) if ! defined $tree_in;
	return ($subtopics, $tree, $cat);	
}

1;
