You are here

function yamlform_update_8053 in YAML Form 8

Issue #2806263: Add property column to submission data table. Fix 'yamlform_submission_data' records.

File

includes/yamlform.update.inc, line 1083
YAML Form module update hooks.

Code

function yamlform_update_8053() {

  /** @var \Drupal\yamlform\YamlFormElementManagerInterface $element_manager */
  $element_manager = \Drupal::service('plugin.manager.yamlform.element');

  /** @var \Drupal\yamlform\YamlFormInterface[] $yamlforms */
  $yamlforms = YamlForm::loadMultiple();
  foreach ($yamlforms as $yamlform) {
    $elements = $yamlform
      ->getElementsFlattenedAndHasValue();
    foreach ($elements as $key => $element) {
      $element_handler = $element_manager
        ->getElementInstance($element);
      $args = [
        ':yamlform_id' => $yamlform
          ->id(),
        ':name' => $key,
      ];
      if ($element_handler
        ->hasMultipleValues($element) && !$element_handler
        ->isComposite()) {

        // Fix broken property where property value is string when is should be
        // numeric delta. Applies only to checkboxes within wizard pages.
        $result = db_query("SELECT * FROM {yamlform_submission_data}  WHERE yamlform_id=:yamlform_id AND name=:name ORDER BY yamlform_id, sid, name, property", $args);
        $deltas = [];
        while ($record = $result
          ->fetchAssoc()) {
          if (!is_numeric($record['property'])) {
            $delta_key = $record['yamlform_id'] . '-' . $record['sid'] . $record['name'];
            if (isset($deltas[$delta_key])) {
              $deltas[$delta_key]++;
              $delta = $deltas[$delta_key];
            }
            else {
              $deltas[$delta_key] = 0;
              $delta = 0;
            }
            $update_args = $args + [
              ':delta' => $delta,
              ':property' => $record['property'],
            ];
            db_query("UPDATE {yamlform_submission_data} SET property=:delta WHERE yamlform_id=:yamlform_id AND name=:name AND property=:property", $update_args);
          }
        }

        // Move 'delta' from 'property' column for element's that have
        // multiple values.
        db_query("UPDATE {yamlform_submission_data} SET delta = property WHERE yamlform_id=:yamlform_id AND name=:name", $args);
        db_query("UPDATE {yamlform_submission_data} SET property = '' WHERE yamlform_id=:yamlform_id AND name=:name", $args);
      }

      // Make sure value property is set for text_format element.
      if (isset($element['#type']) && $element['#type'] == 'text_format') {
        db_query("UPDATE {yamlform_submission_data} SET property = 'value' WHERE yamlform_id=:yamlform_id AND name=:name", $args);
      }
    }
  }
}