|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Drupal\os2forms_fordelingskomponent\Controller; |
| 6 | + |
| 7 | +use Drupal\Core\Controller\ControllerBase; |
| 8 | +use Drupal\Core\Entity\EntityTypeManagerInterface; |
| 9 | +use Drupal\Core\Url; |
| 10 | +use Drupal\webform\WebformInterface; |
| 11 | +use Drupal\webform\WebformSubmissionStorageInterface; |
| 12 | +use Symfony\Component\HttpFoundation\Request; |
| 13 | +use Symfony\Component\HttpFoundation\Response; |
| 14 | + |
| 15 | +/** |
| 16 | + * Returns responses for Fordelingskomponent routes. |
| 17 | + */ |
| 18 | +final class Os2formsFordelingskomponentPayloadPreviewController extends ControllerBase { |
| 19 | + |
| 20 | + /** |
| 21 | + * The webform submission storage. |
| 22 | + */ |
| 23 | + private WebformSubmissionStorageInterface $submissionStorage; |
| 24 | + |
| 25 | + public function __construct( |
| 26 | + EntityTypeManagerInterface $entityTypeManager, |
| 27 | + ) { |
| 28 | + $this->submissionStorage = $entityTypeManager->getStorage('webform_submission'); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Builds the response. |
| 33 | + */ |
| 34 | + public function __invoke(Request $request, WebformInterface $webform, string $handler): array|Response { |
| 35 | + $handler = $webform->getHandler($handler); |
| 36 | + $submissionIds = array_keys($this->submissionStorage->getQuery() |
| 37 | + ->accessCheck() |
| 38 | + ->condition('webform_id', $webform->id()) |
| 39 | + ->sort('created', 'DESC') |
| 40 | + ->execute()); |
| 41 | + $currentSubmission = (int) $request->query->get('submission'); |
| 42 | + $index = array_search($currentSubmission, $submissionIds); |
| 43 | + if (FALSE === $index) { |
| 44 | + $currentSubmission = reset($submissionIds) ?: NULL; |
| 45 | + $index = array_search($currentSubmission, $submissionIds); |
| 46 | + } |
| 47 | + |
| 48 | + $previewUrls = array_map( |
| 49 | + static fn($submission) => Url::fromRoute('os2forms_fordelingskomponent.fordelingskomponent_payload.preview', [ |
| 50 | + 'webform' => $webform->id(), |
| 51 | + 'handler' => $handler->getHandlerId(), |
| 52 | + 'submission' => $submission, |
| 53 | + ]), |
| 54 | + array_filter([ |
| 55 | + 'prev' => $submissionIds[$index + 1] ?? NULL, |
| 56 | + 'self' => $currentSubmission, |
| 57 | + 'next' => $submissionIds[$index - 1] ?? NULL, |
| 58 | + ]) |
| 59 | + ); |
| 60 | + |
| 61 | + $renderUrl = NULL !== $currentSubmission |
| 62 | + ? Url::fromRoute('os2forms_fordelingskomponent.fordelingskomponent_payload.preview_render', [ |
| 63 | + 'webform' => $webform->id(), |
| 64 | + 'handler' => $handler->getHandlerId(), |
| 65 | + 'submission' => $currentSubmission, |
| 66 | + ]) |
| 67 | + : NULL; |
| 68 | + |
| 69 | + return [ |
| 70 | + '#theme' => 'os2forms_fordelingskomponent_payload_preview', |
| 71 | + '#webform' => $webform, |
| 72 | + '#handler' => $handler, |
| 73 | + '#submission' => $currentSubmission, |
| 74 | + '#return_url' => $webform->toUrl('handlers'), |
| 75 | + '#render_url' => $renderUrl, |
| 76 | + '#preview_urls' => $previewUrls, |
| 77 | + ]; |
| 78 | + |
| 79 | + } |
| 80 | + |
| 81 | +} |
0 commit comments