# FILE: fcn-pgdl.pl
# DESCRIPTION: Delete a page from the user interface
#-------------------------------------------------------------------------------
# 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);

###
### remove_page
###
### Removes a page or links.  Incoming values required are:
###		topic =>		Topic number
###		page =>			Page number (parent of pages to be removed)
###		remove =>		Pages to remove (comma-delimited list of numbers)
### Optionally, define $tree_file_in and $cat_array_in.  If you do that,
### the tree array will be updated but changes won't be saved.  If
### not, this handles the tree file for you too.
###

sub remove_page {
	my ($arg, $tree_file_in, $cat_array_in, $file_in) = @_;
	undef my $nuke;
	my @children = ();
	dreq("fcn-tree");
	my $tree = defined $tree_file_in ? $tree_file_in : read_tree($arg->{topic}, { no_unlock => 1 });
	my $cat_array = defined $cat_array_in ? $cat_array_in : tree_categorize($tree);
	if (ref $arg->{remove} eq 'HASH') {
		$nuke = $arg->{remove};
		foreach my $i (keys(%{$arg->{remove}})) {
			push (@children, tree_dump_into_array(tree_build_children_of($tree, $cat_array, $i)));
		}
	} else {
		foreach my $i (split(/,/, $arg->{remove})) {
			$nuke->{$i} = 1;
			push (@children, tree_dump_into_array(tree_build_children_of($tree, $cat_array, $i)));
		}
	}
	my @nuke = ();
	foreach my $i (@children) { push (@nuke, $i->{page}); }
	foreach my $i (keys(%{ $nuke })) { push (@nuke, $i); }
	my $pg = undef;
	($tree, $cat_array, $pg) = tree_remove_page($tree, $cat_array, $arg->{remove}, $arg->{page});
	if (! defined $tree_file_in) {
		write_tree($arg->{topic}, $tree, { no_lock => 1 });
		topic_tree_to_main($tree);
	}
	undef my $ding_msg;
	foreach my $i (@{ $pg }) { $ding_msg->{$i} = 1; }
	my $file = defined $file_in ? $file_in : GetPage($arg->{topic}, $arg->{page}, { lock => 1 });
	$file->{sublist} = expand_sublist($file->{sublist}, $arg->{topic}, $tree);
	my @newst = ();
	foreach my $n (@{ $file->{sublist} }) {
		next if $nuke->{$n->{number}};
		push (@newst, $n);
	}
	$file->{sublist} = \@newst;
	$file->{general}->{subtopic_raw} = 0;
	$file->{general}->{messages_raw} = 1;
	SetPage($file, { unlock => 1 }) if ! defined $file_in;
	recursive_attachment_deleter(\@nuke, $arg);
	dreq("fcn-logs");
	if ($DCONF->{pro} && $GLOBAL_OPTIONS->{database} == 1) {
		delete_entry ({ files_already_locked => $arg->{files_already_locked}, topic => $arg->{topic}, database => "log", messages => $ding_msg });
		delete_entry ({ files_already_locked => $arg->{files_already_locked}, topic => $arg->{topic}, database => "search", messages => $ding_msg });
	} else {
		delete_entry ({ files_already_locked => $arg->{files_already_locked}, topic => $arg->{topic}, messages => $ding_msg });
	}
	if (! $arg->{no_upward_update}) {
	 	pages_upward_update($tree, $cat_array, [ $arg->{page} ]);
		dreq("topic-pg"); regenerate_topic_page();
	}
	return {
		tree => $tree,
		cat_array => $cat_array,
		oldfile => $file,
	};
}

###
### recursive_attachment_deleter
###
### Removes images and attachments associated with certain pages
###

sub recursive_attachment_deleter {
	my ($nukes, $arg, $taskman) = @_;
	my $flag = ($GLOBAL_OPTIONS->{discus_taskman} && $GLOBAL_OPTIONS->{taskman_image});
	$flag = 0 if $taskman;
	my @nuke = ref $nukes eq 'ARRAY' ? @{$nukes} : ($nukes);
	if ($flag == 1) {
		foreach my $pg (@nuke) {
			my @a = ($pg, $arg, 1);
			taskman_create_job("recursive_attachment_deleter", "fcn-pgdl", \@a);
		}
	} else {
		foreach my $pg (@nuke) {
			my $u = GetPage($arg->{topic}, $pg, { no_unlock => 1, no_lock => 1, no_error => 1 });
			my $dir = get_message_path($arg->{topic});
			if (ref $u->{messages} eq 'ARRAY') {
				unlink map { join("/", $dir, $_->{filename}) } @{attachment_scan($u->{messages})};
			}
			unlink "$dir/$pg.$DCONF->{ext}";
		}
	}
	return scalar(@nuke);	
}


1;
