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

/**
 * cfEditor Class
 *
 * Class to manage editor information and instances.
 *
 * @package clearFusionCMS
 */
abstract class cfEditor {

	/**
	 * Get the available editors.
	 *
	 * @param bool $default true if 1st option should be default, else 1st option is no editor.
	 * @param bool $noFiltered true to ignore editors that need an output filter.
	 * @return array The list of available editors.
	 */
	static function getAvailable($default = false, $noFiltered = false) {
		$mm = new flModuleMgr(clearFusionCMS::getDb());
		$modules = $mm->getInstalled('WYSIWYG');
		$el = array('' => $default ? __('[use default]', 'cfBackend') : __('[no editor]', 'cfBackend'));

		foreach($modules as $name => $ver) {
			if($info = $mm->getInfo($name)) {
				if(!$noFiltered || !$info->get('filter'))
					$el[$name] = $info->name;
			}
		}

		return $el;
	}

	/**
	 * Get the content filter required for a given editor.
	 *
	 * @param string $editor The name of the editor to get the filter for.
	 * @return string The name of the filter snippet or empty string if none-required.
	 */
	static function getFilter($editor) {
		$mm = new flModuleMgr(clearFusionCMS::getDb());

		$r = '';
		if($info = $mm->getInfo($editor)) {
			$r = $info->get('filter', '');
		}

		return $r;
	}

	/**
	 * Create an editor instance.
	 *
	 * @param flForm $form The form to add the element to.
	 * @param string $name The name of the form element.
	 * @param string $content The content to edit.
	 * @param bool $small true if generating a small editor; else false for large.
	 * @param array $tags Optional list of tags to add to the editor.
	 * @return flFormElementWYSIWYG The editor object.
	 */
	static function create($editor, $form, $name, $content, $small = false, $tags = array()) {
		$cfg = clearFusionCMS::getSiteConfig('cfSystemCtntEdit');

		$el = new flFormElementWYSIWYG($form, $name, $content);
		$el->addValidator(new flFormValidatorMaxLength(2097152))
			->setRows($small ? 10 : 20)
			->editorSize('100%', $small ? '225' : '450')
			->setDB(clearFusionCMS::getDb())
			->editorModule($editor)
			->editorCSS($cfg->editorCSS)
			->editorInternalLinks(clearFusionCMS::getController()->assemble('', 'browsectnt'))
			->editorFileBrowser(clearFusionCMS::getController()->assemble('', 'browsefiles') . 'public/');

		foreach($tags as $k => $v)
			$el->editorAddTag($k, $v);

		return $el;
	}
}