1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
<?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()
{
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', [
'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';
}
/**
* @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());
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->beforeSuccessfulRedirect();
$this->redirectOnSuccess($this->translate('Basket has been uploaded'));
}
}
|