You are here

function webform_civicrm_update_6200 in Webform CiviCRM Integration 7.3

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

Upgrade from 1.x to 2.x Add columns to webform_civicrm_forms table, and convert existing forms to new multi-entity schema.

File

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

Code

function webform_civicrm_update_6200() {
  $field = array(
    'type' => 'text',
    'serialize' => TRUE,
    'description' => 'Array of entity data for this form',
  );
  db_add_field('webform_civicrm_forms', 'data', $field);
  $field = array(
    'description' => 'Add fieldsets around contacts.',
    'type' => 'int',
    'size' => 'tiny',
    'not null' => TRUE,
    'default' => 0,
  );
  db_add_field('webform_civicrm_forms', 'create_fieldsets', $field);

  // Add value separator to CID
  $field = array(
    'type' => 'varchar',
    'length' => 2000,
    'not null' => TRUE,
    'default' => '',
    'description' => 'CiviCRM contact ids from this submission',
  );
  db_change_field('webform_civicrm_submissions', 'contact_id', 'contact_id', $field);
  db_update('webform_civicrm_submissions')
    ->expression('contact_id', "CONCAT('-', contact_id, '-')")
    ->execute();

  // Match field keys to new format
  civicrm_initialize();
  module_load_include('inc', 'webform_civicrm', 'webform_civicrm_utils');
  $fields = wf_crm_get_fields();
  $match = $contacts = $update = array();

  // Create matching table
  foreach ($fields as $key => $field) {
    list($table, $name) = explode('_', $key, 2);
    $match['civicrm_' . $name] = 'civicrm_1_contact_1_' . $key;
  }

  // Include field types that no longer exist, to be dealt with by subsequent update hooks
  $match['civicrm_state_province'] = 'civicrm_1_contact_1_address_state_province';

  // Collect entity data
  $db = db_query("SELECT form_key, nid FROM {webform_component} WHERE form_key LIKE 'civicrm_%%' AND type <> 'fieldset'");
  foreach ($db as $row) {
    if (array_key_exists($row->form_key, $match)) {
      $update[$row->form_key] = $match[$row->form_key];
      list($a, $b, $c, $d, $e, $f) = explode('_', $match[$row->form_key], 6);
      if ($e != 'contact' && $e != 'other') {
        $contacts[$row->nid]['number_of_' . $e] = 1;
        if ($e == 'address' || $e == 'email' || $e == 'phone') {
          $contacts[$row->nid][$e][1]['location_type_id'] = 1;
        }
        if ($e == 'phone') {
          $contacts[$row->nid][$e][1]['phone_type_id'] = 1;
        }
      }
    }
  }

  // Update field keys
  foreach ($update as $old => $new) {
    db_update('webform_component')
      ->fields(array(
      'form_key' => $new,
    ))
      ->condition('form_key', $old)
      ->execute();
  }

  // Populate entity data
  $db = db_query("SELECT * FROM {webform_civicrm_forms}");
  foreach ($db as $row) {
    $data = array(
      'contact' => array(
        1 => array(
          'contact' => array(
            1 => array(
              'contact_type' => 'individual',
              'contact_sub_type' => NULL,
            ),
          ),
          'activity_target' => 1,
        ),
      ),
    );
    if (!empty($contacts[$row->nid])) {
      $data['contact'][1] += $contacts[$row->nid];
    }
    if ($row->activity_type_id) {
      $data['activity'][1]['add_link'] = TRUE;
      $data['activity'][1]['activity'][1] = array(
        'activity_type_id' => $row->activity_type_id,
        'subject' => $row->activity_subject,
        'status_id' => 2,
      );
    }
    db_update('webform_civicrm_forms')
      ->fields(array(
      'data' => serialize($data),
    ))
      ->condition('nid', $row->nid)
      ->execute();
  }

  // Remove activity fields
  db_drop_field('webform_civicrm_forms', 'activity_type_id');
  db_drop_field('webform_civicrm_forms', 'activity_subject');
  return st('Upgrade to webform_civicrm 2 successful. Note: The field key (machine name) of civicrm-related webform fields has changed. If you were using any of these keys in webform hooks or tokens, please update it to use the new key.');
}