icingaweb2-module-director/application/controllers/SyncruleController.php

199 lines
6.2 KiB
PHP
Raw Normal View History

2015-07-23 16:40:32 +02:00
<?php
2015-10-20 22:34:04 +02:00
namespace Icinga\Module\Director\Controllers;
2015-07-23 16:40:32 +02:00
use Icinga\Module\Director\Web\Controller\ActionController;
use Icinga\Module\Director\Objects\SyncRule;
use Icinga\Module\Director\Objects\SyncRun;
2015-07-24 10:55:05 +02:00
use Icinga\Module\Director\Import\Sync;
use Icinga\Data\Filter\Filter;
2015-07-23 16:40:32 +02:00
use Icinga\Web\Notification;
2016-04-20 15:33:03 +02:00
use Icinga\Web\Url;
2015-07-23 16:40:32 +02:00
2015-10-20 22:34:04 +02:00
class SyncruleController extends ActionController
2015-07-23 16:40:32 +02:00
{
public function addAction()
{
$this->indexAction();
2015-07-23 16:40:32 +02:00
}
public function editAction()
{
$this->indexAction();
2015-07-23 16:40:32 +02:00
}
public function runAction()
{
2016-04-20 15:33:03 +02:00
$id = $this->params->get('id');
$sync = new Sync(SyncRule::load($id, $this->db()));
if ($runId = $sync->apply()) {
Notification::success('Source has successfully been synchronized');
2016-04-20 15:33:03 +02:00
$this->redirectNow(
Url::fromPath(
'director/syncrule/history',
array(
'id' => $id,
'run_id' => $runId
)
)
);
2015-07-23 16:40:32 +02:00
} else {
}
}
public function indexAction()
{
$form = $this->view->form = $this->loadForm('syncRule')
->setSuccessUrl('director/list/syncrule')
2015-07-23 16:40:32 +02:00
->setDb($this->db());
if ($id = $this->params->get('id')) {
$this->prepareRuleTabs($id)->activate('edit');
2015-07-23 16:40:32 +02:00
$form->loadObject($id);
$this->view->title = sprintf(
$this->translate('Sync rule: %s'),
$form->getObject()->rule_name
);
} else {
$this->view->title = $this->translate('Add sync rule');
$this->prepareRuleTabs()->activate('add');
2015-07-23 16:40:32 +02:00
}
$form->handleRequest();
$this->setViewScript('object/form');
2015-07-23 16:40:32 +02:00
}
public function propertyAction()
{
$this->view->stayHere = true;
2016-04-20 15:20:49 +02:00
$db = $this->db();
$id = $this->params->get('rule_id');
2016-04-20 15:20:49 +02:00
$rule = SyncRule::load($id, $db);
$this->prepareRuleTabs($id)->activate('property');
2016-04-20 15:20:49 +02:00
$this->view->addLink = $this->view->qlink(
$this->translate('Add sync property rule'),
'director/syncrule/addproperty',
array('rule_id' => $id),
array('class' => 'icon-plus')
);
2016-04-20 15:20:49 +02:00
$this->view->title = $this->translate('Sync properties') . ': ' . $rule->rule_name;
2016-02-26 12:42:21 +01:00
$this->view->table = $this->loadTable('syncproperty')
->enforceFilter(Filter::where('rule_id', $id))
->setConnection($this->db());
$this->setViewScript('list/table');
}
public function editpropertyAction()
{
$this->addpropertyAction();
}
public function addpropertyAction()
{
$this->view->stayHere = true;
$edit = false;
2016-04-20 15:20:49 +02:00
$db = $this->db();
$ruleId = $this->params->get('rule_id');
$rule = SyncRule::load($ruleId, $db);
if ($id = $this->params->get('id')) {
$edit = true;
}
2016-04-20 15:20:49 +02:00
$this->view->addLink = $this->view->qlink(
$this->translate('back'),
'director/syncrule/property',
array('rule_id' => $ruleId),
array('class' => 'icon-left-big')
);
$form = $this->view->form = $this->loadForm('syncProperty')->setDb($db);
if ($edit) {
$form->loadObject($id);
$rule_id = $form->getObject()->rule_id;
2016-04-20 15:20:49 +02:00
$form->setRule(SyncRule::load($rule_id, $db));
} elseif ($rule_id = $this->params->get('rule_id')) {
2016-04-20 15:20:49 +02:00
$form->setRule(SyncRule::load($rule_id, $db));
}
2016-04-20 15:20:49 +02:00
$form->setSuccessUrl('director/syncrule/property', array('rule_id' => $rule_id));
$form->handleRequest();
$this->prepareRuleTabs($rule_id)->activate('property');
2016-04-20 15:20:49 +02:00
if ($edit) {
$this->view->title = sprintf(
$this->translate('Sync "%s": %s'),
$form->getObject()->destination_field,
$rule->rule_name
);
} else {
$this->view->title = sprintf(
$this->translate('Add sync property: %s'),
$rule->rule_name
);
}
$this->view->table = $this->loadTable('syncproperty')
->enforceFilter(Filter::where('rule_id', $rule_id))
->setConnection($this->db());
$this->setViewScript('list/table');
}
public function historyAction()
{
$this->view->stayHere = true;
2016-04-20 15:20:49 +02:00
$db = $this->db();
$id = $this->params->get('id');
2016-04-20 15:20:49 +02:00
$rule = SyncRule::load($id, $db);
$this->prepareRuleTabs($id)->activate('history');
2016-04-20 15:20:49 +02:00
$this->view->title = $this->translate('Sync history') . ': ' . $rule->rule_name;
$this->view->table = $this->loadTable('syncRun')
->enforceFilter(Filter::where('rule_id', $id))
->setConnection($this->db());
if ($runId = $this->params->get('run_id')) {
$this->view->run = SyncRun::load($runId, $db);
$this->view->formerId = $db->fetchActivityLogIdByChecksum(
$this->view->run->last_former_activity
);
$this->view->lastId = $db->fetchActivityLogIdByChecksum(
$this->view->run->last_related_activity
);
}
}
protected function prepareRuleTabs($ruleId = null)
{
if ($ruleId) {
return $this->getTabs()->add('edit', array(
'url' => 'director/syncrule/edit',
'urlParams' => array('id' => $ruleId),
'label' => $this->translate('Sync rule'),
))->add('property', array(
'label' => $this->translate('Properties'),
'url' => 'director/syncrule/property',
'urlParams' => array('rule_id' => $ruleId)
))->add('history', array(
'label' => $this->translate('History'),
'url' => 'director/syncrule/history',
'urlParams' => array('id' => $ruleId)
));
} else {
return $this->getTabs()->add('add', array(
'url' => 'director/syncrule/add',
'label' => $this->translate('Sync rule'),
));
}
}
2015-07-23 16:40:32 +02:00
}