<?
if (!defined('IN_BANDTOPIA'))
{
	exit;
}

class UserSecyObj
{
	private $int_user_secy_id;
	private $int_user_table_id;
	private $int_secy_cmpt_id;
	private $int_username;
	private $int_user_fullname;
	private $int_secy_cmpt_name;
	
	private $int_action;
	
	/*************************************************************/
	/* List Users
	/*************************************************************/
	public function listUserSecy()
	{
		$query = <<<EOD
			SELECT
				*
			FROM
				user_secy AS us
				LEFT JOIN
					users AS u ON u.user_table_id = us.user_table_id
				LEFT JOIN
					secy_cmpt AS sc ON sc.secy_cmpt_id = us.secy_cmpt_id 
					
EOD;
		if($this->int_user_table_id != '')
		{
			$query .= <<<EOD
				WHERE
					us.user_table_id = '{$this->int_user_table_id}' AND
					u.user_active = 1 AND
					sc.secy_cmpt_active = 1
				ORDER BY
					sc.secy_cmpt_name
EOD;
		}
		elseif($this->int_secy_cmpt_id != '')
		{
			$query .= <<<EOD
				WHERE 
					secy_cmpt_id = '{$this->int_secy_cmpt_id}' AND
					u.user_active = 1 AND
					sc.secy_cmpt_active = 1
				ORDER BY
					u.user_fullname
EOD;
		}

		$result = mysql_query($query) or die(mysql_error()."<br>$query");
		$usersecyArray = array();
		while($row = mysql_fetch_assoc($result)) {
			$tempObj = new UserSecyObj;
			$tempObj -> set_user_secy_id($row['user_secy_id']);
			$tempObj -> set_user_table_id($row['user_table_id']);
			$tempObj -> set_secy_cmpt_id($row['secy_cmpt_id']);
			$tempObj -> set_username($row['username']);
			$tempObj -> set_user_fullname($row['user_fullname']);
			$tempObj -> set_secy_cmpt_name(ucwords(strtolower($row['secy_cmpt_name'])));
			$usersecyArray[] = $tempObj;
		}
		return $usersecyArray;
	}
	
	public function getCmptPermission()
	{
		$permission = false;
		if($this->int_user_table_id != '' && $this->int_secy_cmpt_id != '')
		{
			$query = <<<EOD
				SELECT
					*
				FROM
					user_secy
				WHERE
					user_table_id = {$this->int_user_table_id} AND
					secy_cmpt_id = {$this->int_secy_cmpt_id}
EOD;
			$result = mysql_query($query) or die(mysql_error()."<br>$query");
			$row = mysql_fetch_assoc($result);
			if($row['user_secy_id'] != '')
			{
				$permission = true;
			}
		}
		else
		{
			$int_error = "You have an error.  Please try again.";
		}
		return $permission;
	}
	
	public function updateCmptPermission()
	{
		if($this->int_user_table_id != '' && $this->int_secy_cmpt_id != '')
		{
			if($this->int_action == "remove")
			{
				$query = <<<EOD
					DELETE
					FROM
						user_secy
					WHERE
						user_table_id = {$this->int_user_table_id} AND
						secy_cmpt_id = {$this->int_secy_cmpt_id}
EOD;
			}
			elseif($this->int_action == "insert")
			{
				$query = <<<EOD
					INSERT INTO
						user_secy
						(
							user_table_id,
							secy_cmpt_id
						)
					VALUES
						(
							{$this->int_user_table_id},
							{$this->int_secy_cmpt_id}
						)
EOD;
			}
			else
			{
				echo("Houston, we have a problem.");
				exit;
			}
			$result = mysql_query($query) or die(mysql_error()."<br>$query");
		}
	}
	

	// Get User Secy Id
	public function get_user_secy_id()
	{
		return $this->int_user_secy_id;
	}
	// Set User Secy Id
	public function set_user_secy_id($value)
	{
		$this->int_user_secy_id = $value;
	}

	// Get User Table Id
	public function get_user_table_id()
	{
		return $this->int_user_table_id;
	}
	// Set user Table Id
	public function set_user_table_id($value)
	{
		$this->int_user_table_id = $value;
	}

	// Get Component Id
	public function get_secy_cmpt_id()
	{
		return $this->int_secy_cmpt_id;
	}
	// Set Component Id
	public function set_secy_cmpt_id($value)
	{
		$this->int_secy_cmpt_id = $value;
	}
	
	// Get Username
	public function get_username()
	{
		return $this->int_username;
	}
	// Set Username
	public function set_username($value)
	{
		$this->int_username = $value;
	}
	
	// Get User Fullname
	public function get_user_fullname()
	{
		return $this->int_user_fullname;
	}
	// Set User Fullname
	public function set_user_fullname($value)
	{
		$this->int_user_fullname = $value;
	}
	
	// Get Component Name
	public function get_secy_cmpt_name()
	{
		return $this->int_secy_cmpt_name;
	}
	// Set Component Name
	public function set_secy_cmpt_name($value)
	{
		$this->int_secy_cmpt_name = $value;
	}
	
	
	
	// Get User Perm (does this user have permission
	public function get_action()
	{
		return $this->int_action;
	}
	// Set User Perm
	public function set_action($value)
	{
		$this->int_action = $value;
	}
}

?>