<?php

/**
 * Eppe View Helper Wysiwyg
 * @author wojbach
 *
 */
class Eppe_View_Helper_Wysiwyg extends Zend_View_Helper_Abstract {

	protected $_enabled = false;
	protected $_defaultScript;
	protected $_supported = array(
		'mode' => array('textareas', 'specific_textareas', 'exact', 'none'),
		'theme' => array('simple', 'advanced'),
		'format' => array('html', 'xhtml'),
		'languages' => array('en'),
		'plugins' => array('style', 'layer', 'table', 'save',
			'advhr', 'advimage', 'advlink', 'emotions',
			'iespell', 'insertdatetime', 'preview', 'media',
			'searchreplace', 'print', 'contextmenu', 'paste',
			'directionality', 'fullscreen', 'noneditable', 'visualchars',
			'nonbreaking', 'xhtmlxtras', 'imagemanager', 'filemanager', 'template'));
	protected $_config = array('mode' => '"specific_textareas"',
		'editor_selector' => '"wysiwyg"',
		'language' => '"pl"',
		'theme' => '"advanced"',
		'height' => '"280"',
		'plugins' => '"paste,autolink"',
		'paste_text_sticky' => '"true"',
		'paste_text_sticky_default' => '"true"',
		'theme_advanced_buttons1' => '"pastetext,|,bold,italic,|,formatselect,|,bullist,numlist,|,link,unlink,|,undo,redo"',
		'content_css' => '"/modules/Core/admin/css/wysiwyg.css"',
		'theme_advanced_blockformats' => '"h2,h3,h4,h5,h6"',
		'theme_advanced_path' => 'false',
		'formats' => '{
                                    bold : {inline : "span", "classes" : "bold"},
                                            italic : {inline : "span", "classes" : "italic"},
                                    }',
		'paste_preprocess' => 'function(pl, o) {
//                                        o.content = strip_tags(o.content,"<p>,<br>,<ol>,<ul>,<li>,<b>,<strong>");
										}'
	);
	protected $_scriptPath;
	protected $_scriptFile;
	protected $_useCompressor = false;

	public function __construct() {
		$this->_defaultScript = Core_Module_Module::getInstance()->getAdminJSPath() . '/tiny_mce/tiny_mce.js';
	}

	public function __set($name, $value) {
		$method = 'set' . $name;
		if (!method_exists($this, $method)) {
			throw new Exception('Invalid tinyMce property');
		}
		$this->$method($value);
	}

	public function __get($name) {
		$method = 'get' . $name;
		if (!method_exists($this, $method)) {
			throw new Exception('Invalid tinyMce property');
		}
		return $this->$method();
	}

	public function setOptions(array $options) {
		$methods = get_class_methods($this);
		foreach ($options as $key => $value) {
			$method = 'set' . ucfirst($key);
			if (in_array($method, $methods)) {
				$this->$method($value);
			} else {
				$this->_config[$key] = $value;
			}
		}
		return $this;
	}

	public function wysiwyg() {
		return $this;
	}

	public function setScriptPath($path) {
		$this->_scriptPath = rtrim($path, '/');
		return $this;
	}

	public function setScriptFile($file) {
		$this->_scriptFile = (string) $file;
	}

	public function setCompressor($switch) {
		$this->_useCompressor = (bool) $switch;
		return $this;
	}

	public function render() {
		if (false === $this->_enabled) {
			$this->_renderScript();
			$this->_renderCompressor();
			$this->_renderEditor();
		}
		$this->_enabled = true;
	}

	protected function _renderScript() {
		if (null === $this->_scriptFile) {
			$script = $this->_defaultScript;
		} else {
			$script = $this->_scriptPath . '/' . $this->_scriptFile;
		}

		$this->view->headScript()->appendFile($script);
		return $this;
	}

	protected function _renderCompressor() {
		if (false === $this->_useCompressor) {
			return;
		}

		if (isset($this->_config['plugins']) && is_array($this->_config['plugins'])) {
			$plugins = $this->_config['plugins'];
		} else {
			$plugins = $this->_supported['plugins'];
		}

		$script = 'tinyMCE_GZ.init({' . PHP_EOL
				. 'themes: "' . implode(',', $this->_supported['theme']) . '",' . PHP_EOL
				. 'plugins: "' . implode(',', $plugins) . '",' . PHP_EOL
				. 'languages: "' . implode(',', $this->_supported['languages']) . '",' . PHP_EOL
				. 'disk_cache: true,' . PHP_EOL
				. 'debug: false' . PHP_EOL
				. '});';

		$this->view->headScript()->appendScript($script);
		return $this;
	}

	protected function _renderEditor() {
		$script = 'tinyMCE.init({' . PHP_EOL;

		$params = array();
		foreach ($this->_config as $name => $value) {
			if (is_array($value)) {
				$value = implode(',', $value);
			}
			if (!is_bool($value)) {
				$value = $value;
			}
			$params[] = $name . ': ' . $value;
		}
		$script .= implode(',' . PHP_EOL, $params) . PHP_EOL;
		$script .= '});';

		$this->view->headScript()->appendScript($script);
		return $this;
	}

}