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

abstract class WV42_DestinationFormHelper {
	private static $superselectJSIncluded = false;

	/**
	 * get destination form elements
	 *
	 * @param  pac_Role $role
	 * @param  string   $id
	 * @param  array    $config
	 * @return array
	 */
	public static function getDestinationFormElements(pac_Role $role, $id, array $config) {
		$elements    = array();
		$destination = pac_Core::getDestination($id);

		if ($destination instanceof pac_Destination_Plain) {
			array_push($elements, self::getPlainDestinationFormElements($role, $destination, $config));
		}
		elseif ($destination instanceof pac_Destination_List) {
			$elements = array_merge($elements, self::getListDestinationFormElements($role, $destination, $config));
		}

		return $elements;
	}

	/**
	 * get plain destination form elements
	 *
	 * @param  pac_Role              $role
	 * @param  pac_Destination_Plain $destination
	 * @param  array                 $config
	 * @return sly_Form_Select_Checkbox
	 */
	private static function getPlainDestinationFormElements(pac_Role $role, pac_Destination_Plain $destination, array $config) {
		$name   = 'wv42_perm['.$destination->getId().']';
		$label  = isset($config['title']) ? sly_translate($config['title']) : $id;
		$tokens = $config['token'];
		$value  = array();

		foreach ($tokens as $token => $title) {
			$tokens[$token] = sly_translate($title);
			if ($role->hasPermission($destination, $token)) $value[] = $token;
		}

		uasort($tokens, 'strnatcasecmp');

		self::writeSuperDeclaration($destination, $config);
		return new sly_Form_Select_Checkbox($name, $label, $value, $tokens);
	}

	/**
	 * get list destination form elements
	 *
	 * @param  pac_Role             $role
	 * @param  pac_Destination_List $destination
	 * @param  array                $config
	 * @return array
	 */
	public static function getListDestinationFormElements(pac_Role $role, pac_Destination_List $destination, array $config) {
		$elements = array();
		$helper   = self::getListObjectHelper($config['list']);
		$tokens   = $config['token'];

		if (count($tokens) === 0) {
			return $elements;
		}

		self::writeSuperDeclaration($destination, $config);

		foreach ($tokens as $token => $title) {
			$tokens[$token] = sly_translate($title);
		}

		$tokenSelect = new sly_Form_Select_Checkbox('', '', array(), $tokens);

		foreach ($helper->getObjectIds() as $id) {
			$select = clone $tokenSelect;
			$select->setLabel($helper->getObjectTitle($id));
			$select->setAttribute('id', 'wv42_perm_'.$destination->getId().'_'.$id);
			$select->setAttribute('name', 'wv42_perm['.$destination->getId().']['.$id.']');
			$value = array();

			foreach ($tokens as $token => $title) {
				if ($role->hasPermission($destination, $token, (string) $id)) $value[] = $token;
			}

			$select->setAttribute('value', $value);
			$elements[] = $select;
		}

		return $elements;
	}

	/**
	 * get list object helper
	 *
	 * @param  string $class_name
	 * @return object
	 */
	private static function getListObjectHelper($class_name) {
		if (!class_exists($class_name)) throw new sly_Exception('ListProvider '.$class_name.' not found. Check your authorisation configuration!');
		return new $class_name();
	}

	/**
	 * include supers JS
	 */
	public static function includeSupersJS() {
		if (!self::$superselectJSIncluded) {
			sly_Core::getLayout()->addJavaScriptFile(sly_Util_AddOn::assetBaseUri('webvariants/rbac').'js/superselect.js');
			self::$superselectJSIncluded = true;
		}
	}

	/**
	 * write super declaration
	 *
	 * @param pac_Destination $destination
	 * @param array           $config
	 */
	public static function writeSuperDeclaration(pac_Destination $destination, array $config) {
		$supers = isset($config['supers']) ? $config['supers'] : array();

		if (!empty($supers)) {
			self::includeSupersJS();
			sly_Core::getLayout()->addJavaScript('jQuery(function($) { $(\'#'.$destination->getId().'\').superSelect('.json_encode($supers).') });');
		}
	}
}
