<?php 
class General {

	var $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 channels()
	{
	$sql = "SELECT id,title FROM sources 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 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 lists($id)
	{
	$sql = "SELECT * FROM lists 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 media($id)
	{
	$sql = "SELECT * FROM media 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 channel($id)
	{
	$sql = "SELECT * FROM channels 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 start_period() {
	$sql = "SELECT month,year FROM media ORDER BY id ASC LIMIT 1";
	$query = $this->db->query($sql);
		if ($query->num_rows == 0) {
			return 0;
		} else {
			$row = $query->fetch_assoc();
			return $row;
		}	
	}
	
	function statistics_media($site,$day,$month,$year) {
	$sql = "SELECT video_site,day,month,year FROM media WHERE video_site='$site' AND day='$day' AND month='$month' AND year='$year'";
	$query = $this->db->query($sql);
	return $query->num_rows;
	}
	
	
	function set_options($data,$set) {
	unset($data['save']);
	foreach ($data AS $key=>$value) {
	$check = $this->db->query("SELECT option_name FROM options WHERE option_name='$key'");
	$value = $this->db->real_escape_string(htmlspecialchars($value,ENT_QUOTES));
		if (empty($value)) {
			$value = 0;
		}	
	if ($check->num_rows == 0) {
		
	$excute = $this->db->query("INSERT INTO options (option_name,option_value,option_default,option_set) VALUES ('$key','$value','$value','$set')");	
	} else {
	$excute = $this->db->query("UPDATE options SET option_value='$value' WHERE option_name='$key'");		
	}
	if ($excute) {
	$message = notification('success','All Changes Saved.');
	} else {
	$message = notification('danger','Error Happened.');
	}
	}
	return $message;
	}
	
	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 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 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 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 awaiting_review_media_number()
	{
	$sql = "SELECT * FROM media WHERE published='0' AND deleted='0'";
	$query = $this->db->query($sql);
	return $query->num_rows;
	}
	
	function awaiting_review_users_number()
	{
	$sql = "SELECT * FROM users WHERE active='0'";
	$query = $this->db->query($sql);
	return $query->num_rows;
	}
	
	
	function count_users()
	{
	$sql = "SELECT active FROM users WHERE active='1'";
	$query = $this->db->query($sql);
	return $query->num_rows;
	}
	
	function count_lists()
	{
	$sql = "SELECT id FROM lists";
	$query = $this->db->query($sql);
	return $query->num_rows;
	}
	
	function count_media()
	{
	$sql = "SELECT published,deleted FROM media WHERE published='1' AND deleted='0'";
	$query = $this->db->query($sql);
	return $query->num_rows;
	}
	
	function count_channels()
	{
	$sql = "SELECT id FROM channels";
	$query = $this->db->query($sql);
	return $query->num_rows;
	}
	
}
?>