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

Commit

Permalink
Merge branch 'release/4.1.0'
Browse files Browse the repository at this point in the history
Release/4.1.0
  • Loading branch information
MdNadimHossain committed May 7, 2024
2 parents 260c020 + 5aef3f0 commit 33d2e7f
Show file tree
Hide file tree
Showing 2 changed files with 109 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 }
104 changes: 104 additions & 0 deletions src/Commands/TideWebformSubmissionsCleanUp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

namespace Drupal\tide_webform\Commands;

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

/**
* 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::output()->writeln("Webform with ID $webform_id does not exist.");
return;
}

if (!$webform->getElement($webform_field) || !$webform->getElement($webform_field)['#required']) {
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::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::output()->writeln('Finished processing.');
}
else {
Drush::output()->writeln("Finished with errors.");
}
}

}

0 comments on commit 33d2e7f

Please sign in to comment.