<?php

/**
 * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 * @package clearFusionCMS
 * @copyright Copyright (c) 2012 - 2014 clearFusionCMS. All rights reserved.
 * @link http://clearfusioncms.com
 */

/**
 * cfTemplateEngine Class
 *
 * Template engine to output public pages.
 *
 * @package clearFusionCMS
 */
class cfTemplateEngine extends flTemplateEngine {

	/**
	 * The controller.
	 *
	 * @var cfContentController
	 */
	public $controller;

	/**
	 * Construct the template engine.
	 *
	 * @param cfContentController $controller The site controller to use.
	 */
	function __construct($controller) {

		// Extended the supported tag characters
		$this->tagChars .= '~';

		$this->controller = $controller;
		$this->setDocumentCache(flApp::getCache(), clearFusionCMS::getConfig()->get('pageCacheTime', 86400));
		$this->setTheme($controller->content->getTheme());
		$this->setChildTheme($controller->content->getChildTheme());
		$this->setCSSFramework($controller->content->getCSSFramework());
		$this->setCodeBuffer(clearFusionCMS::getCodeBuffer());
	}

	/**
	 * Process links tags in templates [[~id &abs=`1`]]
	 *
	 * @param string $type The tag type character.
	 * @param string $tag The tag.
	 * @param array $params  Array of parameters.
	 */
	protected function _processTag($type, $tag, $params) {
		if($type == '~') {
			if(preg_match('/^\d+$/', $tag)) {
				// Resolve links so link direct rather than redirect
				if($tgt = self::$db->fetchOne('SELECT content FROM cf_content_' . clearFusionCMS::getSite() . ' WHERE id=? && restype=?', $tag, 'link'))
					echo $tgt;
				else
					echo $this->controller->content->getPageURI($tag, isset($params['abs']) && $params['abs'], isset($params['secure']) ? $params['secure'] : null);
			}
		}
		else
			parent::_processTag($type, $tag, $params);
	}

	/**
	 * Parse a document.
	 *
	 * @param string $name The name of the template chunk to use for the document.
	 * @param string $docid Unique ID of the document.
	 * @param array $placeholders Name value pair array of placeholders.
	 * @param int $cacheTime The time to cache the document for.
	 * @return string The parsed document string.
	 */
	function parseDocument($name, $docid, $placeholders = array(), $cacheTime = 0) {

		if(cfContent::isPreview())
			$this->getCodeBuffer()->requireCore()->bufferBodyJS('system/app/js/cfPreview.js');

		$s = parent::parseDocument($name, $docid, $placeholders);

		// If full cache then set last modified headers
		$response = $this->controller->getResponse();
		if(!cfContent::isPreview() && ($t = $this->lastDocFullCacheTime)) {
			$response->setLastModified($t);

			if($cacheTime == -1) {
				$response->noCache();
			}
			else {
				$response->setHeader('Expires', gmdate('D, d M Y H:i:s', $t + ($cacheTime ? $cacheTime : flApp::getConfig()->get('pageCacheTime', 86400))) . ' GMT');
			}
		}
		else if($cacheTime == -1) {
			$response->noCache();
		}

		return $s;
	}
}
