BasketUploadForm: allow uploading snapshots...

...for existing Baskets

fixes #1952
This commit is contained in:
Thomas Gelf 2023-03-11 09:36:26 +01:00
parent 749ab6a356
commit 62bff81fac
2 changed files with 38 additions and 16 deletions

View File

@ -125,6 +125,26 @@ class BasketController extends ActionController
$this->content()->add($form);
}
public function uploadSnapshotAction()
{
$basket = Basket::load($this->params->get('name'), $this->db());
$this->actions()->add(
Link::create(
$this->translate('back'),
'director/basket/snapshots',
['name' => $basket->get('basket_name')],
['class' => 'icon-left-big']
)
);
$this->basketTabs()->activate('snapshots');
$this->addTitle($this->translate('Upload a Configuration Basket Snapshot'));
$form = (new BasketUploadForm())
->setObject($basket)
->setDb($this->db())
->handleRequest();
$this->content()->add($form);
}
/**
* @throws \Icinga\Exception\NotFoundError
*/
@ -145,6 +165,12 @@ class BasketController extends ActionController
$basket->get('basket_name')
));
$this->basketTabs()->activate('snapshots');
$this->actions()->add(Link::create(
$this->translate('Upload'),
'director/basket/upload-snapshot',
['name' => $basket->get('basket_name')],
['class' => 'icon-upload']
));
}
if ($basket !== null) {
$this->content()->add(

View File

@ -25,10 +25,12 @@ class BasketUploadForm extends DirectorObjectForm
*/
public function setup()
{
if ($this->object === null) {
$this->addElement('text', 'basket_name', [
'label' => $this->translate('Basket Name'),
'required' => true,
]);
}
$this->setAttrib('enctype', 'multipart/form-data');
$this->addElement('file', 'uploaded_file', [
@ -53,16 +55,6 @@ class BasketUploadForm extends DirectorObjectForm
return '\\Icinga\\Module\\Director\\DirectorObject\\Automation\\Basket';
}
protected function setObjectSuccessUrl()
{
/** @var Basket $basket */
$basket = $this->object();
$this->setSuccessUrl(
'director/basket',
['name' => $basket->get('basket_name')]
);
}
/**
* @return bool
* @throws IcingaException
@ -134,13 +126,17 @@ class BasketUploadForm extends DirectorObjectForm
$basket->set('owner_type', 'user');
$basket->set('owner_value', $this->getAuth()->getUser()->getUsername());
if ($basket->hasBeenLoadedFromDb()) {
$this->setSuccessUrl('director/basket/snapshots', ['name' => $basket->get('basket_name')]);
} else {
$this->setSuccessUrl('director/basket', ['name' => $basket->get('basket_name')]);
$basket->store($this->db);
}
BasketSnapshot::forBasketFromJson(
$basket,
$this->rawUpload
)->store($this->db);
$this->setObjectSuccessUrl();
$this->beforeSuccessfulRedirect();
$this->redirectOnSuccess($this->translate('Basket has been uploaded'));
}