<?php
/**
 * @version     1.3.3
 * @package     sellacious
 *
 * @copyright   Copyright (C) 2012-2017 Bhartiy Web Technologies. All rights reserved.
 * @license     SPL Sellacious Private License; see http://www.sellacious.com/spl.html
 * @author      Izhar Aazmi <info@bhartiy.com> - http://www.bhartiy.com
 */
// no direct access
defined('_JEXEC') or die;

/** @var stdClass $displayData */
$data = $displayData;

$field_id   = $data->id;
$field_name = $data->name;
$component  = $data->component;
$section    = $data->section;
$assetId    = $data->assetId;
$actions    = $data->actions;
$groups     = $data->groups;

/** @var JAccessRules $assetRules */
$assetRules = $data->assetRules;

// Prepare full width format output.
?>
<p class="rule-desc"><?php echo JText::_('JLIB_RULES_SETTINGS_DESC') ?></p>

<div id="permissions-sliders" class="tabbable tabs-left">
	<ul class="nav nav-tabs" style="width:200px;">
		<?php
		foreach ($groups as $group)
		{
			?>
			<li class="<?php echo $group->value == 1 ? 'active' : '' ?>">
				<a href="#permission-<?php echo $group->value ?>" data-toggle="tab">
					<?php echo str_repeat('<span class="level">&mdash;</span> ', $curLevel = $group->level) . $group->text; ?></a>
			</li>
			<?php
		}
		?>
	</ul>

	<div class="tab-content" style="margin-left: 220px;">
	<?php
	// Start a row for each user group.
	foreach ($groups as $group)
	{
		?>
		<div class="tab-pane<?php echo $group->value == 1 ? ' active' : '' ?>" id="permission-<?php echo $group->value ?>">
			<table class="table table-striped table-bordered" style="clear: right;">
				<thead>
				<tr>
					<th class="actions" id="actions-th<?php echo $group->value ?>">
						<span class="acl-action"><?php echo JText::_('JLIB_RULES_ACTION') ?></span>
					</th>
					<th class="settings" id="settings-th<?php echo $group->value ?>">
						<span class="acl-action"><?php echo JText::_('JLIB_RULES_SELECT_SETTING') ?></span>
					</th>
					<?php
					// The calculated setting is not shown for the root group of global configuration as it cannot inherit.
					$can_inherit = !($group->parent_id == 0 && empty($component));

					if ($can_inherit)
					{
						?>
						<th id="aclactionth<?php echo $group->value ?>">
							<span class="acl-action"><?php echo JText::_('JLIB_RULES_CALCULATED_SETTING') ?></span>
						</th>
						<?php
					}
					?>
				</tr>
				</thead>
				<tbody>
				<?php
				$choices = array(
					''  => JText::_($can_inherit ? 'JLIB_RULES_INHERITED' : 'JLIB_RULES_NOT_SET'),
					'1' => JText::_('JLIB_RULES_ALLOWED'),
					'0' => JText::_('JLIB_RULES_DENIED'),
				);

				foreach ($actions as $action)
				{
					// Get the actual setting and inherited setting for the action for this group.
					$assetRule     = $assetRules->allow($action->name, $group->value);
					$inheritedRule = JAccess::checkGroup($group->value, $action->name, $assetId);
					$selected      = is_bool($assetRule) ? ($assetRule ? '1' : '0') : '';
					?>
					<tr>
						<td headers="actions-th<?php echo $group->value ?>">
							<?php $label_tip = JText::_($action->title) . ' ' . JText::_($action->description); ?>
							<label for="<?php echo $field_id . '_' . $action->name . '_' . $group->value ?>"
								class="hasTooltip" title="<?php echo htmlspecialchars($label_tip, ENT_COMPAT, 'UTF-8') ?>">
								<?php echo JText::_($action->title) ?>
							</label>
						</td>
						<td headers="settings-th<?php echo $group->value ?>">
							<select name="<?php echo $field_name ?>[<?php echo $action->name ?>][<?php echo $group->value ?>]"
								id="<?php echo $field_name ?>_<?php $action->name ?>_<?php $group->value ?>" class="input-small"
								title="<?php echo JText::sprintf('JLIB_RULES_SELECT_ALLOW_DENY_GROUP', JText::_($action->title), trim($group->text)) ?>">
									<?php echo JHtmlSelect::options($choices, '', '', $selected) ?>
							</select>
							<?php
							// If this asset's rule is allowed, but the inherited rule is deny, we have a conflict.
							if ($assetRule === true && $inheritedRule === false)
							{
								echo JText::_('JLIB_RULES_CONFLICT');
							}
						?>
						</td>
						<?php
						// Build the Calculated Settings column.
						// The inherited settings column is not displayed for the root group in global configuration.
						if ($can_inherit)
						{
							?>
							<td headers="aclactionth<?php echo $group->value ?>">
								<?php
								// This is where we show the current effective settings considering current group, path and cascade.
								// Check whether this is a component or global. Change the text slightly.

								if (JAccess::checkGroup($group->value, 'core.admin', $assetId) !== true)
								{
									if ($inheritedRule === null)
									{
										echo '<span class="label label-important">' . JText::_('JLIB_RULES_NOT_ALLOWED') . '</span>';
									}
									elseif ($inheritedRule === true)
									{
										echo '<span class="label label-success">' . JText::_('JLIB_RULES_ALLOWED') . '</span>';
									}
									elseif ($inheritedRule === false)
									{
										if ($assetRule === false)
										{
											echo '<span class="label label-important">' . JText::_('JLIB_RULES_NOT_ALLOWED') . '</span>';
										}
										else
										{
											echo '<span class="label"><i class="icon-lock icon-white"></i> ' . JText::_('JLIB_RULES_NOT_ALLOWED_LOCKED') . '</span>';
										}
									}
								}
								elseif (!empty($component))
								{
									echo '<span class="label label-success"><i class="icon-lock icon-white"></i> ' . JText::_('JLIB_RULES_ALLOWED_ADMIN') . '</span>';
								}
								else
								{
									// Special handling for  groups that have global admin because they can't  be denied.
									// The admin rights can be changed.
									if ($action->name === 'core.admin')
									{
										echo '<span class="label label-success">' . JText::_('JLIB_RULES_ALLOWED') . '</span>';
									}
									elseif ($inheritedRule === false)
									{
										// Other actions cannot be changed.
										echo '<span class="label label-important"><i class="icon-lock icon-white"></i> ' . JText::_('JLIB_RULES_NOT_ALLOWED_ADMIN_CONFLICT') . '</span>';
									}
									else
									{
										echo '<span class="label label-success"><i class="icon-lock icon-white"></i> ' . JText::_('JLIB_RULES_ALLOWED_ADMIN') . '</span>';
									}
								}
								?>
							</td>
							<?php
						}
						?>
					</tr>
					<?php
				}
				?>
				</tbody>
			</table>
		</div>
		<?php
	}
	?>
	</div>
</div>

<div class="alert">
	<?php
	if ($section == 'component' || $section == null)
	{
		echo JText::_('JLIB_RULES_SETTING_NOTES');
	}
	else
	{
		echo JText::_('JLIB_RULES_SETTING_NOTES_ITEM');
	}
	?>
</div>
