<?php
/*
* @ServerMon v1.1
* @Developed by Maksa988
* @www.Server-Mon.ru
* @Copyright (C) 2014
*/
 if(!defined('SMON'))
	die('Hacking attempt!');
	
class games extends Controller {
	public function __construct() {

		$this->header();
		$this->model('users');
		$this->model('games');
		
		if(empty($_SESSION['user_id'])):
			return $this->view(array('login/index'), $this->data); //Загружаем шаблон
		endif;
		
		$user = $this->usersModel->getUser($_SESSION['user_id']);
		$this->data['user'] = $user;
		
		if($user['user_group'] != $this->setting('admin_group')):
			$this->data['error'] = 'Вы не имеете доступа!';
			return $this->view(array('login/index'), $this->data); //Загружаем шаблон
		endif;
		
		$this->data['activeSection'] = 'modules';
		$this->data['menu'] = array(array('url'=> 'games/add', 'icon' => 'plus', 'name'=> 'Добавить игру')); 
		
		if($this->GET() >= '1'){
			$game = $this->gamesModel->checkGame($this->GET());
			
			if($game){
				$this->data['game'] = $game;
				
				if($this->POST('edit')){
					$name = $this->POST('name');
					$code = $this->POST('code');
					$connect = $this->POST('connect');
					
					if($name && $code && $connect){
						if($this->gamesModel->editGame($this->GET(), $name, $code, $connect)){
							$this->data['success'] = 'Игра отредактирована.';
						} else {
							$this->data['error'] = 'Произошла неизвестная ошибка. Повторите попытку.';
						}
					} else {
						$this->data['error'] = 'Все поля обязательны к заполнению!';
					}
				}
				
				return $this->view(array('common/header', 'games/edit', 'common/footer'), $this->data); //Загружаем шаблон
			} else {
				return $this->view(array('common/header', 'games/404game', 'common/footer'), $this->data); //Загружаем шаблон
			}
		} else {
			return $this->view(array('common/header', 'games/404game', 'common/footer'), $this->data); //Загружаем шаблон
		}
	}
}