Location: PHPKode > projects > JaxBlog > JaxBlog/app/admin/admin_scoreboard/admin_scoreboard.php
<?php

require_once('../../../config.php');
require_once(FOLDER_RELATIVE_COMMON . 'authorization.php');
require_once(FOLDER_RELATIVE_COMMON . 'database.php');
require_once(FOLDER_RELATIVE_COMMON . 'xml.php');

$exitearly = true;
$errors = '';
$stage = isset($_GET['stage']) ? $_GET['stage'] : '';
$request = isset($_GET['request']) ? $_GET['request'] : '';

if ($request == 'xml') {

	header('Content-Type: text/xml');
	$xml = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' . "\n";
	$xml .= "<root>\n";
	
	if ($stage == 'add') {
		$xml .= "	<controls_input>\n";
		$xml .= "		<record><key>request</key><value>add</value></record>\n";
		$xml .= "		<record><key>stage</key><value>$stage</value></record>\n";
		$xml .= "	</controls_input>\n";
		
	} else if ($stage == 'edit') {
		$game_id = $_SESSION['id'];
		$xml .= "	<controls_input>\n";
		$xml .= "		<record><key>request</key><value>edit</value></record>\n";
		$xml .= "		<record><key>stage</key><value>$stage</value></record>\n";
		$xml .= "		<record><key>game_id</key><value>$game_id</value></record>\n";
		
		$sql = 'SELECT ' . databaseGetDate('scheduled_date') . ' AS scheduled_date, opponent, played_at_home, team_runs, team_hits, team_errors, opponent_runs, opponent_hits, opponent_errors FROM Games WHERE id = ?';
		$row = databaseGetRow($sql, array($game_id));
		
		$xml .= "		<record><key>scheduled_date</key><value>" . $row['scheduled_date'] . "</value></record>\n";
		$xml .= "		<record><key>opponent</key><value>" . $row['opponent'] . "</value></record>\n";
		$xml .= "		<record><key>played_at_home</key><value>" . $row['played_at_home'] . "</value></record>\n";
		$xml .= "		<record><key>team_runs</key><value>" . $row['team_runs'] . "</value></record>\n";
		$xml .= "		<record><key>team_hits</key><value>" . $row['team_hits'] . "</value></record>\n";
		$xml .= "		<record><key>team_errors</key><value>" . $row['team_errors'] . "</value></record>\n";
		$xml .= "		<record><key>opponent_runs</key><value>" . $row['opponent_runs'] . "</value></record>\n";
		$xml .= "		<record><key>opponent_hits</key><value>" . $row['opponent_hits'] . "</value></record>\n";
		$xml .= "		<record><key>opponent_errors</key><value>" . $row['opponent_errors'] . "</value></record>\n";
		
		$sql = 'SELECT inning, runs_team, runs_opponent FROM Scores WHERE game_id = ? AND inning <= 7 ORDER BY inning';
		$rows = databaseGetRows($sql, array($game_id));
		$i = 0;
		foreach($rows as $row) {
			$i++;
			$xml .= "		<record><key>runs_team_$i</key><value>" . $row['runs_team'] . "</value></record>\n";
			$xml .= "		<record><key>runs_opponent_$i</key><value>" . $row['runs_opponent'] . "</value></record>\n";
		}
				
		$xml .= "	</controls_input>\n";
	};
	
	$xml .= "</root>\n";
	echo $xml;
	exit;
}

if ($request == 'add') {
	if ($stage == 'add') {
		// User submitted form with edits, time to update.

		$opponent = $_POST['opponent'];
		$scheduled_date = $_POST['scheduled_date'];
		$team_runs = $_POST['team_runs'];
		$team_hits = $_POST['team_hits'];
		$team_errors = $_POST['team_errors'];
		$opponent_runs = $_POST['opponent_runs'];
		$opponent_hits = $_POST['opponent_hits'];
		$opponent_errors = $_POST['opponent_errors'];
		
		// checkbox sadly requires some intervention...
		// great idea would be to interpolate similar to date fields.
		$played_at_home = "N";
		if (isset($_POST['played_at_home'])) {
			if ($_POST['played_at_home'] == "on") {
				$played_at_home = 'Y';
			}
		}
		
		// Numeric values require scrubbing, too.
		if ($team_runs == '') $team_runs = '0';
		if ($team_hits == '') $team_hits = '0';
		if ($team_errors == '') $team_errors = '0';
		if ($opponent_runs == '') $opponent_runs = '0';
		if ($opponent_hits == '') $opponent_hits = '0';
		if ($opponent_errors == '') $opponent_errors = '0';

		$sql = 'INSERT INTO Games (id, opponent, scheduled_date, played_at_home, game_over, team_runs, team_hits, team_errors, opponent_runs, opponent_hits, opponent_errors) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )';
		$args = array(
			0, $opponent, $scheduled_date, $played_at_home, "Y",
			$team_runs, $team_hits, $team_errors,
			$opponent_runs, $opponent_hits, $opponent_errors);

		$game_id = databaseExecuteReturnId($sql, $args, "games_id_seq");
		
		for ($i = 1; $i < 10; $i++) {
			$runs_team = $_POST['runs_team_$i'];
			$runs_opponent = $_POST['runs_opponent_$i'];
			if ($runs_team == '') $runs_team = '0';
			if ($runs_opponent == '') $runs_opponent = '0';
			
			$sql = 'INSERT INTO Scores (game_id, inning, runs_team, runs_opponent) VALUES ( ?, ?, ?, ? )';
			$args = array($game_id, $i, $runs_team, $runs_opponent);
			databaseExecute($sql, $args);
		}
		
	} else {
		// Send user to the 'add' form.
		$_SESSION['id'] = $_GET['id'];
		$stage = 'add';
		$exitearly = false;
	}
	
} else if ($request == 'edit') {
	if ($stage == 'edit') {
		// User submitted form with edits, time to update.

		$game_id = $_POST['game_id'];
		
		$opponent = $_POST['opponent'];
		$scheduled_date = $_POST['scheduled_date'];
		$team_runs = $_POST['team_runs'];
		$team_hits = $_POST['team_hits'];
		$team_errors = $_POST['team_errors'];
		$opponent_runs = $_POST['opponent_runs'];
		$opponent_hits = $_POST['opponent_hits'];
		$opponent_errors = $_POST['opponent_errors'];
		
		// checkbox sadly requires some intervention...
		// great idea would be to interpolate similar to date fields.
		$played_at_home = "N";
		if (isset($_POST['played_at_home'])) {
			if ($_POST['played_at_home'] == "on") {
				$played_at_home = 'Y';
			}
		}
		
		// Numeric values require scrubbing, too.
		if ($team_runs == '') $team_runs = '0';
		if ($team_hits == '') $team_hits = '0';
		if ($team_errors == '') $team_errors = '0';
		if ($opponent_runs == '') $opponent_runs = '0';
		if ($opponent_hits == '') $opponent_hits = '0';
		if ($opponent_errors == '') $opponent_errors = '0';
		
		$sql = '';
		$sql .= ' UPDATE Games ';
		$sql .= ' SET ' ;
		$sql .= '   opponent = ?, scheduled_date = ?, played_at_home = ?, game_over = ? ';
		$sql .= '   , team_runs = ?, team_hits = ?, team_errors = ? ';
		$sql .= '   , opponent_runs = ?, opponent_hits = ?, opponent_errors = ? ';
		$sql .= ' WHERE id = ?';
		$args = array(
			$opponent, $scheduled_date, $played_at_home, 'Y',
			$team_runs, $team_hits, $team_errors,
			$opponent_runs, $opponent_hits, $opponent_errors,
			$game_id);

		databaseExecute($sql, $args);
		
		// Wipe all scores for current game, set to new values. 
		// Could do updates instead, but this is less error prone.
		$sql = 'DELETE FROM Scores WHERE game_id = ?';
		databaseExecute($sql, array($game_id));
		
		for ($i = 1; $i < 10; $i++) {
			$runs_team = $_POST["runs_team_$i"];
			$runs_opponent = $_POST["runs_opponent_$i"];
			if ($runs_team == '') $runs_team = '0';
			if ($runs_opponent == '') $runs_opponent = '0';
			
			$sql = 'INSERT INTO Scores (game_id, inning, runs_team, runs_opponent) VALUES ( ?, ?, ?, ? )';
			$args = array($game_id, $i, $runs_team, $runs_opponent);
			databaseExecute($sql, $args);
		}

	} else {
		// Send user to the 'edit' form.
		$_SESSION['id'] = $_GET['id'];
		$stage = 'edit';
		$exitearly = false;
	}
	
} else if ($request == 'delete') {
	$sql = 'DELETE FROM Scores WHERE game_id = ?';
	databaseExecute($sql, array());
	
	$sql = 'DELETE FROM Games WHERE id = ?';
	databaseExecute($sql, array());
}

if ($exitearly) {
	// TODO: find a neat way to out the error messages to the user.
	if ($errors != '') {
		echo 'an error occurred';
		echo $errors;
		exit;
	}
    header('location:admin_scoreboard_list.php');
	exit;
}

// Show the list form.
require_once(FOLDER_RELATIVE_COMMON . 'builder-admin.php');

$header   = '';
$header  .= '<script type="text/javascript" src="admin_scoreboard.js"></script>' . "\n";
$header  .= '<link type="text/css" rel="stylesheet" href="admin_scoreboard.css">';

$onload   = "jaxFormRegister('admin_scoreboard.php?request=xml&stage=$stage'); initializePage();";
$title    = 'Scoreboard';
$content  = 'admin_scoreboard.html';
$page     = buildAdminPage($header, $onload, $title, $content);

echo $page;
?>
Return current item: JaxBlog