summaryrefslogtreecommitdiffstats
path: root/application/forms/BasketUploadForm.php
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-14 13:17:31 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-14 13:17:31 +0000
commitf66ab8dae2f3d0418759f81a3a64dc9517a62449 (patch)
treefbff2135e7013f196b891bbde54618eb050e4aaf /application/forms/BasketUploadForm.php
parentInitial commit. (diff)
downloadicingaweb2-module-director-f66ab8dae2f3d0418759f81a3a64dc9517a62449.tar.xz
icingaweb2-module-director-f66ab8dae2f3d0418759f81a3a64dc9517a62449.zip
Adding upstream version 1.10.2.upstream/1.10.2
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'application/forms/BasketUploadForm.php')
-rw-r--r--application/forms/BasketUploadForm.php147
1 files changed, 147 insertions, 0 deletions
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 @@
+<?php
+
+namespace Icinga\Module\Director\Forms;
+
+use Exception;
+use Icinga\Exception\IcingaException;
+use Icinga\Module\Director\Core\Json;
+use Icinga\Module\Director\DirectorObject\Automation\Basket;
+use Icinga\Module\Director\DirectorObject\Automation\BasketSnapshot;
+use Icinga\Module\Director\Web\Form\DirectorObjectForm;
+use Icinga\Web\Notification;
+
+class BasketUploadForm extends DirectorObjectForm
+{
+ protected $listUrl = 'director/baskets';
+
+ protected $failed;
+
+ protected $upload;
+
+ protected $rawUpload;
+
+ /**
+ * @throws \Zend_Form_Exception
+ */
+ public function setup()
+ {
+ $this->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'));
+ }
+}