Location: PHPKode > projects > Online Fantasy Football League > offl-0.2.6/www/maintenance/processtrades.php
<?php
/**
 * Processes trades
 * 
 * This file should parse current trades and process those whose deadline is met.  Actual trade processing, however,
 * should be done in the Trade class, as admins may invoke it for singular trades.
 * 
 * This file should be run via cron.  I recommend several times daily (for better response, since it's not time-critical
 * for fairness like waivers) on Tues->Sat at 2AM, but you can pick.
 * 
 * that line would look like:
 * 
 * 0 2,8,14,20 * * 1-5 /path/to/php /path/to/processtrades.php
 * 
 * alternately, win32 servers can use pycron (http://www.kalab.com/freeware/pycron/pycron.htm)
 * and have a line like
 * 
 * 0 2,8,14,20 * * 1-5 "c:\path\to\php" "c:\path\to\processtrades.php"
 * 
 * Since you're not accessing this via the web, you should place it outside of your document root.
 * There are no permission checks; therefore, anyone could prematurely execute a waiver claim if the
 * file is public.  Security through obscurity is a poor substitute.
 * 
 * @author Stephen Rochelle <hide@address.com>
 * @version OFFL v0.2
 * @copyright Copyright (c) 2004 Stephen Rochelle.  Some rights reserved.
 * @package offl-maint
 */

$pageTitle = "Trade Processing";
$public = TRUE;
$maintenance = TRUE;
require_once("offlconfig.php");
require_once($DOC_ROOT . "/lib/header.php"); 

/*
	get all trades, but use only the first legit one found.
	must then re-sort (in case someone traded player A to three teams or whatever)
	repeat until no appropriate trades remain.
*/

$x = new OFFL_Trade();
$all_trades = $x->getAllTrades(TRUE);

foreach($all_trades as $trade)
{
	if (($trade->getDeadline() - time()) > 0) // trade not at deadline
	{	continue;	}

	switch ($trade->getStatus())
	{
		case "accepted":

			if ($trade->checkTradelock() != 0)
			{ // players are tradelocked, delete (failure)
				$trade->deleteTrade();
				$errors[] = "Trade deleted due to tradelock of involved players.";
				break;
			}

			$errors = $trade->checkTradeRosters();

			if (sizeof($errors)) 
			{
				$trade->setStatus("rosterviolation");
				$trade->setDeadline(time(), "rosterviolation"); // sets max time for players to tradelock
				$trade->setTradelock(1);
				$trade->save();
				echo "<h2 class=\"success\">Trade processed, pending roster fixes by participants</h2>\n";
			}
			else
			{
				$errors = $trade->processTrade();
				$trade->deleteTrade();
				
				if (sizeof($errors))
					echo "<h2 class=\"error\">Expected players not on teams!  Trade cancelled.</h2>\n";
				else
					echo "<h2 class=\"success\">Trade processed!</h2>\n";
			}
			break;
		case "rosterviolation":
			$trade->setTradelock(0);
			$trade->deleteTrade();
			break;
		default: // garbage removal for declined / vetoed / protested trades
			$trade->deleteTrade();
			break;
	}

	if (sizeof($errors)) // do something if errors happen?
	{	$foo = 0;	}

}

require($DOC_ROOT . "/lib/footer.php"); ?>
Return current item: Online Fantasy Football League