Location: PHPKode > projects > Online Fantasy Football League > offl-0.2.6/www/rosters.php
<?php  
/**
 * Roster interface
 *
 * Interface for {@link OFFL_FFLTeam team} roster management and transactions.
 *
 * @author Stephen Rochelle <hide@address.com>
 * @version OFFL v0.2
 * @copyright Copyright (c) 2004 Stephen Rochelle.  Some rights reserved.
 * @package offl-ui
 */

$continue_with_page = TRUE;

if(empty($_REQUEST["action"]))
{	$_REQUEST["action"] = NULL;	}
if(empty($_REQUEST["mode"]))
{	$_REQUEST["mode"] = NULL;	}
if(empty($_REQUEST["view"]))
{	$_REQUEST["view"] = NULL;	}
if(empty($_REQUEST["fflteam_id"]))
{	$_REQUEST["fflteam_id"] = NULL;	}

if ($_REQUEST["view"] == "allteams")
{	$pageTitle = "Current Team Rosters";	}
else
{	$pageTitle = "Team Roster";	}

require_once("offlconfig.php");
require_once($DOC_ROOT . "/lib/header.php");

$isAdmin = FALSE;

if(($_REQUEST["mode"] == "admin") && $_SESSION["admin"])
{	$isAdmin = TRUE;	}

if(empty($_REQUEST["year"]))
{	$_REQUEST["year"] = getThisYear();	}

if ($_REQUEST["action"] == "save")
// process the data!
{
	if ($_SESSION["user_id"] == -1)
	{
		require($DOC_ROOT . "/lib/footer.php");
		die();
	}

	// collect roster info
	// DO NOT SAVE YET
	// call FFLTeam->validateRosterFlex($roster)
	// if no errors, save info (return to edit screen?)
	// otherwise return errors and go back to edit screen

	$thisTeam = new OFFL_FFLTeam($_REQUEST["fflteam_id"]);

	$i = 1;
	$roster = array();
	$newstarters = array();
	$newbenchers = array();
	$errors = array();
	while(TRUE)
	{
		if(!isset($_REQUEST["player$i"]))
		{	break;	}
		$x = new OFFL_Player($_REQUEST["playerid$i"], $thisTeam->getLeagueID());
		if ($x->getStarter() != $_REQUEST["player$i"])
		{

			$game_lookup = new OFFL_NFLGame();
			$week = getCurrentWeek($_REQUEST["year"]);
			$game = $game_lookup->getNFLTeamGame($_REQUEST["year"], $week, $x->getNFLTeamID());
			if (is_object($game))
			{
				$opp = $game->getNFLTeamGameOpponent($_REQUEST["year"], $week, $x->getNFLTeamID());
				$gameTime = $game->getGametime();
			}
			else
			{
				$opp = "Bye";
				$gameTime = time() + 3600*24*7;
			}

			$beforeDeadline = TRUE;
			if (($gameTime - 60*15) < time()) // 15 minutes before game lockout
			{	$beforeDeadline = FALSE;	}

			if (!$beforeDeadline)
			{	$errors[] = $x->getName("FL") . " is not eligible for roster changes, game has already begun.";	 }
			
			if ($x->getStarter())
			{	$newbenchers[] = $_REQUEST["playerid$i"];	}
			else
			{	$newstarters[] = $_REQUEST["playerid$i"];	}
		}
		$x->setStarter($_REQUEST["player$i"]);
		array_push($roster, $x);
		$i++;
	}
	// needs a sort, otherwise active WRs can lock out RB flex spots, etc.
	usort($roster, "cmp_players");

	if (sizeof($errors) == 0)
	{	$errors = $thisTeam->validateRosterFlex($roster);	}

	foreach ($errors as $error)
	{	echo "<p class=\"error\">Error: $error</p>\n";	}
	if (sizeof($errors) == 0)
	{
		$transaction = new OFFL_Transaction();
		$transaction->setTransType("lineup");
		$transaction->setLeagueID($thisTeam->getLeagueID());
		$transaction->save();

		foreach($roster as $player)
		{
			if (in_array($player->getPlayerID(), $newbenchers))
			{
				$transItem = new OFFL_TransactionItem();
				$transItem->setTransID($transaction->getTransID());
				$transItem->setTransType("bench");
				$transItem->setPlayerID($player->getPlayerID());
				$transItem->setFromFFLTeamID($player->getFFLTeamID());
				$transItem->setToFFLTeamID($player->getFFLTeamID());
				$transItem->save();
			}
			elseif (in_array($player->getPlayerID(), $newstarters))
			{
				$transItem = new OFFL_TransactionItem();
				$transItem->setTransID($transaction->getTransID());
				$transItem->setTransType("start");
				$transItem->setPlayerID($player->getPlayerID());
				$transItem->setFromFFLTeamID($player->getFFLTeamID());
				$transItem->setToFFLTeamID($player->getFFLTeamID());
				$transItem->save();
			}
			$thisTeam->setStartStatus($player->getPlayerID(), $player->getStarter());
		}

		$thisTeam->setRoster($roster);
		// finally, save the team
		$thisTeam->save();

		echo "<h2 class=\"success\">Roster changes saved</h2>";
	}
	else
	{	echo "<p style=\"color:#F00; font-weight:bold;\">Changes not saved.</p>\n";	}

	// add the meta here and die?
}

if ($_REQUEST["action"] == "dropplayer")
{
	if ($_SESSION["user_id"] == -1)
	{
		require($DOC_ROOT . "/lib/footer.php");
		die();
	}

	// do a confirmation page, then drop.
	if (empty($_REQUEST["step"])) // step 1, confirm
	{
		$thisTeam = new OFFL_FFLTeam($_REQUEST["fflteam_id"]);
		$player = new OFFL_Player($_REQUEST["player_id"], $thisTeam->getLeagueID());
		if (!$thisTeam->onRoster($_REQUEST["player_id"]) || (($_SESSION["user_id"] != $thisTeam->getUserID()) && (!$isAdmin)))
		// not authorized
		{
			echo "<h2 class=\"error\">Unauthorized attempt to drop player!</h1><h2 class=\"error\">Please log in</h2>\n";
			$continue_with_page = FALSE;
		}
		else
		{
			$continue_with_page = FALSE;
			if ($player->getTradelock()) // player in pending trade
			{
				echo "<h2 class=\"error\">You may not drop a player involved in a pending trade!</h2>\n";
			}
			else
			{
				echo "Are you sure you want to drop " . $player->getName() . "?\n";
	?>		<table><tr><td>
				<form name="dropyes" method="post" action="<?php echo $WEB_ROOT; ?>/rosters.php" />
					<input type="hidden" name="player_id" value="<?php echo $player->getPlayerID(); ?>" />
					<input type="hidden" name="fflteam_id" value="<?php echo $thisTeam->getFFLTeamID(); ?>" />
					<input type="hidden" name="action" value="dropplayer" />
					<input type="hidden" name="step" value="2" />
<?php			if ($isAdmin)
				{
					echo "					<input type=\"hidden\" name=\"mode\" value=\"admin\" />\n"; 
				}?>
				<input type="submit" value="Yes" />
			</form>
		</td><td>
			<form name="dropno" method="post" action="<?php echo $WEB_ROOT; ?>/rosters.php" />
				<input type="submit" value="No" />
			</form>
		</td></tr></table>

<?php		}
		}
	}
	else // step 2, drop!
	{
		$thisTeam = new OFFL_FFLTeam($_REQUEST["fflteam_id"]);
		$player = new OFFL_Player($_REQUEST["player_id"], $_SESSION["league_id"]);
		if (!$thisTeam->onRoster($_REQUEST["player_id"]) || (($_SESSION["user_id"] != $thisTeam->getUserID()) && (!$isAdmin)))
		// not authorized
		{
			echo "<h2 class=\"error\">Unauthorized attempt to drop player!</h1><h2 class=\"error\">Please log in</h2>\n";
			$continue_with_page = FALSE;
		}
		else
		{
			$transaction = new OFFL_Transaction();
			$transaction->setTransType("adddrop");
			$transaction->setLeagueID($thisTeam->getLeagueID());
			$transaction->save();
			$transItem = new OFFL_TransactionItem();
			$transItem->setTransID($transaction->getTransID());
			$transItem->setTransType("drop");
			$transItem->setPlayerID($player->getPlayerID());
			$transItem->setFromFFLTeamID($player->getFFLTeamID());
			$transItem->save();

			$thisTeam->dropPlayer($player->getPlayerID());
			$thisTeam->save();
			$waivers = $control->getValue("CONFIG_USE_WW");
			if ($waivers == "Yes")
			{
				$waiver = new OFFL_WaiverPlayer();
				$waiver->setLeagueID($thisTeam->getLeagueID());
				$waiver->setPlayerID($player->getPlayerID());
				$waiver->setWaiverDeadline(time());
				$waiver->save();
			}
	
			echo "<div class=\"success\">" . $player->getName() . " has been dropped successfully.</div>\n";
		}
	}
}
elseif ($_REQUEST["action"] == "addplayer")
{
	if ($_SESSION["user_id"] == -1)
	{
		require($DOC_ROOT . "/lib/footer.php");
		die();
	}

	// we need a drop/add dialog.

	// OK, start by listing free agents a la draft pick method
	// then allow for dropping a roster player
	// validate the proposed roster
	// prompt for confirmation (step 2)
	// save changes
	// return to regular rosters display

	if ($_REQUEST["step"] == "1")
	{

		$thisTeam = new OFFL_FFLTeam($_REQUEST["fflteam_id"]);
		$thisLeague = new OFFL_League($thisTeam->getLeagueID());
		$teams = $thisLeague->getAllFFLTeams();
		$thisTeam_players = $thisTeam->getRoster();

		// this array contains all active non-roster players (FAs and Waivers) in this league
		$players = $thisLeague->getPlayers(2);

	echo "<script type=\"text/javascript\">\n"
	?>
		var freeagentlist = new Array();

		function populateList()
		{
			// draftform: freeagentpick
			// first: position
			// second: player_id

			document.forms.freeagentpick.player_id.options.length = 0;
			var positionSelected = document.forms.freeagentpick.position.options[document.forms.freeagentpick.position.selectedIndex].value;
			var teamSelected = document.forms.freeagentpick.team.options[document.forms.freeagentpick.team.selectedIndex].value;

			var count = 1;
			var name = "";

			document.forms.freeagentpick.player_id.options[0] = new Option("No Add", 0);

			for (var i = 1; i < freeagentlist.length; i++)
			{
				if ( ( (positionSelected == freeagentlist[i][1]) || (positionSelected == "all") ) && 
					 ( (teamSelected == freeagentlist[i][2]) || (teamSelected == "all") ) )
				{
					document.forms.freeagentpick.player_id.options[count] = new Option(freeagentlist[i][0] + ", " + freeagentlist[i][1] + " " + freeagentlist[i][2], freeagentlist[i][3]);
					count++;
				}
			}
			document.forms.freeagentpick.player_id.options[0].defaultSelected = true;
		}

		function updateWaivers()
		{
			for (var i = 0; i < freeagentlist.length; i++)
			{
				if (freeagentlist[i][3] == document.forms.freeagentpick.player_id.options[document.forms.freeagentpick.player_id.selectedIndex].value)
				{
					document.forms.freeagentpick.waiver_status.value = freeagentlist[i][4];
					break;
				}
			}
		}
		
		freeagentlist[0] = ["No Add", "", "", 0, ""];
	<?php
		foreach($players as $i=>$player)
		{
			$waiver_status = $player->getWaiverStatus();
			if (is_null($waiver_status))
			{
				$waiver_status = "Free Agent";
			}
			else
			{
				if ((time() - $waiver_status) > 0)
					$waiver_status = "Waiver Pending";
				else
				{
					$temp = date("D, F j", $waiver_status);
					$waiver_status = "On Waivers until " . $temp;
				}
			}
			echo "    freeagentlist[" . ($i+1) . "] = [\"" . $player->getName() . "\", \"" . $player->getPositionAbbv() . "\", \"" . $player->getNFLTeamAbbv() . "\", " . $player->getPlayerID() . ", \"$waiver_status\"];\n";
		}
	?>

	</script>
	<form name="freeagentpick" method="post" action="<?php echo $WEB_ROOT; ?>/rosters.php">
	<input type="hidden" name="action" value="addplayer" />
	<input type="hidden" name="step" value="2" />
	<input type="hidden" name="fflteam_id" value="<?php echo $_REQUEST["fflteam_id"]; ?>" />
<?php	if ($isAdmin) echo "	<input type=\"hidden\" name=\"mode\" value=\"admin\" />\n"; ?>

	<table>
		<tr>
			<th colspan="3"></th>
			<th>Free Agent (Add)</th>
			<th>Current Roster (Drop)</th>
		</tr>
		<tr valign="top">
			<td align="right"></td>
			<td align="left">
				<select name="position" size="10" style="overflow:hidden; padding-right:15px;" onchange="populateList()">
					<option value="all" selected>All</option>
	<?php
		$x = new OFFL_Position();
		$positions = $x->getAllPositions();
		foreach ($positions as $position)
			if (!strstr($position->getPositionAbbv(), "FLEX"))
				echo "				<option value=\"" . $position->getPositionAbbv() . "\">" . $position->getPositionAbbv() . "s</option>\n";
		unset ($positions);
		unset ($x);
		?>
				</select>
			</td><td>
				<select name="team" size="10" style="padding-right: 5px;" onchange="populateList()">
					<option value="all" selected>All</option>
	<?php
		$x = new OFFL_NFLTeam();
		$nflteams = $x->getAllNFLTeams();
		foreach ($nflteams as $nflteam)
			echo "				<option value=\"" . $nflteam->getNFLTeamAbbv() . "\">" . $nflteam->getNFLTeamAbbv() . "</option>\n";
		unset ($nflteams);
		unset ($x);
		?>
				</select>
			</td><td width="270px">
				<select name="player_id" size="10" style="width:100%" onchange="updateWaivers()">
					<option value="0" selected>No Add</option>
	<?php
				for ($i = 0; $i < sizeof($players); $i++)
				{
	?>				<option value="<?php echo $players[$i]->getPlayerID(); ?>"><?php echo $players[$i]->getName() . ", " . $players[$i]->getPositionAbbv() . " " . $players[$i]->getNFLTeamAbbv(); ?></option>
	<?php		} ?>
				</select>
			</td>
			<td>
	<?php // here is where I should list a player to be dropped ?>
				<select name="drop_player_id" size="10" style="padding-right: 5px;">
					<option value="0" selected>No Drop</option>
	<?php
					foreach($thisTeam_players as $player)
					{

						$x = new OFFL_NFLGame();
						$week = getCurrentWeek($_REQUEST["year"]);
						$game = $x->getNFLTeamGame($_REQUEST["year"], $week, $player->getNFLTeamID());
						if (is_object($game))
						{
							$opp = $game->getNFLTeamGameOpponent($_REQUEST["year"], $week, $player->getNFLTeamID());
							$gameTime = $game->getGametime();
						}
						else
						{
							$opp = "Bye";
							$gameTime = time() + 3600*24*7;
						}

						$beforeDeadline = TRUE;
						if (($gameTime - 60*15) < time()) // 15 minutes before game lockout
						{	$beforeDeadline = FALSE;	}

						if (($player->getTradelock() == FALSE) && $beforeDeadline)
							echo "<option value=\"" . $player->getPlayerID() . "\">" . $player->getName() . ", " . $player->getPositionAbbv() . " " . $player->getNFLTeamAbbv() . "</option>\n";
					}
	?>
				</select>
			</td>
		</tr>
		<tr>
			<td colspan="4" align="right" style="padding-right: 50px;">
				Waiver status: <input type="text" name="waiver_status" size="42" value="" disabled style="color:#000 !important; " />
			</td>
			<td colspan>
				<input type="submit" value="Submit Transaction" />
			</td>
		</tr>
	</table>
	</form>
<?php 
	$continue_with_page = FALSE;
	}
	else // step == 2 of addplayer
	{
		$thisTeam = new OFFL_FFLTeam($_REQUEST["fflteam_id"]);
		$thisLeague = new OFFL_League($thisTeam->getLeagueID());
		$player = new OFFL_Player($_REQUEST["player_id"], $thisTeam->getLeagueID());
		$isWaiver = FALSE;
		if ($_REQUEST["player_id"] != 0)
		// detect potential waiver situation
		{
			if ($player->getWaiverStatus() > 0)
			// waiver situation, time to bypass the regular drop/add sequence
			{	$isWaiver = TRUE;	}
		}

		if ($isWaiver)
		{
			$waiver = new OFFL_Waiver();
			$waiver->setFFLTeamID($thisTeam->getFFLTeamID());
			$waiver->setLeagueID($thisTeam->getLeagueID());
			$waiver->setPlayerID($_REQUEST["player_id"]);
			if ($_REQUEST["drop_player_id"] > 0)
			{	$waiver->setDropPlayerID($_REQUEST["drop_player_id"]);	}
			$waiver->setPriority();

			$waiver->save();

			if ($_REQUEST["drop_player_id"] > 0)
			{	$dropplayer = new OFFL_Player($_REQUEST["drop_player_id"], $thisTeam->getLeagueID());	}

			echo "<h2 class=\"success\">Added to Waiver List:</h2>\n";
			$str = "<h3 class=\"success\">" . $player->getName() . " (" . $player->getPositionAbbv() . " " . $player->getNFLTeamAbbv() . ")";
			if ($_REQUEST["drop_player_id"] > 0)
				$str .= " for " . $dropplayer->getName() . " (" . $dropplayer->getPositionAbbv() . " " . $dropplayer->getNFLTeamAbbv() . ")";
			$str .= "</h3>";
			echo $str;
			
			echo "<h4>Go to <a href=\"$WEB_ROOT/rosters.php\">Roster Management</a></h4>\n<h4>Go to <a href=\"$WEB_ROOT/waivers.php\"> Waiver Management</a></h4>\n";
			$continue_with_page = FALSE;

		
		}
		else // regular drop/add sequence
		{


			// update a temp roster
			// validate
			// if ok, post confirm dialog?  be nice, but tough to pass all the data again.  we'll see.  maybe later.
			// if ok, save
			// else post errors and back to normal roster view

			if ($_REQUEST["drop_player_id"] > 0)
			{	$dropPlayer = new OFFL_Player($_REQUEST["drop_player_id"], $thisTeam->getLeagueID());	}
			if (isset($dropPlayer) && $dropPlayer->getTradelock())
			{
				echo "<h2 class=\"error\">You cannot drop a player involved in a pending trade!</h2\n";
			}
			else
			{
				$roster_old = $thisTeam->getRoster();
				$roster = array();
				$roster_removed = array(); // catch the players who are dropped

				if ($player->getFFLTeamID() > 0)
				{
					echo "<h2 class=\"error\">Cannot add a player on another roster!</h2>\n";
				}
				else
				{
				
					if ($_REQUEST["player_id"] > 0)
					{
						$player->setStarter(0);
					//	$player->setFFLTeamID($thisTeam->getFFLTeamID());
						$roster[] = $player;
					}

					foreach($roster_old as $plyr)
					{
						if ($plyr->getPlayerID() == $_REQUEST["drop_player_id"])
						{
							$roster_removed[] = $plyr;
							continue;
						}
						$roster[] = $plyr;
					}

					usort($roster, "cmp_players");

					$errors = $thisTeam->validateRosterFlex($roster);

					if (!sizeof($errors))
					{
						echo "<h2 class=\"success\">Transaction complete!</h2>\n";
						$continue_with_page = FALSE;

						$transaction = new OFFL_Transaction();
						$transaction->setTransType("adddrop");
						$transaction->setLeagueID($thisTeam->getLeagueID());
						$transaction->save();

						$transItem = new OFFL_TransactionItem();
						$transItem->setTransID($transaction->getTransID());
						$transItem->setTransType("addfa");
						$transItem->setPlayerID($player->getPlayerID());
						$transItem->setToFFLTeamID($thisTeam->getFFLTeamID());
						$transItem->save();

						$thisTeam->addPlayer($_REQUEST["player_id"]);

						foreach ($roster_removed as $player)
						{
							$transItem = new OFFL_TransactionItem();
							$transItem->setTransID($transaction->getTransID());
							$transItem->setTransType("drop");
							$transItem->setPlayerID($player->getPlayerID());
							$transItem->setFromFFLTeamID($thisTeam->getFFLTeamID());
							$transItem->save();
							
							$thisTeam->dropPlayer($player->getPlayerID());
							$waivers = $control->getValue("CONFIG_USE_WW");
							if ($waivers == "Yes")
							{
								$waiver = new OFFL_WaiverPlayer();
								$waiver->setLeagueID($thisTeam->getLeagueID());
								$waiver->setPlayerID($player->getPlayerID());
								$waiver->setWaiverDeadline(time());
								$waiver->save();
							}
						}
						$thisTeam->save();
						?><META http-equiv="refresh" content="1; URL=<?php echo $WEB_ROOT; ?>/rosters.php<?php
						if ($isAdmin)
						{	echo "?mode=admin&fflteam_id=" . $_REQUEST["fflteam_id"];	}
						?>" /><?php
					}
				} // end else player is a free agent

				foreach ($errors as $error)
					echo "<div class=\"error\">ERROR: $error</div>\n";
			} // end if tradelock
		}// end else isWaiver
	}
}

if ($continue_with_page)
{
/* *******************************************************
	REGULAR PAGE DISPLAY BEGINS HERE
   *******************************************************
*/

	if ($_REQUEST["view"] == "allteams")
	{
		// this display should be improved somehow.
		// not a priority, though.
		$teams = $myleague->getAllFFLTeams();

		$get_positions = new OFFL_Position();
		$all_positions = $get_positions->getAllPositions();
		$positions = array();
		foreach ($all_positions as $position)
		{
			if (stristr($position->getPositionAbbv(), "FLEX") === FALSE)
			{	$positions[] = $position;	}
		}
?><table id="roster-breakdown">
  <tr><td></td>
<?php
		foreach($teams as $team)
		{
			echo "    <th><a href=\"" . $WEB_ROOT . "/teams.php?fflteam_id=" . $team->getFFLTeamID() . "\">" . $team->getFFLTeamFullName() . "</a></th>\n";
		}
		echo "  </tr>\n";

		foreach($positions as $position)
		{
			echo "  <tr><th><abbr title=\"" . $position->getPositionName() . "\">" . $position->getPositionAbbv() . "</abbr></th>\n";

			foreach($teams as $team)
			{
				echo "    <td>";
				$players = $team->getRoster(TRUE, $position->getPositionID());
				$player_string_array = array();
				foreach ($players as $player)
				{
					$player_string_array[] = "<a href=\"" . $WEB_ROOT . "/players.php?player_id=" . $player->getPlayerID() . "\">" . $player->getName("FL") . "</a>";
				}
				echo implode("<br />\n", $player_string_array);

				echo "    </td>\n";
			}
			echo "  </tr>\n";
		}
		echo "  <tr><th><abbr title=\"Bench\">BN</abbr></th>\n";
		
		foreach($teams as $team)
		{
			echo "    <td>";
			$players = $team->getRoster(FALSE);
			$player_string_array = array();
			foreach ($players as $player)
			{
				$player_string_array[] = "<a href=\"" . $WEB_ROOT . "/players.php?player_id=" . $player->getPlayerID() . "\">" . $player->getName("FL") . "</a>";
			}
			echo implode("<br />\n", $player_string_array);

			echo "    </td>\n";
		}

		echo "  </tr>\n";
		echo "</table>\n";

	} 

/*

 END LEAGUE SUMMARY

*/

	else // usual single-team view
	{
		if (!isset($_REQUEST["fflteam_id"]))
		{
?>		<div id="subnav"><ul>
			<li id="caption">Rosters Subpages:</li>
			<li><a href="<?php echo $WEB_ROOT; ?>/rosters.php?view=allteams">League Breakdown</a></li>
			<li><a href="<?php echo $WEB_ROOT; ?>/transactions.php">Transaction Report</a></li>
			<li><a href="<?php echo $WEB_ROOT; ?>/trades.php">Trades</a></li>
			<li><a href="<?php echo $WEB_ROOT; ?>/waivers.php">Waivers</a></li>
			<li><a href="<?php echo $WEB_ROOT; ?>/players.php">Free Agents</a></li>

<?php
if ($control->getValue("CONFIG_USE_DRAFT") == "Yes")
{
?>
<li><a href="<?php echo $WEB_ROOT; ?>/drafts.php">Draft</a></li>
<?php
}

if ($control->getValue("CONFIG_USE_AUCTION") == "Yes")
{
?>
<li><a href="<?php echo $WEB_ROOT; ?>/auction.php">Auction</a></li>
<?php
}
?>
		</ul></div>
<?php	}
		$week = getCurrentWeek(getThisYear());

		if (!isset($_REQUEST["fflteam_id"]))
		{	$_REQUEST["fflteam_id"] = $_SESSION["fflteam_id"];	}
		$thisTeam = new OFFL_FFLTeam($_REQUEST["fflteam_id"]);

?><table id="roster"><tr><th style="font-size:larger;"><a href="<?php echo $WEB_ROOT . "/teams.php?fflteam_id=" . $thisTeam->getFFLTeamID() . "\">" . $thisTeam->getFFLTeamCity() . " " . $thisTeam->getFFLTeamName(); ?></a></th><th style="font-size:larger;">Team Rosters</th><tr><td valign="top"><?php

		$isCurrentTeam = FALSE;
		if (($_REQUEST["fflteam_id"] == $_SESSION["fflteam_id"]) && ($_SESSION["fflteam_id"] > 0))
		{	$isCurrentTeam = TRUE;	}

		$editable = FALSE;
		if ($isCurrentTeam || $isAdmin)
		{	$editable = TRUE;	}

		$starters = $thisTeam->getRoster(TRUE); // only starters
		$bench = $thisTeam->getRoster(FALSE); // only bench players

		$maxrostersize = $control->getValue("CONFIG_MAX_TOTAL_PLAYERS");
		$numstarters = $control->getValue("CONFIG_MAX_START_PLAYERS");
		$rostersize = sizeof($starters) + sizeof($bench);
		$availplayers = $maxrostersize - $rostersize;

		$x = new OFFL_Position;
		$positions = $x->getAllPositions();

		$pos_count = array();
		$pos_name = array();
		$pos_count_index = array();
		$lastposid = 0;

		foreach ($positions as $position)
		{
			$posid = $position->getPositionID();
			$pos_count[$position->getPositionAbbv()] = 0;
			$pos_count_index[$posid] = 0;
			$pos_name[$posid] = $position->getPositionAbbv();
			if ($lastposid < $posid)
				$lastposid = $posid;
		}
		$lastposid++;

		// this is not automated as I'm not sure what to do with flex position types.  Maybe they should be added to the position database?  They'd have to be excluded from drafting.
		for ($i = 0; $i < sizeof($pos_count); $i++)
		{
			$key = key($pos_count);
			$key_index = key($pos_count_index);
			$pos = $control->getValue("CONFIG_MAX_START_$key");
			$pos_count[$key] = $pos;
			$pos_count_index[$key_index] = $pos;
			next($pos_count);
			next($pos_count_index);
		}
		unset($pos_count);
?>

<form name="rosterchange" method="post" action="<?php echo $WEB_ROOT; ?>/rosters.php">
	<input type="hidden" name="action" value="save" />
	<input type="hidden" name="fflteam_id" value="<?php echo $_REQUEST["fflteam_id"]; ?>" />
<?php	if ($isAdmin)
		{	?>
	<input type="hidden" name="mode" value="admin" />
<?php	}
		// now display the roster for a team
?>
		<table>
			<tr>
				<th>Status</th>
				<th>Name</th>
				<th>Position</th>
				<th>Team</th>
				<th>Opp.</th>
				<th></th>
			</tr>
<?php
		$player_select_count = 1;

		for ($i = 0; $i < $numstarters; $i++)
		{
			$player = current($starters);
			if ($player)
			{	$current_pos = $player->getPositionID();	}
			else
			{	$current_pos = $lastposid;	}

			// here, for each position id less than current_pos, I note a blank starter space.
			reset($pos_count_index);
			for ($p = key($pos_count_index); $p < $current_pos; $p++)
			{
				while ($pos_count_index[$p] > 0)
				{
					$pos_name_displayed = $pos_name[$p];
					if (strstr($pos_name_displayed, "FLEX"))
					{
						list($p1, $p2, $junk) = explode("_", $pos_name_displayed);
						$pos_name_displayed = $p1 . " / " . $p2;
					}
					echo "<tr><td>Open</td><td style=\"text-align:center;\">---</td><td>$pos_name_displayed</td><td>---</td><td></td><td></td></tr>\n";
					$availplayers--;
					$pos_count_index[$p]--;
				}	
			}

			if ($player)
			// now list current starter stuff
			{
				$posid = $player->getPositionID();
				$posname = $pos_name[$posid];
				// if the string is part of index x on pos_name, subtract 1 from index x on pox_count_index
				foreach($pos_name as $posnum => $pos)
				{
					if (strstr($pos, $posname)) // this is a valid flex position
					{
						if ($pos_count_index[$posnum] > 0)
						{
							$pos_name_displayed = $pos_name[$posnum];
							if (strstr($pos_name_displayed, "FLEX"))
							{
								list($p1, $p2, $junk) = explode("_", $pos_name_displayed);
								$pos_name_displayed = $p1 . " / " . $p2;
							}

							$pos_count_index[$posnum]--;
							break; // don't subtract more positions
						}
					}
				}

				$x = new OFFL_NFLGame();
				$game = $x->getNFLTeamGame($_REQUEST["year"], $week, $player->getNFLTeamID());
				if (is_object($game))
				{
					$opp = $game->getNFLTeamGameOpponent($_REQUEST["year"], $week, $player->getNFLTeamID());
					$gameTime = $game->getGametime();
				}
				else
				{
					$opp = "Bye";
					$gameTime = time() + 3600*24*7;
				}

				$beforeDeadline = TRUE;
				if (($gameTime - 60*15) < time()) // 15 minutes before game lockout
				{	$beforeDeadline = FALSE;	}

				echo "    <tr>\n";

				if ($editable && $beforeDeadline)
				{
					echo "        <td>";
					echo "<select name=\"player" . $player_select_count . "\">\n";
					echo "    <option value=\"1\">" . $player->getPositionAbbv() . "</option>\n";
					echo "    <option value=\"0\">Bench</option>\n</select>\n";
				}
				else
				{
					echo "        <td>" . $player->getPositionAbbv();
					echo "<input type=\"hidden\" name=\"player" . $player_select_count . "\" value=\"1\" />";
				}
				echo "<input type=\"hidden\" name=\"playerid" . $player_select_count . "\" value=\"" . $player->getPlayerID() . "\" /></td>\n";
				$player_select_count++;

				echo "        <td><a href=\"$WEB_ROOT/players.php?player_id=" . $player->getPlayerID() . "\">" . $player->getName() . "</a></td>\n";

				echo "        <td>" . $pos_name_displayed . "</td>\n";
				echo "        <td><abbr title=\"" . $player->getNFLTeamCity() . " " . $player->getNFLTeamName() . "\">" . $player->getNFLTeamAbbv() . "</abbr></td>\n";
				echo "        <td>$opp</td>\n";

				if ($editable && $beforeDeadline)
				{
					echo "        <td class=\"droplink\">";
					echo "<a href=\"$WEB_ROOT/rosters.php?player_id=" . $player->getPlayerID() . "&fflteam_id=" . $player->getFFLTeamID() . "&action=dropplayer";
					if ($isAdmin)
					{	echo "&mode=admin";	}
					echo "\">";
					echo "DROP</a></td>\n";
				}
				else
				{	echo "        <td></td>\n";	}
				echo "    </tr>\n";
			}

			next($starters);
	//		print_r($pos_count_index);
	//		echo "<br>";

		}

	// print_r($pos_name);

	?>	<tr><td colspan="100"><hr></td></tr> <?php // huge colspan ensures I don't need to change it if I build up the table

		foreach ($bench as $player)
		{
			echo "    <tr>\n";

			$x = new OFFL_NFLGame();
			$game = $x->getNFLTeamGame($_REQUEST["year"], $week, $player->getNFLTeamID());
			if (is_object($game))
			{
				$opp = $game->getNFLTeamGameOpponent($_REQUEST["year"], $week, $player->getNFLTeamID());
				$gameTime = $game->getGametime();
			}
			else
			{
				$opp = "Bye";
				$gameTime = time() + 3600;
			}

			$beforeDeadline = 1;
			if (($gameTime - 60*15) < time()) // 15 minutes before game lockout
			{	$beforeDeadline = 0;	}

			if ($editable && $beforeDeadline)
			{
				echo "        <td>";
				echo "<select name=\"player" . $player_select_count . "\">\n";
				echo "    <option value=\"0\">Bench</option>\n";
				echo "    <option value=\"1\">" . $player->getPositionAbbv() . "</option>\n</select>\n";
			}
			else
			{
				echo "        <td>Bench\n";
				echo "<input type=\"hidden\" name=\"player" . $player_select_count . "\" value=\"0\" />";
			}
			echo "<input type=\"hidden\" name=\"playerid" . $player_select_count . "\" value=\"" . $player->getPlayerID() . "\" />\n"; 
			echo "\t</td>\n";
			$player_select_count++;

			echo "        <td><a href=\"$WEB_ROOT/players.php?player_id=" . $player->getPlayerID() . "\">" . $player->getName() . "</a></td>\n";

			echo "        <td>" . $player->getPositionAbbv() . "</td>\n";
			echo "        <td><abbr title=\"" . $player->getNFLTeamCity() . " " . $player->getNFLTeamName() . "\">" . $player->getNFLTeamAbbv() . "</abbr></td>\n";
			echo "        <td>$opp</td>\n";

			if ($editable && $beforeDeadline)
			{
				echo "        <td class=\"droplink\">";
				echo "<a href=\"$WEB_ROOT/rosters.php?player_id=" . $player->getPlayerID() . "&amp;fflteam_id=" . $player->getFFLTeamID() . "&amp;action=dropplayer";
				if ($isAdmin)
				{	echo "&amp;mode=admin";	}
				echo "\">";
				echo "DROP</a></td>\n";
			}
			else
				echo "        <td></td>\n";

			echo "    </tr>\n";
		}

	//	for ($i = $rostersize; $i < $maxrostersize; $i++)
		// note available bench spots
		while ($availplayers > 0)
		{
			echo "<tr><td>Open</td><td style=\"text-align:center;\">---</td><td>Bench</td><td>---</td><td></td></tr>\n";
			$availplayers--;
		}

		echo "</table>\n";

		if ($editable)
			echo "<input type=\"submit\" value=\"Submit Roster\">\n";
		echo "</form>\n";

		// add link for "add player"

		if ($editable)
		{
			echo "<form name=\"addplayer\" method=\"post\" action=\"$WEB_ROOT/rosters.php\">\n";
			echo "<input type=\"hidden\" name=\"action\" value=\"addplayer\" />\n";
			echo "<input type=\"hidden\" name=\"step\" value=\"1\" />\n";
			echo "<input type=\"hidden\" name=\"fflteam_id\" value=\"" . $_REQUEST["fflteam_id"] . "\" />\n";
			if ($isAdmin)
			{
				echo "<input type=\"hidden\" name=\"mode\" value=\"admin\" />\n";
			}
			echo "<input type=\"submit\" value=\"Add Free Agent\" />\n</form>\n";
		}
		if ($isCurrentTeam)
		{
?>	<form name="proposetrade" method="post" action="<?php echo $WEB_ROOT; ?>/trades.php">
<!-- I have removed the fflteam_id input -->
		<input type="hidden" name="action" value="proposetrade" />
		<input type="hidden" name="step" value="1" />
		<input type="submit" value="Propose Trade" />
	</form>
<?php		}
?>	<p>Visit the <a href="http://www.nfl.com/injuries/" title="NFL.com Injury Report">NFL Injury Report</a> for player availability (note: does not include injured reserve).</p>
<?php

	/*
		Now draw the table for teams in league

	*/
	?></td>
	<td style="vertical-align:top;">
	<table id="teamlist">
		<tr class="evenrow"><td>
			<a href="<?php echo $WEB_ROOT; ?>/rosters.php?view=allteams<?php if ($isAdmin) echo "&mode=admin"; ?>">All Teams</a>
		</td></tr>
	<?php
	$teams = $myleague->getAllFFLTeams();
	foreach ($teams as $i=>$team)
	{
		if ($i % 2)
			echo "<tr class=\"evenrow\"><td>\n";
		else
			echo "<tr class=\"oddrow\"><td>\n";
		if ($_REQUEST["fflteam_id"] != $team->getFFLTeamID())
		{
			echo "<a href=\"$WEB_ROOT/rosters.php?fflteam_id=" . $team->getFFLTeamID();
			if ($isAdmin)
				echo "&mode=admin";
			echo "\">";
		}
		echo $team->getFFLTeamFullName();
		if ($_REQUEST["fflteam_id"] != $team->getFFLTeamID())
		{
			echo "</a>";
		}
		echo "</td></tr>\n";
	}

	?></table></td></tr></table>
<?php
	} // end else show single team
} // end if continue_with_page
require("$DOC_ROOT/lib/footer.php"); ?>
Return current item: Online Fantasy Football League