Location: PHPKode > projects > Dev's CMS > devscms/includes/class.system.php
<?php

// Includes
require(ROOT_DIR . 'includes/class.config.php');
require(ROOT_DIR . 'includes/config.php');
require(ROOT_DIR . 'includes/class.mysqli.php');
require(ROOT_DIR . 'includes/class.templates.php');

class system
{
	/**
	 * @var mysqliConnection
	 */
	public $db;
	/**
	 * @var template
	 */
	public $mainTemplate;
	/**
	 * @var template
	 */
	public $contentTemplate;
	public $page;
	
	public $stylesPath;
	public $scriptsPath;
	
	public $modules;
	
	
	public function __construct($page = null, $containerTemplate = 'index')
	{
		// Base classes
		$this->db = new mysqliConnection(
			config::getConfig()->db->host,
			config::getConfig()->db->user,
			config::getConfig()->db->pass,
			config::getConfig()->db->name
		);
		$this->mainTemplate = new template($containerTemplate);
		$this->mainTemplate->set('siteName', config::get('system', 'siteName'));
		
		// Config
		$systemConfig = &config::getConfig()->system;
		$this->stylesPath = $systemConfig->stylesPath;
		$this->scriptsPath = $systemConfig->scriptsPath;
		
		if (isset($page)) {
			$this->setPage($page);
		}
		
		// Load modules
		$this->modules = array();
		foreach (explode(',', config::get('system', 'modules')) as $module) {
			system::includeLib($module);
		}
		
		$this->modules = explode(',', config::get('system', 'enabledModules'));
	}
	
	
	public function addIncludes($files)
	{
		$css = array();
		$js = array();
		if (!is_array($files)) {
			$files = array($files);
		}
		
		foreach ($files as $file) {
			if (substr($file, -3) == '.js') {
				$js[] = $file;
			} elseif (substr($file, -4) == '.css') {
				$css[] = $file;
			}
		}
		
		$this->_addCss($css);
		$this->_addJs($js);
	}
	
	public static function error($message = '')
	{
		throw new Exception($message);
	}
	
	public static function includeLib($module)
	{
		require_once(ROOT_DIR . "includes/class.$module.php");
	}
	
	public function render($includes = array(), $title = '')
	{
		$this->addIncludes($includes);
		$this->mainTemplate->set('title', $title);
		
		$this->mainTemplate->set('content', $this->contentTemplate);
		$this->mainTemplate->render();
	}
	
	public function render404()
	{
		$this->setContentTemplate('404');
		$this->render();
	}
	
	public function setContentTemplate($template)
	{
		$this->contentTemplate = new template($template);
	}
	
	public function setPage($page)
	{
		// Check if page exists
		if (config::get('system', 'checkTemplate') && !is_file(config::getConfig()->template->path . "$page.tpl")) {
			$this->render404();
			exit();
		}
		
		// Set page template
		$this->setContentTemplate($page);
		$this->page = $page;
		$this->mainTemplate->set('page', $page);
	}
	
	private function _addCss($files)
	{
		$html = $this->mainTemplate->get('css', '');
		foreach ($files as $file) {
			$html .= '<link rel="stylesheet" type="text/css" href="' . $this->stylesPath . $file . '" />';
		}
		$this->mainTemplate->set('css', $html);
	}
	
	private function _addJs($files)
	{
		$html = $this->mainTemplate->get('js', '');
		foreach ($files as $file) {
			if (substr($file, 0, 7) != 'http://') {
				$file = $this->scriptsPath . $file;
			}
			
			$html .= '<script type="text/javascript" src="' . $file . '"></script>';
		}
		$this->mainTemplate->set('js', $html);
	}
}
Return current item: Dev's CMS