<?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) 2015 - 2016 clearFusionCMS. All rights reserved.
 * @link http://clearfusioncms.com
 */

/**
 * contentbuilderWYSIWYG Class
 *
 * Class to draw the editor.
 *
 * @package clearFusionCMS\module\contentbuilder
 */
class contentbuilderWYSIWYG extends flFormElementWYSIWYGModule {
	function getCSSClass() {
		return null;
	}

	function dimensionsInStyle() {
		return false;
	}

	/**
	 * Transform the text read from the editor converting fusionCSS to the CSS framework of the template.
	 *
	 * @param $text string The editor text to transform.
	 * @return string The transformed text.
	 */
	function transformGet($text) {

		// Translate to target CSS framework
		$cfg = clearFusionCMS::getSiteConfig('cfSystemSite');

		switch($cfg->get('siteThemeCSSFramework', '')) {
			case 'bootstrap3':
				$text = preg_replace('/span-s(\d+)/', 'col-sm-$1', $text);
				$text = preg_replace('/span-s-push(\d+)/', 'col-sm-push-$1', $text);
				$text = preg_replace('/span-s-pull(\d+)/', 'col-sm-pull-$1', $text);
				$text = preg_replace('/offset-s(\d+)/', 'col-sm-offset-$1', $text);

				$text = preg_replace('/span-t(\d+)/', 'col-xs-$1', $text);
				$text = preg_replace('/span-t-push(\d+)/', 'col-xs-push-$1', $text);
				$text = preg_replace('/span-t-pull(\d+)/', 'col-xs-pull-$1', $text);
				$text = preg_replace('/offset-t(\d+)/', 'col-xs-offset-$1', $text);
				break;

			case 'foundation5':
				$text = preg_replace('/span-s(\d+)/', 'medium-$1 columns', $text);
				$text = preg_replace('/span-s-push(\d+)/', 'medium-push-$1', $text);
				$text = preg_replace('/span-s-pull(\d+)/', 'medium-pull-$1', $text);
				$text = preg_replace('/offset-s(\d+)/', 'medium-offset-$1', $text);

				$text = preg_replace('/span-t(\d+)/', 'small-$1 columns', $text);
				$text = preg_replace('/span-t-push(\d+)/', 'small-push-$1', $text);
				$text = preg_replace('/span-t-pull(\d+)/', 'small-pull-$1', $text);
				$text = preg_replace('/offset-t(\d+)/', 'small-offset-$1', $text);
				break;
		}

		return $text;
	}

	/**
	 * Transform the text written to the editor converting to fusionCSS.
	 *
	 * @param $text string The editor text to transform.
	 * @return string The transformed text.
	 */
	function transformPut($text) {

		// Convert bootstrap3
		$text = preg_replace('/col-sm-(\d+)/', 'span-s$1', $text);
		$text = preg_replace('/col-sm-push-(\d+)/', 'span-s-push$1', $text);
		$text = preg_replace('/col-sm-pull-(\d+)/', 'span-s-pull$1', $text);
		$text = preg_replace('/col-sm-offset-(\d+)/', 'offset-s$1', $text);

		$text = preg_replace('/col-xs-(\d+)/', 'span-t$1', $text);
		$text = preg_replace('/col-xs-push-(\d+)/', 'span-t-push$1', $text);
		$text = preg_replace('/col-xs-pull-(\d+)/', 'span-t-pull$1', $text);
		$text = preg_replace('/col-xs-offset-(\d+)/', 'offset-t$1', $text);

		// Convert foundation5
		$text = preg_replace('/\s*columns/', '', $text);
		$text = preg_replace('/medium-(\d+)/', 'span-s$1', $text);
		$text = preg_replace('/medium-push-(\d+)/', 'span-s-push$1', $text);
		$text = preg_replace('/medium-pull-(\d+)/', 'span-s-pull$1', $text);
		$text = preg_replace('/medium-offset-(\d+)/', 'offset-s$1', $text);

		$text = preg_replace('/small-(\d+)/', 'span-t$1', $text);
		$text = preg_replace('/small-push-(\d+)/', 'span-t-push$1', $text);
		$text = preg_replace('/small-pull-(\d+)/', 'span-t-pull$1', $text);
		$text = preg_replace('/small-offset-(\d+)/', 'offset-t$1', $text);

		// Check that we have rows and spans, if not wrap in a row/span
		if(!empty($text) && !preg_match('/^\s*&lt;div\s+class=&quot;([^&]+)?row([^&]+)?&quot;&gt;/', $text))
			$text = fusionLib::htmlChars('<div class="row"><div class="span-s12">') . $text . fusionLib::htmlChars('</div></div>');

		return $text;
	}

	function render($db, $codeBuffer, $settings) {

		$imageBrowser = empty($settings['fileBrowser']) ? 'undefined' : "'$settings[fileBrowser]contentbuilder/'";
		$fileBrowser = empty($settings['internalLinks']) ? 'undefined' : "'$settings[internalLinks]contentbuilder/'";

		$router = clearFusionCMS::getRouter()->getRoute('cfManagerModule');
		$snippetSrc = "'" . $router->assemble(array(
			'module' => 'contentbuilder',
			'action' => 'snippetfile'
		)) . "'";
		$iconsSrc = "'" . $router->assemble(array(
			'module' => 'contentbuilder',
			'action' => 'ionicons'
		)) . "'";
		$dialogSrc = "'" . $router->assemble(array(
			'module' => 'contentbuilder',
			'action' => 'dialog'
		)) . "'";

		$categories = array(
			0 => 'Default',
			-1 => 'All',
			1 => 'Title',
			2 => 'Title, Subtitle',
			3 => 'Info, Title',
			4 => 'Info, Title, Subtitle',
			5 => 'Heading, Paragraph',
			6 => 'Paragraph',
			7 => 'Paragraph, Images + Caption',
			8 => 'Heading, Paragraph, Images + Caption',
			33 => 'Buttons',
			34 => 'Cards',
			9 => 'Images + Caption',
			10 => 'Images + Long Caption',
			11 => 'Images',
			12 => 'Single Image',
			13 => 'Call to Action',
			14 => 'List',
			15 => 'Quotes',
			16 => 'Profile',
			17 => 'Map',
			20 => 'Video',
			18 => 'Social',
			21 => 'Services',
			22 => 'Contact Info',
			23 => 'Pricing',
			24 => 'Team Profile',
			25 => 'Products/Portfolio',
			26 => 'How It Works',
			27 => 'Partners/Clients',
			28 => 'As Featured On',
			29 => 'Achievements',
			32 => 'Skills',
			30 => 'Coming Soon',
			31 => 'Page Not Found',
			19 => 'Separator'
		);

		$cfg = new flConfigDb('cfContentBuilder', $db);
		$chunks = $cfg->get('chunks', '');
		if(!empty($chunks)) {
			$tpl = new flTemplateEngine();
			$oldDb = flTemplateEngine::setDB($db);

			$chunk = preg_split('/\s*,\s*/', $chunks);
			foreach($chunk as $c) {
				$block = $tpl->getChunk($c);

				$r = array();
				if(preg_match('/data-cat="(\d+)"/', $block, $r)) {
					if(!isset($categories[$r[1]])) {
						$n = preg_split('/([A-Z][a-z0-9]+)/', $c, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);

						$name = array();
						foreach($n as $np) {
							$name[] = ucfirst(strtolower($np));
						}
						$categories[$r[1]] = implode(' ', $name);
					}
				}
			}

			flTemplateEngine::setDB($oldDb);
		}

		$catHTML = array();
		foreach($categories as $id => $name) {
			$catHTML[] = '[' . $id . ',"' . $name . '"]';
		}
		$catHTML = '[' . implode(',', $catHTML) . ']';

		$codeBuffer->requireCore()->includeJQuery()
			->bufferHeadJS('system/modules/contentbuilder/scripts/jquery-ui.min.js')
			->bufferHeadJS('system/modules/contentbuilder/scripts/contentbuilder.js')
			->bufferHeadJS('system/modules/contentbuilder/editor.js')
			->bufferCSS('system/modules/contentbuilder/scripts/contentbuilder.css')
			->bufferCSS('system/modules/contentbuilder/editor.css')
			->bufferBodyJS("$(document).ready(function() {
	$('#{$this->name}CBEditor').contentbuilderCreate($imageBrowser, $fileBrowser, $snippetSrc, $iconsSrc, $dialogSrc, $catHTML);
});");

		return "<div id=\"{$this->name}CBEditor\" class=\"contentBuilderEditRegion\" data-textarea=\"$this->name\"></div>";
	}
}
