<?php 
class General {

	private $mysqli;

    function set_connection($mysqli) {
        $this->db =& $mysqli;
    }
	
	function categories($order)
	{
	$sql = "SELECT * FROM categories ORDER BY $order";
	$query = $this->db->query($sql);
		if ($query->num_rows == 0) {
			return 0;
		} else {
			while ($row = $query->fetch_assoc()) {
				$rows[] = $row;
			}
			return $rows;
		}
	
	}
	
	function pages($order)
	{
	$sql = "SELECT * FROM pages ORDER BY $order";
	$query = $this->db->query($sql);
		if ($query->num_rows == 0) {
			return 0;
		} else {
			while ($row = $query->fetch_assoc()) {
				$rows[] = $row;
			}
			return $rows;
		}
	
	}
	
	
	
	function category($id)
	{
	$sql = "SELECT * FROM categories WHERE id='$id' LIMIT 1";
	$query = $this->db->query($sql);
		if ($query->num_rows == 0) {
			return 0;
		} else {
			$row = $query->fetch_assoc();
			return $row;
		}
	}
	
	function page($id)
	{
	$sql = "SELECT * FROM pages WHERE id='$id' LIMIT 1";
	$query = $this->db->query($sql);
		if ($query->num_rows == 0) {
			return 0;
		} else {
			$row = $query->fetch_assoc();
			return $row;
		}
	}
	
	function featured_media($order,$limit) {
	$sql = "SELECT * FROM media WHERE published='1' AND deleted='0' ORDER BY $order LIMIT $limit";
	$query = $this->db->query($sql);
		if ($query->num_rows == 0) {
			return 0;
		} else {
			while ($row = $query->fetch_assoc()) {
				$rows[] = $row;
			}
			return $rows;
		}
		
	}
	
	function check_username($username)
	{
	$sql = "SELECT username FROM users WHERE username=? LIMIT 1";
	$stmt = $this->db->prepare($sql);
	$stmt->bind_param("s", $username);
	$stmt->execute();
	$stmt->store_result();
	return $stmt->num_rows;
	}
	
	function check_email($email)
	{
	$sql = "SELECT email FROM users WHERE email=? LIMIT 1";
	$stmt = $this->db->prepare($sql);
	$stmt->bind_param("s", $email);
	$stmt->execute();
	$stmt->store_result();
	return $stmt->num_rows;
	}
	
	function user($id)
	{
	$sql = "SELECT * FROM users WHERE id='$id' LIMIT 1";
	$query = $this->db->query($sql);
		if ($query->num_rows == 0) {
			return 0;
		} else {
			$row = $query->fetch_assoc();
			return $row;
		}
	}
	
	function get_options($set) {
	$options = array();
	$query = $this->db->query("SELECT * FROM options WHERE option_set='$set' ORDER BY id ASC");
	while ($row = $query->fetch_assoc()) {
		$options[$row["option_name"]] = $row["option_value"];
	}  
	return $options;
	}
	
	function get_all_options() {
	$options = array();
	$query = $this->db->query("SELECT * FROM options ORDER BY id ASC");
	while ($row = $query->fetch_assoc()) {
		$options[strtolower($row["option_set"]).'_'.$row["option_name"]] = $row["option_value"];
	}  
	return $options;
	}
	
	function media($id)
	{
	$sql = "SELECT * FROM media WHERE id='$id' AND published='1' AND deleted='0' LIMIT 1";
	$query = $this->db->query($sql);
		if ($query->num_rows == 0) {
			return 0;
		} else {
			$this->db->query("UPDATE media SET hits=hits+1 WHERE id='$id'");
			$row = $query->fetch_assoc();
			return $row;
		}
	}
	
	
	function related_media($id,$category_id,$limit) {
	$sql = "SELECT * FROM media WHERE published='1' AND deleted='0' AND id!='$id' AND category_id='$category_id' ORDER BY id DESC LIMIT $limit";
	$query = $this->db->query($sql);
		if ($query->num_rows == 0) {
			return 0;
		} else {
			while ($row = $query->fetch_assoc()) {
				$rows[] = $row;
			}
			return $rows;
		}
		
	}
	
	function user_lists($user_id) {
	$sql = "SELECT * FROM lists WHERE user_id='$user_id' ORDER BY id ASC";
	$query = $this->db->query($sql);
		if ($query->num_rows == 0) {
			return 0;
		} else {
			while ($row = $query->fetch_assoc()) {
				$rows[] = $row;
			}
			return $rows;
		}
		
	}
	
	function count_user_lists($user_id) {
	$sql = "SELECT * FROM lists WHERE user_id='$user_id'";
	$query = $this->db->query($sql);
	return $query->num_rows;
	}
	
	function count_list_media_items($list_id) {
	$sql = "SELECT * FROM lists_media WHERE list_id='$list_id'";
	$query = $this->db->query($sql);
	return $query->num_rows;
	}
	
	function user_list_media_check($user_id,$media_id,$list_id) {
	$sql = "SELECT * FROM lists_media WHERE user_id='$user_id' AND media_id='$media_id' AND list_id='$list_id' LIMIT 1";
	$query = $this->db->query($sql);
	return $query->num_rows;
	}
	
	function check_social_user($social_id,$provider)
	{
	$sql = "SELECT social_id,provider FROM users WHERE social_id='$social_id' AND provider='$provider' LIMIT 1";
	$query = $this->db->query($sql);
	return $query->num_rows;
	}
	
	
	
	function single_list($id)
	{
	$sql = "SELECT * FROM lists WHERE id='$id' LIMIT 1";
	$query = $this->db->query($sql);
		if ($query->num_rows == 0) {
			return 0;
		} else {
			$this->db->query("UPDATE lists SET hits=hits+1 WHERE id='$id'");
			$row = $query->fetch_assoc();
			return $row;
		}
	}
	
	function lists_media($list_id) {
	$sql = "SELECT lists_media.id AS lmid,lists_media.user_id,lists_media.media_id,lists_media.list_id,lists_media.media_order,media.id,media.title,media.details,media.category_id,media.hits,media.published,media.thumbnail,media.tags,media.video_site,media.duration
	FROM lists_media JOIN media 
	ON lists_media.media_id=media.id
	WHERE list_id='$list_id'
	ORDER BY lists_media.media_order ASC";
	$query = $this->db->query($sql);
		if ($query->num_rows == 0) {
			return 0;
		} else {
			while ($row = $query->fetch_assoc()) {
				$rows[] = $row;
			}
			return $rows;
		}
		
	}
	
	
	function featured_lists($order,$number) {
	$sql = "SELECT * FROM lists WHERE privacy='0' ORDER BY $order LIMIT $number";
	$query = $this->db->query($sql);
		if ($query->num_rows == 0) {
			return 0;
		} else {
			while ($row = $query->fetch_assoc()) {
				$rows[] = $row;
			}
			return $rows;
		}
		
	}
	
	
	function process_tokens($token,$expires) {
		$query = $this->db->query("SELECT * FROM options WHERE option_name='facebook_extended_token' LIMIT 1");
		$check = $query->num_rows;
		if ($check == 0) {
			$this->db->query("INSERT INTO options (option_name,option_value,option_set) VALUES ('facebook_extended_token','$token','API')");	
			$expire_time = time()+$expires;
			$this->db->query("INSERT INTO options (option_name,option_value,option_set) VALUES ('facebook_extended_token_expires','$expire_time','API')");
			$this->db->query("INSERT INTO options (option_name,option_value,option_set) VALUES ('facebook_videos_enabled','1','API')");			
		} else {
			$e = $this->db->query("SELECT * FROM options WHERE option_name='facebook_extended_token_expires' LIMIT 1");
			$row = $e->fetch_assoc();
			$past_five = time()-(3600*5);
			if ($row['option_value'] >= $past_five) {
				$this->db->query("UPDATE options SET option_value='$token' WHERE option_name='facebook_extended_token'");	
				$this->db->query("UPDATE options SET option_value='$expires' WHERE option_name='facebook_extended_token_expires'");
			}
		}
	}
	
}
?>