<?php

/* This page displays and modifies the Status Box */

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

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




/* Handle form submission of new updates */

function handle_update_form() {
	global $error,$info,$dbConnection;

	check_nonce('update-statusbox');
	$percent = $_POST['update_percentage'];
	$eta = strtotime($_POST['update_eta']);
	$text = $_POST['update_text'];

	$now = time();
	$percent = trim($percent, "% ");
	if( !is_numeric ( $percent ) ) {
		$error = '<p>Percentage complete must be numeric.</p>';
		return;
	}
	if( $percent < 0 || $percent > 100 ) {
		$error = '<p>Supply percentages between 0 and 100 (inclusive), please.</p>';
		return;
	}

	// ETA must be sane
	if(   ($eta < $now && $percent < 100) ||  ($eta<mktime(0,0,0,1,1,2000) )  ) {
		$error = '<p>Supply estimated time in the future, please. Current time is ' . date("F j, Y, g:i a", $now) . '</p>';
		return;
	}

	$dbConnection->executeUpdate('INSERT INTO status (published, eta, percentage, text) VALUES (NOW(), FROM_UNIXTIME(?), ?, ?)', array($eta, $percent, $text));

	$_POST['update_percentage']=$_POST['update_eta']=$_POST['update_text']='';
	$info = '<p>Statusbox updated successfully.</p>';
}

if( isset($_POST['action']) && $_POST['action'] == 'create-update' )
	handle_update_form();




adminhead('Status Box');
adminmenu('manage-statusbox.php');


/* Simple Presets, Select things said before */


$presets = $dbConnection->fetchAll('SELECT COUNT(*) as c, percentage, text, CONCAT( percentage, "% - ", text ) as p FROM status GROUP BY p HAVING c > 1 ORDER BY c DESC');

?>


<form enctype="multipart/form-data" name="create-update" id="create-update" action="manage-statusbox.php" method="post">
<?php nonce_field('update-statusbox'); ?>
	<input type="hidden" name="action" value="create-update" />

<h2>Update Status Box</h2>
<div class="narrow">
	<table class="editform" width="100%" cellspacing="2" cellpadding="5">

	<tr>
	<th scope="row">Current Time</th>
	<td width="66%"><?php echo date("F j, Y, g:i a"); ?></td>
	</tr>

	<tr>
	<th scope="row">Estimated Time Of Next Update</th>
	<td width="66%"><input name="update_eta" type="text" id="update_eta" value="<?php echo htmlentities($_POST['update_eta']); ?>" /></td>
	</tr>

	<tr>
	<th scope="row">Percentage Complete</th>
	<td width="66%"><input name="update_percentage" type="text" id="update_percentage" value="<?php echo htmlentities($_POST['update_percentage']); ?>" /></td>
	</tr>

	<tr>
	<th scope="row">Status Description</th>
	<td width="66%"><textarea name="update_text" row="6" cols="30" id="update_text"><?php echo htmlentities($_POST['update_text']); ?></textarea></td>
	</tr>

	<script type="text/javascript">
	function PresetHandler( f ) {

		var i = f.preset.selectedIndex;
		var v = f.preset.options[i].value;

		var parts = v.split("% - ");

		if( parts[0] == undefined ) {
			parts[0] = "";
		}
		if( parts[1] == undefined ) {
			parts[1] = "";
		}

		f.update_percentage.value = parts[0];
		f.update_text.value = parts[1];

		return true;
	}
	document.write('<tr><th scope="row">Presets</th><td width="66%"><select name="preset" onchange="PresetHandler(this.form);"><option value="">-none-</option><?php
		foreach( $presets as $p )
			echo '<option value="' . addslashes(htmlentities($p->p)) . '">' . addslashes(htmlentities($p->p)) . '</option>';
	?></select></td></tr>');

	</script>
	</table>

	<p class="submit"><input type="submit" value="Set Update Status &raquo;" name="submit" /></p>
</div>
</form>


</div>
<div class="wrap">

<h2>Status Box History</h2>
<p>Status updates that have gone before.</p>

<?php

$stats = $dbConnection->fetchAll("SELECT published, eta, percentage, text FROM status ORDER BY published DESC LIMIT 5");

?>

<table class="widefat">
	<thead>
	<tr>
	<th scope="col">Published</th>
	<th scope="col">ETA</th>
	<th scope="col">Percentage</th>
	<th scope="col">Text</th>
	</tr>
	</thead>

	<tbody id="the-list">
	<?php
	$alternate=false;
	foreach( $stats as $s ) {
		$alternate=!$alternate;
		?>
		<tr <?php if($alternate) echo 'class="alternate"'; ?>>
		 <th scope="row" style="text-align: center;"><?php echo $s->published; ?></th>
		 <td><?php echo $s->eta; ?></td>
		 <td><?php echo $s->percentage; ?></td>
		 <td><?php echo htmlentities($s->text); ?></td>
		</tr>
		<?php
	}
	?>
	</tbody>
</table>


<?php
adminfooter();
?>
