<?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 _WV2_Extensions_User extends _WV2_Extensions_Base {
	/**
	 * Handler für SLY_USER_ADDED
	 *
	 * Fügt dem neuen Benutzer alle vorhandenen Metainformationen hinzu.
	 *
	 * @param array $params  die von Sally übergebenen Parameter
	 */
	public static function onUserAdded(array $params) {
		$userID    = $params['subject']->getId();
		$metainfos = WV2_MetaProvider::getAllUserMetaInfos();
		$clangs    = sly_Util_Language::findAll(true);
		$service   = new _WV2_Service_Metadata();

		foreach ($metainfos as $name => $info) {
			$metainfos[$name] = $info->getDefault();
		}

		foreach ($clangs as $clang) {
			$service->insertUserData($params['subject'], $clang, $metainfos);
		}

		if (sly_Core::isBackend()) {
			$errors = _WV2_Serializer::processUserFrontendForm($userID);

			if (is_array($errors)) {
				throw new Exception(self::getErrorText($errors, WV2_Metainfo::TYPE_USER));
			}
		}
		else {
			sly_Core::dispatcher()->notify('WV2_USER_META_ADDED', $userID);
		}
	}

	/**
	 * Handler für SLY_USER_FORM
	 *
	 * Erzeugt das Frontend-Formular für Benutzer.
	 *
	 * @param  array $params  die von Sally übergebenen Parameter
	 * @return sly_Form       das Formular
	 */
	public static function onUserForm(array $params) {
		$form = $params['subject'];
		if (!self::canEdit() && !self::canView()) return $form;

		$availableInfos = WV2_MetaProvider::getAllUserMetaInfos();

		if (!empty($availableInfos)) {
			$user  = $params['user'];
			$clang = sly_Core::getCurrentClang();

			// mark the form so we can check for a valid submission on SLY_USER_UPDATED
			$form->addHiddenValue('wv2-form', 1);

			require _WV2_PATH.'templates/user/meta_frontend.phtml';
		}

		return $form;
	}

	/**
	 * Handler für SLY_USER_UPDATED
	 *
	 * Serialisiert das Frontend-Formular und gibt ihm Fehlerfalle die
	 * Fehlermeldung, sonst das Subject zurück.
	 *
	 * @param array $params  die von Sally übergebenen Parameter
	 */
	public static function onUserUpdated(array $params) {
		if (!self::canEdit() || !sly_Core::isBackend()) return;

		// do nothing if this wasn't performed by a real *user form* submission
		// (happens at least on the backend profile page)
		if (empty($_POST['wv2-form'])) return;

		$userID = $params['subject']->getId();
		$errors = _WV2_Serializer::processUserFrontendForm($userID);

		if (is_array($errors)) {
			throw new Exception(self::getErrorText($errors, WV2_Metainfo::TYPE_USER));
		}
	}

	/**
	 * Handler für SLY_USER_DELETED
	 *
	 * Entfernt alle Metadaten für den gelöschten Benutzer.
	 *
	 * @param array $params  die von Sally übergebenen Parameter
	 */
	public static function onUserDeleted(array $params) {
		$service = new _WV2_Service_Metadata();
		$service->deleteObject($params['subject'], WV2_Metainfo::TYPE_USER);

		WV2_Metainfo::clearDataCache();
		sly_Core::dispatcher()->notify('WV2_USER_META_DELETED', $params['subject']);
	}
}
