icingaweb2-module-businessp.../application/controllers/ProcessController.php

479 lines
15 KiB
PHP
Raw Normal View History

<?php
2015-11-17 08:21:59 -05:00
namespace Icinga\Module\Businessprocess\Controllers;
2016-11-29 10:58:03 -05:00
use Icinga\Module\Businessprocess\BusinessProcess;
2015-03-16 04:08:00 -04:00
use Icinga\Module\Businessprocess\ConfigDiff;
use Icinga\Module\Businessprocess\Html\Element;
use Icinga\Module\Businessprocess\Html\HtmlString;
use Icinga\Module\Businessprocess\Html\Icon;
2016-11-29 10:58:03 -05:00
use Icinga\Module\Businessprocess\Node;
use Icinga\Module\Businessprocess\Renderer\Breadcrumb;
2016-11-29 10:58:03 -05:00
use Icinga\Module\Businessprocess\Renderer\Renderer;
use Icinga\Module\Businessprocess\Renderer\TileRenderer;
use Icinga\Module\Businessprocess\Renderer\TreeRenderer;
2015-03-16 04:08:00 -04:00
use Icinga\Module\Businessprocess\Simulation;
use Icinga\Module\Businessprocess\Html\Link;
2016-12-09 03:53:36 -05:00
use Icinga\Module\Businessprocess\Web\Controller;
use Icinga\Module\Businessprocess\Web\Url;
2015-03-16 04:08:00 -04:00
use Icinga\Web\Notification;
use Icinga\Web\Widget\Tabextension\DashboardAction;
2015-11-17 08:21:59 -05:00
class ProcessController extends Controller
{
2016-11-29 10:58:03 -05:00
/** @var Renderer */
protected $renderer;
2015-03-16 04:08:00 -04:00
/**
* Create a new business process configuration
*/
public function createAction()
{
2015-11-17 08:46:50 -05:00
$this->assertPermission('businessprocess/create');
2015-03-16 04:08:00 -04:00
$this->setTitle($this->translate('Create a new business process'));
$this->tabsForCreate()->activate('create');
$this->view->form = $this->loadForm('bpConfig')
2015-03-16 04:08:00 -04:00
->setStorage($this->storage())
->setSuccessUrl('businessprocess/process/show')
2015-03-16 04:08:00 -04:00
->handleRequest();
}
2015-03-16 04:08:00 -04:00
/**
* Upload an existing business process configuration
*/
public function uploadAction()
{
$this->setTitle($this->translate('Upload a business process config file'));
$this->tabsForCreate()->activate('upload');
$this->view->form = $this->loadForm('BpUpload')
->setStorage($this->storage())
->setSuccessUrl('businessprocess/process/show')
->handleRequest();
2015-03-16 04:08:00 -04:00
}
/**
* Show a business process
2015-03-16 04:08:00 -04:00
*/
public function showAction()
{
2016-11-29 10:58:03 -05:00
$bp = $this->prepareProcess();
$node = $this->getNode($bp);
$this->prepareActionBar();
2015-11-17 08:20:26 -05:00
$this->redirectOnConfigSwitch();
2015-03-16 04:08:00 -04:00
$bp->retrieveStatesFromBackend();
2016-11-29 10:58:03 -05:00
$this->handleSimulations($bp);
2014-11-30 06:14:11 -05:00
2016-11-29 10:58:03 -05:00
$this->setTitle('Business Process "%s"', $bp->getTitle());
2016-11-29 10:58:03 -05:00
$renderer = $this->prepareRenderer($bp, $node);
$this->prepareControls($bp, $renderer);
$this->content()->addContent($this->showHints($bp));
$this->content()->addContent($this->showWarnings($bp));
$this->content()->add($renderer);
2016-11-29 10:58:03 -05:00
$this->loadActionForm($bp, $node);
$this->setDynamicAutorefresh();
}
protected function prepareControls($bp, $renderer)
{
$controls = $this->controls();
if ($this->showFullscreen) {
$controls->attributes()->add('class', 'want-fullscreen');
$controls->add(
Link::create(
Icon::create('resize-small'),
$this->url()->without('showFullscreen')->without('view'),
null,
array('style' => 'float: right')
)
);
}
2016-11-29 10:58:03 -05:00
$this->addProcessTabs($bp);
if (! $this->view->compact) {
2016-11-29 10:58:03 -05:00
$controls->add(Element::create('h1')->setContent($this->view->title));
}
$controls->add(Breadcrumb::create($renderer));
if (! $this->showFullscreen && ! $this->view->compact) {
$controls->add($this->actions());
}
2016-11-29 10:58:03 -05:00
}
protected function getNode($bp)
{
if ($nodeName = $this->params->get('node')) {
return $bp->getNode($nodeName);
} else {
return null;
}
}
protected function prepareRenderer($bp, $node)
{
if ($this->renderer === null) {
2016-11-29 10:58:03 -05:00
if ($this->params->get('mode') === 'tile') {
$renderer = new TileRenderer($bp, $node);
} else {
$renderer = new TreeRenderer($bp, $node);
}
$renderer->setUrl($this->url())
->setPath($this->params->getValues('path'));
if (!$bp->isLocked()) {
$renderer->unlock();
}
$this->renderer = $renderer;
}
return $this->renderer;
}
protected function addProcessTabs($bp)
{
if ($this->showFullscreen || $this->view->compact) {
return;
}
$tabs = $this->defaultTab();
if (! $bp->isLocked()) {
$tabs->extend(new DashboardAction());
}
}
protected function handleSimulations(BusinessProcess $bp)
{
$simulation = new Simulation($bp, $this->session());
if ($this->params->get('dismissSimulations')) {
Notification::success(
sprintf(
$this->translate('%d applied simulation(s) have been dropped'),
$simulation->count()
)
);
$simulation->clear();
$this->redirectNow($this->url()->without('dismissSimulations')->without('unlocked'));
}
$bp->applySimulation($simulation);
}
protected function loadActionForm(BusinessProcess $bp, Node $node = null)
{
$action = $this->params->get('action');
$form = null;
2016-12-09 03:53:36 -05:00
2016-11-29 09:32:56 -05:00
if ($action === 'add') {
2016-11-29 10:58:03 -05:00
$form =$this->loadForm('AddNode')
->setProcess($bp)
->setParentNode($node)
->setSession($this->session())
->handleRequest();
2016-11-29 09:32:56 -05:00
} elseif ($action === 'simulation') {
2016-11-29 10:58:03 -05:00
$form = $this->loadForm('simulation')
->setNode($bp->getNode($this->params->get('simulationnode')))
2016-11-29 10:58:03 -05:00
->setSimulation(new Simulation($bp, $this->session()))
->handleRequest();
}
if ($form) {
$this->content()->prependContent(HtmlString::create((string) $form));
2016-11-29 09:32:56 -05:00
}
2016-11-29 10:58:03 -05:00
}
2016-11-29 09:32:56 -05:00
2016-11-29 10:58:03 -05:00
protected function setDynamicAutorefresh()
{
if (! $this->isXhr()) {
// This will trigger the very first XHR refresh immediately on page
// load. Please not that this may hammer the server in case we would
// decide to use autorefreshInterval for HTML meta-refreshes also.
$this->setAutorefreshInterval(1);
2016-11-29 09:32:56 -05:00
return;
}
if ($this->params->get('action')) {
$this->setAutorefreshInterval(45);
} else {
$this->setAutorefreshInterval(10);
}
}
protected function showWarnings(BusinessProcess $bp)
{
if ($bp->hasWarnings()) {
$ul = Element::create('ul', array('class' => 'warning'));
foreach ($bp->getWarnings() as $warning) {
$ul->createElement('li')->addContent($warning);
}
return $ul;
} else {
return null;
}
}
public function showHints(BusinessProcess $bp)
{
$ul = Element::create('ul', array('class' => 'error'));
foreach ($bp->getErrors() as $error) {
$ul->createElement('li')->addContent($error);
}
if ($bp->hasChanges()) {
$ul->createElement('li')->setSeparator(' ')->addContent(sprintf(
$this->translate('This process has %d pending change(s).'),
$bp->countChanges()
))->addContent(
Link::create(
$this->translate('Store'),
'businessprocess/process/config',
array('config' => $bp->getName())
)
)->addContent(
Link::create(
$this->translate('Dismiss'),
$this->url()->with('dismissChanges', true),
null
)
);
}
if ($bp->hasSimulations()) {
$ul->createElement('li')->setSeparator(' ')->addContent(sprintf(
$this->translate('This process shows %d simulated state(s).'),
$bp->countSimulations()
))->addContent(Link::create(
$this->translate('Dismiss'),
$this->url()->with('dismissSimulations', true)
));
}
if ($ul->hasContent()) {
return $ul;
} else {
return null;
}
}
protected function prepareProcess()
{
2016-11-29 10:58:03 -05:00
$bp = $this->loadModifiedBpConfig();
if ($this->params->get('unlocked')) {
$bp->unlock();
}
2016-11-29 10:58:03 -05:00
if ($bp->isEmpty() && $bp->isLocked()) {
$this->redirectNow($this->url()->with('unlocked', true));
2014-11-30 06:20:39 -05:00
}
2016-11-29 10:58:03 -05:00
return $bp;
2015-03-16 04:08:00 -04:00
}
2016-11-29 10:58:03 -05:00
protected function prepareActionBar()
{
$mode = $this->params->get('mode');
$unlocked = (bool) $this->params->get('unlocked');
if ($mode === 'tile') {
$this->actions()->add(
Link::create(
$this->translate('Tree'),
2016-11-30 08:51:09 -05:00
$this->url()->with('mode', 'tree'),
null,
array('class' => 'icon-sitemap')
)
);
} else {
$this->actions()->add(
Link::create(
$this->translate('Tiles'),
$this->url()->with('mode', 'tile'),
null,
array('class' => 'icon-dashboard')
)
);
}
if ($unlocked) {
$this->actions()->add(
Link::create(
$this->translate('Lock'),
2016-11-29 09:32:56 -05:00
$this->url()->without('unlocked')->without('action'),
null,
array(
'class' => 'icon-lock',
'title' => $this->translate('Lock this process'),
)
)
);
} else {
$this->actions()->add(
Link::create(
$this->translate('Unlock'),
$this->url()->with('unlocked', true),
null,
array(
'class' => 'icon-lock-open',
'title' => $this->translate('Unlock this process'),
)
)
);
}
$this->actions()->add(
Link::create(
$this->translate('Config'),
'businessprocess/process/config',
$this->currentProcessParams(),
array(
'class' => 'icon-wrench',
'title' => $this->translate('Modify this process'),
'data-base-target' => '_next',
)
)
);
$this->actions()->add(
Link::create(
$this->translate('Fullscreen'),
$this->url()->with('showFullscreen', true),
null,
array(
'class' => 'icon-resize-full-alt',
'title' => $this->translate('Switch to fullscreen mode'),
'data-base-target' => '_main',
)
)
);
}
2015-03-16 04:08:00 -04:00
/**
* Show the source code for a process
*/
public function sourceAction()
{
$this->prepareProcess();
2015-03-16 04:08:00 -04:00
$this->tabsForConfig()->activate('source');
$bp = $this->loadModifiedBpConfig();
$this->view->source = $bp->toLegacyConfigString();
$this->view->showDiff = (bool) $this->params->get('showDiff', false);
if ($this->view->showDiff) {
$this->view->diff = ConfigDiff::create(
$this->storage()->getSource($this->view->configName),
$this->view->source
);
$this->view->title = sprintf(
$this->translate('%s: Source Code Differences'),
$bp->getTitle()
);
} else {
$this->view->title = sprintf(
$this->translate('%s: Source Code'),
$bp->getTitle()
);
}
}
/**
* Download a process configuration file
*/
public function downloadAction()
{
$this->prepareProcess();
2015-03-16 04:08:00 -04:00
$bp = $this->loadModifiedBpConfig();
header(
sprintf(
'Content-Disposition: attachment; filename="%s.conf";',
$bp->getName()
)
);
header('Content-Type: text/plain');
echo $bp->toLegacyConfigString();
// Didn't have time to lookup how to correctly disable our renderers
// TODO: no exit :)
$this->doNotRender();
}
2014-11-30 06:20:39 -05:00
2015-03-16 04:08:00 -04:00
/**
* Modify a business process configuration
*/
public function configAction()
{
$this->prepareProcess();
2015-03-16 04:08:00 -04:00
$this->tabsForConfig()->activate('config');
$bp = $this->loadModifiedBpConfig();
$this->setTitle(
$this->translate('%s: Configuration'),
$bp->getTitle()
);
$url = Url::fromPath(
'businessprocess/process/show?unlocked',
array('config' => $bp->getName())
2015-03-16 04:08:00 -04:00
);
$this->view->form = $this->loadForm('bpConfig')
->setProcessConfig($bp)
2015-03-16 04:08:00 -04:00
->setStorage($this->storage())
->setSuccessUrl($url)
2015-03-16 04:08:00 -04:00
->handleRequest();
2014-11-30 06:18:04 -05:00
}
2014-11-30 06:14:11 -05:00
2015-03-16 04:08:00 -04:00
/**
* Redirect to our URL plus the chosen config if someone switched the
* config in the appropriate dropdown list
*/
2015-11-17 08:20:26 -05:00
protected function redirectOnConfigSwitch()
2014-11-30 06:18:04 -05:00
{
2015-03-16 04:08:00 -04:00
$request = $this->getRequest();
if ($request->isPost() && $request->getPost('action') === 'switchConfig') {
2015-03-16 04:08:00 -04:00
// We switched the process in the config dropdown list
$params = array(
'config' => $request->getPost('config')
);
$this->redirectNow($this->url()->with($params));
2014-11-30 06:18:04 -05:00
}
}
2015-03-16 04:08:00 -04:00
protected function tabsForShow()
{
return $this->tabs()->add('show', array(
'label' => $this->translate('Business Process'),
'url' => $this->url()
));
}
protected function tabsForCreate()
{
return $this->tabs()->add('create', array(
'label' => $this->translate('Create'),
'url' => 'businessprocess/process/create'
))->add('upload', array(
'label' => $this->translate('Upload'),
'url' => 'businessprocess/process/upload'
));
}
protected function tabsForConfig()
{
return $this->tabs()->add('config', array(
'label' => $this->translate('Process Configuration'),
'url' => $this->getRequest()->getUrl()->without('nix')->setPath('businessprocess/process/config')
))->add('source', array(
'label' => $this->translate('Source'),
'url' => $this->getRequest()->getUrl()->without('nix')->setPath('businessprocess/process/source')
));
}
2015-01-28 13:50:52 -05:00
}