Skip to content

Commit 346e743

Browse files
committed
SUPP0RT-1235: Added submission helper command
1 parent 506fa1e commit 346e743

File tree

4 files changed

+148
-1
lines changed

4 files changed

+148
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
Nedenfor ses dato for release og beskrivelse af opgaver som er implementeret.
66

7-
## In develop
7+
## [Under udvikling]
88

99
* Tilføjede custom modulet `os2forms_permission_alterations`.
1010
* 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.
1313
2.0.0](https://github.com/itk-dev/os2forms_nemlogin_openid_connect/releases/tag/2.0.0)
1414
* Added missing config for updated `os2forms_forloab` module.
1515
(<https://github.com/itk-dev/os2forms_selvbetjening/pull/228>)
16+
* [#218](https://github.com/itk-dev/os2forms_selvbetjening/pull/218)
17+
Added submission helper command
1618

1719
## [2.5.0] 2023-10-04
1820

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# OS2Forms selvbetjening
2+
3+
## Drush commands
4+
5+
The command are only used for debugging and error resolving.
6+
7+
```sh
8+
drush os2forms_selvbetjening:webform:list-revisions --help
9+
drush os2forms_selvbetjening:webform-submission:list-stray-submissions --help
10+
```
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
services:
2+
Drupal\os2forms_selvbetjening\Command\WebformSubmissionCommands:
3+
arguments:
4+
- '@entity_type.manager'
5+
- '@database'
6+
- '@current_user'
7+
tags:
8+
- { name: drush.command }
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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

Comments
 (0)