Location: PHPKode > projects > Online Fantasy Football League > offl-0.2.6/www/lib/classes/offl_control.php
<?php
/**
 * Defines the OFFL_Control class, which stores configuration data for the OFFL system
 *
 * @package offl
 * @author Stephen Rochelle <hide@address.com>
 */

if (strtr(__FILE__, "\\", "/") == $_SERVER["SCRIPT_FILENAME"])
{	die ("Cannot access file directly!");	}

require_once($DOC_ROOT . "/lib/classes/offl_dbobject.php");
/**
 * The OFFL_Control class provides an interface to various settings for the OFFL system
 *
 * API is drastically revised from FFL, check all uses of old Control class.
 *
 * I'll need to see how responsive this is.  It likely slows down things like score calculations where lots of control values are pulled from the database.
 *
 * @package offl
 */
class OFFL_Control extends OFFL_DBObject
{
	/**
	 * @var integer
	 */
	var $_league_id = NULL;
	/**
	 * @var boolean Integral boolean (1/0)
	 */
	var $_all_leagues = 0;

	/**
	 * Class constructor.  Set {@link $_league_id} to get league-specific values instead of defaults.
	 *
	 * @param integer $league_id Optional: set to get league-specific values
	 */
	function OFFL_Control ($league_id = NULL) 
	{
		OFFL_DBObject::OFFL_DBObject();

		$this->_league_id = $league_id;
	}

	/**
	 * Sets the {@link $_league_id} field
	 *
	 * @param integer $league_id
	 */
	function setLeagueID($league_id)
	{
		$this->_league_id = $league_id;
	}

	/**
	 * @return integer
	 */
	function getLeagueID()
	{
		return $this->_league_id;
	}

	/**
	 * @return string|NULL
	 */
	function getValue($key)
	{
		$val = NULL;

		$sql = "SELECT control_value, league_id FROM control WHERE (league_id=0";
		if (isset($this->_league_id))
		{	$sql .= " OR league_id=" . $this->_league_id;	}
		$sql .= ") AND control_key='" . mysql_escape_string($key) . "'";
		$result = mysql_query($sql,$this->_conn) or die (mysql_error() . ": $sql");

		for ($i = 0; $i < mysql_num_rows($result); $i++)
		{
			if (is_null($val) || (mysql_result($result, $i, "league_id") == $this->_league_id))
			{	$val = mysql_result($result, $i, "control_value");	}
		}
		mysql_free_result($result);

		return $val;
	}

	/**
	 * @return string|NULL
	 */
	function getDescription($key)
	{
		$desc = NULL;

		$sql = "SELECT control_desc, league_id FROM control WHERE (league_id=0";
		if (isset($this->_league_id))
		{	$sql .= " OR league_id=" . $this->_league_id;	}
		$sql .= ") AND control_key='" . mysql_escape_string($key) . "'";
		$result = mysql_query($sql,$this->_conn) or die (mysql_error() . ": $sql");

		for ($i = 0; $i < mysql_num_rows($result); $i++)
		{
			if (is_null($desc) || (mysql_result($result, $i, "league_id") == $this->_league_id))
			{	$desc = mysql_result($result, $i, "control_desc");	}
		}
		mysql_free_result($result);

		return $desc;
	}

	/**
	 * @return array Array of strings
	 */
	function getAllowedValues($key)
	{
		$allVal = array();

		$sql = "SELECT control_allowed_values, league_id FROM control WHERE (league_id=0";
		if (isset($this->_league_id))
		{	$sql .= " OR league_id=" . $this->_league_id;	}
		$sql .= ") AND control_key='" . mysql_escape_string($key) . "'";
		$result = mysql_query($sql,$this->_conn) or die (mysql_error() . ": $sql");

		for ($i = 0; $i < mysql_num_rows($result); $i++)
		{
			if ((sizeof($allVal) == 0) || (mysql_result($result, $i, "league_id") == $this->_league_id))
			{
				if (strlen(mysql_result($result, $i, "control_allowed_values")))
					$allVal = explode(",", mysql_result($result, $i, "control_allowed_values"));
			}
		}
		mysql_free_result($result);

		if (sizeof($allVal) == 0)
			return NULL;

		return $allVal;
	}

	/**
	 * Creates a new control key for the given league_id
	 * @param string $key
	 * @return boolean TRUE on success, FALSE otherwise
	 */
	function createKey($key)
	{
		$sql = "SELECT league_id FROM control WHERE control_key='" . mysql_escape_string($key) . "' AND league_id=";
		if (isset($this->_league_id))
		{	$sql .= $this->_league_id;	}
		else
		{	$sql .= "0";	}
		$result = mysql_query($sql,$this->_conn) or die (mysql_error() . ": $sql");
		if (mysql_num_rows($result) > 0)
		{	return FALSE;	}

		$sql = "INSERT INTO controls (control_key";
		if (isset($this->_league_id))
		{	$sql .= ", league_id";	}
		$sql .= ") VALUES ('" . mysql_escape_string($key) . "'";
		if (isset($this->_league_id))
		{	$sql .= ", " . $this->_league_id;	}
		$sql .= ")";

		$result = mysql_query($sql,$this->_conn) or die (mysql_error() . ": $sql");
		return $result;
	}
	
	/**
	 * Sets a key value
	 *
	 * Set for a particular league if {@link $_league_id} is set, otherwise sets league default value
	 * Requires a test for whether or not key already exists (currently doesn't create league-specific
	 * values).
	 * @param string $key Control key to be set
	 * @param string $val Value for control key (not checked against possible allowed values)
	 * @return boolean TRUE on success, FALSE otherwise
	 */
	function setValue($key, $val)
	{
		if (isset($this->_league_id) && ($this->_league_id > 0))
		{
			$result = $this->checkKeyExists($key);
			if ($result === TRUE)
			{
				// lookup description, allowed values; insert them too.
				$sql = "SELECT control_desc, control_allowed_values, all_leagues FROM control WHERE league_id=0 AND control_key='" . $key . "'";
				$result = mysql_query($sql,$this->_conn) or die (mysql_error() . ": $sql");
				$allVals = mysql_result($result, 0, "control_allowed_values");
				$description = mysql_result($result, 0, "control_desc");
				$all_leagues = mysql_result($result, 0, "all_leagues");
				
				if (!$all_leagues)
				{	$sql = "INSERT INTO control (control_key, control_value, control_desc, control_allowed_values, league_id) VALUES ('" . mysql_escape_string($key) . "', '" . mysql_escape_string($val) . "', '" . $description . "', '" . $allVals . "', ". $this->_league_id . ")";	}
				else
				{	$sql = "UPDATE control SET control_value='" . mysql_escape_string($val) . "' WHERE control_key='" . mysql_escape_string($key) . "'";	}
			}
			else
			{	$sql = "UPDATE control SET control_value='" . mysql_escape_string($val) . "' WHERE control_key='" . mysql_escape_string($key) . "' AND league_id=" . $this->_league_id;	}
		}
		else
		{	$sql = "UPDATE control SET control_value='" . mysql_escape_string($val) . "' WHERE control_key='" . mysql_escape_string($key) . "' AND league_id=0";	}

		$result = mysql_query($sql,$this->_conn) or die (mysql_error() . ": $sql");
		return $result;
	}
	
	/**
	 * Sets a key description
	 *
	 * Set for a particular league if {@link $_league_id} is set, otherwise sets league default value
	 * @param string $key Control key to be set
	 * @param string $desc Description for control key
	 * @return boolean TRUE on success, FALSE otherwise
	 */
	function setDesc($key, $desc)
	{
		$sql = "UPDATE control SET control_desc='" . mysql_escape_string($desc) . "' WHERE control_key='" . mysql_escape_string($key) . "' AND league_id=";
		if (isset($this->_league_id))
		{	$sql .= $this->_league_id;	}
		else
		{	$sql .= "0";	}

		$result = mysql_query($sql,$this->_conn) or die (mysql_error() . ": $sql");
		return $result;
	}

	/**
	 * Sets a key's allowed values
	 *
	 * Set for a particular league if {@link $_league_id} is set, otherwise sets league default value
	 * @param string $key Control key to be set
	 * @param array $allVal Array of strings (allowed values)
	 * @return boolean TRUE on success, FALSE otherwise
	 */
	function setAllowedValues($key, $allVal)
	{
		$sql = "UPDATE control SET control_allowed_values='" . implode(",", $allVal) . "' WHERE control_key='" . mysql_escape_string($key) . "' AND league_id=";
		if (isset($this->_league_id))
		{	$sql .= $this->_league_id;	}
		else
		{	$sql .= "0";	}

		$result = mysql_query($sql,$this->_conn) or die (mysql_error() . ": $sql");
		return $result;
	}

	/**
	 * Tests for the existence of a control key, and if that key is league-generic or league-specific
	 * @param string $key
	 * @return boolean|integer FALSE if not found, TRUE if league-generic, league_id if league-specific
	 */
	function checkKeyExists($key)
	{
		$val = NULL;

		$sql = "SELECT league_id FROM control WHERE (league_id=0";
		if (isset($this->_league_id))
		{	$sql .= " OR league_id=" . $this->_league_id;	}
		$sql .= ") AND control_key='" . mysql_escape_string($key) . "'";
		$result = mysql_query($sql,$this->_conn) or die (mysql_error() . ": $sql");

		if (mysql_num_rows($result) == 0)
		{
			mysql_free_result($result);
			return FALSE;
		}
	
		for ($i = 0; $i < mysql_num_rows($result); $i++)
		{
			if (mysql_result($result, $i, "league_id") == $this->_league_id)
			{
				mysql_free_result($result);
				return $this->_league_id;
			}
		}

		mysql_free_result($result);
		return TRUE;
	}

	/**
	 * Returns all control keys, optionally for a specific league
	 *
	 * Set {@link $_league_id} to get league-specific keys
	 * @return array Array of strings (control keys)
	 */
	function getAllControlKeys()
	{
		$retArr = array();
		$sql = "SELECT DISTINCT control_key FROM control WHERE league_id=0";
		if (isset($this->_league_id))
		{	$sql .= " OR league_id=" . $this->_league_id;	}

		$result = mysql_query($sql,$this->_conn) or die (mysql_error() . ": $sql");

		for ($i = 0; $i < mysql_num_rows($result); $i++)
		{
			$retArr[] = mysql_result($result, $i, "control_key");
		}
		mysql_free_result($result);
		return $retArr;
	}
}
?>
Return current item: Online Fantasy Football League