| 1 |
<?php |
| 2 |
/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */ |
| 3 |
|
| 4 |
namespace Icinga\Module\Toplevelview\Forms; |
| 5 |
|
| 6 |
use Icinga\Module\Toplevelview\ViewConfig; |
| 7 |
use Icinga\Module\Toplevelview\Model\View; |
| 8 |
|
| 9 |
use Exception; |
| 10 |
use Icinga\Web\Form; |
| 11 |
use Icinga\Web\Notification; |
| 12 |
use Icinga\Web\Url; |
| 13 |
|
| 14 |
class EditForm extends Form |
| 15 |
{ |
| 16 |
/** |
| 17 |
* @var ViewConfig |
| 18 |
*/ |
| 19 |
protected $viewconfig; |
| 20 |
|
| 21 |
/** |
| 22 |
* @var View |
| 23 |
*/ |
| 24 |
protected $view; |
| 25 |
|
| 26 |
/** |
| 27 |
* {@inheritdoc} |
| 28 |
*/ |
| 29 |
public function init() |
| 30 |
{ |
| 31 |
$this->setName('form_toplevelview_edit'); |
| 32 |
} |
| 33 |
|
| 34 |
public function setViews(View $view) |
| 35 |
{ |
| 36 |
$this->view = $view; |
| 37 |
return $this; |
| 38 |
} |
| 39 |
|
| 40 |
public function setViewConfig(ViewConfig $config) |
| 41 |
{ |
| 42 |
$this->viewconfig = $config; |
| 43 |
return $this; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* @see Form::onSuccess() |
| 48 |
*/ |
| 49 |
public function onSuccess() |
| 50 |
{ |
| 51 |
try { |
| 52 |
$this->view->setName($this->getValue('name')); |
| 53 |
$this->view->setText($this->getValue('config')); |
| 54 |
|
| 55 |
// ensure config can be parsed... |
| 56 |
$this->view->getMetaData(); |
| 57 |
$this->view->getTree(); |
| 58 |
|
| 59 |
$cancel = $this->getElement('btn_submit_cancel'); |
| 60 |
$delete = $this->getElement('btn_submit_delete'); |
| 61 |
|
| 62 |
if ($this->getElement('btn_submit_save_file')->getValue() !== null) { |
| 63 |
// Store the view to its YAML file |
| 64 |
$this->viewconfig->storeToFile($this->view); |
| 65 |
Notification::success($this->translate('Top Level View successfully saved')); |
| 66 |
} elseif ($cancel !== null && $cancel->getValue() !== null) { |
| 67 |
// Clear the stored session data for the view |
| 68 |
$this->viewconfig->clearSession($this->view); |
| 69 |
Notification::success($this->translate('Top Level View restored from disk')); |
| 70 |
} elseif ($delete != null && $delete->getValue() !== null) { |
| 71 |
// Delete the view's YAML file |
| 72 |
$this->viewconfig->delete($this->view); |
| 73 |
$this->setRedirectUrl('toplevelview'); |
| 74 |
Notification::success($this->translate('Top Level View successfully deleted')); |
| 75 |
} else { |
| 76 |
// Store the view to the user's session by default |
| 77 |
$this->viewconfig->storeToSession($this->view); |
| 78 |
Notification::success($this->translate('Top Level View successfully saved for the current session')); |
| 79 |
} |
| 80 |
return true; |
| 81 |
} catch (Exception $e) { |
| 82 |
$this->error(sprintf('Could not save config: %s', $e->getMessage())); |
| 83 |
return false; |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
public function getRedirectUrl() |
| 88 |
{ |
| 89 |
if ($this->redirectUrl === null && ($name = $this->view->getName()) !== null) { |
| 90 |
$this->redirectUrl = Url::fromPath('toplevelview/show', ['name' => $name]); |
| 91 |
} |
| 92 |
return parent::getRedirectUrl(); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Populate form |
| 97 |
* |
| 98 |
* @see Form::onRequest() |
| 99 |
*/ |
| 100 |
public function onRequest() |
| 101 |
{ |
| 102 |
$values = []; |
| 103 |
$values['name'] = $this->view->getName(); |
| 104 |
$values['config'] = $this->view->getText(); |
| 105 |
|
| 106 |
$this->populate($values); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* @see Form::createElements() |
| 111 |
*/ |
| 112 |
public function createElements(array $formData) |
| 113 |
{ |
| 114 |
if ($this->view->hasBeenLoadedFromSession()) { |
| 115 |
$this->warning( |
| 116 |
$this->translate( |
| 117 |
'This config is only stored in your session!' |
| 118 |
. ' Make sure to save it to disk once your work is complete!' |
| 119 |
), |
| 120 |
false |
| 121 |
); |
| 122 |
} |
| 123 |
|
| 124 |
$this->addElement( |
| 125 |
'text', |
| 126 |
'name', |
| 127 |
[ |
| 128 |
'label' => $this->translate('File name'), |
| 129 |
'required' => true |
| 130 |
] |
| 131 |
); |
| 132 |
$this->addElement( |
| 133 |
'textarea', |
| 134 |
'config', |
| 135 |
[ |
| 136 |
'label' => $this->translate('YAML Config'), |
| 137 |
'class' => 'code-editor codemirror', |
| 138 |
'decorators' => [ |
| 139 |
['Label', ['tag' => 'div', 'separator' => '']], |
| 140 |
['HtmlTag', ['tag' => 'div']], |
| 141 |
'ViewHelper' |
| 142 |
], |
| 143 |
'data-codemirror-mode' => 'yaml' |
| 144 |
] |
| 145 |
); |
| 146 |
|
| 147 |
$this->addElement( |
| 148 |
'submit', |
| 149 |
'btn_submit_save_session', |
| 150 |
[ |
| 151 |
'ignore' => true, |
| 152 |
'label' => $this->translate('Save for the current Session'), |
| 153 |
'decorators' => ['ViewHelper'] |
| 154 |
] |
| 155 |
); |
| 156 |
|
| 157 |
$this->addElement( |
| 158 |
'submit', |
| 159 |
'btn_submit_save_file', |
| 160 |
[ |
| 161 |
'ignore' => true, |
| 162 |
'label' => $this->translate('Save to config file'), |
| 163 |
'decorators' => ['ViewHelper'] |
| 164 |
] |
| 165 |
); |
| 166 |
|
| 167 |
if ($this->view->hasBeenLoadedFromSession()) { |
| 168 |
$this->addElement( |
| 169 |
'submit', |
| 170 |
'btn_submit_cancel', |
| 171 |
[ |
| 172 |
'ignore' => true, |
| 173 |
'label' => $this->translate('Cancel editing'), |
| 174 |
'class' => 'btn-cancel', |
| 175 |
'decorators' => ['ViewHelper'] |
| 176 |
] |
| 177 |
); |
| 178 |
} |
| 179 |
|
| 180 |
if ($this->view->hasBeenLoaded()) { |
| 181 |
$this->addElement( |
| 182 |
'submit', |
| 183 |
'btn_submit_delete', |
| 184 |
[ |
| 185 |
'ignore' => true, |
| 186 |
'label' => $this->translate('Delete config'), |
| 187 |
'data-confirmation' => $this->translate('Confirm deletion'), |
| 188 |
'class' => 'btn-remove', |
| 189 |
'decorators' => ['ViewHelper'] |
| 190 |
] |
| 191 |
); |
| 192 |
} |
| 193 |
} |
| 194 |
} |