function webform_civicrm_update_6200 in Webform CiviCRM Integration 7.5
Same name and namespace in other branches
- 6.2 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 199 - Webform CiviCRM module's install, uninstall and upgrade code.
Code
function webform_civicrm_update_6200() {
$field = [
'type' => 'text',
'serialize' => TRUE,
'description' => 'Array of entity data for this form',
];
db_add_field('webform_civicrm_forms', 'data', $field);
$field = [
'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 = [
'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', 'includes/utils');
$fields = wf_crm_get_fields();
$match = $contacts = $update = [];
// 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([
'form_key' => $new,
])
->condition('form_key', $old)
->execute();
}
// Populate entity data
$db = db_query("SELECT * FROM {webform_civicrm_forms}");
foreach ($db as $row) {
$data = [
'contact' => [
1 => [
'contact' => [
1 => [
'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] = [
'activity_type_id' => $row->activity_type_id,
'subject' => $row->activity_subject,
'status_id' => 2,
];
}
db_update('webform_civicrm_forms')
->fields([
'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.');
}