Просмотр файла libraries/controller.php

Размер файла: 3.5Kb
<?php
/**
 * Ant0ha's project
 *
 * @package
 * @author Anton Pisarenko <[email protected]>
 * @copyright Copyright (c) 2006 - 2010, Anton Pisarenko
 * @license http://ant0ha.ru/license.txt
 * @link http://ant0ha.ru
 */

//----------------------------------------

/**
 * Controller class
 *
 * @author Ant0ha
 */
abstract class Controller {
	/**
	* Переменная, в которую записываем ошибки при валидации форм
	*/
	public $error = FALSE;
	/**
	* Класс шаблонизатора
	*/
	public $tpl;
	/**
	* Уровень пользовательского доступа
	*/
	public $access_lever = 0;
	/**
	* Тип шаблонизатора
	*/
	protected $template_type = 'view';
	/**
	* Тема
	*/
	protected $template_theme = 'default';
	/**
	* Количество элементов на страницу по умолчанию
	*/
	protected $per_page = 7;

	/**
	* Constructor
	*/
	public function __construct() {
		$this->config =& $GLOBALS['CONFIG'];
		$this->db =& $GLOBALS['db'];

		# Определение старта для пагинации
		$this->start = !empty($_GET['start']) ? intval($_GET['start']) : 0;
		$this->start = is_numeric($_GET['page']) ? $_GET['page'] * $this->per_page - 1 : $this->start;

		switch($this->template_type) {
			# Смарти
			case  'smarty':
				a_import('libraries/smarty');
				$this->tpl = new MY_Smarty;
				$this->tpl->compile_dir = ROOT .'cache/smarty_templates/';
				break;

			# Native php
			case 'view':
				a_import('libraries/view');
				$this->tpl = new View;
				break;

			# Если шаблонизатор не определен
			default:
				a_error('Укажите тип шаблонизатора!');
		}
		$GLOBALS['tpl'] = $this->tpl;

		if($this->template_theme == 'admin') {
			$this->template_theme = $this->config['system']['admin_theme'];
		}
		else {
			$this->template_theme = $this->config['system']['default_theme'];
		}

		$this->tpl->theme = $this->template_theme;
		define('THEME', $this->tpl->theme);

		# Получение данных о польльзователе
		if(!empty($_SESSION['check_user_id'])) $user_id = $_SESSION['check_user_id'];
    	elseif(!empty($_SESSION['user_id'])) $user_id = $_SESSION['user_id'];
    	else $user_id = -1;

    	$this->user = $this->db->get_row("SELECT * FROM #__users WHERE user_id = $user_id");

		define('USER_ID', $this->user['user_id']);
		$this->tpl->assign('user', $this->user);

		# Обновляем время последнего посещения
    	if(USER_ID != -1) $this->db->query("UPDATE #__users SET last_visit = UNIX_TIMESTAMP() WHERE user_id = '". USER_ID ."'");

		# Управление правами доступа
		$this->access = a_load_class('libraries/access');

		if($this->user) $access_level = $this->access->get_level($this->user['status']);
		else $access_level = 1;

		define('ACCESS_LEVEL', $access_level);

		#$this->access->check($this->access_level);
		if(ACCESS_LEVEL < $this->access_level) {
        	if(USER_ID == -1) {
        		a_error('Для доступа к данной странице необходимо <a href="'. a_url('user/login') .'"><b>войти</b></a> либо <a href="'. a_url('user/registration') .'"><b>зарегистрироваться</b></a>');
        	}
        	else {
        		a_error('У вас нет доступа к данной странице!');
        	}
		}

		# Выполнение событий до вызова контроллера
		main::events_exec(&$this->db, 'pre_controller');
	}
}
?>