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

sly_Core::getLayout()->addCSSFile(sly_Util_AddOn::assetBaseUri('webvariants/rbac').'css/backend.less');

$add_url    = 'index.php?page=rbac&func=addrole';
$edit_url   = 'index.php?page=rbac&func=editrole&role_id=';
$delete_url = 'index.php?page=rbac&amp;func=deleterole&amp;role_id=';
$add_role   = sly_Util_HTML::getSpriteLink($add_url, t('rbac_add_role'), 'article-add');

$table = new sly_Table('rbac-roles-table');
$table->addColumn(new sly_Table_Column($add_role, '', '', array('class' => 'sly-icon')));
$table->addColumn(new sly_Table_Column(t('title'), '', '', array('class' => 'title')));
$table->addColumn(new sly_Table_Column(t('rbac_roles_included'), '', '', array('class' => 'roles_included')));
$table->addColumn(new sly_Table_Column(t('rbac_user_count'), '', '', array('class' => 'user_count')));
$table->addColumn(new sly_Table_Column(t('function'), '', '', array('class' => 'actions')));

$table->openBuffer();

foreach ($roles as $role) {
	$count     = $role['count'];
	$role      = $role['role'];
	$edit_role = sly_Util_HTML::getSpriteLink($edit_url.$role->getId(), t('rbac_add_role'), 'article');
	$includes  = array();

	foreach ($role->getAllIncludedRoles() as $incl) {
		$includes[] = sly_translate($incl->getTitle(), true);
	}
	?>
	<tr>
		<td class="sly-icon"><?php echo $edit_role ?></td>
		<td class="title"><a href="<?php echo sly_html($edit_url).$role->getId() ?>"><?php echo sly_translate($role->getTitle(), true) ?></a></td>
		<td class="roles_included"><?php echo empty($includes) ? '('.t('none').')' : implode(', ', $includes) ?></td>
		<td class="user_count"><?php echo sly_Util_String::formatNumber($count, 0) ?></td>
		<td class="actions">
			<?php if (!$role->isIncluded()): ?>
			<a href="<?php print $delete_url ?><?php print $role->getId() ?>" class="sly-action sly-delete"><?php echo t('delete') ?></a>
			<?php else: ?>
			<span class="sly-strike"><?php echo t('delete') ?></span>
			<?php endif ?>
		</td>
	</tr>
	<?php
}

$table->closeBuffer();
print $table->render();
