Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SUPP0RT-1235: Added submission helper command #218

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
(<https://github.com/itk-dev/os2forms_selvbetjening/pull/228>)
* [#218](https://github.com/itk-dev/os2forms_selvbetjening/pull/218)
Added submission helper command

## [2.5.0] 2023-10-04

Expand Down
10 changes: 10 additions & 0 deletions web/modules/custom/os2forms_selvbetjening/README.md
Original file line number Diff line number Diff line change
@@ -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
```
8 changes: 8 additions & 0 deletions web/modules/custom/os2forms_selvbetjening/drush.services.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
services:
Drupal\os2forms_selvbetjening\Command\WebformSubmissionCommands:
arguments:
- '@entity_type.manager'
- '@database'
- '@current_user'
tags:
- { name: drush.command }
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php

namespace Drupal\os2forms_selvbetjening\Command;

use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\webform\WebformEntityStorageInterface;
use Drupal\webform\WebformSubmissionStorageInterface;
use Drupal\webform_revisions\Entity\WebformRevisions;
use Drush\Commands\DrushCommands;
use Symfony\Component\Yaml\Yaml;

/**
* Webform submission commands.
*/
class WebformSubmissionCommands extends DrushCommands {
/**
* The webform storage.
*
* @var \Drupal\webform\WebformEntityStorageInterface
*/
private readonly WebformEntityStorageInterface $webformStorage;

/**
* The webform submission storage.
*
* @var \Drupal\webform\WebformSubmissionStorageInterface
*/
private readonly WebformSubmissionStorageInterface $webformSubmissionStorage;

/**
* Constructor.
*/
public function __construct(
EntityTypeManagerInterface $entityTypeManager,
private readonly Connection $database,
private readonly AccountInterface $currentUser
) {
$this->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<string, mixed> $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()));
}
}
}
}
}

}