<?php
class class_data extends class_db
{
	function __construct()
	{
		$this->db();
	}

	function get_banners($static_uint_news_cate_id, $static_uint_row_count)
	{
		$str_query = 'SELECT * 
		FROM `banners` 
		WHERE `Active` = \'1\' 
		AND `NewsCateId` = \'' . (int)$static_uint_news_cate_id . '\' 
		ORDER BY RAND() ';

		if( $static_uint_row_count )
			$str_query .= 'LIMIT 0, ' . (int)$static_uint_row_count . ' ';

		if( !( $sql_result = mysqli_query($this->sql_connect, $str_query) ) )
		{
			$this->write_error_logs($str_query);
			return false;
		}
		else
			return $this->result_setto_array($sql_result);
	}

	function get_news_cates()
	{
		$str_query = 'SELECT * 
		FROM `news_cates`
		WHERE 1 
		ORDER BY `Order` ';
		if( !( $sql_result = mysqli_query($this->sql_connect, $str_query) ) )
		{
			$this->write_error_logs($str_query);
			return false;
		}
		else
			return $this->result_setto_array($sql_result);
	}

	function search_news($static_str_search_keyword, $static_uint_offset, $static_uint_row_count)
	{
		$str_query = 'SELECT `news`.*, `news_cates`.* 
		FROM `news` 
		INNER JOIN `news_cates_news` 
		ON `news_cates_news`.`NewsId` = `news`.`NewsId` 
		INNER JOIN `news_cates` 
		ON `news_cates`.`NewsCateId` = `news_cates_news`.`NewsCateId` 
		WHERE `news`.`Active` = \'1\' 
		AND `news`.`Date` <= \'' . date('Y-m-d') . '\' 
		AND ( `news`.`Subject` LIKE \'%' . mysqli_real_escape_string($this->sql_connect, $static_str_search_keyword) . '%\' OR `news`.`Abstract` LIKE \'%' . mysqli_real_escape_string($this->sql_connect, $static_str_search_keyword) . '%\' ) 
		GROUP BY `news`.`NewsId` 
		ORDER BY `news`.`Date` DESC 
		LIMIT ' . (int)$static_uint_offset . ', ' . (int)$static_uint_row_count . ' ';
		if( !( $sql_result = mysqli_query($this->sql_connect, $str_query) ) )
		{
			$this->write_error_logs($str_query);
			return false;
		}
		else
			return $this->result_setto_array($sql_result);
	}

	function get_latest_news($static_uint_row_count)
	{
		$str_query = 'SELECT `news`.*, `news_cates`.* 
		FROM `news` 
		INNER JOIN `news_cates_news` 
		ON `news_cates_news`.`NewsId` = `news`.`NewsId` 
		INNER JOIN `news_cates` 
		ON `news_cates`.`NewsCateId` = `news_cates_news`.`NewsCateId` 
		WHERE `news`.`Active` = \'1\' 
		AND `news`.`Date` <= \'' . date('Y-m-d') . '\' 
		GROUP BY `news`.`NewsId` 
		ORDER BY `news`.`Date` DESC 
		LIMIT 0, ' . (int)$static_uint_row_count . ' ';
		if( !( $sql_result = mysqli_query($this->sql_connect, $str_query) ) )
		{
			$this->write_error_logs($str_query);
			return false;
		}
		else
			return $this->result_setto_array($sql_result);
	}

	function get_suggest_news($static_uint_row_count)
	{
		$str_query = 'SELECT `news`.*, `news_cates`.* 
		FROM `news` 
		INNER JOIN `news_cates_news` 
		ON `news_cates_news`.`NewsId` = `news`.`NewsId` 
		INNER JOIN `news_cates` 
		ON `news_cates`.`NewsCateId` = `news_cates_news`.`NewsCateId` 
		WHERE `news`.`Active` = \'1\' 
		AND `news`.`Date` <= \'' . date('Y-m-d') . '\' 
		GROUP BY `news`.`NewsId` 
		ORDER BY `news`.`PopRate` 
		LIMIT 0, ' . (int)$static_uint_row_count . ' ';
		if( !( $sql_result = mysqli_query($this->sql_connect, $str_query) ) )
		{
			$this->write_error_logs($str_query);
			return false;
		}
		else
			return $this->result_setto_array($sql_result);
	}

	function get_news($static_uint_news_cate_id, $static_uint_offset, $static_uint_row_count)
	{
		$str_query = 'SELECT * 
		FROM `news`
		INNER JOIN `news_cates_news` 
		ON `news_cates_news`.`NewsId` = `news`.`NewsId` 
		WHERE `news`.`Active` = \'1\' 
		AND `news`.`Date` <= \'' . date('Y-m-d') . '\' 
		AND `news_cates_news`.`NewsCateId` = \'' . (int)$static_uint_news_cate_id . '\' 
		ORDER BY `news`.`Date` DESC 
		LIMIT ' . (int)$static_uint_offset . ', ' . (int)$static_uint_row_count . ' ';
		if( !( $sql_result = mysqli_query($this->sql_connect, $str_query) ) )
		{
			$this->write_error_logs($str_query);
			return false;
		}
		else
			return $this->result_setto_array($sql_result);
	}

	function get_top_news($static_uint_news_cate_id = 0)
	{
		if( !$static_uint_news_cate_id )
			$str_query = 'SELECT `news`.*, `news_cates_news`.`NewsCateId` 
			FROM `news`
			INNER JOIN `news_cates_news` 
			ON `news_cates_news`.`NewsId` = `news`.`NewsId` 
			WHERE `news`.`Active` = \'1\' 
			AND `news`.`IsTop` = \'1\' 
			GROUP BY `news`.`NewsId` 
			ORDER BY `news`.`Date` DESC ';
		else
			$str_query = 'SELECT `news`.*, `news_cates_news`.`NewsCateId` 
			FROM `news`
			INNER JOIN `news_cates_news` 
			ON `news_cates_news`.`NewsId` = `news`.`NewsId` 
			WHERE `news`.`Active` = \'1\' 
			AND `news`.`Date` <= \'' . date('Y-m-d') . '\' 
			AND `news`.`IsLocalTop` = \'1\' 
			AND `news_cates_news`.`NewsCateId` = \'' . (int)$static_uint_news_cate_id . '\' ';
		if( !( $sql_result = mysqli_query($this->sql_connect, $str_query) ) )
		{
			$this->write_error_logs($str_query);
			return false;
		}
		else
			return $this->result_setto_array($sql_result);
	}

	function get_news_by_news_id_n_news_cate_id($static_uint_news_id, $static_uint_news_cate_id)
	{
		$str_query = 'SELECT `news`.* 
		FROM `news` 
		INNER JOIN `news_cates_news` 
		ON `news_cates_news`.`NewsId` = `news`.`NewsId` 
		AND `news_cates_news`.`NewsCateId` = \'' . (int)$static_uint_news_cate_id . '\' 
		WHERE `news`.`Active` = \'1\' 
		AND `news`.`Date` <= \'' . date('Y-m-d') . '\' 
		AND `news`.`NewsId` = \'' . (int)$static_uint_news_id . '\' ';
		if( !( $sql_result = mysqli_query($this->sql_connect, $str_query) ) )
		{
			$this->write_error_logs($str_query);
			return false;
		}
		else
			return $this->result_setto_array($sql_result);
	}

	function refresh_news_read_count($static_uint_news_id)
	{
		$str_tsql = 'UPDATE `news` 
		SET `ReadCount` = `ReadCount` + 1, `PopRate` = ( `ReadCount` + 1 ) / ( ' . (time() + 1) . ' - `BuildTime` ) 
		WHERE `NewsId` = \'' . (int)$static_uint_news_id . '\' ';
		if( !mysqli_query($this->sql_connect, $str_tsql) )
		{
			$this->write_error_logs($str_tsql);
			return false;
		}
		else
			return true;
	}

	function get_news_pictures($static_uint_news_id)
	{
		$str_query = 'SELECT * 
		FROM `news_pictures` 
		WHERE `NewsId` = \'' . (int)$static_uint_news_id . '\' 
		ORDER BY `IsCover` DESC, `Order` ASC ';
		if( !( $sql_result = mysqli_query($this->sql_connect, $str_query) ) )
		{
			$this->write_error_logs($str_query);
			return false;
		}
		else
			return $this->result_setto_array($sql_result);
	}

	function get_news_videos($static_uint_news_id)
	{
		$str_query = 'SELECT * 
		FROM `news_videos` 
		WHERE `NewsId` = \'' . (int)$static_uint_news_id . '\' 
		ORDER BY `IsCover` DESC, `VideoId` ';
		if( !( $sql_result = mysqli_query($this->sql_connect, $str_query) ) )
		{
			$this->write_error_logs($str_query);
			return false;
		}
		else
			return $this->result_setto_array($sql_result);
	}
}
?>