icingaweb2-module-businessp.../library/Businessprocess/Web/Controller.php

307 lines
7.9 KiB
PHP
Raw Normal View History

<?php
2014-11-30 09:56:58 -05:00
namespace Icinga\Module\Businessprocess;
use Icinga\Application\Icinga;
use Icinga\Module\Businessprocess\Html\HtmlString;
use Icinga\Module\Businessprocess\Modification\ProcessChanges;
2015-03-16 04:08:00 -04:00
use Icinga\Module\Businessprocess\Storage\LegacyStorage;
use Icinga\Module\Businessprocess\Storage\Storage;
2016-11-22 09:28:37 -05:00
use Icinga\Module\Businessprocess\Web\Component\ActionBar;
use Icinga\Module\Businessprocess\Web\Component\Controls;
use Icinga\Module\Businessprocess\Web\Component\Content;
use Icinga\Module\Businessprocess\Web\Form\FormLoader;
use Icinga\Module\Businessprocess\Web\Url;
2015-03-16 04:08:00 -04:00
use Icinga\Web\Controller as ModuleController;
use Icinga\Web\Notification;
use Icinga\Web\View;
use Icinga\Web\Widget;
2016-12-07 17:15:12 -05:00
use Icinga\Web\Widget\Tabs;
2015-03-16 04:08:00 -04:00
class Controller extends ModuleController
{
2016-12-09 03:53:36 -05:00
/** @var View */
public $view;
/** @deprecated, obsolete */
protected $backend;
/** @var BusinessProcess */
2015-11-17 09:47:29 -05:00
protected $bp;
2016-12-09 03:53:36 -05:00
/** @var Tabs */
protected $tabs;
/** @var Storage */
2015-03-16 04:08:00 -04:00
private $storage;
/** @var bool */
protected $showFullscreen;
/** @var Url */
2015-03-16 04:08:00 -04:00
private $url;
public function init()
{
$m = Icinga::app()->getModuleManager();
if (! $m->hasLoaded('monitoring') && $m->hasInstalled('monitoring')) {
$m->loadModule('monitoring');
}
2015-03-16 04:08:00 -04:00
$this->view->errors = array();
$this->url();
$this->view->showFullscreen
= $this->showFullscreen
= (bool) $this->_helper->layout()->showFullscreen;
2015-03-16 04:08:00 -04:00
$this->view->compact = $this->params->get('view') === 'compact';
}
/**
* @return Url
*/
2015-03-16 04:08:00 -04:00
protected function url()
{
2015-03-16 04:08:00 -04:00
if ($this->url === null) {
$this->url = Url::fromPath(
$this->getRequest()->getUrl()->getPath()
)->setParams($this->params);
}
2015-03-16 04:08:00 -04:00
return $this->url;
}
protected function currentProcessParams()
{
$params = array();
foreach (array('config', 'node') as $name) {
if ($value = $this->params->get($name)) {
$params[$name] = $value;
}
}
return $params;
}
/**
* @return ActionBar
*/
2016-11-22 09:28:37 -05:00
protected function actions()
{
if ($this->view->actions === null) {
$this->view->actions = ActionBar::create();
}
return $this->view->actions;
}
2016-12-07 17:15:12 -05:00
/**
* @return Controls
*/
protected function controls()
{
if ($this->view->controls === null) {
$this->view->controls = Controls::create();
}
return $this->view->controls;
}
2016-11-29 09:32:56 -05:00
/**
* @return Content
*/
protected function content()
{
if ($this->view->content === null) {
$this->view->content = Content::create();
}
return $this->view->content;
}
2016-12-07 17:15:12 -05:00
/**
* @param $label
* @return Tabs
*/
protected function singleTab($label)
{
2016-12-09 03:53:36 -05:00
return $this->tabs()->add(
'tab',
array(
'label' => $label,
'url' => $this->getRequest()->getUrl()
)
)->activate('tab');
}
2016-12-07 17:15:12 -05:00
/**
* @return Tabs
*/
protected function defaultTab()
{
return $this->singleTab($this->translate('Business Process'));
}
2016-12-09 03:53:36 -05:00
/**
* @return Tabs
*/
protected function overviewTab()
{
return $this->tabs()->add(
'overview',
array(
'label' => $this->translate('Business Process'),
'url' => 'businessprocess'
)
)->activate('overview');
}
2015-03-16 04:08:00 -04:00
protected function tabs()
{
2016-12-09 03:53:36 -05:00
// Todo: do not add to view once all of them render controls()
2015-03-16 04:08:00 -04:00
if ($this->view->tabs === null) {
2016-12-09 03:53:36 -05:00
$tabs = Widget::create('tabs');
$this->controls()->add(HtmlString::create($tabs));
$this->view->tabs = $tabs;
2015-03-16 04:08:00 -04:00
}
2016-12-09 03:53:36 -05:00
2015-03-16 04:08:00 -04:00
return $this->view->tabs;
}
protected function session()
{
2014-11-30 09:56:58 -05:00
return $this->Window()->getSessionNamespace('businessprocess');
}
protected function setViewScript($name)
{
$this->_helper->viewRenderer->setNoController(true);
$this->_helper->viewRenderer->setScriptAction($name);
return $this;
}
2015-03-16 04:08:00 -04:00
protected function setTitle($title)
{
2015-03-16 04:08:00 -04:00
$args = func_get_args();
array_shift($args);
$this->view->title = vsprintf($title, $args);
return $this;
2015-03-16 04:08:00 -04:00
}
protected function loadModifiedBpConfig()
{
$bp = $this->loadBpConfig();
$changes = ProcessChanges::construct($bp, $this->session());
if ($this->params->get('dismissChanges')) {
Notification::success(
sprintf(
$this->translate('%d pending change(s) have been dropped'),
$changes->count()
)
);
$changes->clear();
$this->redirectNow($this->url()->without('dismissChanges')->without('unlocked'));
}
$bp->applyChanges($changes);
return $bp;
}
protected function loadBpConfig()
{
$storage = $this->storage();
2015-11-17 08:46:50 -05:00
if ($this->hasPermission('businessprocess/create')) {
$this->view->processList = array_merge(
$storage->listProcesses(),
array('.new' => $this->translate('Create new configuration'))
);
} else {
$this->view->processList = $storage->listProcesses();
}
2015-03-16 04:08:00 -04:00
// No process found? Go to welcome page
if (empty($this->view->processList)) {
$this->redirectNow('businessprocess');
}
$name = $this->params->get(
'config',
key($this->view->processList)
);
2015-11-17 08:46:50 -05:00
if ($name === '.new') {
$this->redirectNow('businessprocess/process/create');
}
if (! $storage->hasProcess($name)) {
$this->httpNotFound(
$this->translate('No such process config: "%s"'),
$name
);
}
2015-03-16 04:08:00 -04:00
$modifications = $this->session()->get('modifications', array());
if (array_key_exists($name, $modifications)) {
$bp = $storage->loadFromString($name, $modifications[$name]);
} else {
$bp = $storage->loadProcess($name);
}
// allow URL parameter to override configured state type
if (null !== ($stateType = $this->params->get('state_type'))) {
if ($stateType === 'soft') {
$bp->useSoftStates();
}
if ($stateType === 'hard') {
$bp->useHardStates();
}
}
2015-11-17 09:47:29 -05:00
$this->view->bpconfig = $this->bp = $bp;
2015-03-16 04:08:00 -04:00
$this->view->configName = $bp->getName();
return $bp;
}
2015-10-06 16:35:44 -04:00
public function loadForm($name)
{
return FormLoader::load($name, $this->Module());
}
2015-03-16 04:08:00 -04:00
protected function storage()
{
if ($this->storage === null) {
$this->storage = new LegacyStorage(
$this->Config()->getSection('global')
);
}
return $this->storage;
}
protected function loadSlas()
{
$bpconf = $this->bpconf;
if (! isset($bpconf->slahosts)) {
return array();
}
// TODO: This doesn't work right now
$sla_hosts = preg_split('~\s*,s*~', $bpconf->slahosts, -1, PREG_SPLIT_NO_EMPTY);
if (isset($bpconf->sla_year)) {
$start = mktime(0, 0, 0, 1, 1, $bpconf->sla_year);
$end = mktime(23, 59, 59, 1, 0, $bpconf->sla_year + 1);
} else {
$start = mktime(0, 0, 0, 1, 1, (int) date('Y'));
$end = null;
// Bis zum Jahresende hochrechnen:
// $end = mktime(23, 59, 59, 1, 0, (int) date('Y') + 1);
}
return $this->backend
->module('BpAddon')
->getBpSlaValues($sla_hosts, $start, $end);
}
}