1<?php
2/* Icinga Web 2 | (c) 2015 Icinga Development Team | GPLv2+ */
3
4namespace Icinga\Module\Setup;
5
6use RecursiveIteratorIterator;
7
8class RequirementsRenderer extends RecursiveIteratorIterator
9{
10    public function beginIteration()
11    {
12        $this->tags[] = '<ul class="requirements">';
13    }
14
15    public function endIteration()
16    {
17        $this->tags[] = '</ul>';
18    }
19
20    public function beginChildren()
21    {
22        $this->tags[] = '<li>';
23        $currentSet = $this->getSubIterator();
24        $state = $currentSet->getState() ? 'fulfilled' : ($currentSet->isOptional() ? 'not-available' : 'missing');
25        $this->tags[] = '<ul class="set-state ' . $state . '">';
26    }
27
28    public function endChildren()
29    {
30        $this->tags[] = '</ul>';
31        $this->tags[] = '</li>';
32    }
33
34    public function render()
35    {
36        foreach ($this as $requirement) {
37            $this->tags[] = '<li class="clearfix">';
38            $this->tags[] = '<div class="title"><h2>' . $requirement->getTitle() . '</h2></div>';
39            $this->tags[] = '<div class="description">';
40            $descriptions = $requirement->getDescriptions();
41            if (count($descriptions) > 1) {
42                $this->tags[] = '<ul>';
43                foreach ($descriptions as $d) {
44                    $this->tags[] = '<li>' . $d . '</li>';
45                }
46                $this->tags[] = '</ul>';
47            } elseif (! empty($descriptions)) {
48                $this->tags[] = $descriptions[0];
49            }
50            $this->tags[] = '</div>';
51            $this->tags[] = '<div class="state ' . ($requirement->getState() ? 'fulfilled' : (
52                $requirement->isOptional() ? 'not-available' : 'missing'
53            )) . '">' . $requirement->getStateText() . '</div>';
54            $this->tags[] = '</li>';
55        }
56
57        return implode("\n", $this->tags);
58    }
59
60    public function __toString()
61    {
62        return $this->render();
63    }
64}
65