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

class TrackObj
{
	public function GetTracks($albumId)
	{
		$query = <<<EOD
			SELECT
				*
			FROM
				Tracks
			WHERE
				 AlbumId = {$albumId}
EOD;

		$result = mysql_query($query) or die(mysql_error() . "<br>$query");
		
		$trackArray = array();
		while($row = mysql_fetch_assoc($result)) {
			$tempObj = new TrackObj;
			$tempObj -> ProfileId = $row['ProfileId'];
			$tempObj -> AlbumId = $row['AlbumId'];
			$tempObj -> TrackId = $row['TrackId'];
			$tempObj -> TrackName = $row['TrackName'];
			$tempObj -> TrackLength = $row['TrackLength'];
			$tempObj -> ReleaseDate = $row['ReleaseDate'];
			$tempObj -> Producer = $row['Producer'];
			
			$tempObj -> IsFeatured = $row['IsFeatured'];
			$tempObj -> IsSingle = $row['IsSingle'];
			
			$trackArray[] = $tempObj;
		}
		
		return $trackArray;
	}
	
	public function GetFeaturedTracks($profileId)
	{
		$query = <<<EOD
			SELECT
				*
			FROM
				Tracks
			WHERE
				 ProfileId = {$profileId}
				 AND IsFeatured = true
EOD;

		$result = mysql_query($query) or die(mysql_error() . "<br>$query");
		
		$trackArray = array();
		while($row = mysql_fetch_assoc($result)) {
			$tempObj = new TrackObj;
			$tempObj -> ProfileId = $row['UserId'];
			$tempObj -> AlbumId = $row['AlbumId'];
			$tempObj -> TrackId = $row['TrackId'];
			$tempObj -> TrackName = $row['TrackName'];
			$tempObj -> TrackLength = $row['TrackLength'];
			$tempObj -> ReleaseDate = $row['ReleaseDate'];
			$tempObj -> Producer = $row['Producer'];
			
			$tempObj -> IsFeatured = $row['IsFeatured'];
			$tempObj -> IsSingle = $row['IsSingle'];
			
			$trackArray[] = $tempObj;
		}
		
		return $trackArray;
	}
	
	public function GetSingles($profileId)
	{
		$query = <<<EOD
			SELECT
				*
			FROM
				Tracks
			WHERE
				 ProfileId = {$profileId}
				 AND IsSingle = true
EOD;

		$result = mysql_query($query) or die(mysql_error() . "<br>$query");
		
		$trackArray = array();
		while($row = mysql_fetch_assoc($result)) {
			$tempObj = new TrackObj;
			$tempObj -> ProfileId = $row['ProfileId'];
			$tempObj -> AlbumId = $row['AlbumId'];
			$tempObj -> TrackId = $row['TrackId'];
			$tempObj -> TrackName = $row['TrackName'];
			$tempObj -> TrackLength = $row['TrackLength'];
			$tempObj -> ReleaseDate = $row['ReleaseDate'];
			$tempObj -> Producer = $row['Producer'];
			
			$tempObj -> IsFeatured = $row['IsFeatured'];
			$tempObj -> IsSingle = $row['IsSingle'];
			
			$trackArray[] = $tempObj;
		}
		
		return $trackArray;
	}
	
	public function GetTrack()
	{
		$query = <<<EOD
			SELECT
				*
			FROM
				Tracks
			WHERE
				 TrackId = {$this->TrackId}
EOD;

		$result = mysql_query($query) or die(mysql_error() . "<br>$query");
		
		$row = mysql_fetch_assoc($result);
		
			$this -> ProfileId = $row['ProfileId'];
			$this -> AlbumId = $row['AlbumId'];
			$this -> TrackId = $row['TrackId'];
			$this -> TrackName = $row['TrackName'];
			$this -> TrackLength = $row['TrackLength'];
			$this -> ReleaseDate = $row['ReleaseDate'];
			$this -> Producer = $row['Producer'];
			
			$this -> IsFeatured = $row['IsFeatured'];
			$this -> IsSingle = $row['IsSingle'];
	}
	
	public function UpdateTrack()
	{
		if($this -> TrackId != '') {
			$query = <<<EOD
				UPDATE
					Tracks
				SET
					ProfileId = {$this->ProfileId},
					AlbumId = {$this->AlbumId}
					TrackName = '{$this->TrackName}',
					TrackLength = '{$this->TrackLength}',
					ReleaseDate = '{$this->ReleaseDate}',
					Producer = '{$this->OfficalWebsite}',
					IsFeatured = {$this->IsFeatured},
					IsSingle = {$this->IsSingle}
				WHERE
					TrackId = {$this->TrackId}
EOD;
			$result = mysql_query($query);
			$userMsg = "Track information has successfully updated.";
		} else {
			$query = <<<EOD
				INSERT INTO
					Tracks
					(ProfileId, 
					AlbumId,
					TrackName,
					TrackLength,
					ReleaseDate,
					Producer,
					IsFeatured,
					IsSingle)
				VALUES
					({$this->ProfileId},
					{$this->AlbumId},
					'{$this->TrackName}',
					'{$this->TrackLength}',
					'{$this->ReleaseDate}',
					'{$this->Producer}',
					'{$this->IsFeatured}',
					'{$this->IsSingle}')
EOD;

			$result = mysql_query($query) or die(mysql_error()."<br>".$query);
			$this -> TrackId = mysql_insert_id();
			$userMsg = "Track 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;
	}
}
?>