Recursive Wiki Page
Site Infrastructure / Recursive Wiki Page

[read modes: view history hierarchy discussion files]
[edit modes: edit]

[begin preamble]
$displayfunc = function {[

	$l = $_caller._this.language;

	if(($l == "wf") || ($l == ""))
		_wiki_format($_args[0])
	else if($l == "html")
		purify($_args[0])
	else if($l == "wiki")
		purify(wikka_format($_args[0]))
	else {Unknown markup language [$l].};

]};

$find_universe = function {[
	$p = $_parent;
	$u = title2id("Universe");

	while( ($p != 0) && (get_template($p) != $u) )
		$p = get_parent($p);

	return $p;
]};

[end preamble]

[begin mode history]
[
override_title($_title, "Page history");
override_parent({./?id=[$_id]}, {current version of [$_title]});

$n = 0;
$dd = foreach_key(@_this) {[
	if($_data == "language")
		$_data = ""
	else	if($_data == "user")
		$_data = ""
	else	if($_data == "redirect")
		$_data = ""
	else
		$_data = {[
			off = strpos(_data, " by ");
			if(off != -1) {
				[
					author = substr(_data, off + 4);
					date = substr(_data, 0, off);
				]
				<li><a href="./?id=[_id]&mode=view_history&date=[_data]">[date]</a> by [page("link", get_profile_id(author))]
			} else {<li><a href="./?id=[$_id]&mode=view_history&date=[$_data]">[_data]</a>};
		]};
	$n++;
]};

if($n > 0) {
	<ul>[$dd]</ul>
} else {
	<i>No revision history.</i><br>
};

]
Current version: <a href="./?id=[$_id]">[$_date]</a> by [if(_this.user > 0) page("link", get_profile_id(_this.user)) else {?}].
[end mode history]

[begin mode view_history]
[

if(@_this[$_params.date] != "") {[
	override_title($_title, {Revision from [$_params.date]});
	override_parent({./?id=[$_id]&mode=history}, {history of [$_title]});
	displayfunc(@_this[$_params.date]);

	{
		<br>
		[msg_start({font-size: 80%})]
		This is the page [$_title] as it appeared at [$_params.date].
		You can view other old versions at the
		<a href="./?id=[$_id]&mode=history">history page</a>.<br><br>

		Alternatively, you may want to settle for <a href="./?id=[$_id]">
		the current version</a>.
		[msg_end()]
	};
]} else {[
	override_title("Couldn't retrieve history", "Revision not found");
	override_parent({./?id=[$_id]&mode=history}, {history of [$_title]});
	
	{
		<p>The historical record you requested could not be found.
		Please return to the <a href="./?id=[$_id]&mode=history">history
		page</a> and select a valid entry.</p>

		<p>Alternatively, you may want to settle for <a href="./?id=[$_id]">
		the current version</a>.</p>
	};
]};

]
[end mode view_history]

[begin mode files]
[
uni = find_universe();

if(uni == 0)
	ut = "general"
else
	ut = get_title(uni);

override_title($_title, "Attached files");
override_parent({./?id=[$_id]}, $_title);

$path = {universes/[ut]/[$_id]};
$fullpath = {[$_globals.VROOT]/[$path]};

if(dir_exists($fullpath))
	$flist = dir($path)
else
	$flist = list();

{<ul style="display: relative;">};
foreach($flist) {[
	$_data = {
		<li style="clear: both;"><a href="./pictures/[$path]/[$_data]">[$_data]</a> 
		<span style="float: right; font-size: 80%;">
		[
		$fn = {[$_globals.VROOT]/[$path]/[$_data]};
		$fs = filesize($fn);
		$fsu = "B";

		if($fs > 1024) {[
			$fs = $fs / 1024;
			$fsu = "KB";
		]};

		if($fs > 1024) {[
			$fs = $fs / 1024;
			$fsu = "MB";
		]};

		if($fs > 1024) {[
			$fs = $fs / 1024;
			$fsu = "GB";
		]};

		{[int($fs * 100) / 100] [$fsu]};

		if(can_direct()) {
			<a href="./?id=[$_id]&mode=delete_file&file=[$_data]">delete</a> <a href="./?id=[$_id]&mode=rename_file&file=[$_data]">rename</a>
		}]
		</span>
	};
]};
{</ul>};

$n = count($flist);
if($n == 1) {1 attachment.} else {[$n] attachments.};

if(can_direct()) {
	<hr style="border: rgba(128, 128, 128, 0.4) 1px solid">

	<b style="display: block; margin-bottom: 10px; font-size: 80%">add a file</b>
	<form method="post" action="" enctype="multipart/form-data">
		<input type="file" name="ufile">
		<input type="hidden" name="id" value="[$_id]">
		<input type="hidden" name="mode" value="post_file">
		<input type="submit" value="Upload" name="button">
	</form>
}

]
[end mode files]

[begin mode delete_file]
[if(can_direct()) {[
	
	uni = find_universe();

	if(uni == 0)
		ut = "general"
	else
		ut = get_title(uni);

	$fn = {universes/[ut]/[$_id]/[basename($_params.file)]};
	$fullfn = {[$_globals.VROOT]/[$fn]};

	if(file_exists($fullfn) == false) {
		File not found. <a href="./?id=[$_id]&mode=files">Click here to go back</a>.
	} else {[
		$s = rm($fullfn);
		if($s == 1) {
			File removed.
			<a href="./?id=[$_id]&mode=files">Click here to go back</a>.
		} else {
			File not removed. Suspect file system permissions errors.
 			Contact <a href="mailto:[$_globals.ADMIN_EMAIL]">an 
			administrator</a> if you want it fixed.
			<a href="./?id=[$_id]&mode=files">Click here to go back</a>.			
		};
	]}

]} else {You shouldn't be here. <a href="./?id=[$_id]&mode=files">Click here to go back</a>.}]
[end mode delete_file]

[begin mode rename_file]
[if(can_direct()) {
[
	override_title("Rename Attachment", $_title);
	override_parent({./?id=[$_id]&mode=files}, {[$_title] attachments});
]

<form method="post" action="">
	<input type="hidden" name="id" value="[$_id]">
	<input type="hidden" name="mode" value="rename_file_submit">
	<input type="hidden" name="file" value="[basename($_params.file)]">
	<table>
	<tr><td>From:<td>[basename($_params.file)]
	<tr><td>To:<td><input type="text" name="newname" value="[basename($_params.file)]">
	<tr><td><td><input type="submit" value="Rename"> <a href="./?id=[$_id]&mode=files">Cancel</a>
	</table>
</form>

} else {You shouldn't be here. <a href="./?id=[$_id]&mode=files">Click here to go back</a>.}]
[end mode rename_file]

[begin mode rename_file_submit]
[if(can_direct()) {[

	override_title("Renaming Attachment", $_title);
	override_parent({./?id=[$_id]&mode=files}, {[$_title] attachments});

	$pp = basename($_params.file);
	$pt = basename($_params.newname);

	uni = find_universe();
	
	if(uni == 0)
		ut = "general"
	else
		ut = get_title(uni);

	$path = {universes/[ut]/[$_id]};

	if($pp == $pt) {
		No? Alright...<br><br>
		<a href="./?id=[$_id]&mode=files">Go back to the attachments page.</a>
	} else if(file_exists({[$_globals.VROOT]/[$_group]/[$_id]/[$pt]})) {
		Destination file already exists. Perhaps you already
		renamed the file?<br><br>
		<a href="./?id=[$_id]&mode=files">Go back to the attachments page.</a>
	} else if(file_exists({[$_globals.VROOT]/[$path]/[$pp]})) {[
		$fpp = {[$_globals.VROOT]/[$path]/[$pp]};
		$fpt = {[$_globals.VROOT]/[$path]/[$pt]};
		if(mv($fpp, $fpt)) {
			File renamed!
			[refresh_to({./?id=[$_id]&mode=files}, 0)]
			<script type="javascript"><!--
				document.location = "./?id=[$_id]&mode=files";
			--></script>
		} else {
			File could not be renamed.
		}]
		<br><br>
		<a href="./?id=[$_id]&mode=files">Go back to the attachments page.</a>
	} else {
		The source file was not found.<br><br>
		<a href="./?id=[$_id]&mode=files">Go back to the attachments page.</a>
	};

]} else {You shouldn't be here. <a href="./?id=[$_id]&mode=files">Click here to go back</a>.}]
[end mode rename_file_submit]

[begin mode post_file]
[if(can_direct()) {[

uni = find_universe();
	
if(uni == 0)
	ut = "general"
else
	ut = get_title(uni);

$parentpath = {universes/[ut]};
$parentfn = {[$_globals.VROOT]/[$parentpath]};
$fullfn = {[$parentfn]/[$_id]};

if(dir_exists($parentfn) == false) mkdir($parentfn);
if(dir_exists($fullfn) == false) mkdir($fullfn);

$na = file_upload("ufile", $fn, 20971520);
$fn = slice($na, 2, 1);
$size = slice($na, 1, 1);

if($fn != "null") {
	[
	mute mv($fn, $fullfn & "/" & basename($fn));

	if(_group != 0) _post_message_group(_group, {<a href="http://anthologi.ca/?id=[_viewer.profile_id]">[_viewer.name]</a> added the file [basename($fn)] to <b>[_title]</b> on the project <b>[get_group_name(_group)]</b>.<br><br><a href="http://anthologi.ca/?id=[_id]&mode=files">Click here to view the attachments for that page.</a>}, _viewer.id, "Uploaded attachment: " & basename($fn));
	]
	Done. Upload successful! <a href="./?id=[$_id]&mode=files">Click here to go back</a>.

	[// refresh_to({./?id=[$_id]&mode=files}, 2)]
	<script type="javascript"><!--
		// document.location = "./?id=[$_id]&mode=files";
	--></script>
} else {
	File upload failed.[debug($na)]

	[if($size > 20971520) {
		The file was too large. Keep it under 20 MB!
	} else {
		Why is anyone's guess.
		Contact <a href="mailto:[$_globals.ADMIN_EMAIL]">an 
		administrator</a> if you want it fixed.
	}]

	<a href="./?id=[$_id]&mode=files">Click here to go back</a>.
};

]} else {You shouldn't be here. <a href="./?id=[$_id]&mode=files">Click here to go back</a>.}]
[end mode post_file]

[begin mode delete]
[msg_start()]
<p>This is the deletion page! There is no way to get a page back once it is deleted except from backups. All of its children will also be deleted. Any attachments will be left behind until cleaned up by an administrator. Do you really want to do this?</p>
<p><a href="./?id=[$_id]&mode=edit">No! This idea was bad and I should feel bad!</a></p>
[if(can_direct()) {
<p><a href="./?id=[$_id]&mode=delete_submit">Yes! Kill it with fire!</a></p>
<p>Other users in the project will be notified of the page's deletion. Administrators will be notified if a page with attachments is deleted.</p>
}]
[msg_end()]
[end mode delete]

[begin mode delete_submit]
[
search("delete", "parent", $_id, "template", title2id("Generic Comment"), list("no_return"));
search("delete_submit", "parent", $_id, "template", title2id("Recursive Wiki Page"), list("no_msg"));

if($_args[0] != "no_msg") if(_group != 0) _post_message_group(_group, {<a href="http://anthologi.ca/?id=[_viewer.profile_id]">[_viewer.name]</a> deleted <b>[_title]</b> from the project <b>[get_group_name(_group)]</b>.}, _viewer.id, "Deleted page: " & _title);

uni = find_universe();
	
if(uni == 0)
	pt = "general"
else
	pt = get_title(uni);

$path = {universes/[$pt]/[$_id]};
$parentfn = {[$_globals.VROOT]/universes/[$pt]/};
$fullfn = {[$_globals.VROOT]/[$path]};

if(dir_exists($parentfn) && dir_exists($fullfn)) {[

	$flist = dir($path);
	$fc = count($flist);

	if($fc == 0) {[rmdir($fullfn)]
		<p><i>Removing empty attachment directory</i>.</p>
	};

]} else {[
	$fc = 0;
]};

$success = delete_page($_id);

if($success > 0) {
	<p><b>[$_title]</b> has been removed from 
	<b>[get_title($_parent)]</b>.</p>
	[if($fc > 0) {
		[$m = _post_message_group(1, {Entry [$path] was deleted by [if($_owner.profile_id != 0) page("link", $_owner.profile_id) else $_owner.name], leaving [if($fc > 1) {[$fc] attachments} else {1 attachment}].}, "", "Deleted page with attachments: " & _title)]
		<p>Since this page had [if($fc > 1) {[$fc] attachments} else {an attachment}] when it was deleted, a message has been sent to the administrator group notifying [if($fc > 1) {they need} else {it needs}] to be cleaned up.</p>
		[if($m != 1) {<p>(However, there were errors in doing so.)</p>}]
	}]
} else {
	<p><b>[$_title]</b> could not be removed from 
	<b>[get_title($_parent)]</b> by you ([$_viewer.name]). If you 
	feel that should be able to do so, now might be a good time to
	<a href="mailto:[$_globals.ADMIN_EMAIL]?Subject=Can't delete 
	project page"> write a letter to 
	the administrator</a>.</p>
}]
<p><a href="./?id=[$_parent]">Return to [get_title($_parent)]</a></p>
[end mode delete_submit]

[begin mode abbr]
<li>[if(@_this.redirect != "") {<b><a href="./?id=[@_this.redirect]">[$_title]</a></b><span style="font-size: 60%; display: inline-block; vertical-align: top"> ↗[if(can_edit()) { <a href="./?id=[_id]&mode=edit">(edit)</a> }]</span>} else {<b><a href="./?id=[$_id]">[$_title]</a></b>}]
[
if($_subtitle != "") {<br><i>[$_subtitle]</i>};

$c = search("abbr", "parent", $_id, "template", $_template);
if($c != "") {
	<ul>[$c]</ul>
};
]
</li>
[end mode abbr]

[begin mode name]
<a href="./?id=[$_id]">[$_title]</a>  
[end mode name]
[begin mode hierarchy]
<ul>
[
	search("abbr", "parent", $_id, "template", $_template);
	override_parent({./?id=[$_id]}, $_title);
	override_title({Page hierarchy at [$_title]}, get_title(get_group_profile_id($_group)));
]
</ul>
[end mode hierarchy]
[begin mode option][
if($_args[0] != "show_project") {
	<option value="[$_id]">[$_title]</option>
} else {
	<option value="[$_id]">[$_title] ([get_title(get_group_profile_id($_group))])</option>
};
][end mode option]
[begin mode view]
[if(@_this.redirect != "") {[
	http_header({HTTP/1.1 307 Temporary Redirect});
	http_header({Location: http://anthologi.ca/?id=[@_this.redirect]});
]};

if(_group != 0) {[
	gpid = get_group_profile_id(_group);
	if(gpid != 0) page("css", gpid);
]};

if($_body != "") displayfunc($_body) else {
<h2>blank page</h2>
<p>This page was created [soft_age(_date)] ago by [if($_owner.profile_id != 0) page("link", $_owner.profile_id) else $_owner.name] and does not yet have any text.[if(can_edit()) { Click the edit link at the right to start working on it.}]</p>
}]
[
$listsize = 20;

$q = query("childs", $listsize, "title", "parent", $_id, "template", $_template);
$qc = query_count($q);
$c = query("comments", 1, "date desc, id", "parent", $_id, "template", title2id("Generic Comment"));
$cc = query_count($c);

$cc = $cc + (reduce( split(children("children_count", _id), chr(10)) ) ?(x, y) (x + y));
if($cc == "") $cc = 0;

if($cc > 0) $cdate = get_date(query_run("id", $c));
]

[
	uni = find_universe();
	
	if(uni == 0)
		ut = "general"
	else
		ut = get_title(uni);

$path = {universes/[ut]/[$_id]};
$fullpath = {[$_globals.VROOT]/[$path]};

if(dir_exists($fullpath))
	$flist = dir($path)
else
	$flist = list();

$fc = count($flist);

if(($qc > 0) || ($cc > 0) || ($fc > 0)) {	<hr style="border: rgba(128, 128, 128, 0.4) 1px solid">

	<b style="display: block; margin-bottom: 10px; font-size: 80%">read more</b>

	[if($qc > 0) {
		[
			query_run("name", $q);
			if($qc > $listsize) { ... };
		]
		<span style="display: block; text-align: right; font-size: 80%;">
		<a href="./?id=[$_id]&mode=hierarchy">see full hierarchy</a></span>
	}]

	[if($cc > 0) {
		<span style="display: block; text-align: right; font-size: 80%;">
		<a href="./?id=[$_id]&mode=discussion">see discussion 
		([
			if($cc > 1) {[$cc] messages} else {1 message}
		]; [soft_age($cdate)] ago)
		</a></span>
	}]

	[if($fc > 0) {
		<span style="display: block; text-align: right; font-size: 80%;">
		[if($fc == 1) {
			<a href="./pictures/[$path]/[$flist[0]]">download attachment
			([$flist[0]])</a>
		} else {
			<a href="./?id=[$_id]&mode=files">see attachments
			([$fc] files)</a>
		}]
		</span>
	}]
};
]

<br>

[
if(can_edit()) {<span style="text-align: right; display: block"><a href="./?id=[$_id]&mode=edit">Edit this page...</a></span>};
]

[
if(is_user_in_group($_viewer.id, $_group) && can_edit() && can_comment()) {
	<span style="text-align: right; display: block"><a href="javascript:void(0)" onclick="toggleId('newbox')">Create sub-page...</a></span>
	<form method="post" action="" id="newbox" style="display: none" class="mbox_parent">
		<input type="hidden" name="mode" value="new_child">
		<input type="hidden" name="id" value="[$_id]">
		<table><tr><th>Title:<td><input type="text" name="title" value="">
		<tr><td><td><input type="submit" value="Create Page"> <a href="javascript:void(0)" onclick="toggleId('newbox')">Cancel</a><br>
		<br>
		<small>This page will be created as a descendant of [$_title]. If you wish to create a child of some other page within the project, go to that page and follow the provided instructions. You may also create new pages by following dead links.</small>
		</table>		
	</form>
};
]

[end mode view]
[begin mode new_child]
[
	if(is_user_in_group($_viewer.id, $_group)) {[
		$i = new_page($_params.title, $_id);
		set_template($i, $_template);
		set_group($i, $_group);
		set_permissions($i, "cadrec-dr-cadrec--r-");
		_post_message_group($_group, {[if($_viewer.profile_id != 0) page("link", $_viewer.profile_id) else $_viewer.name] just created the article <a href="[bare_url()]?id=[$i]">[$_params.title]</a> under <a href="[bare_url()]?id=[$_id]">[$_title]</a> (Project: <a href="./?id=[get_group_profile_id($_group)]">[get_title(get_group_profile_id($_group))]</a>).}, $_viewer.id);
	]
	<b>Created</b>! Return to:

<ul>
	<li><a href="./?id=[$_id]">[$_title]</a>
	<li><a href="./?id=[get_group_profile_id($_group)]">
		[get_title(get_group_profile_id($_group))]</a>
	<li><a href="./?id=[$i]">[$_params.title]</a>
</ul>

	} else {
		<b>Error</b>: you don't belong to [get_group_name($_group)]. Contact [
		pi = get_profile_id(get_founder($_group));
		if(pi != 0)
			page("link", pi);
		else
			uid2name(pi);

] to get access.<br>
		<a href="javascript:window.history.back()">Go back.</a>
	};
]
[end mode new_child]

[begin mode edit]
	[if(_this.draft != "") msg({A draft version of this document exists. <b><a href="/?id=[_id]&mode=edit&restore_draft=1">Click here to edit it instead.</a></b>})]
	[
		if(_params.restore_draft == 1) {[
			_params.body = _this.draft;
		]};
	]
	<form method="post" action="" width="100%">
		<input type="hidden" name="id" value="[$_id]">
		<input type="hidden" name="mode" value="submit">
		<table width="100%">
		<tr><th>Title:</th><td><input type="text" class="textbox" name="title" value="[edit_display(iif($_params.title != "", $_params.title, $_title))]" style="width: 100%"></td></tr>
		<tr><th>Subtitle:</th><td><input type="text" class="textbox" name="subtitle" value="[edit_display(iif($_params.subtitle != "", $_params.subtitle, $_subtitle))]" style="width: 100%"></td></tr>
		<tr><th>Markup Format:</th><td>[
			_select(iif($_params.language != "", $_params.language, @_this.language), {wf<!>WF markup (BBcode/wiki hybrid)
html<!>HTML
wiki<!>Wikka transitional}, "language", "", {onchange="markup_hinter(this.value)"});
		]<br>
<span style="font-size: 80%" id="markup_hint"></span>
		<tr><th>Highlight:</th><td><input type="checkbox" name="priority" value="1" [if(($_params.priority == 1) || ($_priority == 1)) {checked}] id="priority"><label for="priority">Sort this page above others in lists.</label>
</td></tr>
		<tbody id="w:me_body" style="display: none;">
		<tr><th>Parent:</th><td><input type="text" class="textbox" name="parent" value="[iif($_params.parent != "", $_params.parent, $_parent)]"></td></tr>
		<tr><th>Permissions:</th><td>[permissions_control(iif($_params.permissions != "", $_params.permissions, $_permissions))]</td></tr>
		<tr><th>Redirect:</th><td><input type="text" class="textbox" name="redirect" value="[@_this.redirect]">
<p style="font-size: 80%">You can use this to redirect viewers to another page when they access this one. Specify the page's ID number, or clear this field to disable redirection.</p>
</td></tr>
		<tr><th>Owner:</th><td>[users_dropdown("author", iif($_params.author != "", $_params.author, $_author))]
<p style="font-size: 80%">You can use this to let people from outside the project work on just this one page.<br><b>Current owner: [if(get_profile_id(_author) != 0) page("link", get_profile_id(_author)) else uid2name(_author)]</b></p></td></tr>
		<tr><th>Group:</th><td>[groups_dropdown("group", iif($_params.group != "", $_params.group, $_group))]<br>
<p style="font-size: 80%">To move a page to a new project, you'll also need to re-parent it (see above) and move its attachments folder (if it has one.) Contact an administrator for assistance with moving attachments.</p></td></tr>
		</tbody>
		<tr><th>Body:</th><td><textarea class="textbox" name="body" rows="20" cols="70" width="100%">[edit_display(iif($_params.body != "", $_params.body, $_body))]</textarea><br>

<script type="text/javascript">
function markup_hinter(t) {
	target = document.getElementById("markup_hint");
	switch(t) {
		default:
		case "wf":
			target.innerHTML = 'Information on this markup format can be found in <a href="./?Formatting and Markup" target="_blank">the WF Markup Guide</a>.'
			break;
		case "wiki":
			target.innerHTML = 'Information on this markup format can be found in <a href="./?Wikka Markup Guide" target="_blank">the Wikka Markup Guide</a>.';
			break;
		case "html":
			target.innerHTML = 'Information on HTML can be found <a href="http://www.w3schools.com/html/default.asp" target="_blank">at W3C Schools</a>.';
			break;
	}
}

markup_hinter("[@_this.language]");
</script>

</td></tr>
		<tr><td></td><td>
		<input type="checkbox" name="notify" id="notify" value="1"><label for="notify">Notify other project contributors about this edit.</label><br>
		<span class="buttonbox"><input name="submitButton" class="submit" type="submit" value="Submit"></span> 
		<span class="buttonbox"><input name="submitButton" class="submit" type="submit" value="Preview"></span>

		[if(can_direct()) {
		<span class="buttonbox"><input name="Delete" class="delete" type="submit" value="Delete" onclick="document.location = './?id=[$_id]&mode=delete'; return false;"></span>}] <i>your permissions</i>: [summarize_permissions($_viewer.permissions)]<br>

		</td></tr></table>
		<center><a href="#" onclick="document.getElementById('w:me_body').style.display = (document.getElementById('w:me_body').style.display == 'none' ? '' : 'none'); ">Show/Hide Page Setup Controls</a></center>
	</form>
[end mode edit]

[begin mode submit]
[
if($_params.submitButton == "Submit") {
	<h2>Article updated.</h2>
	[
	if(_this.draft != "") {[
		_this.draft = "";
		{<p>The draft buffer has been cleared.</p>};
	]};

	if(($_body != $_params.body) && ($_body != ""))
		@_this[$_date & " by " & @_this.user] = $_body;

	@_this.user = _viewer.id;

	@_this.language = $_params.language;
	@_this.redirect = $_params.redirect;

	if(_params.notify == 1) if(_group != 0) _post_message_group(_group, {<a href="http://anthologi.ca/?id=[_viewer.profile_id]">[_viewer.name]</a> edited the page <b>[_title]</b> in the project <b>[get_group_name(_group)]</b>.<br><br><a href="http://anthologi.ca/?id=[_id]">Click here to view the current version of the page.</a>}, _viewer.id, "Edited page: " & _title);

	$_title = $_params.title;
	$_subtitle = $_params.subtitle;
	$_parent = $_params.parent;
	$_permissions = $_params.permissions;
	$_author = $_params.author;
	$_group = $_params.group;
	$_body = $_params.body;
	$_priority = $_params.priority;
	if($_priority == "") $_priority = 0;

	[refresh_to({./?id=[$_id]}, 1)]
	]
	<a href="?id=[$_id]">Return to viewing [$_title].</a>
} else if($_params.submitButton == "Preview") {
	[_this.draft = _params.body;]
	[msg_start()]
	This is a preview of how the page will look once it has been modified. It will not be saved until you click "Submit". A draft has been saved.
<form method="post">
	<input type="hidden" name="id" value="[$_id]">
	<input type="hidden" name="mode" value="edit">
	<input type="hidden" name="body" value="[edit_display($_params.body)]">
	<input type="hidden" name="title" value="[edit_display($_params.title)]">
	<input type="hidden" name="subtitle" value="[edit_display($_params.subtitle)]">
	<input type="hidden" name="parent" value="[edit_display($_params.parent)]">
	<input type="hidden" name="permissions" value="[edit_display($_params.permissions)]">
	<input type="hidden" name="author" value="[edit_display($_params.author)]">
	<input type="hidden" name="group" value="[edit_display($_params.group)]">
	<input type="hidden" name="priority" value="[edit_display($_params.priority)]">

	<input type="submit" value="Return to Editing">
</form>
	[msg_end()]

	[displayfunc($_params.body)]
};

]

[end mode submit]

[begin mode discussion]
[
if(can_comment()) {
	<a name="post"></a>
	[_post_comment($_id)]
};

override_title($_title, "Discussion");
override_parent({./?id=[$_id]}, "page");

$ps = 50;

$q = query("discussion", $ps, "date desc, id", "parent", $_id, "template", title2id("Generic Comment"));

query_run("view", $q);

$z = query_count($q);
if($z > $ps) {[
	if($ps != 0) {<center><br>[
		$np = next_page($q);
		$pp = previous_page($q);

		if($pp != "-1") {<a href="[$pp]">Newer</a> };
		{<div style="display: inline-block;">};
		page_list($q, 2); // by date
		{</div>};
		if($np != "-1") { <a href="[$np]">Older</a>};
	]<br></center>};
]};
]
[end mode discussion]

[begin mode comment_submit]
[
	override_title({Commenting on [$_title]}, "");
	override_parent({./?id=[$_id]}, $_title);
	$title = {Comment on [$_title] at [now()] by [$_viewer.name]};
    	$e = new_page($title, $_id);
	if($e) {[
		set_body($e, $_params.body);
		set_template($e, title2id("Generic Comment"));
        	set_permissions($e, "cadrec-dr-c-dr-c--r-");
		set_group($e, $_group);

		bdy = get_body(get_group_profile_id(_group));
		debug(bdy);
		if(getva("no_mail", bdy) == 1)
			$m = 1
		else
			$m = _post_message_group($_group, {
				[page("link", $_viewer.profile_id)] posted a comment on 
				<a href="[bare_url()]?id=[$_id]&mode=discussion">[$_title]</a>
				(Project: <a href="[bare_url()]?id=[get_group_profile_id($_group)]">
				[get_title(get_group_profile_id($_group))]</a>):
				<br>[_emoticons(_wiki_format($_params.body, "no toc"), _viewer.id)]
			});

        	msg_start();]
        	Commented successfully!<br>
		[
		if($m == "0") {<b>Could not post notice message</b>. Check settings.<br>}
		else refresh_to({./?id=[$_id]&mode=discussion}, 2);
		]
		<a href="./?id=[$_id]&mode=discussion">Return to discussion.</a><br>
        	<a href="./?id=[$_id]">Return to [$_title].</a>
        	[msg_end();
        ]} else {[
        	msg_start();]
        	Couldn't comment.<br>
        	Maybe you don't have permissions?<br>
        	Are you logged in?<br>
        	<a href="./?id=[$_id]">Return to [$_title].</a>
        	[msg_end();
        ]};
]
[end mode comment_submit]