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

/**
 * liveeditorWYSIWYG Class
 *
 * Class to draw the editor.
 *
 * @package clearFusionCMS\module\liveeditor
 */
class liveeditorWYSIWYG extends flFormElementWYSIWYGCodeModule {
	/**
	 * Get the CSS class to add to the textarea.
	 *
	 * @return string The CSS class or null if not used.
	 */
	function getCSSClass() {
		return null;
	}

	/**
	 * Get if dimensions must be included in the style of the textarea.
	 *
	 * @return bool true if dimensions in style attribute; else false.
	 */
	function dimensionsInStyle() {
		return false;
	}

	/**
	 * Render the editor code.
	 *
	 * @param flDb $db The database object.
	 * @param flBufferCode $codeBuffer The code buffer.
	 * @param array $settings The array of settings for editor.
	 * @return string The HTML for inclusion.
	 */
	function render($db, $codeBuffer, $settings) {

		// Load the language if available
		$locale = fusionLib::getLocale(true);
		if(is_readable(FLMODPATH . "liveeditor/scripts/language/$locale/editor_lang.js"))
			$codeBuffer->bufferHeadJS("system/modules/liveeditor/scripts/language/$locale/editor_lang.js");

		// Include editor JS
		$codeBuffer->bufferHeadJS('system/modules/liveeditor/scripts/innovaeditor.js')
			->bufferCSS('system/modules/liveeditor/liveeditor.css');

		$flickrUser = '';
		$gfonts = false;
		if($db !== null) {

			// Get the editor config (per site)
			$cfg = clearFusionCMS::getSiteConfig('cfLiveEditor');

			if($cfg->googleFonts) {
				$gfonts = true;
				$codeBuffer->includeJQuery()
				->bufferHeadJS('http://ajax.googleapis.com/ajax/libs/webfont/1/webfont.js')
				->bufferHeadJS('system/modules/liveeditor/scripts/common/webfont.js');
			}

			$flickrUser = $cfg->flickrUser;
		}

		$varName = 'o' . str_replace('.', '_', $this->name) . 'Editor';
		$html = "<script type=\"text/javascript\">
//<![CDATA[
var {$varName} = new InnovaEditor('{$varName}');
{$varName}.width = '" . $settings['width'] . "';
{$varName}.height = '" . $settings['height'] . "';
{$varName}.enableCssButtons = false;
{$varName}.enableLightbox = false;
{$varName}.publishingPath=\"" . clearFusionCMS::getRequest()->getBasePath(true) . "\";\n";

		if($cfg->html5video)
			$html .= "{$varName}.arrCustomButtons = [['HTML5Video', 'modalDialog(\"system/modules/liveeditor/scripts/common/webvideo.htm\",690,650, \"HTML5 Video\");', 'HTML5 Video', 'btnVideo.gif']];\n";

		$html .= "{$varName}.groups = [
	['group1', '', ['FontName', 'FontSize', 'Superscript', 'ForeColor', 'BackColor', 'Quote', 'BRK', 'Bold', 'Italic', 'Underline', 'Strikethrough', 'TextDialog', " . ($gfonts ? "'FontDialog', " : '') . " 'RemoveFormat']],
	['group2', '', ['JustifyLeft', 'JustifyCenter', 'JustifyRight', 'Paragraph', 'BRK', 'Bullets', 'Numbering', 'Indent', 'Outdent']],
	['group3', '', ['TableDialog', 'Emoticons', 'ImageDialog', 'FlashDialog', 'CharsDialog', 'Line', 'BRK', 'LinkDialog'" . (empty($settings['internalLinks']) ? '' : ", 'InternalLink'") . ", 'YoutubeDialog'" . ($cfg->html5video ? ", 'HTML5Video'" : '')  . (count($settings['tags']) ? ", 'CustomTag'" : '') . "]],
	['group4', '', ['SearchDialog', 'SourceDialog', 'Styles', 'BRK', 'Undo', 'Redo', 'FullScreen']]
];
";

		if(count($settings['tags']))
			$html .= "{$varName}.arrCustomTag = [\n" . join(",\n", $settings['tags']) . "\n];\n";

		if(!empty($settings['internalLinks']))
			$html .= "{$varName}.cmdInternalLink = 'modalDialog(\"$settings[internalLinks]liveeditor/\",365,500)';\n";

		if(!empty($settings['css'])) {
			$html .= "{$varName}.css = '$settings[css]';\n";
			$codeBuffer->bufferCSS($settings['css']);
		}

		if(!empty($settings['fileBrowser']))
			$html .= "{$varName}.fileBrowser = '$settings[fileBrowser]liveeditor/';\n";

		if(empty($flickrUser))
			$html .= "{$varName}.enableFlickr = false;\n";
		else
			$html .= "{$varName}.enableFlickr = true;\no{$this->name}Editor.flickrUser = '$flickrUser';\n";

		$html .= "{$varName}.returnKeyMode = 3;
{$varName}.REPLACE('{$this->name}');
//]]>
</script>
";
		return $html;
	}
}
