You are here

function webform_civicrm_update_6203 in Webform CiviCRM Integration 7.2

Same name and namespace in other branches
  1. 6.2 webform_civicrm.install \webform_civicrm_update_6203()
  2. 7.5 webform_civicrm.install \webform_civicrm_update_6203()
  3. 7.3 webform_civicrm.install \webform_civicrm_update_6203()
  4. 7.4 webform_civicrm.install \webform_civicrm_update_6203()

Support new state/prov chain-select feature.

File

./webform_civicrm.install, line 309
Webform CiviCRM module's install, uninstall and upgrade code.

Code

function webform_civicrm_update_6203() {
  module_load_include('inc', 'webform', 'includes/webform.components');

  // First get rid of redundant fields
  $db = db_query("SELECT c1.* FROM {webform_component} c1, {webform_component} c2 WHERE c1.nid = c2.nid AND c1.form_key LIKE 'civicrm_%%_contact_%%_address_state_province' AND c2.form_key = CONCAT(c1.form_key, '_id')");
  foreach ($db as $item) {
    webform_component_delete($item, (array) $item);
  }

  // Update state_province fields
  $submitted = array();
  $db = db_query("SELECT * FROM {webform_component} WHERE form_key LIKE 'civicrm_%%_contact_%%_address_state_province%%'");
  foreach ($db as $item) {
    if (substr($item->form_key, -3) == '_id') {
      $submitted[] = '(nid = ' . $item->nid . ' AND cid = ' . $item->cid . ')';
    }
    else {
      $item->form_key .= '_id';
    }
    $item->type = 'textfield';
    $item->extra = array(
      'maxlength' => 5,
      'width' => 4,
      'private' => 0,
    );
    webform_component_update((array) $item);
  }

  // Update submission results - change numeric state/prov ids to abbreviations
  if ($submitted) {
    $where = implode(' OR ', $submitted);
    civicrm_initialize();
    module_load_include('inc', 'webform_civicrm', 'webform_civicrm_utils');
    $db = db_query('SELECT DISTINCT data FROM {webform_submitted_data} WHERE ' . $where);
    foreach ($db as $row) {
      if ($row->data && is_numeric($row->data)) {
        db_query('UPDATE {webform_submitted_data} SET data = \'' . webform_civicrm_state_abbr($row->data) . '\' WHERE data = ' . $row->data . ' AND (' . $where . ')');
      }
    }
  }
  return t('Upgrade successful. Note: If you upgraded via drush you will now need to clear all caches with the command drush cc');
}