From 2fb0e7c9c5e17278afe09c6c24bc922904fc95eb Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Fri, 6 Nov 2015 09:07:37 +0100 Subject: [PATCH] Activity log: add tabs, reorganize output --- application/controllers/ShowController.php | 149 +++++++++++++++--- .../scripts/show/activitylog-create.phtml | 8 - .../scripts/show/activitylog-delete.phtml | 8 - .../scripts/show/activitylog-modify.phtml | 16 -- .../views/scripts/show/activitylog.phtml | 19 +-- 5 files changed, 125 insertions(+), 75 deletions(-) delete mode 100644 application/views/scripts/show/activitylog-create.phtml delete mode 100644 application/views/scripts/show/activitylog-delete.phtml delete mode 100644 application/views/scripts/show/activitylog-modify.phtml diff --git a/application/controllers/ShowController.php b/application/controllers/ShowController.php index 94012a4b..3a480903 100644 --- a/application/controllers/ShowController.php +++ b/application/controllers/ShowController.php @@ -9,6 +9,125 @@ use Icinga\Module\Director\Objects\IcingaObject; class ShowController extends ActionController { + protected $defaultTab; + + protected function activityTabs($entry) + { + $tabs = $this->getTabs(); + if ($entry->action_name === 'modify') { + $tabs->add('diff', array( + 'label' => $this->translate('Diff'), + 'url' => 'director/show/activitylog', + 'urlParams' => array('id' => $entry->id) + )); + + $this->defaultTab = 'diff'; + } + + if (in_array($entry->action_name, array('create', 'modify'))) { + $tabs->add('new', array( + 'label' => $this->translate('New object'), + 'url' => 'director/show/activitylog', + 'urlParams' => array('id' => $entry->id, 'show' => 'new') + )); + + if ($this->defaultTab === null) { + $this->defaultTab = 'diff'; + } + } + + if (in_array($entry->action_name, array('create', 'modify'))) { + $tabs->add('old', array( + 'label' => $this->translate('Former object'), + 'url' => 'director/show/activitylog', + 'urlParams' => array('id' => $entry->id, 'show' => 'old') + )); + + if ($this->defaultTab === null) { + $this->defaultTab = 'diff'; + } + } + + return $tabs; + } + + protected function showDiff($entry) + { + $this->view->title = sprintf('%s config diff', $entry->object_name); + $this->getTabs()->activate('diff'); + $d = ConfigDiff::create( + $this->oldObject($entry), + $this->newObject($entry) + ); + + $this->view->output = $d->renderHtml(); + } + + protected function showOld($entry) + { + $this->view->title = sprintf('%s former config', $entry->object_name); + $this->getTabs()->activate('old'); + $this->showObject($this->oldObject($entry)); + } + + protected function showNew($entry) + { + $this->view->title = sprintf('%s new config', $entry->object_name); + $this->getTabs()->activate('new'); + $this->showObject($this->newObject($entry)); + } + + protected function oldObject($entry) + { + return $this->createObject($entry->object_type, $entry->old_properties); + } + + protected function newObject($entry) + { + return $this->createObject( + $entry->object_type, + $entry->old_properties + )->setProperties((array) json_decode($entry->new_properties)); + } + + protected function showObject($object) + { + $this->view->output = '
' . $this->view->escape(
+            (string) $object
+        ) . '
'; + } + + protected function showInfo($entry) + { + $typeName = $this->translate( + ucfirst(preg_replace('/^icinga_/', '', $entry->object_type)) // really? + ); + + switch ($entry->action_name) { + case 'create': + $this->view->title = sprintf( + $this->translate('%s "%s" has been created'), + $typeName, + $entry->object_name + ); + break; + case 'delete': + $this->view->title = sprintf( + $this->translate('%s "%s" has been deleted'), + $typeName, + $entry->object_name + ); + break; + case 'modify': + $this->view->title = sprintf( + $this->translate('%s "%s" has been modified'), + $typeName, + $entry->object_name + ); + break; + } + } + public function activitylogAction() { if ($id = $this->params->get('id')) { @@ -18,32 +137,10 @@ class ShowController extends ActionController } $entry = $this->view->entry; - - if ($entry->old_properties) { - $old = $this->createObject($entry->object_type, $entry->old_properties); - if ($entry->new_properties) { - $new = $this->createObject($entry->object_type, $entry->old_properties); - $new->setProperties((array) json_decode($entry->new_properties)); - } else { - $new = null; - } - } else { - $old = null; - $new = $this->createObject($entry->object_type, $entry->new_properties); - } - - if ($old && $new) { - $d = ConfigDiff::create($old, $new); - $this->view->diff = strtr( - $d->renderHtml(), - array('\\n' => "\\n\n") - ); - } - - $this->view->entry = $entry; - $this->view->newObject = $new; - $this->view->oldObject = $old; - $this->view->title = $this->translate('Activity'); + $this->activityTabs($entry); + $this->showInfo($entry); + $func = 'show' . ucfirst($this->params->get('show', $this->defaultTab)); + $this->$func($entry); } protected function createObject($type, $props) diff --git a/application/views/scripts/show/activitylog-create.phtml b/application/views/scripts/show/activitylog-create.phtml deleted file mode 100644 index 54f8779e..00000000 --- a/application/views/scripts/show/activitylog-create.phtml +++ /dev/null @@ -1,8 +0,0 @@ -

object_type)), - $entry->object_name -) ?>

- -
escape((string) $this->newObject) ?>
- diff --git a/application/views/scripts/show/activitylog-delete.phtml b/application/views/scripts/show/activitylog-delete.phtml deleted file mode 100644 index 55e327f2..00000000 --- a/application/views/scripts/show/activitylog-delete.phtml +++ /dev/null @@ -1,8 +0,0 @@ -

object_type)), - $entry->object_name -) ?>

-
-oldObject ?>
-
diff --git a/application/views/scripts/show/activitylog-modify.phtml b/application/views/scripts/show/activitylog-modify.phtml deleted file mode 100644 index 1806b687..00000000 --- a/application/views/scripts/show/activitylog-modify.phtml +++ /dev/null @@ -1,16 +0,0 @@ -

object_type)), - $entry->object_name -) ?>

- - - -
-

translate('Former state') ?>

-diff ?> -
-

translate('New object') ?>

-diff ?> -
- diff --git a/application/views/scripts/show/activitylog.phtml b/application/views/scripts/show/activitylog.phtml index 55b39e34..06a9d4fd 100644 --- a/application/views/scripts/show/activitylog.phtml +++ b/application/views/scripts/show/activitylog.phtml @@ -1,23 +1,8 @@
tabs ?> +

escape($this->title) ?>

-action_name) { -case 'create': - echo $this->render('show/activitylog-create.phtml'); - break; -case 'modify': - echo $this->render('show/activitylog-modify.phtml'); - break; -case 'delete': - echo $this->render('show/activitylog-delete.phtml'); - break; -default: - echo 'Invalid action'; -} - -?> +output ?>