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

/**
 * @author christoph@webvariants.de
 */
class WV_Form_PropertyHelper {
	protected $properties;
	protected $format;
	protected $label;

	public function __construct(array $properties = array(), $format = '#%s#') {
		$this->properties = $properties;
		$this->format     = $format;
		$this->label      = t('devutils_available_placeholders').':';
	}

	public function setLabel($label) {
		$this->label = $label;
	}

	public function addProperty($name, $description) {
		$this->properties[$name] = $description;
	}

	public function removeProperty($name, $description) {
		unset($this->properties[$name]);
	}

	public function addArticleMetainfos($articleType = null) {
		$this->addMetainfos('TYPE_ARTICLE', $articleType);
	}

	public function addCategoryMetainfos() {
		$this->addMetainfos('TYPE_CATEGORY');
	}

	public function addMediumMetainfos() {
		$this->addMetainfos('TYPE_MEDIUM');
	}

	public function addUserMetainfos() {
		$this->addMetainfos('TYPE_USER');
	}

	public function addFrontendUserAttributes($userType = null) {
		if (!class_exists('WV16_Provider')) throw new Exception('Getting frontenduser attributes requires frontenduser to be available.');
		$infos = WV16_Provider::getAttributes($userType);

		foreach ($infos as $name => $info) {
			$this->addProperty($name, $info);
		}

		// add special placeholders
		$this->addProperty('login', 'Benutzername');
		$this->addProperty('confirmation_code', 'Bestätigungscode');
		$this->addProperty('registered', 'Datum der Anmeldung');
	}

	public function addVarisaleAttributes($productType = null, $fixed = true, $variable = true) {
		if (!class_exists('Varisale_Provider')) throw new Exception('Getting varisale attributes requires varisale to be available.');
		$infos = Varisale_Provider::getAttributes($productType, $fixed, $variable);

		foreach ($infos as $name => $info) {
			$this->addProperty($name, $info);
		}
	}

	/**
	 * @return array
	 */
	public function getProperties() {
		return $this->properties;
	}

	public function prepareFormElement(sly_Form_ElementBase $element) {
		$element->setHelpText($this->getHelpText());
		$element->setHelpTextIsHTML(true);
	}

	/**
	 * @return string
	 */
	public function getHelpText() {
		$help  = $this->label;
		$texts = array();

		foreach ($this->properties as $name => $description) {
			if ($description instanceof WV_IProperty) {
				$description = $description->getTitle();
			}

			$name    = sprintf($this->format, strtoupper($name));
			$texts[] = sprintf('<strong>%s</strong> (%s)', $name, sly_translate($description, true));
		}

		if (empty($texts)) {
			return '';
		}

		return $help.(count($texts) > 2 ? '<br />' : ' ').implode(', ', $texts);
	}

	private function addMetainfos($metaType, $articleType = null) {
		if (!class_exists('WV2_MetaProvider')) throw new Exception('Getting metainfos requires metainfo to be available.');
		$infos = WV2_MetaProvider::getAllObjectMetaInfos(constant('WV2_Metainfo::'.$metaType), null, $articleType);

		foreach ($infos as $name => $info) {
			$this->addProperty($name, $info);
		}
	}
}
