Importsource: add new preview and related forms

This commit is contained in:
Thomas Gelf 2016-06-26 15:51:05 +02:00
parent 4c70a61060
commit c969d4c775
4 changed files with 193 additions and 24 deletions

View File

@ -11,14 +11,50 @@ use Icinga\Web\Url;
class ImportsourceController extends ActionController
{
public function indexAction()
{
$id = $this->params->get('id');
$this->prepareTabs($id)->activate('show');
$source = $this->view->source = ImportSource::load($id, $this->db());
$this->view->title = sprintf(
$this->translate('Import source: %s'),
$source->source_name
);
$this->view->checkForm = $this
->loadForm('ImportCheck')
->setImportSource($source)
->handleRequest();
$this->view->runForm = $this
->loadForm('ImportRun')
->setImportSource($source)
->handleRequest();
}
public function addAction()
{
$this->indexAction();
$this->editAction();
}
public function editAction()
{
$this->indexAction();
$id = $this->params->get('id');
$form = $this->view->form = $this->loadForm('importSource')->setDb($this->db());
if ($id) {
$form->loadObject($id)->setListUrl('director/list/importsource');
$this->prepareTabs($id)->activate('edit');
$this->view->title = $this->translate('Edit import source');
} else {
$form->setSuccessUrl('director/list/importsource');
$this->view->title = $this->translate('Add import source');
$this->prepareTabs()->activate('add');
}
$form->handleRequest();
$this->setViewScript('object/form');
}
public function runAction()
@ -80,26 +116,6 @@ class ImportsourceController extends ActionController
$this->setViewScript('list/table');
}
public function indexAction()
{
$id = $this->params->get('id');
$form = $this->view->form = $this->loadForm('importSource')->setDb($this->db());
if ($id) {
$form->loadObject($id)->setListUrl('director/list/importsource');
$this->prepareTabs($id)->activate('edit');
$this->view->title = $this->translate('Edit import source');
} else {
$form->setSuccessUrl('director/list/importsource');
$this->view->title = $this->translate('Add import source');
$this->prepareTabs()->activate('add');
}
$form->handleRequest();
$this->setViewScript('object/form');
}
public function historyAction()
{
$url = $this->getRequest()->getUrl();
@ -157,9 +173,12 @@ class ImportsourceController extends ActionController
$tabs = $this->getTabs();
if ($id) {
$tabs->add('edit', array(
'url' => 'director/importsource/edit' . '?id=' . $id,
$tabs->add('show', array(
'url' => 'director/importsource' . '?id=' . $id,
'label' => $this->translate('Import source'),
))->add('edit', array(
'url' => 'director/importsource/edit' . '?id=' . $id,
'label' => $this->translate('Modify'),
))->add('modifier', array(
'url' => 'director/importsource/modifier' . '?source_id=' . $id,
'label' => $this->translate('Modifiers'),

View File

@ -0,0 +1,53 @@
<?php
// TODO: Check whether this can be removed
namespace Icinga\Module\Director\Forms;
use Exception;
use Icinga\Module\Director\Import\Import;
use Icinga\Module\Director\Objects\ImportSource;
use Icinga\Module\Director\Web\Form\QuickForm;
class ImportCheckForm extends QuickForm
{
protected $source;
public function setImportSource(ImportSource $source)
{
$this->source = $source;
return $this;
}
public function setup()
{
$this->submitLabel = false;
$this->addElement('submit', 'submit', array(
'label' => $this->translate('Check for changes'),
'decorators' => array('ViewHelper')
));
}
public function onSuccess()
{
$source = $this->source;
if ($source->checkForChanges()) {
$this->setSuccessMessage(
$this->translate('This Import Source provides modified data')
);
} else {
$this->setSuccessMessage(
$this->translate(
'Nothing to do, data provided by this Import Source'
. " didn't change since the last import run"
)
);
}
if ($source->import_state === 'failing') {
$this->addError($this->translate('Checking this Import Source failed'));
} else {
parent::onSuccess();
}
}
}

View File

@ -0,0 +1,53 @@
<?php
// TODO: Check whether this can be removed
namespace Icinga\Module\Director\Forms;
use Exception;
use Icinga\Module\Director\Import\Import;
use Icinga\Module\Director\Objects\ImportSource;
use Icinga\Module\Director\Web\Form\QuickForm;
class ImportRunForm extends QuickForm
{
protected $source;
public function setImportSource(ImportSource $source)
{
$this->source = $source;
return $this;
}
public function setup()
{
$this->submitLabel = false;
$this->addElement('submit', 'submit', array(
'label' => $this->translate('Trigger Import Run'),
'decorators' => array('ViewHelper')
));
}
public function onSuccess()
{
$source = $this->source;
if ($source->runImport()) {
$this->setSuccessMessage(
$this->translate('Imported new data from this Import Source')
);
} else {
$this->setSuccessMessage(
$this->translate(
'Nothing to do, data provided by this Import Source'
. " didn't change since the last import run"
)
);
}
if ($source->import_state === 'failing') {
$this->addError($this->translate('Triggering this Import Source failed'));
} else {
parent::onSuccess();
}
}
}

View File

@ -0,0 +1,44 @@
<div class="controls">
<?= $this->tabs ?>
<h1><?= $this->escape($this->title) ?></h1>
</div>
<div class="content">
<?php if ($source->import_state === 'unknown'): ?>
<p><?= $this->translate(
"It's currently unknown whether we are in sync with this Import Source."
. ' You should either check for changes or trigger a new Import Run.'
) ?></p>
<?php elseif ($source->import_state === 'in-sync'): ?>
<p><?= sprintf(
$this->translate(
'This Import Source was last found to by in sync at %s.'
),
$source->last_attempt
) /*
TODO: check whether...
- there have been imports since then, differing from former ones
- there have been activities since then
*/ ?></p>
<?php elseif ($source->import_state === 'pending-changes'): ?>
<p class="warning"><?= $this->translate(
'There are pending changes for this Import Source. You should trigger a new'
. ' Import Run.'
) ?></p>
<?php elseif ($source->import_state === 'failing'): ?>
<p class="error"><?= sprintf(
$this->translate(
'This Import Source failed when last checked at %s: %s'
),
$source->last_attempt,
$source->last_error_message
) /*
TODO: check whether...
- there have been imports since then, differing from former ones
- there have been activities since then
*/ ?></p>
<?php endif ?>
<?= $this->checkForm ?>
<?= $this->runForm ?>
</div>