<?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\module\contentbuilder
 * @copyright Copyright (c) 2014 - 2016 clearFusionCMS. All rights reserved.
 * @link http://clearfusioncms.com
 */

/**
 * contentbuilderBackendController Class
 *
 * Backend controller to support editor AJAX functionality.
 *
 * @package clearFusionCMS\module\contentbuilder
 */
class contentbuilderBackendController extends cfBackendController {

	/**
	 * Return the snippet file.
	 */
	function snippetfileAction() {

		$this->response->setContentType('text/html')
			->setHeader('Content-Disposition', 'inline; filename=contentbuilder.blocks')
			->setHeader('Expires', gmdate('D, d M Y H:i:s', (time() + flApp::getConfig()->get('pageCacheTime', 86400))) . ' GMT')
			->sendHeaders();

		ob_start('ob_gzhandler');

		$cfg = new flConfigDb('cfContentBuilder', $this->db);
		$chunks = $cfg->get('chunks', '');
		$chunk = preg_split('/\s*,\s*/', $chunks);

		cfTemplateEngine::setDB($this->db);

		echo $this->tpl->getChunk('contentbuilder.blocks');

		foreach($chunk as $c) {
			echo $this->tpl->getChunk($c);
		}

		ob_end_flush();
	}

	/**
	 * Return the icon file.
	 */
	function ioniconsAction() {

		$this->response->setContentType('text/html')
			->setHeader('Content-Disposition', 'inline; filename=contentbuilder.ionicons')
			->setHeader('Expires', gmdate('D, d M Y H:i:s', (time() + flApp::getConfig()->get('pageCacheTime', 86400))) . ' GMT')
			->sendHeaders();

		cfTemplateEngine::setDB($this->db);

		ob_start('ob_gzhandler');
		echo $this->tpl->parseChunk('contentbuilder.ionicons');
		ob_end_flush();
	}

	/**
	 * Return the dialog
	 */
	function dialogAction() {
		switch($this->request->getQuery('dialog')) {
			case 'headings.html':
				$chunk = 'contentbuilder.headings';
				break;

			case 'fonts.html':
				$chunk = 'contentbuilder.fonts';
				break;

			case 'fontsize.html':
				$chunk = 'contentbuilder.fontsize';
				break;

			default:
				return;
		}

		$this->response->setContentType('text/html')
			->setHeader('Content-Disposition', 'inline; filename=' . $chunk)
			->setHeader('Expires', gmdate('D, d M Y H:i:s', (time() + flApp::getConfig()->get('pageCacheTime', 86400))) . ' GMT')
			->sendHeaders();

		cfTemplateEngine::setDB($this->db);

		ob_start('ob_gzhandler');
		echo $this->tpl->parseChunk($chunk);
		ob_end_flush();
	}
}
