# FILE: fcn-pgad.pl
# DESCRIPTION: Adds new pages
#-------------------------------------------------------------------------------
# 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);

###
### 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) = @_;
	undef 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 = GetPage($param->{topic}, $param->{page}, { lock => 1} );
	$file->{head}->{param} = $param->{param};
	$file->{sublist} = expand_sublist($file->{sublist}, $file->{head}->{topic_number});
	SetPage($file, { unlock => 1 });

	dreq("fcn-tree");
	undef my $tree;
	if (defined $arg->{tree_ref}) {
		$tree = $arg->{tree_ref};
	} else {
		$tree = 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 (! defined $arg->{tree_ref}) {
		write_tree($param->{topic}, $tree_out, { no_lock => 1 }) if defined $tree_out;
		topic_tree_to_main($tree_out, $param->{topic}) if $param->{topic} == $param->{page};
	}
	return {
		file => $file,
		tree => $tree_out,
		cat => $cat,
	};
}

###
### add_link
###
### Adds a link as a subtopic onto your board.  You should use these inputs:
###		name =>			Name of this link
###		URL =>			URL for this link
###		target =>		Target (4=_self, 5=Main, 6=_top, 7=_parent, 8=_blank)
###		topic =>		Topic Number
###		host =>			Host (Page Number of this subtopic's parent)
### Here are some other inputs you might want to use:
###		descr =>		Description for this link
###		position =>		Position (0=bottom, 1=top, 2=alphabetized)
###		tree_ref =>		Tree reference (if defined, tree file won't be written)
###		cat_ref =>		Categorized tree reference
###

sub add_link {
	my ($arg) = @_;
	undef my $param;
	$param->{target} = ("", "", "", "", "_self", "Main", "_top", "_parent", "_blank")[$arg->{type}];
	$param->{URL} = ($arg->{'URL'} =~ m|^(\w+)://| ? $arg->{'URL'} : error_message("Add Link Error", "Link to [$param->{URL}] is not valid.  It should start with http:// or https://!", 0, 1));
	$param->{descr} = ($arg->{'descr'} =~ m|\S| ? $arg->{'descr'} : "");
	$param->{topic} = $arg->{'topic'}; $param->{'topic'} =~ s/\D//g;
	$param->{host} = $arg->{'host'}; $param->{'host'} =~ s/\D//g;
	$param->{number} = ( defined($arg->{number}) ? $arg->{number} : get_number() );
	$param->{name} = ( defined($arg->{name}) ? $arg->{name} : "Untitled Link" );
	$param->{position} = ($arg->{'position'} == 0 ? 0 : $arg->{'position'});
	$param->{icon} = $1 if $arg->{'icon'} =~ m|^(\w+)$|;
	undef my $tree;
	if (defined $arg->{tree_ref}) {
		$tree = $arg->{tree_ref};
	} else {
		$tree = read_tree($param->{topic}, { no_unlock => 1 });
	}
	my $page_hash = GetPage($param->{topic}, $param->{host}, { lock => 1 });
	undef my $new_subtopic_hash;
	$new_subtopic_hash->{name} = $param->{name};
	$new_subtopic_hash->{number} = $param->{number};
	$new_subtopic_hash->{descr} = $param->{descr};
	$new_subtopic_hash->{type} = 1;
	$new_subtopic_hash->{url} = $param->{'URL'};
	$new_subtopic_hash->{target} = $param->{'target'};
	$new_subtopic_hash->{icon} = $param->{'icon'} || 'tree_m';
	$page_hash->{sublist} = expand_sublist($page_hash->{sublist}, $param->{topic}, $tree);
	$page_hash->{general}->{subtopic_raw} = 0;
	push (@{ $page_hash->{sublist} }, $new_subtopic_hash) if $param->{position} != 1;
	unshift (@{ $page_hash->{sublist} }, $new_subtopic_hash) if $param->{position} == 1;
	@{ $page_hash->{sublist} } = sort alpha_sort @{ $page_hash->{sublist} } if $param->{position} == 2;
	@{ $page_hash->{sublist} } = sort sort_archive_method @{ $page_hash->{sublist} } if $GLOBAL_OPTIONS->{sort_archive_method_on};
	SetPage($page_hash, { unlock => 1 });

	undef my $new_subtopic_tree;
	$new_subtopic_tree->{topic} = $param->{topic};
	$new_subtopic_tree->{page} = $param->{number};
	$new_subtopic_tree->{name} = $param->{name};
	$new_subtopic_tree->{parent} = $param->{host};
	$new_subtopic_tree->{islink} = 1;
	$new_subtopic_tree->{target} = $param->{'target'};
	$new_subtopic_tree->{url} = $param->{'URL'};
	$new_subtopic_tree->{posts} = 0;
	$new_subtopic_tree->{param} = undef;
	$new_subtopic_tree->{properties} = undef;
	$new_subtopic_tree->{props}->{icon} = $new_subtopic_hash->{icon};
	$new_subtopic_tree->{lastmod} = 0;
	$new_subtopic_tree->{originator} = "";
	$new_subtopic_tree->{subs} = 0;
	$new_subtopic_tree->{last_poster} = "";

	dreq("fcn-tree");
	my ($treeref2, $cat) = tree_insert_record($tree, $arg->{cat_ref}, $new_subtopic_tree, $param);
	($treeref2, $cat) = tree_upward_update($treeref2, $cat, $param->{host}, { metoo => 1, subs => '+1' });
	if (! defined $arg->{tree_ref}) {
		write_tree($param->{topic}, $treeref2, { no_lock => 1 });
		$arg->{main_tree} = topic_tree_to_main($treeref2);
	}
	return {
		number => $param->{number},
		tree => $treeref2,
		cat => $cat,
		file => $page_hash,
		main_tree => $arg->{main_tree},
	};
}

###
### add_page
###
### Adds a discussion page onto your board.  You should use these inputs:
###		name =>			Name of this subtopic
###		topic =>		Topic Number
###		host =>			Host (Page Number of this subtopic's parent)
###		type =>			Page type (0=Private Index,1=Public Index,2=Message,3=Closed Thread,4=Archive)
### Here are some other inputs you might want to use:
###		position =>		Position (0=bottom, 1=top, 2=alphabetized)
###		descr =>		Description for this subtopic
###		lastmod =>		Last modified time (seconds since epoch)
###		last_poster =>	Name of last person to post
###		originator =>	First poster in this conversation
###		msg_count =>	Number of messages under this point
###		post_update =>	Update the number of posts above this point
###		post_list =>	Set the post list for this new page
###		subs =>			Number of subtopics under this point
###		tree_ref =>		Tree reference (for adding multiple pages)
###		cat_ref =>		Categorized tree reference
###

sub add_page {
	my ($arg) = @_;
	undef my $param;
	$param->{descr} = $arg->{'descr'} =~ m|\S| ? $arg->{'descr'} : "";
	$param->{topic} = $arg->{'topic'}; $param->{'topic'} =~ s/\D//g;
	$param->{host} = $arg->{'host'}; $param->{'host'} =~ s/\D//g;
	$param->{lastmod} = $arg->{lastmod} != 0 ? $arg->{lastmod} : time;
	$param->{icon} = $arg->{icon} if $arg->{icon} ne "";
	$param->{last_poster} = $arg->{'last_poster'} eq "" ? "&nbsp;" : $arg->{'last_poster'};
	$param->{originator} = $arg->{'originator'} eq "" ? "&nbsp;" : $arg->{'originator'};
	$param->{msg_count} = $arg->{'msg_count'} == 0 ? 0 : $arg->{'msg_count'};
	$param->{subs} = $arg->{'subs'} == 0 ? 1 : $arg->{'subs'};
	$param->{number} = defined $arg->{force_new_page_number} ? $arg->{force_new_page_number} : get_number();
	$param->{name} = defined($arg->{name}) ? $arg->{name} : "Untitled Subtopic";
	$param->{position} = $arg->{'position'} == 0 ? 0 : $arg->{'position'};
	$param->{post_update} = $arg->{new_post} > 0 ? "+$arg->{new_post}" : "";
	$param->{post_list} = $arg->{post_list} eq "" ? "" : $arg->{post_list};
	$param->{emoticon} = $arg->{emoticon} eq "" ? "" : $arg->{emoticon};
	$param->{messages} = defined $arg->{messages} ? $arg->{messages} : \();
	$param->{type} = defined $arg->{type} ? $arg->{type} : 0;
	$param->{stype} = defined $arg->{stype} ? $arg->{stype} : 0;
	my $page_hash = defined $arg->{pagehash} ? $arg->{pagehash} : GetPage($param->{topic}, $param->{host}, { lock => 1 });
	undef my $new_page_hash;
	$new_page_hash->{head} = $page_hash->{head};
	$new_page_hash->{parent} = $param->{host};
	$new_page_hash->{me_number} = $param->{number};
	$new_page_hash->{me_name} = $param->{name};
	$new_page_hash->{topic_number} = $param->{topic};
	$new_page_hash->{topic_name} = $page_hash->{head}->{topic_name};
	$new_page_hash->{levels} = $page_hash->{head}->{levels};
	$new_page_hash->{properties} = "emot=$param->{emoticon}" if $param->{emoticon};
	if ($arg->{type} =~ m|^\d+$|) {
		$new_page_hash->{param} = "Sublist" if $arg->{type} == 0;
		$new_page_hash->{param} = "SublistCreate" if $arg->{type} == 1;
		$new_page_hash->{param} = "MessagesAdd" if $arg->{type} == 2;
		$new_page_hash->{param} = "Messages" if $arg->{type} == 3;
		$new_page_hash->{param} = "MessagesArchive" if $arg->{type} == 4;
		$new_page_hash->{param} = "SublistArchive" if $arg->{type} == 5;
	} else {
		$new_page_hash->{param} = $arg->{type};
	}
	$page_hash->{general}->{subtopic_raw} = 0;
	undef my $file;
	undef my $new_subtopic_hash;
	$new_subtopic_hash->{name} = $param->{name};
	$new_subtopic_hash->{number} = $param->{number};
	$new_subtopic_hash->{descr} = $param->{descr};
	$new_subtopic_hash->{stype} = $param->{stype};
	$new_subtopic_hash->{url} = "$DCONF->{message_url}/$param->{topic}/$param->{number}.$DCONF->{ext}";
	$new_subtopic_hash->{url} .= "?$param->{lastmod}" if !$DCONF->{noqm};
	$new_subtopic_hash->{last_poster} = $param->{last_poster};
	$new_subtopic_hash->{originator} = $param->{originator};
	$new_subtopic_hash->{msg_count} = $param->{msg_count};
	$new_subtopic_hash->{subs} = $param->{subs};
	$new_subtopic_hash->{lastmod} = $param->{lastmod};
	$new_subtopic_hash->{property_emot} = $param->{emoticon};
	$new_subtopic_hash->{param} = $new_page_hash->{param};
	$new_subtopic_hash->{'icon'} = tree_icon_chooser($param->{'icon'}, $new_page_hash->{'param'}, undef);
	my $tree = defined $arg->{tree_ref} ? $arg->{tree_ref} : read_tree($param->{topic}, { no_unlock => 1 });
	undef $PARAMS->{"lookup:$param->{topic}"};
	$page_hash->{sublist} = expand_sublist($page_hash->{sublist}, $param->{topic}, $tree);
	$page_hash->{head}->{param} .= "Sublist" if $page_hash->{head}->{param} !~ /Sublist/;
	push (@{ $page_hash->{sublist} }, $new_subtopic_hash) if $param->{position} != 1;
	unshift (@{ $page_hash->{sublist} }, $new_subtopic_hash) if $param->{position} == 1;
	@{ $page_hash->{sublist} } = sort alpha_sort @{ $page_hash->{sublist} } if $param->{position} == 2;
	@{ $page_hash->{sublist} } = sort sort_archive_method @{ $page_hash->{sublist} } if $GLOBAL_OPTIONS->{sort_archive_method_on};
	SetPage($page_hash, { privcache => $arg->{privcache}, unlock => 1 }) if ! defined $arg->{pagehash};
	$file = $page_hash;
	my $inp = create_new_page($new_page_hash, $page_hash);
	$inp->{messages} = $param->{messages} if ref $param->{messages} eq "ARRAY";
	push @{$inp->{messages}}, $arg->{first_post} if defined $arg->{first_post};
	SetPage( $inp ) if ! $arg->{nosetpage};
	$file = $inp if $arg->{jump};
	undef my $new_subtopic_tree;
	$new_subtopic_tree->{topic} = $param->{topic};
	$new_subtopic_tree->{page} = $param->{number};
	$new_subtopic_tree->{name} = $param->{name};
	$new_subtopic_tree->{parent} = $param->{host};
	$new_subtopic_tree->{posts} = $param->{msg_count};
	$new_subtopic_tree->{param} = $new_page_hash->{param};
	$new_subtopic_tree->{properties} = undef;
	# Next line was commented out
	$new_subtopic_tree->{props}->{icon} = $new_subtopic_hash->{icon} if defined $new_subtopic_hash->{icon};
	$new_subtopic_tree->{props}->{emot} = $param->{emoticon} if $param->{emoticon};
	$new_subtopic_tree->{lastmod} = $param->{lastmod};
	$new_subtopic_tree->{originator} = $param->{originator};
	$new_subtopic_tree->{subs} = $param->{subs};
	$new_subtopic_tree->{last_poster} = $param->{last_poster};
	$new_subtopic_tree->{post_list} = $param->{post_list};
	dreq("fcn-tree");
	my ($treeref2, $cat) = tree_insert_record($tree, $arg->{cat_ref}, $new_subtopic_tree, $param);
	($treeref2, $cat) = tree_upward_update($treeref2, $cat, $param->{host}, { metoo => 1, subs => '+1' });
	if (! defined $arg->{tree_ref} ) {
		write_tree($param->{topic}, $treeref2, { no_lock => 1 });
		$arg->{main_tree} = topic_tree_to_main($treeref2);
	}
	return {
		number => $param->{number},
		tree => $treeref2,
		cat => $cat,
		file => $file,
		main_tree => $arg->{main_tree},
		pghash => $page_hash,
		newfile => $inp,
	};
}

###
### create_new_page
###
### Creates a new, blank page (called internally)
###

sub create_new_page {
	my ($params, $parent_param) = @_;
	undef my $result;
	$result->{pginfo}->{headers_asstring} = "<!--Me: $params->{me_number}/$params->{me_name}-->\n";
	$result->{head}->{me_number} = $params->{me_number};
	$result->{head}->{me_name} = $params->{me_name};
	$result->{pginfo}->{headers_asstring} .= "<!--Topic: $params->{topic_number}/$params->{topic_name}-->\n";
	$result->{head}->{topic_number} = $params->{topic_number};
	$result->{head}->{topic_name} = $params->{topic_name};
	$result->{pginfo}->{headers_asstring} .= "<!--Param: $params->{param}-->\n";
	$result->{head}->{param} = $params->{param};
	$result->{pginfo}->{headers_asstring} .= "<!--Properties: $params->{properties}-->\n";
	$result->{head}->{properties} = $params->{properties};
	$result->{pginfo}->{headers_asstring} .= "<!--Parent: $params->{parent}-->\n";
	$result->{head}->{parent} = $params->{parent};
	$result->{head}->{levels} = [{}]; # Cool huh?
	if ($params->{me_number} != $params->{topic_number}) {
		my $ctr = 0;
		foreach my $x (@{ $params->{levels} }) {
			next if $x->{level_number} == 0;
			$ctr++;
			$result->{pginfo}->{headers_asstring} .= "<!--Level $ctr: $x->{level_number}/$x->{level_name}-->\n";
			push (@{ $result->{head}->{levels} }, $x);
		}
		$ctr++;
		$result->{pginfo}->{headers_asstring} .= "<!--Level $ctr: $params->{me_number}/$params->{me_name}-->\n";
		undef my $nref;
		$nref->{level_number} = $params->{me_number};
		$nref->{level_name} = $params->{me_name};
		push (@{ $result->{head}->{levels} }, $nref);
	}
	$result->{general}->{showadminfromhere} = 1;
	@{ $result->{sublist} } = ();
	@{ $result->{messages} } = ();
	# Inherit META tags from parent
	$result->{meta_description} = $parent_param->{meta_description};
	$result->{meta_keywords} = $parent_param->{meta_keywords};
	$result->{meta_robots} = $parent_param->{meta_robots};
	return $result;
}

###
### page_mgr_add_subtopic
###
### Adds a subtopic to a page via the Page Manager
###

sub page_mgr_add_subtopic {
	my ($FORMref) = @_;
	dreq("webtags");
	my ($lint_subj, $subtopic_name) = webtags($FORMref->{subtopic}, 3, 1, 1);
	error_message("Add Subtopic Error - Problem With Name", $subtopic_name, 0, 1) if $lint_subj eq "!Error";
	my $add_args = {};
	if ($FORMref->{descr} ne "") {
		my ($lint_subj2, $descr) = webtags($FORMref->{descr}, 3, 1, 1);
		error_message("Add Subtopic Error - Problem With Description", $descr, 0, 1) if $lint_subj2 eq "!Error";
		$add_args->{'descr'} = $descr;
	}
	$add_args->{name} = $subtopic_name;
	$add_args->{topic} = $FORMref->{topic};
	$add_args->{host} = $FORMref->{page};
	$add_args->{type} = $FORMref->{createas};
	$add_args->{position} = $FORMref->{position};
	$add_args->{jump} = $FORMref->{jumpinto} == 1;
	$add_args->{emoticon} = $FORMref->{emoticon};
	$add_args->{icon} = $FORMref->{icon} if $FORMref->{icon} ne "";
	my $add_ref = {};
	if ($add_args->{'type'} <= 3) {
		$add_ref = add_page($add_args);
		$FORMref->{HTTP_REFERER} = "/$FORMref->{topic}/$add_ref->{number}.$DCONF->{ext}" if $add_args->{jump};
	} else {
		$add_args->{'URL'} = $FORMref->{'URL'};
		$add_ref = add_link($add_args);
	}
	pages_upward_update($add_ref->{tree}, $add_ref->{cat}, [ $add_args->{host} ]);
	dreq("topic-pg"); regenerate_topic_page(undef, $add_ref->{main_tree});
	$PARAMS->{"lookup:$add_args->{topic}"} = undef;
	page_mgr_sublist($FORMref, $add_ref->{file}, $add_ref->{tree});
}

1;
