summaryrefslogtreecommitdiffstats
path: root/vendor/gipfl/icingaweb2/src/Zf1/SimpleViewRenderer.php
blob: 89b36a40847e6d86c148d7094217c6b0cc7193ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php

namespace gipfl\IcingaWeb2\Zf1;

use gipfl\IcingaWeb2\Widget\Content;
use gipfl\IcingaWeb2\Widget\Controls;
use ipl\Html\Error;
use Icinga\Application\Icinga;
use ipl\Html\ValidHtml;
use Zend_Controller_Action_Helper_Abstract as Helper;
use Zend_Controller_Action_HelperBroker as HelperBroker;

class SimpleViewRenderer extends Helper implements ValidHtml
{
    private $disabled = false;

    private $rendered = false;

    /** @var \Zend_View_Interface */
    public $view;

    public function init()
    {
        // Register view with action controller (unless already registered)
        if ((null !== $this->_actionController) && (null === $this->_actionController->view)) {
            $this->_actionController->view = $this->view;
        }
    }

    public function disable($disabled = true)
    {
        $this->disabled = $disabled;
        return $this;
    }

    public function replaceZendViewRenderer()
    {
        /** @var \Zend_Controller_Action_Helper_ViewRenderer $viewRenderer */
        $viewRenderer = Icinga::app()->getViewRenderer();
        $viewRenderer->setNeverRender();
        $viewRenderer->setNeverController();
        HelperBroker::removeHelper('viewRenderer');
        HelperBroker::addHelper($this);
        $this->view = $viewRenderer->view;
        return $this;
    }

    public function render($action = null, $name = null, $noController = null)
    {
        if (null === $name) {
            $name = null; // $this->getResponseSegment();
        }
        // Compat.
        if (isset($this->_actionController)
            && get_class($this->_actionController) === 'Icinga\\Controllers\\ErrorController'
        ) {
            $html = $this->simulateErrorController();
        } else {
            $html = '';
            if (null !== $this->view->controls) {
                $html .= $this->view->controls->__toString();
            }

            if (null !== $this->view->content) {
                $html .= $this->view->content->__toString();
            }
        }

        $this->getResponse()->appendBody($html, $name);
        // $this->setNoRender();
        $this->rendered = true;
    }

    protected function simulateErrorController()
    {
        $errorHandler = $this->_actionController->getParam('error_handler');
        if (isset($errorHandler->exception)) {
            $error = Error::show($errorHandler->exception);
        } else {
            $error = 'An unknown error occured';
        }

        /** @var \Icinga\Web\Request $request */
        $request = $this->getRequest();
        $controls = new Controls();
        $controls->getTabs()->add('error', [
            'label' => t('Error'),
            'url' => $request->getUrl(),
        ])->activate('error');
        $content = new Content();
        $content->add($error);

        return $controls . $content;
    }

    public function shouldRender()
    {
        return ! $this->disabled && ! $this->rendered;
    }

    public function postDispatch()
    {
        if ($this->shouldRender()) {
            $this->render();
        }
    }

    public function getName()
    {
        // TODO: This is wrong, should be 'viewRenderer' - but that would
        //       currently break nearly everything, starting with full layout
        //       rendering
        return 'ViewRenderer';
    }
}