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

374 lines
12 KiB
PHP
Raw Normal View History

<?php
2015-11-17 08:21:59 -05:00
namespace Icinga\Module\Businessprocess\Controllers;
2014-11-30 09:56:58 -05:00
use Icinga\Module\Businessprocess\Controller;
2015-03-16 04:08:00 -04:00
use Icinga\Module\Businessprocess\ConfigDiff;
use Icinga\Module\Businessprocess\Renderer\Breadcrumb;
use Icinga\Module\Businessprocess\Renderer\TileRenderer;
2015-03-16 04:08:00 -04:00
use Icinga\Module\Businessprocess\Simulation;
use Icinga\Module\Businessprocess\Html\Link;
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
{
protected function currentProcessParams()
{
$params = array();
foreach (array('config', 'node') as $name) {
if ($value = $this->params->get($name)) {
$params[$name] = $value;
}
}
return $params;
}
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 tree
*/
public function showAction()
{
$mode = $this->params->get('mode');
$unlocked = (bool) $this->params->get('unlocked');
$this->prepareProcessActions();
$this->prepareProcess();
2015-11-17 08:20:26 -05:00
$this->redirectOnConfigSwitch();
2015-03-16 04:08:00 -04:00
if ($unlocked) {
2015-03-16 04:08:00 -04:00
$bp = $this->loadModifiedBpConfig();
$bp->unlock();
} else {
$bp = $this->loadBpConfig();
}
2015-03-16 04:08:00 -04:00
// Do not lock empty configs
if ($bp->isEmpty() && ! $this->view->compact && $bp->isLocked()) {
$this->redirectNow($this->url()->with('unlocked', true));
2015-03-03 04:50:19 -05:00
}
2015-03-16 04:08:00 -04:00
if ($node = $this->params->get('node')) {
// Render a specific node
2015-03-16 04:08:00 -04:00
$this->view->nodeName = $node;
$bpNode = $this->view->bp = $bp->getNode($node);
} else {
2015-03-16 04:08:00 -04:00
// Render a single process
$this->view->bp = $bp;
if ($bp->hasWarnings()) {
2015-01-28 13:50:52 -05:00
$this->view->warnings = $bp->getWarnings();
}
$bpNode = null;
}
2014-11-30 06:11:46 -05:00
2015-03-16 04:08:00 -04:00
$bp->retrieveStatesFromBackend();
if ($this->params->get('addSimulation')) {
$this->simulationForm();
}
$this->setTitle('Business Process "%s"', $bp->getTitle());
$this->tabsForShow()->activate('show');
2014-11-30 06:14:11 -05:00
2015-03-16 04:08:00 -04:00
if ($bp->isLocked()) {
$this->tabs()->extend(new DashboardAction());
} else {
2015-03-16 04:08:00 -04:00
$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'));
}
2014-11-30 06:20:39 -05:00
2015-03-16 04:08:00 -04:00
$bp->applySimulation($simulation);
}
// TODO: ...
$renderer = new TileRenderer($this->view, $bp, $bpNode);
$renderer->setBaseUrl($this->url())
->setPath($this->params->getValues('path'));
$this->view->bpRenderer = $renderer;
$this->view->breadcrumb = Breadcrumb::create($renderer);
if (! $bp->isLocked()) {
$renderer->unlock();
}
if ($this->isXhr()) {
if ($this->params->get('addSimulation')) {
$this->setAutorefreshInterval(30);
} else {
$this->setAutorefreshInterval(10);
}
} else {
// 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);
}
if ($mode === 'tile') {
$this->setViewScript('process/bprenderer');
}
}
protected function prepareProcess()
{
if ($this->params->get('unlocked')) {
$bp = $this->loadModifiedBpConfig();
$bp->unlock();
} else {
$bp = $this->loadBpConfig();
}
if ($node = $this->params->get('node')) {
// Render a specific node
$this->view->nodeName = $node;
$this->view->bp = $bp->getNode($node);
2014-11-30 06:20:39 -05:00
}
2015-03-16 04:08:00 -04:00
}
protected function prepareProcessActions()
{
$mode = $this->params->get('mode');
$unlocked = (bool) $this->params->get('unlocked');
if ($mode === 'tile') {
$this->actions()->add(
Link::create(
$this->translate('Tree'),
'businessprocess/process/show',
$this->currentProcessParams(),
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'),
$this->url()->without('unlocked'),
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('Store'),
'businessprocess/process/config',
$this->currentProcessParams(),
array(
'class' => 'icon-wrench',
'title' => $this->translate('Modify this process'),
'data-base-target' => '_next',
)
)
);
}
protected function simulationForm()
{
$this->prepareProcess();
$bp = $this->loadBpConfig();
$nodename = $this->getParam('simulationNode');
$node = $bp->getNode($nodename);
$url = $this->getRequest()->getUrl()->without('addSimulation')->without('simulationNode');
$this->view->form = $this->loadForm('simulation')
->setSimulation(new Simulation($bp, $this->session()))
->setNode($node)
->setSuccessUrl($url)
->handleRequest();
$this->view->node = $node;
}
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
}