[icinga-checkins] icinga.org: icingaweb2-module-director/master: Importsource: add new preview and related forms

git at icinga.org git at icinga.org
Mon Jun 27 09:17:10 CEST 2016


Module: icingaweb2-module-director
Branch: master
Commit: c969d4c775ce4256b6a2d84ed4b8bc2b0f38ba54
URL:    https://git.icinga.org/?p=icingaweb2-module-director.git;a=commit;h=c969d4c775ce4256b6a2d84ed4b8bc2b0f38ba54

Author: Thomas Gelf <thomas at gelf.net>
Date:   Sun Jun 26 15:51:05 2016 +0200

Importsource: add new preview and related forms

---

 application/controllers/ImportsourceController.php |   67 +++++++++++++-------
 application/forms/ImportCheckForm.php              |   53 ++++++++++++++++
 application/forms/ImportRunForm.php                |   53 ++++++++++++++++
 application/views/scripts/importsource/index.phtml |   44 +++++++++++++
 4 files changed, 193 insertions(+), 24 deletions(-)

diff --git a/application/controllers/ImportsourceController.php b/application/controllers/ImportsourceController.php
index 8924bbd..e826eac 100644
--- a/application/controllers/ImportsourceController.php
+++ b/application/controllers/ImportsourceController.php
@@ -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'),
diff --git a/application/forms/ImportCheckForm.php b/application/forms/ImportCheckForm.php
new file mode 100644
index 0000000..2b00941
--- /dev/null
+++ b/application/forms/ImportCheckForm.php
@@ -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();
+        }
+    }
+}
diff --git a/application/forms/ImportRunForm.php b/application/forms/ImportRunForm.php
new file mode 100644
index 0000000..b3e53a3
--- /dev/null
+++ b/application/forms/ImportRunForm.php
@@ -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();
+        }
+    }
+}
diff --git a/application/views/scripts/importsource/index.phtml b/application/views/scripts/importsource/index.phtml
new file mode 100644
index 0000000..2e596f0
--- /dev/null
+++ b/application/views/scripts/importsource/index.phtml
@@ -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>



More information about the icinga-checkins mailing list