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

233 lines
7 KiB
PHP
Raw Normal View History

<?php
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\Simulation;
use Icinga\Module\Businessprocess\ProcessChanges;
2014-11-30 09:56:58 -05:00
use Icinga\Module\Businessprocess\Storage\LegacyStorage;
2015-03-16 04:08:00 -04:00
use Icinga\Module\Businessprocess\Forms\BpConfigForm;
use Icinga\Module\Businessprocess\Forms\DeleteConfigForm;
use Icinga\Web\Notification;
use Icinga\Web\Widget\Tabextension\DashboardAction;
2014-11-30 09:56:58 -05:00
class Businessprocess_ProcessController extends Controller
{
2015-03-16 04:08:00 -04:00
/**
* Create a new business process configuration
*/
public function createAction()
{
2015-03-16 04:08:00 -04:00
$this->setTitle($this->translate('Create a new business process'));
$this->tabsForCreate()->activate('create');
2015-03-16 04:08:00 -04:00
$this->view->form = BpConfigForm::construct()
->setStorage($this->storage())
->setRedirectUrl('businessprocess/process/show')
->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');
}
/**
* Show a business process tree
*/
public function showAction()
{
2015-03-16 04:08:00 -04:00
$this->redirectIfConfigChosen();
if ($this->params->get('unlocked')) {
$bp = $this->loadModifiedBpConfig();
$bp->unlock();
} else {
$bp = $this->loadBpConfig();
}
2015-03-16 04:08:00 -04:00
$this->setTitle('Business Process "%s"', $bp->getTitle());
$this->tabsForShow()->activate('show');
// 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
$this->view->nodeName = $node;
$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();
}
}
$this->setAutorefreshInterval(10);
2014-11-30 06:11:46 -05:00
2015-03-16 04:08:00 -04:00
$bp->retrieveStatesFromBackend();
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 {
$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);
}
2014-11-30 06:20:39 -05:00
if ($this->params->get('mode') === 'toplevel') {
$this->render('toplevel');
}
2015-03-16 04:08:00 -04:00
}
/**
* Show a business process from a toplevel perspective
*/
public function toplevelAction()
{
$this->redirectIfConfigChosen();
}
/**
* Show the source code for a process
*/
public function sourceAction()
{
$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()
{
$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->tabsForConfig()->activate('config');
$bp = $this->loadModifiedBpConfig();
$this->setTitle(
$this->translate('%s: Configuration'),
$bp->getTitle()
);
$url = sprintf(
'businessprocess/process/show?config=%s&unlocked#!%s',
$bp->getName(),
$this->getRequest()->getUrl()
);
$this->view->form = BpConfigForm::construct()
->setProcessConfig($bp)
->setStorage($this->storage())
2015-03-16 04:08:00 -04:00
->setRedirectUrl($url)
->handleRequest();
$this->view->deleteForm = DeleteConfigForm::construct()
->setStorage($this->storage())
->setController($this)
->setBpConfig($bp)
->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
*/
protected function redirectIfConfigChosen()
2014-11-30 06:18:04 -05:00
{
2015-03-16 04:08:00 -04:00
$request = $this->getRequest();
if ($request->isPost()) {
// 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
}