From f66ab8dae2f3d0418759f81a3a64dc9517a62449 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 14 Apr 2024 15:17:31 +0200 Subject: Adding upstream version 1.10.2. Signed-off-by: Daniel Baumann --- application/forms/BasketUploadForm.php | 147 +++++++++++++++++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 application/forms/BasketUploadForm.php (limited to 'application/forms/BasketUploadForm.php') diff --git a/application/forms/BasketUploadForm.php b/application/forms/BasketUploadForm.php new file mode 100644 index 0000000..a88dc06 --- /dev/null +++ b/application/forms/BasketUploadForm.php @@ -0,0 +1,147 @@ +addElement('text', 'basket_name', [ + 'label' => $this->translate('Basket Name'), + 'required' => true, + ]); + $this->setAttrib('enctype', 'multipart/form-data'); + + $this->addElement('file', 'uploaded_file', [ + 'label' => $this->translate('Choose file'), + 'destination' => $this->getTempDir(), + 'valueDisabled' => true, + 'isArray' => false, + 'multiple' => false, + 'ignore' => true, + ]); + + $this->setSubmitLabel($this->translate('Upload')); + } + + protected function getTempDir() + { + return sys_get_temp_dir(); + } + + protected function getObjectClassname() + { + 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 + */ + protected function processUploadedSource() + { + if (! array_key_exists('uploaded_file', $_FILES)) { + throw new IcingaException('Got no file'); + } + + if (! isset($_FILES['uploaded_file']['tmp_name']) + || ! is_uploaded_file($_FILES['uploaded_file']['tmp_name']) + ) { + $this->addError('Got no uploaded file'); + $this->failed = true; + + return false; + } + $tmpFile = $_FILES['uploaded_file']['tmp_name']; + $originalFilename = $_FILES['uploaded_file']['name']; + + $source = file_get_contents($tmpFile); + unlink($tmpFile); + try { + $json = Json::decode($source); + $this->rawUpload = $source; + $this->upload = $json; + } catch (Exception $e) { + $this->addError($originalFilename . ' failed: ' . $e->getMessage()); + Notification::error($originalFilename . ' failed: ' . $e->getMessage()); + $this->failed = true; + + return false; + } + + return true; + } + + public function onRequest() + { + if ($this->hasBeenSent()) { + try { + $this->processUploadedSource(); + } catch (Exception $e) { + $this->addError($e->getMessage()); + return; + } + } + } + + /** + * @throws \Icinga\Module\Director\Exception\DuplicateKeyException + */ + public function onSuccess() + { + /** @var Basket $basket */ + $basket = $this->object(); + + foreach ($this->upload as $type => $content) { + if ($type !== 'Datafield') { + $basket->addObjects($type, array_keys((array) $content)); + } + } + if ($basket->isEmpty()) { + $this->addError($this->translate("It's not allowed to store an empty basket")); + + return; + } + + $basket->set('owner_type', 'user'); + $basket->set('owner_value', $this->getAuth()->getUser()->getUsername()); + $basket->store($this->db); + + BasketSnapshot::forBasketFromJson( + $basket, + $this->rawUpload + )->store($this->db); + $this->setObjectSuccessUrl(); + $this->beforeSuccessfulRedirect(); + $this->redirectOnSuccess($this->translate('Basket has been uploaded')); + } +} -- cgit v1.2.3