<?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 "Mehrfach-Eingabe"
 *
 * @ingroup datatypes
 */
class _WV_Datatype_Multi extends _WV_Datatype_Base implements WV_IDatatype {
	public function renderForm(sly_Form $form, WV_IProperty $property, $value) {
		$params = $property->getParams();
		$propID = $property->getID();
		$title  = $property->getTitle();
		$name   = 'property_'.$propID.'_value';
		$value  = $this->decodeFormValue($property, $value);
		$value  = json_decode($value, true);
		$params = $this->deserializeParams($params);
		$multi  = new WV_Form_Multiple($name, $title, $value);
		$count  = count($params['children']);

		foreach ($params['children'] as $childName => $child) {
			if (!isset($child['type'])) {
				throw new WV_Exception_Datatype('Child "'.$childName.'" does not have a "type" declaration.');
			}

			$type  = $child['type'];
			$style = isset($child['style']) ? $child['style'] : null;

			switch ($type) {
				case 'input':
				case 'string':
				case 'stringline':
					$element = new sly_Form_Input_Text($childName, '');
					break;

				case 'textarea':
				case 'stringtext':
					$element = new sly_Form_Textarea($childName, '', '');
					break;

				case 'check':
				case 'checkbox':
				case 'boolean':
					$description = isset($child['desc']) ? $child['desc'] : '';
					$element = new sly_Form_Input_Checkbox($childName, '', 1, $description);

					if (isset($child['checked']) && $child['checked'])
						$element->setChecked();
					break;

				case 'user':
					$element = new WV_Form_User($childName, '');
					break;

				case 'dropdown':
					$element = new sly_Form_Select_DropDown($childName, '', '', sly_makeArray($child['values']));
					break;

				default:
					throw new WV_Exception_Datatype('Unrecognized child type "'.$type.'" found.');
			}

			if ($count > 1 && isset($child['alt'])) {
				$element->setAttribute('alt', $child['alt']);
			}

			if ($style) {
				$element->addStyle($style);
			}

			$multi->add($element);
		}

		WV_Datatype::setHelpTextOnElement($form, $multi, $property);
		$form->add($multi);

		return $form;
	}

	protected function getDefaultParams() {
		return array('children' => array());
	}

	public function serializeValue($value, array $params) {
		return json_encode($value);
	}

	public function deserializeValue($value, array $params) {
		return json_decode($value, true);
	}

	public function isValueContained($valueToSearch, $valueFromDatabase, array $params, $operator) {
		throw new WV_Exception_Datatype('not yet implemented');
	}

	public function serializeFormFilter($list, WV_IProperty $property) {
		foreach ($list as $clangID => $values) {
			$values = sly_makeArray($values);
			$values = array_values($values);

			$list[$clangID] = json_encode($values);
		}

		return $list;
	}
}
