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

$action = $role ? 'editrole' : 'addrole';
$roles  = pac_RoleService::getInstance()->getAllRoles();
$includes_values = array();

foreach ($roles as $tmprole) {
	if ($tmprole->getId() != $roleId) {
		$includes_values[$tmprole->getId()] = sly_translate($tmprole->getTitle());
	}
}

$form = new sly_Form('index.php?page=rbac&func='.$action, 'POST', $title);
$form->addHiddenValue('role_id', $roleId);
$form->addHiddenValue('save', 1);
$form->addElement(new sly_Form_Input_Text('role_title', t('title'), $role_title));

if (empty($includes_values)) {
	$form->addElement(new sly_Form_Text(t('rbac_roles_included'), t('rbac_no_more_rules')));
}
else {
	$form->addElement(new sly_Form_Select_Checkbox('role_includes', t('rbac_roles_included'), $role_includes, $includes_values));
}

$form->setFocus('role_name');
$form->setDeleteButton(null);

if ($role) {
	$destinationConfig = sly_Authorisation::getConfig();

	foreach ($destinationConfig as $id => $config) {
		if (isset($config['renderer'])) {
			$renderer    = $config['renderer'];
			$renderer    = new $renderer();
			$destination = pac_Core::getDestination($id);

			$form = $renderer->render($form, $role, $destination, $config);
		}
		else {
			$perm_fieldset = new sly_Form_Fieldset(sly_translate($config['title']), $id);
			$elements      = WV42_DestinationFormHelper::getDestinationFormElements($role, $id, $config);

			foreach ($elements as $element) {
				$perm_fieldset->addRow(array($element));
			}

			$form->addFieldset($perm_fieldset);
		}
	}

	$form->beginFieldset(t('functions'));
	$form->setApplyButton(new sly_Form_Input_Button('submit', 'apply', t('apply')));
}

print $form->render();
