diff --git a/CHANGELOG.md b/CHANGELOG.md index 18bb0769..f237c045 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ Nedenfor ses dato for release og beskrivelse af opgaver som er implementeret. -## In develop +## [Under udvikling] * Tilføjede custom modulet `os2forms_permission_alterations`. * Tilføjede `administer leaflet layers` permission til site admin rollen. @@ -13,6 +13,8 @@ Nedenfor ses dato for release og beskrivelse af opgaver som er implementeret. 2.0.0](https://github.com/itk-dev/os2forms_nemlogin_openid_connect/releases/tag/2.0.0) * Added missing config for updated `os2forms_forloab` module. () +* [#218](https://github.com/itk-dev/os2forms_selvbetjening/pull/218) + Added submission helper command ## [2.5.0] 2023-10-04 diff --git a/web/modules/custom/os2forms_selvbetjening/README.md b/web/modules/custom/os2forms_selvbetjening/README.md new file mode 100644 index 00000000..d6dbf0fd --- /dev/null +++ b/web/modules/custom/os2forms_selvbetjening/README.md @@ -0,0 +1,10 @@ +# OS2Forms selvbetjening + +## Drush commands + +The command are only used for debugging and error resolving. + +```sh +drush os2forms_selvbetjening:webform:list-revisions --help +drush os2forms_selvbetjening:webform-submission:list-stray-submissions --help +``` diff --git a/web/modules/custom/os2forms_selvbetjening/drush.services.yml b/web/modules/custom/os2forms_selvbetjening/drush.services.yml new file mode 100644 index 00000000..8a9078fd --- /dev/null +++ b/web/modules/custom/os2forms_selvbetjening/drush.services.yml @@ -0,0 +1,8 @@ +services: + Drupal\os2forms_selvbetjening\Command\WebformSubmissionCommands: + arguments: + - '@entity_type.manager' + - '@database' + - '@current_user' + tags: + - { name: drush.command } diff --git a/web/modules/custom/os2forms_selvbetjening/src/Command/WebformSubmissionCommands.php b/web/modules/custom/os2forms_selvbetjening/src/Command/WebformSubmissionCommands.php new file mode 100644 index 00000000..300a5654 --- /dev/null +++ b/web/modules/custom/os2forms_selvbetjening/src/Command/WebformSubmissionCommands.php @@ -0,0 +1,125 @@ +webformStorage = $entityTypeManager->getStorage('webform'); + $this->webformSubmissionStorage = $entityTypeManager->getStorage('webform_submission'); + } + + /** + * List webforms revisions. + * + * @command os2forms_selvbetjening:webform:list-revisions + */ + public function listWebformRevisions() { + /** @var \Drupal\webform\WebformInterface[] $webforms */ + $webforms = $this->webformStorage->loadMultiple(); + + foreach ($webforms as $webform) { + $this->writeln( + Yaml::dump([ + 'id' => $webform->id(), + 'label' => $webform->label(), + 'revisionId' => $webform instanceof WebformRevisions ? $webform->getRevisionId() : '👻', + ]) + ); + } + } + + /** + * List stray submissions. + * + * List submission where the loaded webform does not match the expected webform. + * + * @param string $webformId + * The webform id. Use '*' to list check all webforms. + * @param array $options + * The command options. + * + * @option bool fix + * Fix webform revision on stray submissions. + * + * @command os2forms_selvbetjening:webform-submission:list-stray-submissions + * + * @phpstan-param array $options + */ + public function listStraySubmissions(string $webformId, array $options = [ + 'fix' => FALSE, + ]) { + $webforms = '*' === $webformId + ? $this->webformStorage->loadMultiple() + : $this->webformStorage->loadMultiple([$webformId]); + + foreach ($webforms as $webform) { + /** @var \Drupal\webform\WebformSubmissionInterface[] $submissions */ + $submissions = $this->webformSubmissionStorage->loadByProperties([ + 'webform_id' => $webform->id(), + ]); + + foreach ($submissions as $submission) { + if ($webform->id() !== $submission->getWebform()->id()) { + $this->writeln( + Yaml::dump([ + 'submission' => [ + 'id' => $submission->id(), + 'webform.id' => $submission->getWebform()->id(), + 'data' => $submission->getData(), + ], + 'webform' => [ + 'id' => $webform->id(), + 'revisionId' => $webform instanceof WebformRevisions ? $webform->getRevisionId() : '👻', + ], + ], PHP_INT_MAX) + ); + + if ($options['fix']) { + $this->database->update('webform_submission') + ->fields([ + 'webform_revision' => NULL, + ]) + ->condition('sid', $submission->id()) + ->execute(); + + $this->writeln(sprintf('Fixed webform revision on submission %s', $submission->id())); + } + } + } + } + } + +}