Use a custom RecursiveIteratorIterator to render a RequirementSet

refs #8508
This commit is contained in:
Johannes Meyer 2015-03-10 09:31:57 +01:00
parent bc450c573d
commit c44d5d2a73
4 changed files with 144 additions and 52 deletions

View File

@ -1,66 +1,35 @@
<?php
use \RecursiveIteratorIterator;
use Icinga\Web\Wizard;
$requirements = $form->getRequirements();
$iterator = new RecursiveIteratorIterator($requirements);
echo $requirements;
?>
<table class="requirements">
<tbody>
<?php foreach ($iterator as $requirement): ?>
<tr>
<td><h2><?= $requirement->getTitle(); ?></h2></td>
<td style="width: 50%">
<?php $descriptions = $requirement->getDescriptions(); ?>
<?php if (count($descriptions) > 1): ?>
<ul>
<?php foreach ($descriptions as $desc): ?>
<li><?= $desc; ?></li>
<?php endforeach ?>
</ul>
<?php elseif (! empty($descriptions)): ?>
<?= $descriptions[0]; ?>
<?php endif ?>
</td>
<td class="state <?= $requirement->getState() ? 'fulfilled' : (
$requirement->isOptional() ? 'not-available' : 'missing'
); ?>"><?= $requirement->getStateText(); ?></td>
</tr>
<?php endforeach ?>
<tr>
<td></td>
<td></td>
<td class="btn-update">
<div class="buttons">
<?php $title = $this->translate('You may also need to restart the web-server for the changes to take effect!'); ?>
<?= $this->qlink(
$this->translate('Refresh'),
null,
null,
array(
'class' => 'button-like',
'title' => $title,
'aria-label' => sprintf($this->translate('Refresh the page; %s'), $title)
)
); ?>
</div>
</td>
</tr>
</tbody>
</table>
<div class="buttons requirements-refresh">
<?php $title = $this->translate('You may also need to restart the web-server for the changes to take effect!'); ?>
<?= $this->qlink(
$this->translate('Refresh'),
null,
null,
array(
'class' => 'button-like',
'title' => $title,
'aria-label' => sprintf($this->translate('Refresh the page; %s'), $title)
)
); ?>
</div>
<form id="<?= $form->getName(); ?>" name="<?= $form->getName(); ?>" enctype="<?= $form->getEncType(); ?>" method="<?= $form->getMethod(); ?>" action="<?= $form->getAction(); ?>">
<?= $form->getElement($form->getTokenElementName()); ?>
<?= $form->getElement($form->getUidElementName()); ?>
<div class="buttons" style="margin: 0 0 1.5em;">
<div class="buttons">
<?= $form->getElement(Wizard::BTN_PREV); ?>
<?php
$btn = $form->getElement(Wizard::BTN_NEXT);
if (false === $requirements->fulfilled()) {
if (! $requirements->fulfilled()) {
$btn->setAttrib('disabled', 1);
}
echo $btn;
?>
</div>
</form>
</form>

View File

@ -319,4 +319,15 @@ class RequirementSet implements RecursiveIterator
{
next($this->requirements);
}
/**
* Return this set of requirements rendered as HTML
*
* @return string
*/
public function __toString()
{
$renderer = new RequirementsRenderer($this);
return (string) $renderer;
}
}

View File

@ -0,0 +1,83 @@
<?php
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
namespace Icinga\Module\Setup;
use RecursiveIteratorIterator;
class RequirementsRenderer extends RecursiveIteratorIterator
{
public function beginIteration()
{
$this->tags[] = '<table class="requirements">';
$this->tags[] = '<tbody>';
}
public function endIteration()
{
$this->tags[] = '</tbody>';
$this->tags[] = '</table>';
}
public function beginChildren()
{
$this->tags[] = '<tr>';
$currentSet = $this->getSubIterator();
$state = $currentSet->getState() ? 'fulfilled' : (
$currentSet->isOptional() ? 'not-available' : 'missing'
);
$colSpanRequired = $this->hasSingleRequirements($this->getSubIterator($this->getDepth() - 1));
$this->tags[] = '<td class="set-state ' . $state . '"' . ($colSpanRequired ? ' colspan=3' : '') . '>';
$this->beginIteration();
}
public function endChildren()
{
$this->endIteration();
$this->tags[] = '</td>';
$this->tags[] = '</tr>';
}
protected function hasSingleRequirements(RequirementSet $requirements)
{
$set = $requirements->getAll();
foreach ($set as $entry) {
if ($entry instanceof Requirement) {
return true;
}
}
return false;
}
public function render()
{
foreach ($this as $requirement) {
$this->tags[] = '<tr>';
$this->tags[] = '<td class="title"><h2>' . $requirement->getTitle() . '</h2></td>';
$this->tags[] = '<td class="desc">';
$descriptions = $requirement->getDescriptions();
if (count($descriptions) > 1) {
$this->tags[] = '<ul>';
foreach ($descriptions as $d) {
$this->tags[] = '<li>' . $d . '</li>';
}
$this->tags[] = '</ul>';
} elseif (! empty($descriptions)) {
$this->tags[] = $descriptions[0];
}
$this->tags[] = '</td>';
$this->tags[] = '<td class="state ' . ($requirement->getState() ? 'fulfilled' : (
$requirement->isOptional() ? 'not-available' : 'missing'
)) . '">' . $requirement->getStateText() . '</td>';
$this->tags[] = '</tr>';
}
return implode("\n", $this->tags);
}
public function __toString()
{
return $this->render();
}
}

View File

@ -110,7 +110,8 @@
}
#setup div.buttons {
margin: 1.5em 0;
margin-top: 1.5em; // Yes, -top and -bottom, keep it like that...
margin-bottom: 1.5em;
.double {
position: absolute;
@ -166,25 +167,53 @@
}
}
#setup table.requirements {
form#setup_requirements {
padding-top: 0.5em;
border-top: 2px solid @colorPetrol;
}
div.requirements-refresh {
width: 25%;
margin-left: 75%;
text-align: center;
}
#setup > table.requirements {
font-size: 0.9em;
margin: -1em -1em 2em;
}
#setup table.requirements {
margin: -1em;
border-spacing: 1em;
border-collapse: separate;
border-bottom: 2px solid @colorPetrol;
td {
padding: 0;
h2 {
margin: 0 1em 0 0;
}
table {
font-size: 102%; // Just a hack for webkit, remove this in case you can't see any difference or make it work without it
}
ul {
margin: 0;
padding-left: 1em;
list-style-type: square;
}
&.title {
width: 25%;
}
&.desc {
width: 50%;
}
&.state {
width: 25%;
color: white;
padding: 0.4em;