|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Drupal\os2forms_selvbetjening\Command; |
| 4 | + |
| 5 | +use Drupal\Core\Database\Connection; |
| 6 | +use Drupal\Core\Entity\EntityTypeManagerInterface; |
| 7 | +use Drupal\Core\Session\AccountInterface; |
| 8 | +use Drupal\webform\WebformEntityStorageInterface; |
| 9 | +use Drupal\webform\WebformSubmissionStorageInterface; |
| 10 | +use Drupal\webform_revisions\Entity\WebformRevisions; |
| 11 | +use Drush\Commands\DrushCommands; |
| 12 | +use Symfony\Component\Yaml\Yaml; |
| 13 | + |
| 14 | +/** |
| 15 | + * Webform submission commands. |
| 16 | + */ |
| 17 | +class WebformSubmissionCommands extends DrushCommands { |
| 18 | + /** |
| 19 | + * The webform storage. |
| 20 | + * |
| 21 | + * @var \Drupal\webform\WebformEntityStorageInterface |
| 22 | + */ |
| 23 | + private readonly WebformEntityStorageInterface $webformStorage; |
| 24 | + |
| 25 | + /** |
| 26 | + * The webform submission storage. |
| 27 | + * |
| 28 | + * @var \Drupal\webform\WebformSubmissionStorageInterface |
| 29 | + */ |
| 30 | + private readonly WebformSubmissionStorageInterface $webformSubmissionStorage; |
| 31 | + |
| 32 | + /** |
| 33 | + * Constructor. |
| 34 | + */ |
| 35 | + public function __construct( |
| 36 | + EntityTypeManagerInterface $entityTypeManager, |
| 37 | + private readonly Connection $database, |
| 38 | + private readonly AccountInterface $currentUser |
| 39 | + ) { |
| 40 | + $this->webformStorage = $entityTypeManager->getStorage('webform'); |
| 41 | + $this->webformSubmissionStorage = $entityTypeManager->getStorage('webform_submission'); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * List webforms revisions. |
| 46 | + * |
| 47 | + * @command os2forms_selvbetjening:webform:list-revisions |
| 48 | + */ |
| 49 | + public function listWebformRevisions() { |
| 50 | + /** @var \Drupal\webform\WebformInterface[] $webforms */ |
| 51 | + $webforms = $this->webformStorage->loadMultiple(); |
| 52 | + |
| 53 | + $userInfo = sprintf('%s (#%s)', $this->currentUser->getAccountName(), $this->currentUser->id()); |
| 54 | + foreach ($webforms as $webform) { |
| 55 | + $this->writeln( |
| 56 | + Yaml::dump([ |
| 57 | + 'id' => $webform->id(), |
| 58 | + 'label' => $webform->label(), |
| 59 | + 'revisionId' => $webform instanceof WebformRevisions ? $webform->getRevisionId() : '👻', |
| 60 | + 'current user' => $userInfo, |
| 61 | + ]) |
| 62 | + ); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * List stray submissions. |
| 68 | + * |
| 69 | + * List submission where the loaded webform does not match the expected webform. |
| 70 | + * |
| 71 | + * @param string $webformId |
| 72 | + * The webform id. Use '*' to list check all webforms. |
| 73 | + * @param array $options |
| 74 | + * The command options. |
| 75 | + * |
| 76 | + * @option bool fix |
| 77 | + * Fix webform revision on stray submissions. |
| 78 | + * |
| 79 | + * @command os2forms_selvbetjening:webform-submission:list-stray-submissions |
| 80 | + * |
| 81 | + * @phpstan-param array<string, mixed> $options |
| 82 | + */ |
| 83 | + public function listStraySubmissions(string $webformId, array $options = [ |
| 84 | + 'fix' => FALSE, |
| 85 | + ]) { |
| 86 | + $webforms = '*' === $webformId |
| 87 | + ? $this->webformStorage->loadMultiple() |
| 88 | + : $this->webformStorage->loadMultiple([$webformId]); |
| 89 | + |
| 90 | + foreach ($webforms as $webform) { |
| 91 | + /** @var \Drupal\webform\WebformSubmissionInterface[] $submissions */ |
| 92 | + $submissions = $this->webformSubmissionStorage->loadByProperties([ |
| 93 | + 'webform_id' => $webform->id(), |
| 94 | + ]); |
| 95 | + |
| 96 | + foreach ($submissions as $submission) { |
| 97 | + if ($webform->id() !== $submission->getWebform()->id()) { |
| 98 | + $this->writeln( |
| 99 | + Yaml::dump([ |
| 100 | + 'submission' => [ |
| 101 | + 'id' => $submission->id(), |
| 102 | + 'webform.id' => $submission->getWebform()->id(), |
| 103 | + 'data' => $submission->getData(), |
| 104 | + ], |
| 105 | + 'webform' => [ |
| 106 | + 'id' => $webform->id(), |
| 107 | + 'revisionId' => $webform instanceof WebformRevisions ? $webform->getRevisionId() : '👻', |
| 108 | + ], |
| 109 | + ], PHP_INT_MAX) |
| 110 | + ); |
| 111 | + |
| 112 | + if ($options['fix']) { |
| 113 | + $this->database->update('webform_submission') |
| 114 | + ->fields([ |
| 115 | + 'webform_revision' => NULL, |
| 116 | + ]) |
| 117 | + ->condition('sid', $submission->id()) |
| 118 | + ->execute(); |
| 119 | + |
| 120 | + $this->writeln(sprintf('Fixed webform revision on submission %s', $submission->id())); |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | +} |
0 commit comments