<?php
/*****************************************************************************
 * MainTemplate.php
 * Defines the key template MainTemplate.
 *
 * Kabuki CMS (C) 2013-2015, Aaron van Geffen
 *****************************************************************************/

class MainTemplate extends Template
{
	private $title = '';
	private $classes = [];
	private $area;
	private $css = '';
	private $header_html = '';
	private $canonical_url = '';

	public function __construct($title = '')
	{
		$this->title = $title;
	}

	public function html_main()
	{
		echo '<!DOCTYPE html>
<html lang="en">
	<head>
		<title>', $this->title, '</title>', !empty($this->canonical_url) ? '
		<link rel="canonical" href="' . $this->canonical_url . '">' : '', '
		<link type="text/css" rel="stylesheet" href="', BASEURL, '/css/default.css">
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">', !empty($this->css) ? '
		<style type="text/css">' . $this->css . '
		</style>' : '', $this->header_html, '
		<script type="text/javascript" src="', BASEURL, '/js/main.js"></script>
	</head>
	<body', !empty($this->classes) ? ' class="' . implode(' ', $this->classes) . '"' : '', '>
		<header>
			<a href="', BASEURL, '/">
				<h1 id="logo">#pics</h1>
			</a>
			<ul id="nav">
				<li><a href="', BASEURL, '/">albums</a></li>
				<li><a href="', BASEURL, '/people/">people</a></li>
				<li><a href="', BASEURL, '/timeline/">timeline</a></li>
			</ul>
		</header>
		<div id="wrapper">';

		foreach ($this->_subtemplates as $template)
			$template->html_main();

		echo '
			<footer>';

		if (Registry::has('user') && Registry::get('user')->isAdmin())
		{
			if (class_exists('Cache'))
				echo '
				<span class="cache-info">Cache info: ', Cache::$hits, ' hits, ', Cache::$misses, ' misses, ', Cache::$puts, ' puts, ', Cache::$removals, ' removals</span>';

			if (Registry::has('start'))
				echo '<br>
				<span class="creation-time">Page creation time: ', sprintf('%1.4f', microtime(true) - Registry::get('start')), ' seconds</span>';

			if (Registry::has('db'))
				echo '<br>
				<span class="query-count">Database interaction: ', Registry::get('db')->getQueryCount(), ' queries</span>';
		}
		else
			echo '
				<span class="vanity">Powered by <a href="https://aaronweb.net/projects/kabuki/">Kabuki CMS</a></span>';

		echo '
			</footer>
		</div>';

		if (Registry::has('db') && defined("DB_LOG_QUERIES") && DB_LOG_QUERIES)
			foreach (Registry::get('db')->getLoggedQueries() as $query)
				echo '<pre>', strtr($query, "\t", " "), '</pre>';

		echo '
	</body>
</html>';
	}

	public function appendCss($css)
	{
		$this->css .= $css;
	}

	public function appendHeaderHtml($html)
	{
		$this->header_html .= "\n\t\t" . $html;
	}

	public function appendStylesheet($uri)
	{
		$this->appendHeaderHtml('<link text="text/css" rel="stylesheet" href="'. $uri . '">');
	}

	public function setCanonicalUrl($url)
	{
		$this->canonical_url = $url;
	}

	public function addClass($class)
	{
		if (!in_array($class, $this->classes))
			$this->classes[] = $class;
	}

	public function removeClass($class)
	{
		if ($key = array_search($class, $this->classes) !== false)
			unset($this->classes[$key]);
	}
}