You are here

function webform_update_6313 in Webform 6.3

Create keys for all questions (which don't currently have keys at all), create keys for all options that don't yet have any, and then convert the existing data to use these keys.

File

./webform.install, line 1123
Webform module install/schema hooks.

Code

function webform_update_6313() {
  $ret = array();
  $result = db_query("SELECT * FROM {webform_component} WHERE type = 'grid'");
  while ($row = db_fetch_object($result)) {
    $extra = unserialize($row->extra);
    $lines = explode("\n", $extra['options']);

    // Get a list of items that have manual keys or no keys at all.
    $keys = array();
    $values = array();
    foreach ($lines as $line) {
      $line = rtrim($line, "\r\n");
      if (strlen($line) == 0) {
        continue;
      }
      $matches = array();
      if (preg_match('/^([^|]+)\\|(.*)$/', $line, $matches)) {
        $keys[] = $matches[1];
        $values[] = $matches[2];
      }
      else {
        $keys[] = '<unknown>';
        $values[] = $line;
      }
    }

    // Assign new keys to options that have none.
    $new_key = 0;
    $new_keys = array();
    $options = '';
    foreach ($keys as $n => $key) {
      if ($key == '<unknown>') {
        while (in_array($new_key, $keys, TRUE) || in_array($new_key, $new_keys, TRUE)) {
          $new_key++;
        }
        $new_keys[$n] = $new_key;
        $options .= $new_key . '|' . $values[$n] . "\n";
      }
      else {
        $options .= $key . '|' . $values[$n] . "\n";
      }
    }
    $extra['options'] = $options;

    // Assign question keys. This is easier since they don't have keys at all.
    $lines = explode("\n", $extra['questions']);
    $questions = array();
    foreach ($lines as $delta => $line) {
      $line = rtrim($line, "\r\n");
      if (strlen($line) == 0) {
        continue;
      }
      $questions[$delta] = $delta . '|' . $line;
    }
    $extra['questions'] = implode("\n", $questions);

    // While we're here, get rid of the 'Y' value for options.
    foreach ($extra as $key => $value) {
      if ($value === 'Y') {
        $extra[$key] = '1';
      }
      elseif ($value === 'N') {
        $extra[$key] = 0;
      }
    }

    // Update the component.
    db_query("UPDATE {webform_component} SET extra = '%s' WHERE nid = %d AND cid = %d", serialize($extra), $row->nid, $row->cid);

    // Convert the option values into keys if new ones were created.
    if (!empty($new_keys)) {
      $args = array();
      $sql = "UPDATE {webform_submitted_data} SET data = CASE data";
      foreach ($new_keys as $delta => $new_key) {
        $sql .= " WHEN '%s' THEN '%s'";
        $args[] = $values[$delta];
        $args[] = $new_key;
      }
      $sql .= " ELSE data END WHERE nid = %d AND cid = %d";
      $args[] = $row->nid;
      $args[] = $row->cid;
      db_query($sql, $args);
    }

    // Note: Converting the question values into keys is not necessary because
    // data was already stored based on the question position. Since we assigned
    // permanent keys based on position, all our keys are already accurate.
    // Delete empty rows, which are no longer stored for grids.
    db_query("DELETE FROM {webform_submitted_data} WHERE data = '' AND nid = %d AND cid = %d");
  }
  return $ret;
}