summaryrefslogtreecommitdiffstats
path: root/library/Reporting/Web/Forms/TemplateForm.php
diff options
context:
space:
mode:
Diffstat (limited to 'library/Reporting/Web/Forms/TemplateForm.php')
-rw-r--r--library/Reporting/Web/Forms/TemplateForm.php190
1 files changed, 107 insertions, 83 deletions
diff --git a/library/Reporting/Web/Forms/TemplateForm.php b/library/Reporting/Web/Forms/TemplateForm.php
index bb062bb..4cd44a9 100644
--- a/library/Reporting/Web/Forms/TemplateForm.php
+++ b/library/Reporting/Web/Forms/TemplateForm.php
@@ -1,23 +1,21 @@
<?php
+
// Icinga Reporting | (c) 2019 Icinga GmbH | GPLv2
namespace Icinga\Module\Reporting\Web\Forms;
+use Exception;
+use GuzzleHttp\Psr7\UploadedFile;
use Icinga\Authentication\Auth;
use Icinga\Module\Reporting\Database;
-use Icinga\Module\Reporting\Web\Forms\Decorator\CompatDecorator;
+use Icinga\Util\Json;
use ipl\Html\Contract\FormSubmitElement;
use ipl\Html\Html;
+use ipl\Html\HtmlDocument;
use ipl\Web\Compat\CompatForm;
-use reportingipl\Html\FormElement\FileElement;
class TemplateForm extends CompatForm
{
- use Database;
-
- /** @var bool Hack to disable the {@link onSuccess()} code upon deletion of the template */
- protected $callOnSuccess;
-
protected $template;
public function getTemplate()
@@ -25,126 +23,141 @@ class TemplateForm extends CompatForm
return $this->template;
}
- public function setTemplate($template)
+ /**
+ * Create a new form instance with the given report
+ *
+ * @param $template
+ *
+ * @return static
+ */
+ public static function fromTemplate($template): self
{
- $this->template = $template;
+ $form = new static();
+
+ $template->settings = Json::decode($template->settings, true);
+ $form->template = $template;
if ($template->settings) {
- $this->populate(array_filter($template->settings, function ($value) {
+ /** @var array<string, mixed> $settings */
+ $settings = $template->settings;
+ $form->populate(array_filter($settings, function ($value) {
// Don't populate files
return ! is_array($value);
}));
}
- return $this;
+ return $form;
}
- protected function assemble()
+ public function hasBeenSubmitted(): bool
{
- $this->setDefaultElementDecorator(new CompatDecorator());
+ return $this->hasBeenSent() && ($this->getPopulatedValue('submit') || $this->getPopulatedValue('remove'));
+ }
+ protected function assemble()
+ {
$this->setAttribute('enctype', 'multipart/form-data');
$this->add(Html::tag('h2', 'Template Settings'));
$this->addElement('text', 'name', [
- 'label' => 'Name',
- 'placeholder' => 'Template name',
+ 'label' => $this->translate('Name'),
+ 'placeholder' => $this->translate('Template name'),
'required' => true
]);
- $this->add(Html::tag('h2', 'Cover Page Settings'));
+ $this->add(Html::tag('h2', $this->translate('Cover Page Settings')));
- $this->addElement(new FileElement('cover_page_background_image', [
- 'label' => 'Background Image',
- 'accept' => 'image/png, image/jpeg'
- ]));
+ $this->addElement('file', 'cover_page_background_image', [
+ 'label' => $this->translate('Background Image'),
+ 'accept' => ['image/png', 'image/jpeg', 'image/jpg'],
+ 'destination' => sys_get_temp_dir()
+ ]);
- if ($this->template !== null
- && isset($this->template->settings['cover_page_background_image'])
+ if (
+ $this->template !== null
+ && isset($this->template->settings['cover_page_background_image'])
) {
$this->add(Html::tag(
'p',
- ['style' => ['margin-left: 14em;']],
- 'Upload a new background image to override the existing one'
+ ['class' => 'override-uploaded-file-hint'],
+ $this->translate('Upload a new background image to override the existing one')
));
$this->addElement('checkbox', 'remove_cover_page_background_image', [
- 'label' => 'Remove background image'
+ 'label' => $this->translate('Remove background image')
]);
}
- $this->addElement(new FileElement('cover_page_logo', [
- 'label' => 'Logo',
- 'accept' => 'image/png, image/jpeg'
- ]));
+ $this->addElement('file', 'cover_page_logo', [
+ 'label' => $this->translate('Logo'),
+ 'accept' => ['image/png', 'image/jpeg', 'image/jpg'],
+ 'destination' => sys_get_temp_dir()
+ ]);
- if ($this->template !== null
+ if (
+ $this->template !== null
&& isset($this->template->settings['cover_page_logo'])
) {
$this->add(Html::tag(
'p',
- ['style' => ['margin-left: 14em;']],
- 'Upload a new logo to override the existing one'
+ ['class' => 'override-uploaded-file-hint'],
+ $this->translate('Upload a new logo to override the existing one')
));
$this->addElement('checkbox', 'remove_cover_page_logo', [
- 'label' => 'Remove Logo'
+ 'label' => $this->translate('Remove Logo')
]);
}
$this->addElement('textarea', 'title', [
- 'label' => 'Title',
- 'placeholder' => 'Report title'
+ 'label' => $this->translate('Title'),
+ 'placeholder' => $this->translate('Report title')
]);
$this->addElement('text', 'color', [
- 'label' => 'Color',
- 'placeholder' => 'CSS color code'
+ 'label' => $this->translate('Color'),
+ 'placeholder' => $this->translate('CSS color code')
]);
- $this->add(Html::tag('h2', 'Header Settings'));
+ $this->add(Html::tag('h2', $this->translate('Header Settings')));
- $this->addColumnSettings('header_column1', 'Column 1');
- $this->addColumnSettings('header_column2', 'Column 2');
- $this->addColumnSettings('header_column3', 'Column 3');
+ $this->addColumnSettings('header_column1', $this->translate('Column 1'));
+ $this->addColumnSettings('header_column2', $this->translate('Column 2'));
+ $this->addColumnSettings('header_column3', $this->translate('Column 3'));
- $this->add(Html::tag('h2', 'Footer Settings'));
+ $this->add(Html::tag('h2', $this->translate('Footer Settings')));
- $this->addColumnSettings('footer_column1', 'Column 1');
- $this->addColumnSettings('footer_column2', 'Column 2');
- $this->addColumnSettings('footer_column3', 'Column 3');
+ $this->addColumnSettings('footer_column1', $this->translate('Column 1'));
+ $this->addColumnSettings('footer_column2', $this->translate('Column 2'));
+ $this->addColumnSettings('footer_column3', $this->translate('Column 3'));
$this->addElement('submit', 'submit', [
- 'label' => $this->template === null ? 'Create Template' : 'Update Template'
+ 'label' => $this->template === null
+ ? $this->translate('Create Template')
+ : $this->translate('Update Template')
]);
if ($this->template !== null) {
/** @var FormSubmitElement $removeButton */
$removeButton = $this->createElement('submit', 'remove', [
- 'label' => 'Remove Template',
+ 'label' => $this->translate('Remove Template'),
'class' => 'btn-remove',
'formnovalidate' => true
]);
$this->registerElement($removeButton);
- $this->getElement('submit')->getWrapper()->prepend($removeButton);
- if ($removeButton->hasBeenPressed()) {
- $this->getDb()->delete('template', ['id = ?' => $this->template->id]);
-
- // Stupid cheat because ipl/html is not capable of multiple submit buttons
- $this->getSubmitButton()->setValue($this->getSubmitButton()->getButtonLabel());
- $this->callOnSuccess = false;
- $this->valid = true;
-
- return;
- }
+ /** @var HtmlDocument $wrapper */
+ $wrapper = $this->getElement('submit')->getWrapper();
+ $wrapper->prepend($removeButton);
}
}
public function onSuccess()
{
- if ($this->callOnSuccess === false) {
+ if ($this->getPopulatedValue('remove')) {
+ Database::get()->delete('template', ['id = ?' => $this->template->id]);
+
return;
}
@@ -153,20 +166,17 @@ class TemplateForm extends CompatForm
$settings = $this->getValues();
try {
- /** @var $uploadedFile \GuzzleHttp\Psr7\UploadedFile */
- foreach ($this->getRequest()->getUploadedFiles() as $name => $uploadedFile) {
- if ($uploadedFile->getError() === UPLOAD_ERR_NO_FILE) {
- continue;
+ foreach ($settings as $name => $setting) {
+ if ($setting instanceof UploadedFile) {
+ $settings[$name] = [
+ 'mime_type' => $setting->getClientMediaType(),
+ 'size' => $setting->getSize(),
+ 'content' => base64_encode((string) $setting->getStream())
+ ];
}
-
- $settings[$name] = [
- 'mime_type' => $uploadedFile->getClientMediaType(),
- 'size' => $uploadedFile->getSize(),
- 'content' => base64_encode((string) $uploadedFile->getStream())
- ];
}
- $db = $this->getDb();
+ $db = Database::get();
$now = time() * 1000;
@@ -179,19 +189,21 @@ class TemplateForm extends CompatForm
'mtime' => $now
]);
} else {
- if (isset($settings['remove_cover_page_background_image'])) {
+ if ($this->getValue('remove_cover_page_background_image', 'n') === 'y') {
unset($settings['cover_page_background_image']);
unset($settings['remove_cover_page_background_image']);
- } elseif (! isset($settings['cover_page_background_image'])
+ } elseif (
+ ! isset($settings['cover_page_background_image'])
&& isset($this->template->settings['cover_page_background_image'])
) {
$settings['cover_page_background_image'] = $this->template->settings['cover_page_background_image'];
}
- if (isset($settings['remove_cover_page_logo'])) {
+ if ($this->getValue('remove_cover_page_logo', 'n') === 'y') {
unset($settings['cover_page_logo']);
unset($settings['remove_cover_page_logo']);
- } elseif (! isset($settings['cover_page_logo'])
+ } elseif (
+ ! isset($settings['cover_page_logo'])
&& isset($this->template->settings['cover_page_logo'])
) {
$settings['cover_page_logo'] = $this->template->settings['cover_page_logo'];
@@ -204,7 +216,8 @@ class TemplateForm extends CompatForm
if ($settings[$type] === 'image') {
$value = "{$headerOrFooter}_column{$i}_value";
- if (! isset($settings[$value])
+ if (
+ ! isset($settings[$value])
&& isset($this->template->settings[$value])
) {
$settings[$value] = $this->template->settings[$value];
@@ -219,7 +232,7 @@ class TemplateForm extends CompatForm
'mtime' => $now
], ['id = ?' => $this->template->id]);
}
- } catch (\Exception $e) {
+ } catch (Exception $e) {
die($e->getMessage());
}
}
@@ -240,20 +253,31 @@ class TemplateForm extends CompatForm
]
]);
+ $valueType = $this->getValue($type, 'none');
+ $populated = $this->getPopulatedValue($value);
+ if (
+ ($valueType === 'image' && ! $populated instanceof UploadedFile)
+ || ($valueType !== 'image' && $populated instanceof UploadedFile)
+ ) {
+ $this->clearPopulatedValue($value);
+ }
+
switch ($this->getValue($type, 'none')) {
case 'image':
- $this->addElement(new FileElement($value, [
- 'label' => 'Image',
- 'accept' => 'image/png, image/jpeg'
- ]));
+ $this->addElement('file', $value, [
+ 'label' => 'Image',
+ 'accept' => ['image/png', 'image/jpeg', 'image/jpg'],
+ 'destination' => sys_get_temp_dir()
+ ]);
- if ($this->template !== null
+ if (
+ $this->template !== null
&& $this->template->settings[$type] === 'image'
&& isset($this->template->settings[$value])
) {
$this->add(Html::tag(
'p',
- ['style' => ['margin-left: 14em;']],
+ ['class' => 'override-uploaded-file-hint'],
'Upload a new image to override the existing one'
));
}
@@ -270,7 +294,7 @@ class TemplateForm extends CompatForm
'page_of' => 'Page Number + Total Number of Pages',
'date' => 'Date'
],
- 'value' => 'report_title'
+ 'value' => 'report_title'
]);
break;
case 'text':