1<?php
2
3namespace ipl\Web\Compat;
4
5use InvalidArgumentException;
6use Icinga\Web\Controller;
7use ipl\Html\HtmlDocument;
8use ipl\Html\ValidHtml;
9use ipl\Web\Layout\Content;
10use ipl\Web\Layout\Controls;
11use ipl\Web\Layout\Footer;
12use ipl\Web\Widget\Tabs;
13
14class CompatController extends Controller
15{
16    /** @var Content */
17    protected $content;
18
19    /** @var Controls */
20    protected $controls;
21
22    /** @var HtmlDocument */
23    protected $document;
24
25    /** @var Footer */
26    protected $footer;
27
28    /** @var Tabs */
29    protected $tabs;
30
31    protected function prepareInit()
32    {
33        parent::prepareInit();
34
35        unset($this->view->tabs);
36
37        $this->params->shift('isIframe');
38        $this->params->shift('showFullscreen');
39        $this->params->shift('showCompact');
40        $this->params->shift('renderLayout');
41        $this->params->shift('_disableLayout');
42        $this->params->shift('_dev');
43
44        $this->document = new HtmlDocument();
45        $this->document->setSeparator("\n");
46        $this->controls = new Controls();
47        $this->content = new Content();
48        $this->footer = new Footer();
49        $this->tabs = new Tabs();
50
51        $this->controls->setTabs($this->tabs);
52
53        ViewRenderer::inject();
54
55        $this->view->document = $this->document;
56    }
57
58    /**
59     * Get the document
60     *
61     * @return HtmlDocument
62     */
63    public function getDocument()
64    {
65        return $this->document;
66    }
67
68    /**
69     * Get the tabs
70     *
71     * @return Tabs
72     */
73    public function getTabs()
74    {
75        return $this->tabs;
76    }
77
78    /**
79     * Add content
80     *
81     * @param ValidHtml $content
82     *
83     * @return $this
84     */
85    protected function addContent(ValidHtml $content)
86    {
87        $this->content->add($content);
88
89        return $this;
90    }
91
92    /**
93     * Add a control
94     *
95     * @param ValidHtml $control
96     *
97     * @return $this
98     */
99    protected function addControl(ValidHtml $control)
100    {
101        $this->controls->add($control);
102
103        return $this;
104    }
105
106    /**
107     * Add footer
108     *
109     * @param ValidHtml $footer
110     *
111     * @return $this
112     */
113    protected function addFooter(ValidHtml $footer)
114    {
115        $this->footer->add($footer);
116
117        return $this;
118    }
119
120    /**
121     * Add an active tab with the given title and set it as the window's title too
122     *
123     * @param string $title
124     * @param mixed  ...$args
125     *
126     * @return $this
127     *
128     * @throws InvalidArgumentException
129     */
130    protected function setTitle($title, ...$args)
131    {
132        if (! empty($args)) {
133            $title = vsprintf($title, $args);
134        }
135
136        $this->view->title = $title;
137
138        $this->getTabs()->add(uniqid(), [
139            'active'    => true,
140            'label'     => $title,
141            'url'       => $this->getRequest()->getUrl()
142        ]);
143
144        return $this;
145    }
146
147    public function setAutorefreshInterval($interval)
148    {
149        $interval = (int) $interval;
150        if ($interval < 0) {
151            throw new InvalidArgumentException('Negative autorefresh intervals are not supported');
152        }
153
154        $this->autorefreshInterval = $interval;
155        $this->_helper->layout()->autorefreshInterval = $interval;
156
157        return $this;
158    }
159
160    public function postDispatch()
161    {
162        if (! $this->content->isEmpty()) {
163            $this->document->prepend($this->content);
164        }
165
166        if (! $this->view->compact && ! $this->controls->isEmpty()) {
167            $this->document->prepend($this->controls);
168        }
169
170        if (! $this->footer->isEmpty()) {
171            $this->document->add($this->footer);
172        }
173
174        parent::postDispatch();
175    }
176}
177