You are here

function webform_update_6312 in Webform 6.3

Convert select options to use numeric keys if none are manually specified.

File

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

Code

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

    // 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[] = '<group>';
        $values[] = $line;
      }
      elseif (preg_match('/^([^|]+)\\|(.*)$/', $line, $matches)) {
        $keys[] = $matches[1];
        $values[] = $matches[2];
      }
      else {
        $keys[] = '<unknown>';
        $values[] = $line;
      }
    }

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

    // 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.
    $extra['items'] = $items;
    db_query("UPDATE {webform_component} SET extra = '%s' WHERE nid = %d AND cid = %d", serialize($extra), $row->nid, $row->cid);

    // Update the saved results.
    foreach ($new_keys as $delta => $new_key) {
      db_query("UPDATE {webform_submitted_data} SET data = '%s' WHERE nid = %d AND cid = %d AND data = '%s'", $new_key, $row->nid, $row->cid, $values[$delta]);
    }

    // Delete empty rows, which are no longer stored for select lists.
    db_query("DELETE FROM {webform_submitted_data} WHERE data = '' AND nid = %d AND cid = %d");
  }
  return $ret;
}