<?php
/*
 * Copyright (c) 2013, webvariants GbR, http://www.webvariants.de
 *
 * This file is released under the terms of the MIT license. You can find the
 * complete text in the attached LICENSE file or online at:
 *
 * http://www.opensource.org/licenses/mit-license.php
 */

/**
 * WYMEditor
 *
 * Diese Klasse stellt die öffentliche API für den WYMEditor zur Verfügung Sie
 * wird genutzt um Module zu erstellen.
 *
 * @author     Dave Gööck
 * @copyright  webvariants GbR, www.webvariants.de
 */
class WV14_WYMEditor {
	protected $language;
	protected $plugins;
	protected $embedded;
	protected $skin;
	protected $styleCSS;
	protected $containers;
	protected $cssFiles;

	private static $editor;

	/**
	 * Erzeugt ein neues WYMEditor Objekt.
	 *
	 * Dieses Objekt dient der weiteren Konfiguration. Es ist stattdessen auch
	 * möglich durch WV14_WYMEditor::getSimpleEditor()->embedd() direkt einen
	 * vorkonfigurierten Editor zu öffnen.
	 */
	public function __construct() {
		$this->language   = 'de';
		$this->embedded   = false;
		$this->skin       = 'sallycms';
		$this->styleCSS   = null;
		$this->cssFiles   = array();
		$this->containers = null;
		$this->plugins    = null;
	}

	/**
	 * @return WV14_WYMEditor
	 */
	public static function getSimpleEditor() {
		if (!isset(self::$editor)) {
			$locale = substr(sly_Core::getI18N()->getLocale(), 0, 2);
			self::$editor = new self();
			self::$editor->setLanguage($locale);
			self::$editor->setContainers(sly_Core::config()->get('wym_containers'));
		}

		return self::$editor;
	}

	public function setLanguage($language) {
		if (file_exists(self::getEditorDir().'/wymeditor/lang/'.$language.'.js')) $this->language = $language;
		else throw new Exception('Language '.$language.' is not available');
		return $this;
	}

	public function setPlugins(array $plugins = null) {
		$this->plugins = $plugins;
		return $this;
	}

	public function setSkin($skin) {
		$this->skin = $skin;
		return $this;
	}

	public function setEditorCSS($cssFile) {
		$this->styleCSS = $cssFile;
		return $this;
	}

	public function setContainers(array $containers = null) {
		$this->containers = $containers;
		return $this;
	}

	public function addSytlesheetToIframe($cssFile) {
		$this->cssFiles[] = $cssFile;
	}

	public function embed(sly_Layout $layout = null, $forceAssets = false) {
		$layout = $layout ? $layout : sly_Core::getLayout();

		if ($this->plugins === null) {
			$this->plugins = array('anchor', 'email', 'embed', 'filelink', 'fullscreen', 'hovertools', 'list', 'resizable', 'table');
		}

		if (!isset($layout->wv14HasAssets) || $forceAssets) {
			// prepare configuration
			$plugins      = array_values($this->plugins);
			$wymStyles    = $this->styleCSS ? $this->styleCSS : 'assets/css/wym_styles.css';
			$hasWymStyles = file_exists(SLY_BASE.'/'.$wymStyles);

			// All declared variables are PUBLIC API.
			// Do not remove any of them before unless you're going to the next major version!
			$vars = array(
				'wym_plugins = '.json_encode($plugins),
				'wym_language = '.json_encode($this->language),
				'wym_slyversion = '.json_encode(sly_Core::getVersion('X.Y')),
				'wym_iframe_url = "index.php?'.http_build_query(array('page' => 'wymiframe', 'stylesheets' => $this->cssFiles)).'"'
			);

			if (!sly_Core::isBackend()) {
				$vars[] = 'wym_backend = false';
				$vars[] = 'wym_jquery_path = "backend/assets/js/jquery.min.js"';
				$vars[] = 'wym_sallyjs_path = "backend/assets/js/standard.min.js"';
				$vars[] = 'wym_styles_css = "'.($hasWymStyles ? $wymStyles : '').'"';
			}
			else {
				$vars[] = 'wym_backend = true';
				$vars[] = 'wym_jquery_path = "assets/js/jquery.min.js"';
				$vars[] = 'wym_sallyjs_path = "assets/js/standard.min.js"';
				$vars[] = 'wym_styles_css = "'.($hasWymStyles ? '../'.$wymStyles : '').'"';
			}

			// add resources
			$layout->addJavaScript('var '.implode(',', $vars).';');

			$this->addJavaScriptFile($layout, 'jquery.wymeditor.min.js');
			$this->addPlugins($layout);
			$this->addJavaScriptFile($layout, 'skins/'.$this->skin.'/main.js');
			$this->addContainerDefinitions($layout);

			$layout->wv14HasAssets = true;
		}
	}

	protected function addContainerDefinitions(sly_Layout $layout) {
		if ($this->containers !== null) {
			$layout->addJavaScript('var wym_containers = '.json_encode(array_values($this->containers)).';');
		}
	}

	protected function addPlugins(sly_Layout $layout) {
		foreach ($this->plugins as $plugin) {
			if ($plugin === 'rdfa') {
				$layout->addJavaScriptFile(WV14_Util::getEditorURI().'jquery/jquery.rdfquery.rdfa.min-1.0.js');
			}

			$this->addJavaScriptFile($layout, 'plugins/'.$plugin.'/jquery.wymeditor.'.$plugin.'.js');
		}
	}

	protected function addJavaScriptFile(sly_Layout $layout, $relname, $group = 'wymeditor') {
		$layout->addJavaScriptFile($this->getRealURI($relname), $group);
	}

	protected function addCSSFile(sly_Layout $layout, $relname, $group = 'wymeditor') {
		$layout->addCSSFile($this->getRealURI($relname), $group);
	}

	protected function getRealURI($relname) {
		$localDir = SLY_BASE.'/assets/wymeditor/';

		if (file_exists($localDir.'/'.$relname)) {
			$localURI = (sly_Core::isBackend() ? '../' : '').'assets/wymeditor/';
			return $localURI.$relname;
		}
		else {
			return self::getEditorURI().'wymeditor/'.$relname;
		}
	}

	/**
	 * @deprecated  since 2012-Sep-05, use WV14_Util::getEditorDir() instead, will be gone in 2.x
	 */
	public static function getEditorDir() {
		return WV14_Util::getEditorDir();
	}

	/**
	 * @deprecated  since 2012-Sep-05, use WV14_Util::getEditorURI() instead, will be gone in 2.x
	 */
	public static function getEditorURI() {
		return WV14_Util::getEditorURI();
	}

	/**
	 * @deprecated  since 2012-Sep-05, use WV14_Util::fixMediaInBackend() instead, will be gone in 2.x
	 */
	public static function fixMediaInBackend($html) {
		return WV14_Util::fixMediaInBackend($html);
	}
}
