Use a custom RecursiveIteratorIterator to render a RequirementSet
refs #8508
This commit is contained in:
parent
bc450c573d
commit
c44d5d2a73
|
@ -1,39 +1,12 @@
|
|||
<?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">
|
||||
<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'),
|
||||
|
@ -46,18 +19,14 @@ $iterator = new RecursiveIteratorIterator($requirements);
|
|||
)
|
||||
); ?>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
<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;
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
|
||||
|
|
Loading…
Reference in New Issue