#!/usr/bin/perl
require "weblib.lib";
use POSIX "strftime";
$style=<<EOF;
style=
body, table	{font: 8pt/10pt verdana;}
td {padding: 3px;}
.window {float: right; width: 1024px; min-width: 600px; height: 768px; min-height: 800px;}
iframe  {width: 1024px; height: 768px; float: right;}
.addressbox {font-weight: bold; text-align: center; margin-bottom: 5px; border: 1px solid black;
 box-shadow: 3px 3px 3px #888; -moz-box-shadow: 3px 3px 3px #888; -webkit-box-shadow: 3px 3px 3px #888;}
EOF
$TRDATA="/var/www/vhosts/trconnection.com/private/trconn.dat";
chdir "/var/www/vhosts/trconnection.com/private/articles";
&getparams;
$bydate=$param{bydate}?"/bydate":"";
&ReadParse;
htmlopen("Article Editor|",$style);
&getSections;
if($articles=$in{ReorderedArticles}){
	$articles=~s/\r/\n/g;
	$articles=~s/\n+/\n/g;
	$articles=join(",",split(/\n/,$articles));
	$section{$param{article}}=$in{Section};
	&RewriteSectionFile($in{Section},$articles);
}
print qq{<h1 style="display: inline; padding: bottom: 10px;"><a href="$weblib::URL">Article Editor</a></h1>\n};
if($in{file}){
	$param{article}="$in{file}.art";
	open(FILE,">$param{article}");
	$date=strftime("%Y%m%d%H%M",localtime(time));
	print FILE <<EOF;
<h3>$in{title}</h3>
<!-- keywords: -->
<new $date>
<div>
</div>
</new>
EOF
	close(FILE);
	open(FILE,">>index");
	print FILE "$param{article}:$in{title}\n";
	close(FILE);
}
if(-f $param{article}){
	print qq{<div class="window">};
	print qq{<div class="addressbox">http://$ENV{SERVER_NAME}/trconn.php/article=$param{article}</div>};
	print qq{<iframe src="/trconn.php/article=$param{article}" name="browser"></iframe></div>\n};
	if($in{article}){
		if($param{keepdate}){
			@stat=stat($param{article});
			$keep=strftime("%Y%m%d%H%M.%S",localtime($stat[9]));
		}
		open(FILE,">$param{article}");
		print FILE $in{article};
		close(FILE);
		if($param{keepdate}){system("touch -t $keep $param{article}");}
	}
	if($in{MoveDelete} eq "Move"){$in{Move}="on";}
	if($in{MoveDelete} eq "Delete"){$in{Delete}="on";}
	if($in{Move} && $in{Section}){putSection($param{article},$in{Section});}
	elsif($in{Delete} && $in{Section}){removeArticleFromSection($param{article},$in{Section});}
	print qq{ - Editing <a href="/trconn.php/article=$param{article}" target="_blank">$param{article}</a> },
		$param{keepdate}?"with":"without"," date preservation<br><br>\n";
	open(FILE,$param{article});
	while(read(FILE,$text,5000)){
		$filetext.=$text;
	}
	close(FILE);
	$keepdate=$param{keepdate}?"/keepdate":"";
	$color=$param{keepdate}?"blue":"red";
	$sectionselect=qq{<select name="Section"><option value="">Add to section…</option>\n};
	$hadselection="";
	foreach $section(@sections){
		$selected=$section{$param{article}} eq $section?" selected":"";
		$hadselection|=$selected;
		$sectionselect.=qq{<option value="$section"$selected>$title{$section}</option>\n};
	}
	$sectionselect.="</select>";
	$moveoption=qq{<input type="checkbox" name="Move">Move to };
	if($hadselection){
		$moveoption=qq{<input type="radio" name="MoveDelete" value="Move">Move to };
		$deleteoption=qq{/ <input type="radio" name="MoveDelete" value="Delete">Delete from };
	}
	print qq{<div style="border: 3px purple ridge; padding: 5px; width: 618px; height: 75px; overflow: hidden; margin: 1px;" id="Portal"},
		qq{>\n}; 
	print qq{<input type="button" onClick='t=getElementById("Portal");t.style.height="100%"' value="Open">\n};
	print qq{<input type="button" onClick='t=getElementById("Portal");t.style.height="75px"' value="Close">\n};
	#@chunk=chunkText($filetext);
	print <<EOF;
<form action="$weblib::URL/article=$param{article}/update$keepdate$bydate" method="POST">
$moveoption$deleteoption$sectionselect <input type="submit" value="Submit">
<textarea rows="40" name="article" style="height: 16px; overflow: hidden; padding: 5px; border: 3px $color ridge; display: block; width: 600px; margin: 1px;"
 onMouseOver='this.style.height="100%"; this.style.overflow="auto"' onMouseOut='this.style.height="100%"'>$filetext</textarea>
</form>
</div>
EOF
}
else{
	print <<EOF;
<div style="border: 1px solid blue; margin: 3px; padding: 3px; width: 300px;">Create New Article<br>
<form action="$weblib::URL/bydate" method="POST">
Article Title <input name="title"size="30"><br>
File name <input name="file" style="text-align: right; border: 0;">.art<br>
<input type="submit" value="Create Article">
</form>
</div>
EOF
}
open(IDX,"index");
while(<IDX>){
	($f,$desc)=split(/:/,$_,2);
	$INDEX{$f}=$desc;
}
opendir(DIR,".");
while($f=readdir(DIR)){
	next if $f !~ /\.art$/;
	push(@articles,$f);
	@stat=stat($f);
	$date{$f}=$stat[9];
}
if($param{article}){
}
print "<table>\n";
print qq{<tr><th><a href="/todd/listart">Article</a></th>};
print qq{<th>Title</th><th><a href="/todd/listart/bydate">Date</a></th><th>Section</th></tr>\n};
@articlesbydate=sort {$date{$b} <=> $date{$a}} keys %date;
@articles=$param{bydate}?@articlesbydate:sort @articles;
foreach $f(@articles){
	$color=-w $f?"green":"red";
	$artdate=strftime("%Y/%m/%d %H:%M:%S",localtime($date{$f}));
	print qq{<tr>};
	print qq{<td style="border: 1px solid $color"><a href="/trconn.php/article=$f" target="browser">$f</a></td>};
	print qq{<td><a href="$weblib::URL/article=$f$bydate">},$INDEX{$f}?$INDEX{$f}:"NOT IN INDEX",qq{</a></td>};
	print qq{<td><a href="$weblib::URL/article=$f/keepdate$bydate">$artdate</a></td>};
	print qq{<td>$section{$f}</td>};
	print qq{</tr>\n};
}
print "</table>\n";
&htmlclose;
sub getSections{
	local($section,$article,$artlist,$title,$description);
# Creates @sections, %section, %articles, %title, %description
	open(FILE,$TRDATA);
	while(<FILE>){
		last if /^EOD$/;
		chop;
		($section,$artlist)=split(/\t/);
		$articles{$section}=$artlist;
		push(@sections,$section);
		foreach $article(split(/,\s*/,$artlist)){
			if($section{$article}){$section{$article}.=",$section";}
			else{$section{$article}=$section;}
		}
	}
	while(<FILE>){
		chop;
		($section,$description)=split(/\t/);
		($section,$title)=split(/#/,$section);
		$title{$section}=$title;
		$description{$section}=$description;
	}
}
sub removeArticleFromSection{
	local($article,$section)=@_;
	local(@articles,$articlesave);
	delete $section{$article};
	foreach $articlesave(split(/,/,$articles{$section})){
		push (@articles,$articlesave) unless $articlesave eq $article;
	}
	$articles{$section}=join(",",@articles);
	&RewriteSectionFile($section,$articles{$section});
}
sub putSection{
	local($article,$section)=@_;
	local(@articles,$rows);
	if($articles{$section}){$articles{$section}.=",$article";}
	else{$articles{$section}=$article;}
	@articles=split(/,/,$articles{$section});
	$articles=join("\n",@articles);
	$section{$article}=$section;
	$rows=$#articles+1;
	print <<EOF;
<form action="$weblib::URL/$bydate/article=$article" method="POST">
<input type="hidden" name="Section" value="$section">
<textarea name="ReorderedArticles" cols="25" rows="$rows">$articles</textarea>
<input type="submit" value="Reorder Articles">
</form>
EOF
}
sub RewriteSectionFile{
	local($updatedsection,$articles)=@_;
	local($section);
	open(FILE,">${TRDATA}");
	foreach $section(@sections){
		if($updatedsection eq $section){$articles{$section}=$articles;}
		print FILE "$section\t$articles{$section}\n";
	}
	print FILE "EOD\n";
	foreach $section(@sections){
		print FILE "$section#$title{$section}\t$description{$section}\n";
	}
	close(FILE);
}
sub chunkText{
	local($text)=shift;
	local($firstchunkseen);
	$text=~s/\r\n/\n/sg;
	while($text =~ /<new/){
		($pretext,$newdate,$table,$expiredate,$textcontent,$text)=($text=~/(.*?)<new ([0-9]+)( table)?( expires=[0-9]+)?>(.*?)<\/new>(.*)/s);
		chomp($pretext,$newdate,$table,$expiredate,$textcontent,$text);
		if(!$firstchunkseen){
			($prepretext,$header,$keywords,$pretext)=($pretext=~/(.*?)(<h\d>.*?<\/h\d>.*?)(<!--.*?-->.*?)(.*)/s);
			$firstchunkseen=1;
			print qq{<div style="border: 3px red ridge; padding: 5px; width: 600px; height: 16px; overflow: hidden; margin: 1px;"},
				qq{ onMouseOver='this.style.height="100%"' onMouseOut='this.style.height="16px"'>\n};
			if($header){
				$header=~s/"/&quot;/g;
				print qq{<label for="header">Header</label><input name="header" id="header" value="$header" size="80">\n};
			}
			print qq{<dl>\n};
			print qq{<dt>Prepretext</dt><dd><textarea style="width: 99%;" rows="10">$prepretext</textarea></dd>\n} if $prepretext;
			print qq{<dt>Keywords</dt><dd><textarea style="width: 99%;" rows="10">$keywords</textarea></dd>\n} if $keywords;
			print "</dl></div>\n";
		}
		print qq{<div style="border: 3px gold ridge; padding: 5px; width: 600px; height: 16px; overflow: hidden; margin: 1px;"},
				qq{ onMouseOver='this.style.height="100%"' onMouseOut='this.style.height="16px"'>\n};
		print qq{<label>New Date <input name="newdate" value="$newdate"></label>\n} if $newdate;
		$checked=$table?" checked":"";
		print qq{<label>Table  <input type="checkbox" value="$table"$checked></label>\n};
		$expiredate=~s/ expires=//;
		print qq{<label>Expire Date <input name="expiredate" value="$expiredate"></label>\n};
		print qq{<dl>\n};
		print qq{<dt>Pretext</dt><dd><textarea style="width: 99%;" rows="10">$pretext</textarea></dd>\n} if $pretext;
		print qq{<dt>Text Content</dt><dd><textarea style="width: 99%;" rows="10">$textcontent</textarea></dd>\n} if $textcontent;
		print "</dl></div>\n";
	}
	print qq{<dl style="border: 3px green ridge; padding: 5px; width: 600px; height: 16px; overflow: hidden; margin: 1px;"},
		qq{ onMouseOver='this.style.height="100%"' onMouseOut='this.style.height="16px"'>\n},
		qq{<dt>Post text</dt><dd><textarea style="width: 99%;" rows="10">$text</textarea></dd>\n</dl>} if $text;
}