<?php
/*
* @ServerMon v1.1
* @Developed by Maksa988
* @www.Server-Mon.ru
* @Copyright (C) 2014
*/
 if(!defined('SMON'))
	die('Hacking attempt!');
	
class newsModel extends Controller {
	public function getNews($param = '') {
		global $db;
		$sql = "SELECT * FROM `".$this->cfg('prefix')."news` ".$param;
		$result = $db->query($sql);

		$items = array();
		
		if($result->num_rows >= 1){
			while($data = $result->fetch_assoc())
			$items[] = $data;
			
			return $items;
		}
		return false;
	}
	public function updateViews($news, $views){
		global $db;
		$sql = "UPDATE `".$this->cfg('prefix')."news` SET `news_views` = '{$views}'+1 WHERE `news_id` = '{$news}'";
		$db->query($sql);
		
		if(!$db->error) {
			return true;
		}
		return false;
	}
	public function getNewsInfo($id){
		global $db;
		$sql = "SELECT * FROM `".$this->cfg('prefix')."news` WHERE `news_id` = '{$id}' LIMIT 1";
		$result = $db->query($sql);

		if($result->num_rows == 1){
			return $result->fetch_assoc();
		}
		return false;
	}
	public function checkServer($ip, $port){
		global $db;
		$sql = "SELECT * FROM `".$this->cfg('prefix')."servers` WHERE `server_ip` = '{$ip}' AND `server_port` = '{$port}' LIMIT 1";
		$result = $db->query($sql);

		if($result->num_rows == 1){
			return true;
		}
		return false;
	}
	public function updateServer($serverid, $param){
		global $db;
		$id = '-1';
		$sql .= "UPDATE `".$this->cfg('prefix')."servers` SET";
		$sql .= "`server_name` = '{$param[0]}',";
		$sql .= "`server_players` = '{$param[1]}',";
		$sql .= "`server_maxplayers` = '{$param[2]}',";
		$sql .= "`server_mapname` = '{$param[3]}',";
		$sql .= "`server_gamename` = '{$param[4]}'";
		$sql .= "WHERE `server_id` = '{$serverid}'";
		$db->query($sql);
		
		if(!$db->error) {
			return true;
		}
		return false;
	}
	public function updateServerStatus($serverid, $status){
		global $db;
		$id = '-1';
		$sql .= "UPDATE `".$this->cfg('prefix')."servers` SET";
		$sql .= "`server_status` = '{$status}',";
		$sql .= "`server_time_update` = NOW()";
		$sql .= "WHERE `server_id` = '{$serverid}'";
		$db->query($sql);
		
		if(!$db->error) {
			return true;
		}
		return false;
	}
	public function buyStore($serverid, $month, $type, $color = ''){
		global $db;
		$sql .= "UPDATE `".$this->cfg('prefix')."servers` SET";
		if($type == 'vip'){
			$sql .= "`server_vip_time` = NOW() + INTERVAL {$month} MONTH ";
		} elseif($type == 'color'){
			$sql .= "`server_selection_time` = NOW() + INTERVAL {$month} MONTH,";
			$sql .= "`server_selection_type` = '{$color}'";
		}
		$sql .= "WHERE `server_id` = '{$serverid}'";
		$db->query($sql);
		
		if(!$db->error) {
			return true;
		}
		return false;
	}
	public function editServer($serverid, $ip, $port, $weburl, $vote = '') {
		global $db;
		$sql .= "UPDATE `".$this->cfg('prefix')."servers` SET";
		if(!empty($vote)){
		$sql .= "`server_vote` = '{$vote}',";
		}
		$sql .= "`server_ip` = '{$ip}',";
		$sql .= "`server_port` = '{$port}',";
		$sql .= "`server_weburl` = '{$weburl}'";
		$sql .= "WHERE `server_id` = '{$serverid}'";
		$db->query($sql);
		
		if(!$db->error) {
			return true;
		}
		return false;
	}
	public function addServer($game, $ip, $port, $weburl) {
		global $db;
		$sql = "INSERT INTO `".$this->cfg('prefix')."servers` 
		(	game_code, 
			user_id, 
			server_ip, 
			server_port, 
			server_status, 
			server_name,
			server_mapname,
			server_gamename,
			server_weburl,
			server_add_time
		) VALUES (
			'{$game}', 
			'{$_SESSION['user_id']}', 
			'{$ip}', 
			'{$port}', 
			'1',
			'Retrieving info...',
			'Retrieving info...',
			'Retrieving info...',
			'{$weburl}',
			NOW())";
		$db->query($sql);
		
		if(!$db->error) {
			return $db->insert_id;
		}
		return false;
	}
	public function deleteServer($id) {
		global $db;
		$sql = "DELETE FROM `".$this->cfg('prefix')."servers` WHERE `server_id` = '{$id}'";
		$db->query($sql);
		
		if(!$db->error) {
			return true;
		}
		return false;
	}
}