Skip to content
This repository has been archived by the owner on Sep 30, 2024. It is now read-only.

Commit

Permalink
TideWebformSubmissionsCleanUp drush command
Browse files Browse the repository at this point in the history
### Change
The Drush command requires three parameters: `$webform_id`, `$date_string`, and `$webform_field`.
For example, `drush twc tide_webform_content_rating 07-May-2024 was_this_page_helpful` deletes submissions for the `tide_webform_content_rating` form on `07-May-2024`, where the **required** field `was_this_page_helpful` is either empty or missing.
if `was_this_page_helpful` is not a required field, just return.
  • Loading branch information
vincent-gao committed May 7, 2024
1 parent 7826b7d commit 2ef378a
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
5 changes: 5 additions & 0 deletions drush.services.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
services:
tide_webform_cleanup:
class: \Drupal\tide_webform\Commands\TideWebformSubmissionsCleanUp
tags:
- { name: drush.command }
94 changes: 94 additions & 0 deletions src/Commands/TideWebformSubmissionsCleanUp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php
namespace Drupal\tide_webform\Commands;

use Drush\Commands\DrushCommands;
use Drupal\webform\Entity\Webform;
use Drupal\webform\Entity\WebformSubmission;
use Drupal\Core\Datetime\DrupalDateTime;

/**
* Drush command
*/
class TideWebformSubmissionsCleanUp extends DrushCommands {
/**
* Clean up webform submissions based on the specified conditions.
*
* @param string $webform_id
* The webform ID.
* @param string $date_string
* The date string.
* @param string $webform_field
* The webform field to check.
*
* @command tide_webform:cleanup_submissions
* @aliases twc
* @usage tide_webform:cleanup_submissions tide_webform_content_rating 07-May-2024 was_this_page_helpful
* Clean up submissions for 'tide_webform_content_rating' on '07-May-2024' where 'was_this_page_helpful' field is empty or does not exist.
*/
public function cleanupSubmissions($webform_id, $date_string, $webform_field) {
$webform = Webform::load($webform_id);
if (!$webform) {
\Drush\Drush::output()->writeln("Webform with ID $webform_id does not exist.");
return;
}

if (!$webform->getElement($webform_field) || !$webform->getElement($webform_field)['#required']) {
\Drush\Drush::output()->writeln("The field $webform_field is not a required field.");
return;
}

$date_start = new DrupalDateTime($date_string);
$date_start->setTime(0, 0, 0);
$date_end = new DrupalDateTime($date_string);
$date_end->setTime(23, 59, 59);

$query = \Drupal::entityQuery('webform_submission')
->condition('webform_id', $webform_id)
->condition('created', $date_start->getTimestamp(), '>=')
->condition('created', $date_end->getTimestamp(), '<=')
->accessCheck(FALSE);
$sids = $query->execute();

if (empty($sids)) {
\Drush\Drush::output()->writeln("No submissions found for the specified criteria.");
return;
}

$batch = [
'title' => t('Deleting submissions...'),
'operations' => [],
'finished' => [get_class($this), 'cleanupSubmissionsFinished'],
];

foreach ($sids as $sid) {
$batch['operations'][] = [[get_class($this), 'deleteSubmission'], [$sid, $webform_field]];
}

batch_set($batch);
drush_backend_batch_process();
}

/**
* Batch operation callback for deleting a submission.
*/
public static function deleteSubmission($sid, $webform_field, &$context) {
$submission = WebformSubmission::load($sid);
if ($submission && ($submission->getElementData($webform_field) === '' || is_null($submission->getElementData($webform_field)))) {
$submission->delete();
$context['results']['deleted'][] = $sid;
} else {
$context['results']['skipped'][] = $sid;
}
}

/**
* Finished callback for the batch.
*/
public static function cleanupSubmissionsFinished($success, $results, $operations) {
if ($success) {
\Drush\Drush::output()->writeln('Finished processing.');
} else {
\Drush\Drush::output()->writeln("Finished with errors.");
}
}
}

0 comments on commit 2ef378a

Please sign in to comment.