diff --git a/application/controllers/SyncpropertyController.php b/application/controllers/SyncpropertyController.php deleted file mode 100644 index e4ccfa1e..00000000 --- a/application/controllers/SyncpropertyController.php +++ /dev/null @@ -1,57 +0,0 @@ -indexAction(); - } - - public function editAction() - { - $this->indexAction(); - } - - public function indexAction() - { - $edit = false; - - if ($id = $this->params->get('id')) { - $edit = true; - } - - if ($edit) { - $this->view->title = $this->translate('Edit sync property rule'); - $this->getTabs()->add('edit', array( - 'url' => 'director/syncproperty/edit' . '?id=' . $id, - 'label' => $this->view->title, - ))->activate('edit'); - } else { - $this->view->title = $this->translate('Add sync property rule'); - $this->getTabs()->add('add', array( - 'url' => 'director/syncproperty/add', - 'label' => $this->view->title, - ))->activate('add'); - } - - $form = $this->view->form = $this->loadForm('syncProperty')->setDb($this->db()); - - if ($edit) { - $form->loadObject($id); - $rule_id = $form->getObject()->rule_id; - $form->setRule(SyncRule::load($rule_id, $this->db())); - } elseif ($rule_id = $this->params->get('rule_id')) { - $form->setRule(SyncRule::load($rule_id, $this->db())); - } - $form->setSuccessUrl('director/syncrule/property', array('rule_id' => $rule_id)); - - $form->handleRequest(); - - $this->render('object/form', null, true); - } -} diff --git a/application/controllers/SyncruleController.php b/application/controllers/SyncruleController.php index dc8022cc..70cca0e9 100644 --- a/application/controllers/SyncruleController.php +++ b/application/controllers/SyncruleController.php @@ -2,41 +2,117 @@ namespace Icinga\Module\Director\Controllers; +use Icinga\Module\Director\Forms\SyncCheckForm; +use Icinga\Module\Director\Forms\SyncPropertyForm; +use Icinga\Module\Director\Forms\SyncRuleForm; +use Icinga\Module\Director\Forms\SyncRunForm; use Icinga\Module\Director\Web\Controller\ActionController; use Icinga\Module\Director\Objects\SyncRule; use Icinga\Module\Director\Objects\SyncRun; -use Icinga\Module\Director\Import\Sync; -use Icinga\Data\Filter\Filter; -use Icinga\Web\Notification; -use Icinga\Web\Url; +use Icinga\Module\Director\Web\Table\SyncpropertyTable; +use Icinga\Module\Director\Web\Table\SyncRunTable; +use Icinga\Module\Director\Web\Tabs\SyncRuleTabs; +use Icinga\Module\Director\Web\Widget\SyncRunDetails; +use ipl\Html\Html; +use ipl\Html\Link; class SyncruleController extends ActionController { public function indexAction() { $this->setAutoRefreshInterval(10); - $id = $this->params->get('id'); - $this->prepareRuleTabs($id)->activate('show'); - $rule = $this->view->rule = SyncRule::load($id, $this->db()); - $this->view->title = sprintf( - $this->translate('Sync rule: %s'), - $rule->rule_name - ); + $rule = $this->requireSyncRule(); + $this->tabs(new SyncRuleTabs($rule))->activate('show'); + $ruleName = $rule->get('rule_name'); + $this->addTitle($this->translate('Sync rule: %s'), $ruleName); if ($lastRunId = $rule->getLastSyncRunId()) { - $this->loadSyncRun($lastRunId); + $run = SyncRun::load($lastRunId, $this->db()); } else { - $this->view->run = null; + $run = null; } - $this->view->checkForm = $this - ->loadForm('syncCheck') - ->setSyncRule($rule) - ->handleRequest(); - $this->view->runForm = $this - ->loadForm('syncRun') - ->setSyncRule($rule) - ->handleRequest(); + $c = $this->content(); + $c->add(Html::p($rule->get('description'))); + if (! $rule->hasSyncProperties()) { + $this->addPropertyHint($rule); + return; + } + + if (! $run) { + $this->warning($this->translate('This Sync Rule has never been run before.')); + } + + switch ($rule->get('sync_state')) { + case 'unknown': + $c->add(Html::p($this->translate( + "It's currently unknown whether we are in sync with this rule." + . ' You should either check for changes or trigger a new Sync Run.' + ))); + break; + case 'in-sync': + $c->add(Html::p(sprintf( + $this->translate('This Sync Rule was last found to by in Sync at %s.'), + $rule->get('last_attempt') + ))); + /* + TODO: check whether... + - there have been imports since then, differing from former ones + - there have been activities since then + */ + break; + case 'pending-changes': + $this->warning($this->translate( + 'There are pending changes for this Sync Rule. You should trigger a new' + . ' Sync Run.' + )); + break; + case 'failing': + $this->error(sprintf( + $this->translate( + 'This Sync Rule failed when last checked at %s: %s' + ), + $rule->get('last_attempt'), + $rule->get('last_error_message') + )); + break; + } + + $c->add(SyncCheckForm::load()->setSyncRule($rule)->handleRequest()); + $c->add(SyncRunForm::load()->setSyncRule($rule)->handleRequest()); + + if ($run) { + $c->add(Html::h3($this->translate('Last sync run details'))); + $c->add(new SyncRunDetails($run)); + if ($run->get('rule_name') !== $ruleName) { + $c->add(Html::p(sprintf( + $this->translate("It has been renamed since then, its former name was %s"), + $run->get('rule_name') + ))); + } + } + } + + protected function addPropertyHint(SyncRule $rule) + { + $this->warning(Html::sprintf( + $this->translate('You must define some %s before you can run this Sync Rule'), + new Link( + $this->translate('Sync Properties'), + 'director/syncrule/property', + ['rule_id' => $rule->get('id')] + ) + )); + } + + protected function warning($msg) + { + $this->content()->add(Html::p(['class' => 'warning'], $msg)); + } + + protected function error($msg) + { + $this->content()->add(Html::p(['class' => 'error'], $msg)); } public function addAction() @@ -46,75 +122,46 @@ class SyncruleController extends ActionController public function editAction() { - $form = $this->view->form = $this->loadForm('syncRule') - ->setSuccessUrl('director/list/syncrule') + $form = SyncRuleForm::load() + ->setListUrl('director/list/syncrule') ->setDb($this->db()); if ($id = $this->params->get('id')) { - $this->prepareRuleTabs($id)->activate('edit'); $form->loadObject($id); - $this->view->title = sprintf( + /** @var SyncRule $rule */ + $rule = $form->getObject(); + $this->tabs(new SyncRuleTabs($rule))->activate('edit'); + $this->addTitle(sprintf( $this->translate('Sync rule: %s'), - $form->getObject()->rule_name - ); + $rule->rule_name + )); + + if (! $rule->hasSyncProperties()) { + $this->addPropertyHint($rule); + } } else { - $this->view->title = $this->translate('Add sync rule'); - $this->prepareRuleTabs()->activate('add'); + $this->addTitle($this->translate('Add sync rule')); + $this->tabs(new SyncRuleTabs())->activate('add'); } $form->handleRequest(); - $this->setViewScript('object/form'); - } - - public function runAction() - { - $id = $this->params->get('id'); - $rule = SyncRule::load($id, $this->db()); - $changed = $rule->applyChanges(); - - if ($changed) { - $runId = $rule->getCurrentSyncRunId(); - Notification::success('Source has successfully been synchronized'); - $this->redirectNow( - Url::fromPath( - 'director/syncrule/history', - array( - 'id' => $id, - 'run_id' => $runId - ) - ) - ); - } elseif ($rule->sync_state === 'in-sync') { - Notification::success('Nothing changed, rule is in sync'); - } else { - Notification::error('Synchronization failed'); - } - - $this->redirectNow('director/syncrule?id=' . $id); + $this->content()->add($form); } public function propertyAction() { - $this->view->stayHere = true; + $rule = $this->requireSyncRule('rule_id'); + $this->tabs(new SyncRuleTabs($rule))->activate('property'); - $db = $this->db(); - $id = $this->params->get('rule_id'); - $rule = SyncRule::load($id, $db); - - $this->prepareRuleTabs($id)->activate('property'); - - $this->view->addLink = $this->view->qlink( + $this->actions()->add(Link::create( $this->translate('Add sync property rule'), 'director/syncrule/addproperty', - array('rule_id' => $id), - array('class' => 'icon-plus') - ); + ['rule_id' => $rule->get('id')], + ['class' => 'icon-plus'] + )); - $this->view->title = $this->translate('Sync properties') . ': ' . $rule->rule_name; - $this->view->table = $this->loadTable('syncproperty') - ->enforceFilter(Filter::where('rule_id', $id)) - ->setConnection($this->db()); - $this->setViewScript('list/table'); + $this->addTitle($this->translate('Sync properties') . ': ' . $rule->get('rule_name')); + SyncpropertyTable::create($rule)->renderTo($this); } public function editpropertyAction() @@ -124,121 +171,56 @@ class SyncruleController extends ActionController public function addpropertyAction() { - $this->view->stayHere = true; - $edit = false; - $db = $this->db(); - $ruleId = $this->params->get('rule_id'); - $rule = SyncRule::load($ruleId, $db); + $rule = $this->requireSyncRule('rule_id'); + $ruleId = (int) $rule->get('id'); + $form = SyncPropertyForm::load()->setDb($db); if ($id = $this->params->get('id')) { - $edit = true; - } - - $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; - $form->setRule(SyncRule::load($rule_id, $db)); - } elseif ($rule_id = $this->params->get('rule_id')) { - $form->setRule(SyncRule::load($rule_id, $db)); - } - - $form->setSuccessUrl('director/syncrule/property', array('rule_id' => $rule_id)); - $form->handleRequest(); - - $this->prepareRuleTabs($rule_id)->activate('property'); - - if ($edit) { - $this->view->title = sprintf( + $this->addTitle( $this->translate('Sync "%s": %s'), - $form->getObject()->destination_field, - $rule->rule_name + $form->getObject()->get('destination_field'), + $rule->get('rule_name') ); } else { - $this->view->title = sprintf( + $this->addTitle( $this->translate('Add sync property: %s'), - $rule->rule_name + $rule->get('rule_name') ); } + $form->setRule($rule); + $form->setSuccessUrl('director/syncrule/property', ['rule_id' => $ruleId]); - $this->view->table = $this->loadTable('syncproperty') - ->enforceFilter(Filter::where('rule_id', $rule_id)) - ->setConnection($this->db()); - $this->setViewScript('list/table'); + $this->actions()->add(new Link( + $this->translate('back'), + 'director/syncrule/property', + ['rule_id' => $ruleId], + ['class' => 'icon-left-big'] + )); + + $this->content()->add($form->handleRequest()); + $this->tabs(new SyncRuleTabs($rule))->activate('property'); + SyncpropertyTable::create($rule)->renderTo($this); } public function historyAction() { - $this->view->stayHere = true; - - $db = $this->db(); - $id = $this->params->get('id'); - $rule = SyncRule::load($id, $db); - - $this->prepareRuleTabs($id)->activate('history'); - $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()); + $this->setAutoRefreshInterval(30); + $rule = $this->requireSyncRule(); + $this->tabs(new SyncRuleTabs($rule))->activate('history'); + $this->addTitle($this->translate('Sync history') . ': ' . $rule->rule_name); if ($runId = $this->params->get('run_id')) { - $this->loadSyncRun($runId); + $run = SyncRun::load($runId, $this->db()); + $this->content()->add(new SyncRunDetails($run)); } + SyncRunTable::create($rule)->renderTo($this); } - protected function loadSyncRun($id) + protected function requireSyncRule($key = 'id') { - $db = $this->db(); - $this->view->run = SyncRun::load($id, $db); - if ($this->view->run->last_former_activity !== null) { - $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) { - $tabs = $this->getTabs()->add('show', array( - 'url' => 'director/syncrule', - 'urlParams' => array('id' => $ruleId), - 'label' => $this->translate('Sync rule'), - ))->add('edit', array( - 'url' => 'director/syncrule/edit', - 'urlParams' => array('id' => $ruleId), - 'label' => $this->translate('Modify'), - ))->add('property', array( - 'label' => $this->translate('Properties'), - 'url' => 'director/syncrule/property', - 'urlParams' => array('rule_id' => $ruleId) - )); - - $tabs->add('history', array( - 'label' => $this->translate('History'), - 'url' => 'director/syncrule/history', - 'urlParams' => array('id' => $ruleId) - )); - - return $tabs; - } else { - return $this->getTabs()->add('add', array( - 'url' => 'director/syncrule/add', - 'label' => $this->translate('Sync rule'), - )); - } + $id = $this->params->get($key); + return SyncRule::load($id, $this->db()); } } diff --git a/application/forms/SyncCheckForm.php b/application/forms/SyncCheckForm.php index c00104c2..814f0c9c 100644 --- a/application/forms/SyncCheckForm.php +++ b/application/forms/SyncCheckForm.php @@ -1,12 +1,11 @@ $this->translate('Host'), - 'hostgroup' => $this->translate('Host group'), + 'hostgroup' => $this->translate('Host Group'), 'service' => $this->translate('Service'), - 'servicegroup' => $this->translate('Service group'), + 'servicegroup' => $this->translate('Service Group'), 'serviceSet' => $this->translate('Service Set'), 'user' => $this->translate('User'), - 'usergroup' => $this->translate('User group'), - 'datalistEntry' => $this->translate('Datalist entry'), + 'usergroup' => $this->translate('User Group'), + 'datalistEntry' => $this->translate('Data List Entry'), 'command' => $this->translate('Command'), - 'timePeriod' => $this->translate('Time period'), + 'timePeriod' => $this->translate('Time Period'), 'endpoint' => $this->translate('Endpoint'), 'zone' => $this->translate('Zone'), ); diff --git a/application/forms/SyncRunForm.php b/application/forms/SyncRunForm.php index fd1b3750..25875ab8 100644 --- a/application/forms/SyncRunForm.php +++ b/application/forms/SyncRunForm.php @@ -1,12 +1,11 @@ 'sr.id', - 'rule_id' => 'sr.rule_id', - 'rule_name' => 'sr.rule_name', - 'start_time' => 'sr.start_time', - 'duration_ms' => 'sr.duration_ms', - 'objects_deleted' => 'sr.objects_deleted', - 'objects_created' => 'sr.objects_created', - 'objects_modified' => 'sr.objects_modified', - 'last_former_activity' => 'sr.last_former_activity', - 'last_related_activity' => 'sr.last_related_activity', - ); - } - - protected function getActionUrl($row) - { - return $this->url( - 'director/syncrule/history', - array( - 'id' => $row->rule_id, - 'run_id' => $row->id, - ) - ); - } - - public function getTitles() - { - $singleRule = false; - - foreach ($this->enforcedFilters as $filter) { - if (in_array('rule_id', $filter->listFilteredColumns())) { - $singleRule = true; - break; - } - } - - $view = $this->view(); - - if ($singleRule) { - return array( - 'start_time' => $view->translate('Start time'), - 'objects_created' => $view->translate('Created'), - 'objects_modified' => $view->translate('Modified'), - 'objects_deleted' => $view->translate('Deleted'), - ); - } else { - return array( - 'rule_name' => $view->translate('Rule name'), - 'start_time' => $view->translate('Start time'), - ); - } - } - - public function getBaseQuery() - { - return $this->db()->select()->from( - array('sr' => 'sync_run'), - array() - )->order('start_time DESC'); - } -} diff --git a/application/tables/SyncpropertyTable.php b/application/tables/SyncpropertyTable.php deleted file mode 100644 index a95e0244..00000000 --- a/application/tables/SyncpropertyTable.php +++ /dev/null @@ -1,62 +0,0 @@ - 'p.id', - 'rule_id' => 'p.rule_id', - 'rule_name' => 'r.rule_name', - 'source_id' => 'p.source_id', - 'source_name' => 's.source_name', - 'source_expression' => 'p.source_expression', - 'destination_field' => 'p.destination_field', - 'priority' => 'p.priority', - 'filter_expression' => 'p.filter_expression', - 'merge_policy' => 'p.merge_policy' - ); - } - - protected function getActionUrl($row) - { - return $this->url( - 'director/syncrule/editproperty', - array( - 'id' => $row->id, - 'rule_id' => $row->rule_id, - ) - ); - } - - public function getTitles() - { - $view = $this->view(); - return array( - //'rule_name' => $view->translate('Rule name'), - 'source_name' => $view->translate('Source name'), - 'source_expression' => $view->translate('Source field'), - 'destination_field' => $view->translate('Destination') - ); - } - - public function getBaseQuery() - { - return $this->db()->select()->from( - array('r' => 'sync_rule'), - array() - )->join( - array('p' => 'sync_property'), - 'r.id = p.rule_id', - array() - )->join( - array('s' => 'import_source'), - 's.id = p.source_id', - array() - )->order('id'); - } -} diff --git a/application/views/scripts/syncrule/history.phtml b/application/views/scripts/syncrule/history.phtml deleted file mode 100644 index 9acc5a14..00000000 --- a/application/views/scripts/syncrule/history.phtml +++ /dev/null @@ -1,19 +0,0 @@ -
= $this->escape($rule->description) ?>
-hasSyncProperties()): ?> --= sprintf( - $this->translate('You must define some %s before you can run this Sync Rule'), - $this->qlink('Sync Properties', 'director/syncrule/property', array('rule_id' => $rule->id)) -) -?> -
--= $this->translate('This Sync Rule has never been run before.') ?> -
- -sync_state === 'unknown'): ?> -= $this->translate( - "It's currently unknown whether we are in sync with this rule." - . ' You should either check for changes or trigger a new Sync Run.' -) ?>
-sync_state === 'in-sync'): ?> -= sprintf( - $this->translate( - 'This Sync Rule was last found to by in Sync at %s.' - ), - $rule->last_attempt -) /* -TODO: check whether... - - there have been imports since then, differing from former ones - - there have been activities since then -*/ ?>
-sync_state === 'pending-changes'): ?> -= $this->translate( - 'There are pending changes for this Sync Rule. You should trigger a new' - . ' Sync Run.' -) ?>
-sync_state === 'failing'): ?> -= sprintf( - $this->translate( - 'This Sync Rule failed when last checked at %s: %s' - ), - $rule->last_attempt, - $rule->last_error_message -) /* -TODO: check whether... - - there have been imports since then, differing from former ones - - there have been activities since then -*/ ?>
- -= $this->checkForm ?> -= $this->runForm ?> -run): ?> -= $this->translate('Start time') ?> | -= $this->escape($run->start_time) ?> | -
---|---|
= $this->translate('Duration') ?> | -= sprintf('%.2fs', $run->duration_ms / 1000) ?> | -
= $this->translate('Activity') ?> | -= $this->render('syncrule/runSummary.phtml') ?> | -