1<?php
2
3namespace Icinga\Module\Businessprocess\Web;
4
5use Icinga\Application\Icinga;
6use Icinga\Module\Businessprocess\BpConfig;
7use Icinga\Module\Businessprocess\Modification\ProcessChanges;
8use Icinga\Module\Businessprocess\Storage\LegacyStorage;
9use Icinga\Module\Businessprocess\Storage\Storage;
10use Icinga\Module\Businessprocess\Web\Component\ActionBar;
11use Icinga\Module\Businessprocess\Web\Component\Controls;
12use Icinga\Module\Businessprocess\Web\Component\Content;
13use Icinga\Module\Businessprocess\Web\Component\Tabs;
14use Icinga\Module\Businessprocess\Web\Form\FormLoader;
15use Icinga\Web\Controller as ModuleController;
16use Icinga\Web\Notification;
17use Icinga\Web\View;
18use ipl\Html\Html;
19
20class Controller extends ModuleController
21{
22    /** @var View */
23    public $view;
24
25    /** @var BpConfig */
26    protected $bp;
27
28    /** @var Tabs */
29    protected $mytabs;
30
31    /** @var Storage */
32    private $storage;
33
34    /** @var bool */
35    protected $showFullscreen;
36
37    /** @var Url */
38    private $url;
39
40    public function init()
41    {
42        $m = Icinga::app()->getModuleManager();
43        if (! $m->hasLoaded('monitoring') && $m->hasInstalled('monitoring')) {
44            $m->loadModule('monitoring');
45        }
46        $this->controls();
47        $this->content();
48        $this->url();
49        $this->view->showFullscreen
50            = $this->showFullscreen
51            = (bool) $this->_helper->layout()->showFullscreen;
52
53        $this->view->compact = $this->params->get('view') === 'compact';
54        $this->setViewScript('default');
55    }
56
57    /**
58     * @return Url
59     */
60    protected function url()
61    {
62        if ($this->url === null) {
63            $this->url = Url::fromPath(
64                $this->getRequest()->getUrl()->getPath()
65            )->setParams($this->params);
66        }
67
68        return $this->url;
69    }
70
71    /**
72     * @return ActionBar
73     */
74    protected function actions()
75    {
76        if ($this->view->actions === null) {
77            $this->view->actions = new ActionBar();
78        }
79
80        return $this->view->actions;
81    }
82
83    /**
84     * @return Controls
85     */
86    protected function controls()
87    {
88        if ($this->view->controls === null) {
89            $controls = $this->view->controls = new Controls();
90            if ($this->view->compact) {
91                $controls->getAttributes()->add('class', 'compact');
92            }
93        }
94
95        return $this->view->controls;
96    }
97
98    /**
99     * @return Content
100     */
101    protected function content()
102    {
103        if ($this->view->content === null) {
104            $content = $this->view->content = new Content();
105            if ($this->view->compact) {
106                $content->getAttributes()->add('class', 'compact');
107            }
108        }
109
110        return $this->view->content;
111    }
112
113    /**
114     * @param $label
115     * @return Tabs
116     */
117    protected function singleTab($label)
118    {
119        return $this->tabs()->add(
120            'tab',
121            array(
122                'label' => $label,
123                'url'   => $this->getRequest()->getUrl()
124            )
125        )->activate('tab');
126    }
127
128    /**
129     * @return Tabs
130     */
131    protected function defaultTab()
132    {
133        return $this->singleTab($this->translate('Business Process'));
134    }
135
136    /**
137     * @return Tabs
138     */
139    protected function overviewTab()
140    {
141        return $this->tabs()->add(
142            'overview',
143            array(
144                'label' => $this->translate('Business Process'),
145                'url'   => 'businessprocess'
146            )
147        )->activate('overview');
148    }
149
150    /**
151     * @return Tabs
152     */
153    protected function tabs()
154    {
155        // Todo: do not add to view once all of them render controls()
156        if ($this->mytabs === null) {
157            $tabs = new Tabs();
158            //$this->controls()->add($tabs);
159            $this->mytabs = $tabs;
160        }
161
162        return $this->mytabs;
163    }
164
165    protected function session()
166    {
167        return $this->Window()->getSessionNamespace('businessprocess');
168    }
169
170    protected function setViewScript($name)
171    {
172        $this->_helper->viewRenderer->setNoController(true);
173        $this->_helper->viewRenderer->setScriptAction($name);
174        return $this;
175    }
176
177    protected function setTitle($title)
178    {
179        $args = func_get_args();
180        array_shift($args);
181        $this->view->title = vsprintf($title, $args);
182        return $this;
183    }
184
185    protected function addTitle($title)
186    {
187        $args = func_get_args();
188        array_shift($args);
189        $this->view->title = vsprintf($title, $args);
190        $this->controls()->add(Html::tag('h1', null, $this->view->title));
191        return $this;
192    }
193
194    protected function loadModifiedBpConfig()
195    {
196        $bp = $this->loadBpConfig();
197        $changes = ProcessChanges::construct($bp, $this->session());
198        if ($this->params->get('dismissChanges')) {
199            Notification::success(
200                sprintf(
201                    $this->translate('%d pending change(s) have been dropped'),
202                    $changes->count()
203                )
204            );
205            $changes->clear();
206            $this->redirectNow($this->url()->without('dismissChanges')->without('unlocked'));
207        }
208        $bp->applyChanges($changes);
209        return $bp;
210    }
211
212    protected function doNotRender()
213    {
214        $this->_helper->layout()->disableLayout();
215        $this->_helper->viewRenderer->setNoRender(true);
216        return $this;
217    }
218
219    protected function loadBpConfig()
220    {
221        $name = $this->params->get('config');
222        $storage = $this->storage();
223
224        if (! $storage->hasProcess($name)) {
225            $this->httpNotFound(
226                $this->translate('No such process config: "%s"'),
227                $name
228            );
229        }
230
231        $modifications = $this->session()->get('modifications', array());
232        if (array_key_exists($name, $modifications)) {
233            $bp = $storage->loadFromString($name, $modifications[$name]);
234        } else {
235            $bp = $storage->loadProcess($name);
236        }
237
238        // allow URL parameter to override configured state type
239        if (null !== ($stateType = $this->params->get('state_type'))) {
240            if ($stateType === 'soft') {
241                $bp->useSoftStates();
242            }
243            if ($stateType === 'hard') {
244                $bp->useHardStates();
245            }
246        }
247
248        $this->view->bpconfig = $this->bp = $bp;
249        $this->view->configName = $bp->getName();
250
251        return $bp;
252    }
253
254    public function loadForm($name)
255    {
256        return FormLoader::load($name, $this->Module());
257    }
258
259    /**
260     * @return LegacyStorage|Storage
261     */
262    protected function storage()
263    {
264        if ($this->storage === null) {
265            $this->storage = LegacyStorage::getInstance();
266        }
267
268        return $this->storage;
269    }
270}
271