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

class WV42_RolesDatatype extends _WV_Datatype_Select {
	public function deserializeValue($value, array $params) {
		if (empty($value)) return array();

		$elements = array();

		if ($value !== null && strlen($value) != 0) {
			$values = array_map('intval', explode('|', $value));
			$values = array_filter($values);
		}

		// Werte werden übernommen, wenn sie entweder in der Liste ($values) sind
		// oder gar keine Liste vorgegeben wurde (in diesem Fall wollte der Aufrufer
		// eine Liste der verfügbaren Optionen, nicht der ausgewählten).

		extract($this->deserializeParams($params));

		foreach ($sourceValues as $key) {
			if (!isset($values) || in_array($key, $values, true)) $elements[] = $key;
		}

		return $elements;
	}

	public function serializeValue($value, array $params) {
		return is_array($value) && !empty($value) ? '|'.implode('|', $value).'|' : '';
	}

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

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

	public function isValueContained($valueToSearch, $valueFromDatabase, array $params, $operator) {
		extract($this->deserializeParams($params));

		if ($operator === null) {
			$operator = WV_Datatype::FILTER_EXACTMATCH;
		}

		$elements         = empty($valueFromDatabase) ? array() : explode('|', $valueFromDatabase);
		$elementsToSearch = empty($valueToSearch)     ? array() : explode('|', $valueToSearch);

		foreach ($elements as $element) {
			if ($operator & WV_Datatype::FILTER_EXACTMATCH) {
				if (strval($valueToSearch) == strval($element)) return true;
			}
			elseif (in_array(strval($element), $elementsToSearch, true)) {
				return true;
			}
		}

		return false;
	}

	protected function deserializeParams(array $params, $prefix = '') {
		if (isset($params['roles'])) {
			$roles = $params['roles'];
		}
		else {
			$roles = pac_RoleService::getInstance()->getAllRoles();
			$roles = array_keys($roles);
		}

		$params = array('sourceValues' => $roles);

		// if no prefix is given, return params unchanged
		if (strlen($prefix) === 0) {
			return $params;
		}

		// prepend with prefix
		$result = array();

		foreach ($params as $key => $value) {
			$newKey = $prefix.ucfirst($key);
			$result[$newKey] = $value;
		}

		return $result;
	}

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

		$params = $property->getParams();

		foreach ($value as $idx => $list) {
			$value[$idx] = $this->deserializeValue($list === null ? '' : $list, $params);
		}

		if (!$property->isMultilingual()) {
			$value = reset($value);
		}

		extract($this->deserializeParams($params));

		$service = pac_RoleService::getInstance();
		$values  = array();

		foreach ($sourceValues as $roleID) {
			$values[$roleID] = sly_translate($service->getRoleById($roleID)->getTitle());
		}

		$propID = $property->getID();
		$title  = $property->getTitle();
		$id     = 'property_'.$propID.'_value';
		$select = new sly_Form_Select_Checkbox($id, $title, $value, $values);

		$select->setHelpText($property->getHelpText());
		$select->setMultilingual($property->isMultilingual());

		$form->add($select);
		return $form;
	}

	public function serializeFormFilter($list, WV_IProperty $property) {
		$params = $property->getParams();
		extract($this->deserializeParams($params));

		foreach ($list as $clangID => $values) {
			$values = sly_makeArray($values);
			$result = array();

			// Gültige Elemente auswählen

			foreach ($values as $element) {
				if (in_array($element, $sourceValues)) $result[] = $element;
			}

			$list[$clangID] = empty($result) ? '' : '|'.implode('|', $result).'|';
		}

		return $list;
	}
}
