<?php

require_once('include/admin.inc.php');

auth_redirect(); // Require logged in user to access this page.

#Actual logic. Only trigger if something submitted.
if('post_twitter' == $_REQUEST['action'])
{
	check_nonce('new-character-twitter');

	#Fetch the password from the DB.
	$acct = $mtdb->getRow(sprintf("SELECT username, password FROM twitter_user WHERE id = '%d'", $_REQUEST['twitter-account']));

	$post_at = strtotime($_REQUEST['date18']);

	if($post_at)
	{
		if($post_at <= strtotime('now'))
		{
			#If we can post immediately, do so. Bypass the scheduler whenever possible.
			#Treat a date/time in the past as immediate.
			$ret = twitterpost($_REQUEST['message'], $acct->username, $acct->password);

			if($ret)
			{
				$info.='Update posted to Twitter. <a href="http://www.twitter.com/'.$acct->username.'">View Twitter</a>.';
				adminlog('New manual post to Twitter for user '. $acct->username .'.', MTS_TWITTER, MTA_ADD);
			}
			else
			{
				$error.='There was an error posting to Twitter.';
			}
		}
		else
		{
			#No luck, gotta schedule.
			$mtdb->query(
				sprintf("INSERT INTO twitter_post (status, user, time, text)VALUES ('scheduled', '%d', FROM_UNIXTIME('%d'), '%s')",
					mysqli_real_escape_string($mtdb->link, $_REQUEST['twitter-account']),
					$post_at,
					mysqli_real_escape_string($mtdb->link, $_REQUEST['message'])
				)
			);
			$info .= "Your tweet for user " . htmlentities($acct->username) . " has been scheduled.";
			adminlog('Tweet for account ' . $acct->username . ' has been scheduled.', MTS_TWITTER, MTA_ADD);
		}
	}
	else
	{
		$error .= 'Could not make sense of your designated time/date. Please try again.';
	}
}

$characters = $mtdb->getAll("SELECT id, username FROM twitter_user ORDER BY username");

$scheduled = $mtdb->getAll("SELECT username, text, status, twitter_post.id AS id, time
	FROM twitter_post JOIN twitter_user
		ON twitter_post.user = twitter_user.id
	WHERE twitter_post.status = 'scheduled' ORDER BY time");

adminhead('Manage Character Twitters');
adminmenu();
?>

<h2>Manage Character Twitters</h2>
<form method="post" action="character-twitter.php">
<?php nonce_field('new-character-twitter'); ?>
<input type="hidden" name="action" value="post_twitter" />
<p><select name="twitter-account">
<option value="">Select twitter</option>
<?php foreach($characters as $c) {
	printf( '<option value="%s">%s</option>', htmlentities($c->id), htmlentities($c->username) );
} ?>
</select></p>
<p style="padding-bottom:1em;">
	<input type="text" name="message" maxlength="140" size="70" />
At: <script type="text/javascript" src="CalendarPopup.js" ></script>
	<script type="text/javascript">
		var cal18 = new CalendarPopup("testdiv1");
		cal18.setCssPrefix("TEST");
	</script>
	<INPUT TYPE="text" NAME="date18" VALUE="now" SIZE=25>
	<A HREF="#" onClick="cal18.select(document.forms[0].date18,'anchor18','yyyy/MM/dd'); return false;" TITLE="cal18.select(document.forms[0].date18,'anchor18','MM/dd/yyyy'); return false;" NAME="anchor18" ID="anchor18">select</A>
	<DIV ID="testdiv1" STYLE="position:absolute;visibility:hidden;background-color:white;layer-background-color:white;"></DIV>
	<input type="submit" value="Send" />
</p>
</form>

<table class="widefat">
	<thead>
	<tr>
	<th scope="col">User</th>
	<th scope="col">Tweet</th>
	<th scope="col">Time</th>
	<th scope="col"></th>
	</tr>
	</thead>

	<tbody id="the-list">
	<?php
	$alternate=false;
	foreach( $scheduled as $s ) {
		$alternate=!$alternate;
		?>
		<tr <?php if($alternate) echo 'class="alternate"'; ?>>
		 <td><?php echo $s->username;	 ?></td>
		 <td><?php echo $s->text; ?></td>
		 <td><?php echo htmlentities($s->time); ?></td>
		 <td style="text-align: center;"><a class="delete" href="delete-tweet.php?tweet_id=<?php echo $s->id; ?>">Delete</a></td>
		</tr>
		<?php
	}
	?>
	</tbody>
</table>

<script type="text/javascript" src="<?php echo SITE_HOST.SITE_PATH; ?>/resources.js"></script>

<?php
adminfooter();
?>
