diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 12:47:21 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 12:47:21 +0000 |
commit | 1ac4a2050c8076eb96e07e83721ebc9db864db94 (patch) | |
tree | da9b32212bf99154450a7668f61a75f65617a9fa /application/forms/EditForm.php | |
parent | Initial commit. (diff) | |
download | icingaweb2-module-toplevelview-upstream.tar.xz icingaweb2-module-toplevelview-upstream.zip |
Adding upstream version 0.3.3.upstream/0.3.3upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | application/forms/EditForm.php | 176 |
1 files changed, 176 insertions, 0 deletions
diff --git a/application/forms/EditForm.php b/application/forms/EditForm.php new file mode 100644 index 0000000..86e7880 --- /dev/null +++ b/application/forms/EditForm.php @@ -0,0 +1,176 @@ +<?php +/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */ + +namespace Icinga\Module\Toplevelview\Forms; + +use Exception; +use Icinga\Module\Toplevelview\ViewConfig; +use Icinga\Web\Form; +use Icinga\Web\Notification; +use Icinga\Web\Url; + +class EditForm extends Form +{ + /** + * @var ViewConfig + */ + protected $viewConfig; + + /** + * {@inheritdoc} + */ + public function init() + { + $this->setName('form_toplevelview_edit'); + } + + public function setViewConfig(ViewConfig $viewConfig) + { + $this->viewConfig = $viewConfig; + return $this; + } + + /** + * @see Form::onSuccess() + */ + public function onSuccess() + { + try { + $this->viewConfig->setName($this->getValue('name')); + $this->viewConfig->setText($this->getValue('config')); + + // ensure config can be parsed... + $this->viewConfig->getMetaData(); + $this->viewConfig->getTree(); + + $this->viewConfig->storeToSession(); + + $cancel = $this->getElement('btn_submit_cancel'); + $delete = $this->getElement('btn_submit_delete'); + + if ($this->getElement('btn_submit_save_file')->getValue() !== null) { + $this->viewConfig->store(); + Notification::success($this->translate('Top Level View successfully saved')); + } elseif ($cancel !== null && $cancel->getValue() !== null) { + $this->viewConfig->clearSession(); + Notification::success($this->translate('Top Level View restored from disk')); + } elseif ($delete != null && $delete->getValue() !== null) { + $this->viewConfig->delete(); + $this->setRedirectUrl('toplevelview'); + Notification::success($this->translate('Top Level View successfully deleted')); + } else { + Notification::success($this->translate('Top Level View successfully saved for the current session')); + } + return true; + } catch (Exception $e) { + $this->addError(sprintf('Could not save config: %s', $e->getMessage())); + return false; + } + } + + public function getRedirectUrl() + { + if ($this->redirectUrl === null && ($name = $this->viewConfig->getName()) !== null) { + $this->redirectUrl = Url::fromPath('toplevelview/show', array('name' => $name)); + } + return parent::getRedirectUrl(); + } + + /** + * Populate form + * + * @see Form::onRequest() + */ + public function onRequest() + { + $values = array(); + $values['name'] = $this->viewConfig->getName(); + $values['config'] = $this->viewConfig->getText(); + + $this->populate($values); + } + + /** + * @see Form::createElements() + */ + public function createElements(array $formData) + { + if ($this->viewConfig->hasBeenLoadedFromSession()) { + $this->warning( + $this->translate( + 'This config is only stored in your session!' + . ' Make sure to save it to disk once your work is complete!' + ), + false + ); + } + + $this->addElement( + 'text', + 'name', + array( + 'label' => $this->translate('File name'), + 'required' => true + ) + ); + $this->addElement( + 'textarea', + 'config', + array( + //'required' => true, + 'label' => $this->translate('YAML Config'), + 'class' => 'code-editor codemirror', + 'decorators' => array( + array('Label', array('tag'=>'div', 'separator' => '')), + array('HtmlTag', array('tag' => 'div')), + 'ViewHelper' + ), + 'data-codemirror-mode' => 'yaml' + ) + ); + + $this->addElement( + 'submit', + 'btn_submit_save_session', + array( + 'ignore' => true, + 'label' => $this->translate('Save for the current Session'), + 'decorators' => array('ViewHelper') + ) + ); + + $this->addElement( + 'submit', + 'btn_submit_save_file', + array( + 'ignore' => true, + 'label' => $this->translate('Save to config file'), + 'decorators' => array('ViewHelper') + ) + ); + + if ($this->viewConfig->hasBeenLoadedFromSession()) { + $this->addElement( + 'submit', + 'btn_submit_cancel', + array( + 'ignore' => true, + 'label' => $this->translate('Cancel editing'), + 'decorators' => array('ViewHelper') + ) + ); + } + + if ($this->viewConfig->hasBeenLoaded()) { + $this->addElement( + 'submit', + 'btn_submit_delete', + array( + 'ignore' => true, + 'label' => $this->translate('Delete config'), + 'decorators' => array('ViewHelper') + ) + ); + } + } +} |