diff options
Diffstat (limited to 'application/controllers/ReportController.php')
-rw-r--r-- | application/controllers/ReportController.php | 216 |
1 files changed, 177 insertions, 39 deletions
diff --git a/application/controllers/ReportController.php b/application/controllers/ReportController.php index 090c759..0a694f2 100644 --- a/application/controllers/ReportController.php +++ b/application/controllers/ReportController.php @@ -1,55 +1,137 @@ <?php + // Icinga Reporting | (c) 2018 Icinga GmbH | GPLv2 namespace Icinga\Module\Reporting\Controllers; -use GuzzleHttp\Psr7\ServerRequest; +use Exception; use Icinga\Application\Hook; use Icinga\Module\Pdfexport\ProvidedHook\Pdfexport; use Icinga\Module\Reporting\Database; +use Icinga\Module\Reporting\Model; use Icinga\Module\Reporting\Report; use Icinga\Module\Reporting\Web\Controller; use Icinga\Module\Reporting\Web\Forms\ReportForm; use Icinga\Module\Reporting\Web\Forms\ScheduleForm; use Icinga\Module\Reporting\Web\Forms\SendForm; use Icinga\Module\Reporting\Web\Widget\CompatDropdown; +use Icinga\Web\Notification; use ipl\Html\Error; +use ipl\Html\HtmlElement; +use ipl\Stdlib\Filter; use ipl\Web\Url; use ipl\Web\Widget\ActionBar; use Icinga\Util\Environment; +use ipl\Web\Widget\ActionLink; class ReportController extends Controller { - use Database; - /** @var Report */ protected $report; public function init() { - $this->report = Report::fromDb($this->params->getRequired('id')); + $reportId = $this->params->getRequired('id'); + + /** @var Model\Report $report */ + $report = Model\Report::on(Database::get()) + ->with(['timeframe']) + ->filter(Filter::equal('id', $reportId)) + ->first(); + + if ($report === null) { + $this->httpNotFound($this->translate('Report not found')); + } + + $this->report = Report::fromModel($report); } public function indexAction() { $this->addTitleTab($this->report->getName()); + $this->controls->getAttributes()->add('class', 'default-layout'); $this->addControl($this->assembleActions()); + if ($this->isXhr()) { + /** @var string $contentId */ + $contentId = $this->content->getAttributes()->get('id')->getValue(); + $this->sendExtraUpdates([ + $contentId => Url::fromPath('reporting/report/content', ['id' => $this->report->getId()]) + ]); + + // Will be replaced once the report content is rendered + $this->addContent(new HtmlElement('div')); + } else { + Environment::raiseExecutionTime(); + Environment::raiseMemoryLimit(); + + try { + $this->addContent($this->report->toHtml()); + } catch (Exception $e) { + $this->addContent(Error::show($e)); + } + } + } + + public function contentAction(): void + { Environment::raiseExecutionTime(); Environment::raiseMemoryLimit(); + $this->view->compact = true; + $this->_helper->layout()->disableLayout(); + try { - $this->addContent($this->report->toHtml()); - } catch (\Exception $e) { - $this->addContent(Error::show($e)); + $this->getDocument()->addHtml($this->report->toHtml()); + } catch (Exception $e) { + $this->getDocument()->addHtml(Error::show($e)); + } + } + + public function cloneAction() + { + $this->assertPermission('reporting/reports'); + $this->addTitleTab($this->translate('Clone Report')); + + $values = ['timeframe' => (string) $this->report->getTimeframe()->getId()]; + + $reportlet = $this->report->getReportlets()[0]; + + $values['reportlet'] = $reportlet->getClass(); + + foreach ($reportlet->getConfig() as $name => $value) { + if ($name === 'name') { + if (preg_match('/(?:Clone )(\d+)$/', $value, $m)) { + $value = preg_replace('/\d+$/', (string) ((int) $m[1] + 1), $value); + } else { + $value .= ' Clone 1'; + } + } + + $values[$name] = $value; } + + $form = (new ReportForm()) + ->setSubmitButtonLabel($this->translate('Clone Report')) + ->setAction((string) Url::fromRequest()) + ->populate($values) + ->on(ReportForm::ON_SUCCESS, function (ReportForm $form) { + Notification::success($this->translate('Cloned report successfully')); + + $this->sendExtraUpdates(['#col1']); + + $this->redirectNow(Url::fromPath('reporting/report', ['id' => $form->getId()])); + }) + ->handleRequest($this->getServerRequest()); + + $this->addContent($form); } public function editAction() { $this->assertPermission('reporting/reports'); - $this->addTitleTab('Edit Report'); + $this->addTitleTab($this->translate('Edit Report')); $values = [ 'name' => $this->report->getName(), @@ -66,29 +148,44 @@ class ReportController extends Controller $values[$name] = $value; } - $form = new ReportForm(); - $form->setId($this->report->getId()); - $form->populate($values); - $form->handleRequest(ServerRequest::fromGlobals()); - - $this->redirectForm($form, 'reporting/reports'); + $form = ReportForm::fromId($this->report->getId()) + ->setAction((string) Url::fromRequest()) + ->populate($values) + ->on(ReportForm::ON_SUCCESS, function (ReportForm $form) { + $pressedButton = $form->getPressedSubmitElement(); + if ($pressedButton && $pressedButton->getName() === 'remove') { + Notification::success($this->translate('Removed report successfully')); + + $this->switchToSingleColumnLayout(); + } else { + Notification::success($this->translate('Updated report successfully')); + + $this->closeModalAndRefreshRemainingViews( + Url::fromPath('reporting/report', ['id' => $this->report->getId()]) + ); + } + }) + ->handleRequest($this->getServerRequest()); $this->addContent($form); } public function sendAction() { - $this->addTitleTab('Send Report'); + $this->addTitleTab($this->translate('Send Report')); Environment::raiseExecutionTime(); Environment::raiseMemoryLimit(); - $form = new SendForm(); - $form + $form = (new SendForm()) ->setReport($this->report) - ->handleRequest(ServerRequest::fromGlobals()); - - $this->redirectForm($form, "reporting/report?id={$this->report->getId()}"); + ->setAction((string) Url::fromRequest()) + ->on(SendForm::ON_SUCCESS, function () { + $this->closeModalAndRefreshRelatedView( + Url::fromPath('reporting/report', ['id' => $this->report->getId()]) + ); + }) + ->handleRequest($this->getServerRequest()); $this->addContent($form); } @@ -96,16 +193,38 @@ class ReportController extends Controller public function scheduleAction() { $this->assertPermission('reporting/schedules'); - $this->addTitleTab('Schedule'); - - $form = new ScheduleForm(); - $form - ->setReport($this->report) - ->handleRequest(ServerRequest::fromGlobals()); - - $this->redirectForm($form, "reporting/report?id={$this->report->getId()}"); + $this->addTitleTab($this->translate('Schedule')); + + $form = ScheduleForm::fromReport($this->report); + $form->setAction((string) Url::fromRequest()) + ->on(ScheduleForm::ON_SUCCESS, function () use ($form) { + $pressedButton = $form->getPressedSubmitElement(); + if ($pressedButton) { + $pressedButton = $pressedButton->getName(); + } + + if ($pressedButton === 'remove') { + Notification::success($this->translate('Removed schedule successfully')); + } elseif ($pressedButton === 'send') { + Notification::success($this->translate('Report sent successfully')); + } elseif ($this->report->getSchedule() !== null) { + Notification::success($this->translate('Updated schedule successfully')); + } else { + Notification::success($this->translate('Created schedule successfully')); + } + + $this->closeModalAndRefreshRelatedView( + Url::fromPath('reporting/report', ['id' => $this->report->getId()]) + ); + }) + ->handleRequest($this->getServerRequest()); $this->addContent($form); + + $parts = $form->getPartUpdates(); + if (! empty($parts)) { + $this->sendMultipartUpdate(...$parts); + } } public function downloadAction() @@ -124,8 +243,9 @@ class ReportController extends Controller switch ($type) { case 'pdf': - /** @var Hook\PdfexportHook */ - Pdfexport::first()->streamPdfFromHtml($this->report->toPdf(), $name); + /** @var Hook\PdfexportHook $exports */ + $exports = Pdfexport::first(); + $exports->streamPdfFromHtml($this->report->toPdf(), $name); exit; case 'csv': $response = $this->getResponse(); @@ -184,24 +304,42 @@ class ReportController extends Controller $actions = new ActionBar(); if ($this->hasPermission('reporting/reports')) { - $actions->addLink( - 'Modify', - Url::fromPath('reporting/report/edit', ['id' => $reportId]), - 'edit' + $actions->addHtml( + (new ActionLink( + $this->translate('Modify'), + Url::fromPath('reporting/report/edit', ['id' => $reportId]), + 'edit' + ))->openInModal() + ); + + $actions->addHtml( + (new ActionLink( + $this->translate('Clone'), + Url::fromPath('reporting/report/clone', ['id' => $reportId]), + 'clone' + ))->openInModal() ); } if ($this->hasPermission('reporting/schedules')) { - $actions->addLink( - 'Schedule', - Url::fromPath('reporting/report/schedule', ['id' => $reportId]), - 'calendar-empty' + $actions->addHtml( + (new ActionLink( + $this->translate('Schedule'), + Url::fromPath('reporting/report/schedule', ['id' => $reportId]), + 'calendar-empty' + ))->openInModal() ); } $actions ->add($download) - ->addLink('Send', Url::fromPath('reporting/report/send', ['id' => $reportId]), 'forward'); + ->addHtml( + (new ActionLink( + $this->translate('Send'), + Url::fromPath('reporting/report/send', ['id' => $reportId]), + 'forward' + ))->openInModal() + ); return $actions; } |