You are here

function webform_encrypt_uninstall in Webform Encrypt 8

Same name and namespace in other branches
  1. 6 webform_encrypt.install \webform_encrypt_uninstall()
  2. 7 webform_encrypt.install \webform_encrypt_uninstall()

Implements hook_uninstall().

File

./webform_encrypt.install, line 14
Contains install and update related functions for the Webform Encrypt module.

Code

function webform_encrypt_uninstall() {

  // Decrypt all encrypted form values.
  $submissions = \Drupal::database()
    ->select('webform_submission_data', 'wsd')
    ->fields('wsd', [])
    ->execute()
    ->fetchAll();
  foreach ($submissions as $submission) {
    $webform = Webform::load($submission->webform_id);
    $config = $webform
      ->getThirdPartySetting('webform_encrypt', 'element');
    if (!empty($config[$submission->name])) {
      $unserialized = unserialize($submission->value);
      if (isset($unserialized['data']) && isset($unserialized['encrypt_profile'])) {
        $data = $unserialized['data'];
        $encryption_profile = EncryptionProfile::load($unserialized['encrypt_profile']);
      }
      else {
        $data = $submission->value;
        $encryption_profile = EncryptionProfile::load($config[$submission->name]['encrypt_profile']);
      }
      $value = Drupal::service('encryption')
        ->decrypt($data, $encryption_profile);
      \Drupal::database()
        ->update('webform_submission_data')
        ->fields([
        'value' => $value,
      ])
        ->condition('sid', $submission->sid)
        ->condition('name', $submission->name)
        ->condition('property', $submission->property)
        ->condition('delta', $submission->delta)
        ->execute();
    }
  }
}