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

class LinksObj
{
    public function GetSocialMediaLinks($profileId)
	{
		$query = <<<EOD
			SELECT
				*
			FROM
				Links
			WHERE
				 ProfileId = {$profileId}
				 AND IsSocialMedia = true
EOD;

		$result = mysql_query($query) or die(mysql_error() . "<br>$query");
		
		$linkArray = array();
		while($row = mysql_fetch_assoc($result)) {
			$tempObj = new LinksObj;
			$tempObj -> ProfileId = $row['ProfileId'];
			$tempObj -> LinkId = $row['LinkId'];
			$tempObj -> LinkName = $row['LinkName'];
			$tempObj -> LinkUrl = $row['LinkUrl'];
			$tempObj -> LinkIcon = $row['LinkIcon'];
			$tempObj -> IsSocialMedia = $row['IsSocialMedia'];
			$tempObj -> LinkUsername = $row['LinkUsername'];
			$tempObj -> LinkPassword = $row['LinkPassword'];
			$tempObj -> SocialMediaType = $row['SocialMediaType'];
			$tempObj -> SocialMediaAccountName = $row['SocialMediaAccountName'];
			$tempObj -> UserToken = $row['UserToken'];
			$tempObj -> UserKey = $row['UserKey'];
			
			$linkArray[] = $tempObj;
		}
		
		return $linkArray;
	}
	
	public function GetExternalLinks($profileId)
	{
		$query = <<<EOD
			SELECT
				*
			FROM
				Links
			WHERE
				 ProfileId = {$profileId}
				 AND IsSocialMedia = false
EOD;

		$result = mysql_query($query) or die(mysql_error() . "<br>$query");
		
		$linkArray = array();
		while($row = mysql_fetch_assoc($result)) {
			$tempObj = new LinksObj;
			$tempObj -> ProfileId = $row['ProfileId'];
			$tempObj -> LinkId = $row['LinkId'];
			$tempObj -> LinkName = $row['LinkName'];
			$tempObj -> LinkUrl = $row['LinkUrl'];
			$tempObj -> LinkIcon = $row['LinkIcon'];
			$tempObj -> IsSocialMedia = $row['IsSocialMedia'];
			$tempObj -> LinkUsername = $row['LinkUsername'];
			$tempObj -> LinkPassword = $row['LinkPassword'];
			$tempObj -> SocialMediaType = $row['SocialMediaType'];
			$tempObj -> SocialMediaAccountName = $row['SocialMediaAccountName'];
			$tempObj -> UserToken = $row['UserToken'];
			$tempObj -> UserKey = $row['UserKey'];			
			
			$linkArray[] = $tempObj;
		}
		
		return $linkArray;
	}
	
	public function GetLink()
	{
		$query = <<<EOD
			SELECT
				*
			FROM
				Links
			WHERE
				 LinkId = {$this->LinkId}
EOD;

		$result = mysql_query($query) or die(mysql_error() . "<br>$query");
		
		$row = mysql_fetch_assoc($result);

			$this -> ProfileId = $row['ProfileId'];
			$this -> LinkId = $row['LinkId'];
			$this -> LinkName = $row['LinkName'];
			$this -> LinkUrl = $row['LinkUrl'];
			$this -> LinkIcon = $row['LinkIcon'];
			$this -> IsSocialMedia = $row['IsSocialMedia'];
			$this -> LinkUsername = $row['LinkUsername'];
			$this -> LinkPassword = $row['LinkPassword'];
			$this -> SocialMediaType = $row['SocialMediaType'];
			$this -> SocialMediaAccountName = $row['SocialMediaAccountName'];
			$this -> UserToken = $row['UserToken'];
			$this -> UserKey = $row['UserKey'];
	}
	
	public function UpdateLink()
	{
		if($this -> LinkId != '') {
			$query = <<<EOD
				UPDATE
					Links
				SET
					ProfileId = {$this->ProfileId},
					LinkName = '{$this->LinkName}',
					LinkUrl = '{$this->LinkUrl}',
					LinkIcon = '{$this->LinkIcon}',
					IsSocialMedia = {$this->IsSocialMedia},
					LinkUsername = '{$this->LinkUsername}',
					LinkPassword = '{$this->LinkPassword}',
					SocialMediaType = {$this->SocialMediaType},
					SocialMediaAccountName = '{$this->SocialMeidaAccountName}'
				WHERE
					LinkId = {$this->LinkId}
EOD;
			$result = mysql_query($query);
			$userMsg = "Link information has successfully updated.";
		} else {
			$query = <<<EOD
				INSERT INTO
					Links
					(ProfileId, 
					LinkName,
					LinkUrl,
					LinkIcon,
					IsSocialMedia,
					LinkUsername,
					LinkPassword,
					SocialMediaType,
					SocialMediaAccountName)
				VALUES
					({$this->ProfileId},
					'{$this->LinkName}',
					'{$this->LinkUrl}',
					'{$this->LinkIcon}',
					{$this->IsSocialMedia},
					'{$this->LinkUsername}',
					'{$this->LinkPassword}',
					{$this->SocialMediaType},
					'{$this->SocialMediaAccountName}')
EOD;

			$result = mysql_query($query) or die(mysql_error()."<br>".$query);
			$this -> LinkId = mysql_insert_id();
			$userMsg = "Link has been added successfully.";
		}
		if(mysql_error() != '') {
			$userMsg = "An error has occurred while attempting your request.  Please try again.  If you continue to experience problems, please contact the site administrator<br>";
			$userMsg .= mysql_error() . "<br>$query";
			die($userMsg);
		}
		return $userMsg;
	}
	
	function UpdateTwitterTokens()
	{
		$query = <<<EOD
				UPDATE
					Links
				SET
					UserToken = '{$this->UserToken}',
					UserKey = '{$this->UserKey}'
				WHERE
					LinkId = {$this->LinkId}
EOD;
			$result = mysql_query($query);
			$userMsg = "Link information has successfully updated.";
			
		if(mysql_error() != '') {
			$userMsg = "An error has occurred while attempting your request.  Please try again.  If you continue to experience problems, please contact the site administrator<br>";
			$userMsg .= mysql_error() . "<br>$query";
			die($userMsg);
		}
		return $userMsg;
	}
}
?>