define('DEFAULT_THEME', 'rc_black');
define('THEME_DIR','../themes/');
require("./database.class.php");
require("../config.php");
$database = new Database($config);
/**
* Class Render
*/
class Render
{
private $vars;
public $renderedPage;
/**
* Render constructor.
* @param $mobileDetect
*/
public function __construct($mobileDetect)
{
$this->mobileDetect = $mobileDetect;
}
/**
* Загружает файл шаблона страницы
* @param string $theme тема, из которой нужно загрузить шаблон
* @param string $file файл шаблона
*/
public function getTemplate($theme, $file)
{
global $themes;
$chkd_theme = null;
$chkd_file = null;
if(!empty($theme) && in_array($theme, $themes))
{
$chkd_theme = $theme;
}
else
{
$chkd_theme = DEFAULT_THEME;
}
if($this->mobileDetect->isMobile() || $this->mobileDetect->isTablet())
{
$themeDir = THEME_DIR . '/' . $chkd_theme . '/mobile/';
}
else
{
$themeDir = THEME_DIR . '/' . $chkd_theme . '/desktop/';
}
if(!empty($file) && file_exists($themeDir . $file))
{
$chkd_file = $file;
$this->renderedPage = file_get_contents($themeDir . $chkd_file);
}
else
{
$this->renderedPage = "<error>No Theme Found</error>";
}
}
/**
* Создает элемент меню по данным БД
* @return string $menu элемент меню
*/
public function generateMenu()
{
$m_query = Database::query_DB("SELECT * FROM `menu`");
$menu = '<ul id="menu">';
while($m_array = Database::fetch_array_DB($m_query)) {
$menu .= '<li><a href="' . $m_array['link'] . '">' . $m_array['name'] . '</a>';
}
$menu .= '</ul>';
return $menu;
}
/**
* Получает тему, установленную у пользователя
* @param string $login имя пользователя
* @return string $theme название темы
*/
public function getUserTheme($login)
{
if($login!="Offline")
{
$t_query = Database::query_DB("SELECT * FROM `users` WHERE `name`='" . $login . "'");
$t_array = Database::fetch_array_DB($t_query);
$theme = $t_array['theme'];
}
else
{
$theme = DEFAULT_THEME;
}
return $theme;
}
/**
* Заменяет указанный $key в шаблоне на элемент $var
* @param string $key ключ, который надо заменить
* @param string $var элемент, на который производится замена
*/
public function setElement($key, $var)
{
$this->vars[$key] = $var;
}
/**
* Обрабатывает шаблон согласно данных, указанных пользователем с помощью setElement()
*/
public function render()
{
foreach($this->vars as $find => $replace)
{
$this->renderedPage = str_replace($find, $replace, $this->renderedPage);
}
}
}