You are here

function webform_update_6302 in Webform 6.3

Convert node-level e-mail settings to new webform_emails table.

File

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

Code

function webform_update_6302() {
  $ret = array();

  // Safety check to prevent recreating the webform_emails table.
  if (db_table_exists('webform_emails')) {
    return $ret;
  }
  $table = array(
    'fields' => array(
      'nid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'eid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'size' => 'small',
        'not null' => TRUE,
        'default' => 0,
      ),
      'email' => array(
        'type' => 'text',
        'not null' => FALSE,
      ),
      'subject' => array(
        'type' => 'varchar',
        'length' => '255',
        'not null' => FALSE,
      ),
      'from_name' => array(
        'type' => 'varchar',
        'length' => '255',
        'not null' => FALSE,
      ),
      'from_address' => array(
        'type' => 'varchar',
        'length' => '255',
        'not null' => FALSE,
      ),
      'template' => array(
        'type' => 'text',
        'not null' => FALSE,
      ),
    ),
    'primary key' => array(
      'nid',
      'eid',
    ),
  );
  db_create_table($ret, 'webform_emails', $table);

  // Move over data from the webform table.
  $result = db_query("SELECT w.nid, w.email, w.email_from_name, w.email_from_address, w.email_subject, wc.cid, wc.extra FROM {webform} w LEFT JOIN {webform_component} wc ON w.nid = wc.nid AND type IN ('select', 'hidden', 'email') ORDER BY nid");
  $nid = 0;
  while ($row = db_fetch_object($result)) {

    // Insert an e-mail settings row for the default e-mail.
    if ($row->nid != $nid) {
      $nid = $row->nid;
      $eid = 0;
      if (!empty($row->email)) {
        $eid++;
        db_query("INSERT INTO {webform_emails} (nid, eid, email, subject, from_name, from_address, template) VALUES (%d, %d, '%s', '%s', '%s', '%s', 'default')", $nid, $eid, $row->email, $row->email_subject, $row->email_from_name, $row->email_from_address);
      }
    }

    // Check for an e-mail based on a component.
    if ($row->extra) {
      $extra = unserialize($row->extra);
      if ($extra['email']) {
        $eid++;
        unset($extra['email']);
        db_query("INSERT INTO {webform_emails} (nid, eid, email, subject, from_name, from_address, template) VALUES (%d, %d, '%s', '%s', '%s', '%s', 'default')", $nid, $eid, $row->cid, $row->email_subject, $row->email_from_name, $row->email_from_address);
        db_query("UPDATE {webform_component} SET extra = '%s' WHERE nid = %d AND cid = %d", serialize($extra), $row->nid, $row->cid);
      }
    }
  }

  // Remove columns from webform table.
  db_drop_field($ret, 'webform', 'email');
  db_drop_field($ret, 'webform', 'email_from_name');
  db_drop_field($ret, 'webform', 'email_from_address');
  db_drop_field($ret, 'webform', 'email_subject');
  return $ret;
}