<?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
 */

/**
 * Datentyp
 */
class _WV_Datatype_Richtext extends _WV_Datatype_StringText {

	public function renderForm(sly_Form $form, WV_IProperty $property, $value) {
		if ($value === false) {
			$default = $property->getDefault();
			$value = $property->isMultilingual() ? array($default) : $default;
		} elseif (!$property->isMultilingual()) {
			$value = reset($value);
		}

		$params = $property->getParams();
		extract($this->deserializeParams($params));

		$name = 'property_' . $property->getID() . '_value';

		if (array_key_exists('richtext_class', $params)) {
			$class = $params['richtext_class'];
		} else {
			$class = sly_Core::config()->get('richtext/class');
			if (!$class) {
				throw new Exception('no default richtext editor set. please set "richtext/class"');
			}
		}
		if (!class_exists($class)) {
			throw new Exception('Richtext editor class not found: ' . $class);
		}

		$editor = new $class($name, $property->getTitle(), $value);

		$editor->setHelpText($property->getHelpText());
		$form->add($editor);

		return $form;
	}

	public function checkParamCompatibility(array $oldParameters, array $newParameters) {
		return true;
	}

	public function getIncompatibilityUpdateStatement(array $oldParameters, array $newParameters) {
		return array();
	}

}
