Skip to content

Commit

Permalink
Notification preview
Browse files Browse the repository at this point in the history
  • Loading branch information
rimi-itk committed Jun 5, 2023
1 parent c7d9cd3 commit c851d38
Show file tree
Hide file tree
Showing 6 changed files with 389 additions and 68 deletions.
16 changes: 16 additions & 0 deletions web/sites/default/modules/os2forms_forloeb/os2forms_forloeb.module
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,22 @@ function os2forms_forloeb_mail_alter(&$message) {
* Implements hook_theme().
*/
function os2forms_forloeb_theme(array &$variables) {
$theme['os2forms_forloeb_notification_preview'] = [
'variables' => [
'webform' => NULL,
'handler' => NULL,
'type' => NULL,
'submission' => NULL,
'return_url' => NULL,
'render_url' => NULL,
'preview_urls' => [
'prev' => NULL,
'self' => NULL,
'next' => NULL,
],
],
];

$theme['os2forms_forloeb_notification_message_email_html'] = [
'variables' => [
'message' => [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,37 @@ os2forms_forloeb.settings:
_title: 'OS2Forms forløb'
requirements:
_permission: 'administer site configuration'

os2forms_forloeb.meastro_notification.preview:
path: '/admin/structure/webform/manage/{webform}/os2forms_forloeb/{handler}/preview/{content_type}'
defaults:
_controller: '\Drupal\os2forms_forloeb\Controller\MaestroNotificationController::preview'
_title: 'Maestro notification preview'
options:
parameters:
webform:
type: 'entity:webform'
requirements:
_permission: 'view any webform submission'

os2forms_forloeb.meastro_notification.preview_render:
path: '/admin/structure/webform/manage/{webform}/os2forms_forloeb/{handler}/preview/{content_type}/render/{submission}'
defaults:
_controller: '\Drupal\os2forms_forloeb\Controller\MaestroNotificationController::previewRender'
_title: 'Maestro notification render preview'
options:
parameters:
webform:
type: 'entity:webform'
submission:
type: 'entity:webform_submission'
requirements:
_permission: 'view any webform submission'

os2forms_forloeb.meastro_notification.preview_message:
path: '/os2forms_forloeb/notification/message'
defaults:
_controller: '\Drupal\os2forms_forloeb\Controller\MaestroNotificationController::message'
_title: 'Maestro notification message'
requirements:
_permission: 'view any webform submission'
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php

namespace Drupal\os2forms_forloeb\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Drupal\os2forms_digital_post\Model\Document;
use Drupal\os2forms_forloeb\MaestroHelper;
use Drupal\webform\WebformInterface;
use Drupal\webform\WebformSubmissionInterface;
use Drupal\webform\WebformSubmissionStorageInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

/**
* Maestro notification controller.
*/
class MaestroNotificationController extends ControllerBase {

/**
* Constructor.
*/
public function __construct(
readonly private WebformSubmissionStorageInterface $webformSubmissionStorage,
readonly private MaestroHelper $maestroHelper
) {
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('entity_type.manager')->getStorage('webform_submission'),
$container->get(MaestroHelper::class)
);
}

/**
* Preview action.
*/
public function preview(Request $request, WebformInterface $webform, string $handler, string $content_type, RouteMatchInterface $routeMatch) {
$submissionIds = array_keys($this->webformSubmissionStorage->getQuery()
->condition('webform_id', $webform->id())
->sort('created', 'DESC')
->execute());
$currentSubmission = (int) $request->query->get('submission');
$index = array_search($currentSubmission, $submissionIds);
if (FALSE === $index) {
$currentSubmission = reset($submissionIds) ?: NULL;
$index = array_search($currentSubmission, $submissionIds);
}

$previewUrls = array_map(
static fn ($submission) => Url::fromRoute('os2forms_forloeb.meastro_notification.preview', [
'webform' => $webform->id(),
'handler' => $handler,
'content_type' => $content_type,
'submission' => $submission,
]),
array_filter([
'prev' => $submissionIds[$index + 1] ?? NULL,
'self' => $currentSubmission,
'next' => $submissionIds[$index - 1] ?? NULL,
])
);

$renderUrl = NULL !== $currentSubmission
? Url::fromRoute('os2forms_forloeb.meastro_notification.preview_render', [
'webform' => $webform->id(),
'handler' => $handler,
'content_type' => $content_type,
'submission' => $currentSubmission,
])
: NULL;

return [
'#theme' => 'os2forms_forloeb_notification_preview',
'#webform' => $webform,
'#handler' => $handler,
'#type' => $content_type,
'#submission' => $currentSubmission,
'#return_url' => $webform->toUrl('handlers'),
'#render_url' => $renderUrl,
'#preview_urls' => $previewUrls,
];
}

/**
* Render notification preview.
*/
public function previewRender(Request $request, WebformInterface $webform, string $handler, string $content_type, WebformSubmissionInterface $submission) {
$templateTask = [];
$maestroQueueID = 0;
[$content, $contentType] = $this->maestroHelper->renderNotification($submission, $handler, $templateTask, $maestroQueueID, $content_type);

$response = new Response($content);
if ('pdf' === $contentType) {
$response->headers->set('content-type', Document::MIME_TYPE_PDF);
}

return $response;
}

/**
* Message action.
*/
public function message(Request $request): Response {
$content[] = '<h1>' . $request->get('message') . '</h1>';
if ($referer = $request->headers->get('referer')) {
$content[] = sprintf('<a href="%s">Go back</a>', $referer);
}

return new Response(implode(PHP_EOL, $content));
}

}
Loading

0 comments on commit c851d38

Please sign in to comment.