function webform_civicrm_update_6200 in Webform CiviCRM Integration 6.2
Same name and namespace in other branches
- 7.5 webform_civicrm.install \webform_civicrm_update_6200()
- 7.2 webform_civicrm.install \webform_civicrm_update_6200()
- 7.3 webform_civicrm.install \webform_civicrm_update_6200()
- 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 188 - Webform CiviCRM module's install, uninstall and upgrade code.
Code
function webform_civicrm_update_6200() {
$ret = array();
$field = array(
'type' => 'text',
'serialize' => TRUE,
'description' => 'Array of entity data for this form',
);
db_add_field($ret, 'webform_civicrm_forms', 'data', $field);
$field = array(
'description' => 'Add fieldsets around contacts.',
'type' => 'int',
'size' => 'tiny',
'not null' => TRUE,
'default' => 0,
);
db_add_field($ret, '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($ret, 'webform_civicrm_submissions', 'contact_id', 'contact_id', $field);
db_query("UPDATE {webform_civicrm_submissions} SET contact_id = CONCAT('-', contact_id, '-')");
// Match field keys to new format
civicrm_initialize();
module_load_include('inc', 'webform_civicrm', 'webform_civicrm_utils');
$fields = webform_civicrm_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'");
while ($row = db_fetch_object($db)) {
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_query("UPDATE {webform_component} SET form_key = '%s' WHERE form_key = '%s'", $new, $old);
}
// Populate entity data
$db = db_query("SELECT * FROM {webform_civicrm_forms}");
while ($row = db_fetch_array($db)) {
$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_query("UPDATE {webform_civicrm_forms} SET data = '%s' WHERE nid = %d", serialize($data), $row['nid']);
}
// Remove activity fields
db_drop_field($ret, 'webform_civicrm_forms', 'activity_type_id');
db_drop_field($ret, 'webform_civicrm_forms', 'activity_subject');
return $ret;
}