5 * Webform CiviCRM module's install, uninstall and upgrade code.
9 * Implements hook_requirements().
11 function webform_civicrm_requirements($phase) {
12 $requirements = array();
14 if ($phase == 'runtime') {
16 $status = _webform_civicrm_status();
18 if (!$status['webform_civicrm']) {
20 $requirements['webform_civicrm'] = array(
21 'title' => 'Webform CiviCRM Integration',
22 'value' => $t('Version error'),
23 'severity' => REQUIREMENT_ERROR,
24 'description' => $t('The versions of the Webform CiviCRM Integration, Webform, CiviCRM enabled are not compatible. ') .
25 l($t('See the Webform CiviCRM Integration project page for version compatibility'), 'https://drupal.org/project/webform_civicrm'),
30 $requirements['webform_civicrm'] = array(
31 'title' => 'Webform CiviCRM Integration',
32 'severity' => REQUIREMENT_OK,
33 'value' => t('Required version of CiviCRM and Webform are enabled.'),
42 * Implements hook_schema().
44 function webform_civicrm_schema() {
46 $schema['webform_civicrm_forms'] = array(
47 'description' => 'CiviCRM settings for individual Webform nodes.',
54 'description' => 'Webform Node ID',
59 'description' => 'Array of entity data for this webform',
61 'prefix_known' => array(
62 'description' => 'Form prefix for known users.',
66 'prefix_unknown' => array(
67 'description' => 'Form prefix for unknown users.',
76 'description' => 'Message to show to known users',
78 'confirm_subscription' => array(
79 'description' => 'Send confirmation for mailing list subscriptions.',
85 'block_unknown_users' => array(
86 'description' => 'Only allow known contacts to use form.',
92 'create_fieldsets' => array(
93 'description' => 'Add fieldsets around contacts.',
99 'new_contact_source' => array(
104 'description' => 'Source label for newly created contacts',
107 'primary key' => array('nid'),
109 $schema['webform_civicrm_submissions'] = array(
110 'description' => 'Link between form submissions and CiviCRM data.',
117 'description' => 'Webform Submission ID',
119 'contact_id' => array(
124 'description' => 'CiviCRM contact ids from this submission',
126 'civicrm_data' => array(
129 'description' => 'Array of entity ids for this submission',
132 'primary key' => array('sid'),
139 * Add column for message to webform_civicrm_forms table.
141 function webform_civicrm_update_6100() {
147 'description' => 'Message to show to known users',
149 db_add_field('webform_civicrm_forms', 'message', $message);
153 * Add missing activity targets.
155 function webform_civicrm_update_6101() {
156 civicrm_initialize();
157 $db = db_query('SELECT * FROM {webform_civicrm_submissions} WHERE contact_id <> :contact_id AND activity_id <> :activity_id', array(':contact_id' => 0, ':activity_id' => 0));
158 $sql = 'INSERT INTO civicrm_activity_target (activity_id, target_contact_id) VALUES (%1,%2)';
162 foreach ($db as $row) {
163 $params[$n] = array($row->activity_id, 'Integer');
164 $params[$n + 1] = array($row->contact_id, 'Integer');
166 $sql .= ',(%' . $n . ',%' . ($n + 1) . ')';
172 CRM_Core_DAO::executeQuery($sql, $params);
174 return st('@num activity target contacts added.', array('@num' => $c));
178 * Add column for confirm_subscription to webform_civicrm_forms table.
180 function webform_civicrm_update_6102() {
181 $confirm_subscription = array(
182 'description' => 'Send confirmation for mailing list subscriptions.',
188 db_add_field('webform_civicrm_forms', 'confirm_subscription', $confirm_subscription);
192 * Upgrade from 1.x to 2.x
193 * Add columns to webform_civicrm_forms table, and convert existing forms to new multi-entity schema.
195 function webform_civicrm_update_6200() {
199 'description' => 'Array of entity data for this form',
201 db_add_field('webform_civicrm_forms', 'data', $field);
203 'description' => 'Add fieldsets around contacts.',
209 db_add_field('webform_civicrm_forms', 'create_fieldsets', $field);
211 // Add value separator to CID
217 'description' => 'CiviCRM contact ids from this submission',
219 db_change_field('webform_civicrm_submissions', 'contact_id', 'contact_id', $field);
221 db_update('webform_civicrm_submissions')
222 ->expression('contact_id', "CONCAT('-', contact_id, '-')")
225 // Match field keys to new format
226 civicrm_initialize();
227 module_load_include('inc', 'webform_civicrm', 'includes/utils');
228 $fields = wf_crm_get_fields();
229 $match = $contacts = $update = array();
230 // Create matching table
231 foreach ($fields as $key => $field) {
232 list($table, $name) = explode('_', $key, 2);
233 $match['civicrm_' . $name] = 'civicrm_1_contact_1_' . $key;
235 // Include field types that no longer exist, to be dealt with by subsequent update hooks
236 $match['civicrm_state_province'] = 'civicrm_1_contact_1_address_state_province';
238 // Collect entity data
239 $db = db_query("SELECT form_key, nid FROM {webform_component} WHERE form_key LIKE 'civicrm_%%' AND type <> 'fieldset'");
240 foreach ($db as $row) {
241 if (array_key_exists($row->form_key, $match)) {
242 $update[$row->form_key] = $match[$row->form_key];
243 list($a, $b, $c, $d, $e, $f) = explode('_', $match[$row->form_key], 6);
244 if ($e != 'contact' && $e != 'other') {
245 $contacts[$row->nid]['number_of_' . $e] = 1;
246 if ($e == 'address' || $e == 'email' || $e == 'phone') {
247 $contacts[$row->nid][$e][1]['location_type_id'] = 1;
250 $contacts[$row->nid][$e][1]['phone_type_id'] = 1;
257 foreach ($update as $old => $new) {
258 db_update('webform_component')
259 ->fields(array('form_key' => $new))
260 ->condition('form_key', $old)
264 // Populate entity data
265 $db = db_query("SELECT * FROM {webform_civicrm_forms}");
266 foreach ($db as $row) {
270 'contact' => array(1 => array(
271 'contact_type' => 'individual',
272 'contact_sub_type' => NULL,
274 'activity_target' => 1,
278 if (!empty($contacts[$row->nid])) {
279 $data['contact'][1] += $contacts[$row->nid];
281 if ($row->activity_type_id) {
282 $data['activity'][1]['add_link'] = TRUE;
283 $data['activity'][1]['activity'][1] = array(
284 'activity_type_id' => $row->activity_type_id,
285 'subject' => $row->activity_subject,
289 db_update('webform_civicrm_forms')
290 ->fields(array('data' => serialize($data)))
291 ->condition('nid', $row->nid)
294 // Remove activity fields
295 db_drop_field('webform_civicrm_forms', 'activity_type_id');
296 db_drop_field('webform_civicrm_forms', 'activity_subject');
297 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.');
301 * Add column for new_contact_source to webform_civicrm_forms table.
303 function webform_civicrm_update_6201() {
309 'description' => 'Source label for newly created contacts',
311 db_add_field('webform_civicrm_forms', 'new_contact_source', $field);
315 * Add column for contact_matching to webform_civicrm_forms table.
317 function webform_civicrm_update_6202() {
319 'description' => 'How to match and autofill for contacts',
325 db_add_field('webform_civicrm_forms', 'contact_matching', $field);
329 * Support new state/prov chain-select feature.
331 function webform_civicrm_update_6203() {
332 module_load_include('inc', 'webform', 'includes/webform.components');
334 // First get rid of redundant fields
335 $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')");
336 foreach ($db as $item) {
337 webform_component_delete($item, (array) $item);
340 // Update state_province fields
341 $submitted = array();
342 $db = db_query("SELECT * FROM {webform_component} WHERE form_key LIKE 'civicrm_%%_contact_%%_address_state_province%%'");
343 foreach ($db as $item) {
344 if (substr($item->form_key, -3) == '_id') {
345 $submitted[] = '(nid = ' . $item->nid . ' AND cid = ' . $item->cid . ')';
348 $item->form_key .= '_id';
350 $item->type = 'textfield';
351 $item->extra = array(
356 webform_component_update((array) $item);
359 // Update submission results - change numeric state/prov ids to abbreviations
361 $where = implode(' OR ', $submitted);
362 civicrm_initialize();
363 module_load_include('inc', 'webform_civicrm', 'includes/utils');
364 $db = db_query('SELECT DISTINCT data FROM {webform_submitted_data} WHERE ' . $where);
365 foreach ($db as $row) {
366 if ($row->data && is_numeric($row->data)) {
367 db_query('UPDATE {webform_submitted_data} SET data = \'' . wf_crm_state_abbr($row->data) . '\' WHERE data = ' . $row->data . ' AND (' . $where . ')');
372 return st('Upgrade successful. Note: If you upgraded via drush you will now need to clear all caches with the command drush cc');
376 * Group participant options into a single array.
378 function webform_civicrm_update_6204() {
379 module_load_include('inc', 'webform_civicrm', 'includes/utils');
381 $db = db_query("SELECT * FROM {webform_civicrm_forms}");
382 foreach ($db as $form) {
383 $data = unserialize($form->data);
384 if (isset($data['event_type'])) {
385 $data['reg_options'] = array(
386 'event_type' => $data['event_type'],
387 'show_past_events' => wf_crm_aval($data, 'show_past_events', 0),
389 $form = (array) $form;
390 $form['data'] = $data;
391 drupal_write_record('webform_civicrm_forms', $form, 'nid');
395 return st('Upgrade successful. Note: If you upgraded via drush you will now need to clear all caches with the command drush cc');
399 * Upgrade for CiviCRM 4.1 compatibility.
401 function webform_civicrm_update_6205() {
402 civicrm_initialize();
403 module_load_include('inc', 'webform_civicrm', 'includes/utils');
404 module_load_include('inc', 'webform', 'includes/webform.components');
405 $db = db_query("SELECT * FROM {webform_component} WHERE form_key LIKE 'civicrm_%%_other_tags' OR form_key LIKE 'civicrm_%%_other_group%%'");
406 $tag = $group = array();
407 foreach ($db as $row) {
408 if ($pieces = wf_crm_explode_key($row->form_key)) {
409 list( , $c, $ent, $n, $table, $name) = $pieces;
410 if ($name == 'groups') {
411 if (empty($group[$row->nid][$c])) {
412 $group[$row->nid][$c] = array('value' => '');
416 $type = $name == 'groups_hidden' ? 'group' : 'tag';
417 ${$type}[$row->nid][$c] = (array) $row;
420 db_query("UPDATE {webform_component} SET form_key = REPLACE(form_key, 'other_groups', 'other_group') WHERE form_key LIKE 'civicrm_%%_other_groups'");
421 $db = db_query("SELECT * FROM {webform_civicrm_forms}");
422 foreach ($db as $form) {
424 $data = unserialize($form->data);
425 if (!empty($data['activity'][1])) {
426 $data['activity'][1]['details'] = array(
427 'view_link' => !empty($data['activity'][1]['add_link']) ? 'view_link' : 0,
429 $data['activity'][1]['existing_activity_status'] = empty($data['activity'][1]['existing_activity_status']) ? array() : array($data['activity'][1]['existing_activity_status']);
431 foreach ($data['contact'] as &$con) {
432 if (empty($con['contact'][1]['contact_sub_type'])) {
433 $con['contact'][1]['contact_sub_type'] = array();
436 $con['contact'][1]['contact_sub_type'] = (array) $con['contact'][1]['contact_sub_type'];
439 if (!empty($tag[$nid]) || !empty($group[$nid])) {
440 $node = node_load($nid);
441 $both = wf_crm_aval($tag, $nid, array()) + wf_crm_aval($group, $nid, array());
442 foreach ($both as $c => $val) {
443 $data['contact'][$c]['number_of_other'] = 1;
444 if (!empty($tag[$nid][$c])) {
446 $tag_names = drupal_explode_tags($tag[$nid][$c]['value']);
447 foreach ($tag_names as $t) {
448 $result = wf_civicrm_api('tag', 'get', array('name' => $t));
449 if ($tid = wf_crm_aval($result, 'id')) {
450 $tag_ids[$tid] = $tid;
453 $data['contact'][$c]['other'][1]['tag'] = $tag_ids;
454 webform_component_delete($node, $tag[$nid][$c]);
456 if (!empty($group[$nid][$c]['form_key'])) {
457 if (empty($group[$nid][$c]['value'])) {
458 $data['contact'][$c]['other'][1]['group'] = array();
461 $data['contact'][$c]['other'][1]['group'] = drupal_map_assoc(explode(',', $group[$nid][$c]['value']));
463 webform_component_delete($node, $group[$nid][$c]);
468 drupal_write_record('webform_civicrm_forms', $form, 'nid');
471 return st('Hidden tag and group fields have been removed, those options are now integrated into the CiviCRM tab of webforms.');
475 * Note: There are differences in how contact references and relationships work in the 3.x branch.
476 * Read the upgrade instructions at http://drupal.org/node/1615380
478 function webform_civicrm_update_7300() {
479 module_load_include('inc', 'webform_civicrm', 'includes/wf_crm_admin_form');
480 module_load_include('inc', 'webform_civicrm', 'includes/contact_component');
481 module_load_include('inc', 'webform', 'includes/webform.components');
482 civicrm_initialize();
483 $fields = wf_crm_get_fields();
484 $db = db_query("SELECT * FROM {webform_civicrm_forms}");
485 foreach ($db as $form) {
486 $form = (array) $form;
487 $form['data'] = unserialize($form['data']);
488 $data = &$form['data'];
489 $node = node_load($form['nid']);
490 $enabled = wf_crm_enabled_fields($node);
491 // Add existing contact field
492 $field = $fields['contact_existing'];
493 $field['nid'] = $form['nid'];
494 $field['form_key'] = 'civicrm_1_contact_1_contact_existing';
495 $field['weight'] = -255;
496 $field['extra']['default'] = $form['contact_matching'] ? 'user' : '';
497 $field['extra']['hide_fields'] = array();
498 wf_crm_admin_form::insertComponent($field, $enabled, $form);
501 if (!empty($data['activity'][1]['activity'])) {
502 foreach ($data['contact'] as $c => $contact) {
503 if (!empty($contact['activity_target'])) {
504 $data['activity'][1]['activity'][1]['target_contact_id'][$c] = $c;
509 // Update relationship type ids
510 foreach ($data['contact'] as $c => &$contact) {
511 if (!empty($contact['relationship'])) {
512 foreach ($contact['relationship'] as $n => $rel) {
513 if (!empty($rel['relationship_type_id'])) {
514 $r_types = wf_crm_field_options(array('form_key' => "civicrm_{$c}_contact_{$n}_relationship_relationship_type_id"), 'config_form', $data);
515 $type = $rel['relationship_type_id'];
516 list($r, $a_b) = explode('_', $type);
517 if (!isset($r_types[$type]) && isset($r_types[$r . '_r'])) {
518 $contact['relationship'][$n]['relationship_type_id'] = $r . '_r';
520 // Change current employer rel
522 // Get regular employer rel type
523 foreach (wf_crm_get_relationship_types() as $e_type => $r) {
524 if ($r['name_a_b'] == 'Employee of') {
528 $contact['relationship'][$n]['relationship_type_id'] = $e_type . '_' . $a_b;
529 $i = $a_b == 'a' ? $c : $n;
530 $e = $a_b == 'a' ? $n : $c;
531 $data['contact'][$i]['contact'][1]['employer_id'] = $e;
532 if ($n == 1 && empty($enabled["civicrm_{$c}_contact_1_contact_existing"])) {
533 // Add existing contact field
534 $field = $fields['contact_existing'];
535 $field['name'] = $a_b == 'a' ? t('Employee') : t('Employer');
536 $field['nid'] = $form['nid'];
537 $field['weight'] = -255;
538 $field['form_key'] = "civicrm_{$c}_contact_1_contact_existing";
539 $field['extra']['widget'] = 'hidden';
540 $field['extra']['default'] = 'relationship';
541 $field['extra']['default_relationship'] = array($e_type . '_' . $a_b);
542 $field['extra']['hide_fields'] = array();
543 wf_crm_admin_form::insertComponent($field, $enabled, $form);
551 // Activity assignee & case manager
552 foreach (array('activity' => 'assignee_contact_id', 'case' => 'creator_id') as $table => $name) {
553 if (!empty($data[$table][1][$table][1][$name])) {
554 $val = &$data[$table][1][$table][1][$name];
555 if ($val[0] == '#') {
556 $val = substr($val, 1);
560 $c = count($data['contact']) + 1;
561 $data['contact'][$c] = array(
562 'contact' => array(1 => array(
563 'contact_type' => 'individual',
564 'contact_sub_type' => array(),
567 // Set existing contact id
568 $field = $fields['contact_existing'] + _webform_defaults_civicrm_contact();
569 $field['nid'] = $form['nid'];
570 $field['form_key'] = 'civicrm_' . $c . '_contact_1_contact_existing';
571 $field['weight'] = 255;
572 $field['name'] = $table == 'case' ? st('Case Manager') : st('Activity Assignee');
573 $field['extra']['widget'] = 'hidden';
574 $field['extra']['default'] = 'contact_id';
575 $field['extra']['default_contact_id'] = $val;
577 webform_component_insert($field);
581 // Update some fields
582 foreach ($enabled as $key => $field_id) {
583 list( , $c, $ent, $n, $table, $name) = wf_crm_explode_key($key);
584 $field = $node->webform['components'][$field_id];
585 // Convert ContactRef fields to real contacts
586 if (wf_crm_aval($fields, "{$table}_$name:data_type") == 'ContactReference') {
587 // No big changes if selecting from webform contacts, just strip the # symbols
588 if (empty($field['extra']['civicrm_group'])) {
589 $field['value'] = str_replace('#', '', $field['value']);
590 // Hidden fields are no longer needed for referencing a webform contact
591 if ($field['type'] == 'hidden') {
592 $data[$ent][$c][$table][$n][$name] = $field['value'];
593 webform_component_delete($node, $field);
596 if (!empty($field['extra']['items'])) {
597 $old = wf_crm_str2array($field['extra']['items']);
599 foreach ($old as $k => $v) {
600 $new[substr($k, 1)] = $v;
602 $field['extra']['items'] = wf_crm_array2str($new);
605 // Big changes if selecting contact by group!
606 elseif ($field['type'] === 'select') {
607 $group = $field['extra']['civicrm_group'];
608 // Get a sample from the group for a hint of this contact's type
609 $result = wf_civicrm_api('contact', 'get', array('rowCount' => 1, 'group' => array($group => 1), 'return.contact_type' => 1));
610 $type = 'individual';
611 if (!empty($result['values'])) {
612 $result = array_pop($result['values']);
613 $type = strtolower($result['contact_type']);
616 $c = count($data['contact']) + 1;
617 $data['contact'][$c] = array(
618 'contact' => array(1 => array(
619 'contact_type' => $type,
620 'contact_sub_type' => array(),
623 $data[$ent][$c][$table][$n][$name] = $c;
624 // Change this field type from select to civicrm_contact
625 $field['form_key'] = 'civicrm_' . $c . '_contact_1_contact_existing';
626 $field['type'] = 'civicrm_contact';
627 $field['extra'] = $fields['contact_existing']['extra'];
628 $field['extra']['widget'] = 'select';
629 $field['extra']['none_prompt'] = t('- None Found -');
630 $default = _webform_defaults_civicrm_contact();
631 $field['extra']['filters'] = $default['extra']['filters'];
632 $field['extra']['filters']['group'] = array($group);
634 webform_component_update($field);
636 // Update relationship type ids
637 elseif ($name == 'relationship_type_id') {
638 $r_types = wf_crm_field_options($field, '', $data);
639 if (!empty($field['extra']['items'])) {
640 $old = wf_crm_str2array($field['extra']['items']);
642 foreach ($old as $k => $v) {
643 if (!isset($r_types[$k])) {
644 list($r, $a_b) = explode('_', $k);
645 if (isset($r_types[$r . '_r'])) {
654 $field['extra']['items'] = wf_crm_array2str($new);
657 if (!empty($field['value'])) {
658 list($r, $a_b) = explode('_', $field['value']);
659 if (isset($r_types[$r . '_r'])) {
660 $field['value'] = $r . '_r';
663 webform_component_update($field);
666 drupal_write_record('webform_civicrm_forms', $form, 'nid');
668 db_drop_field('webform_civicrm_forms', 'contact_matching');
670 $msg = st('There are important changes in Webform CiviCRM 3 that require your attention. Read the upgrade instructions at http://drupal.org/node/1615380 and review your existing webforms to ensure they are working as expected. Enjoy the new contact matching features!');
671 drupal_set_message($msg, 'warning');
676 * Update case features.
678 function webform_civicrm_update_7301() {
679 module_load_include('inc', 'webform_civicrm', 'includes/utils');
681 $db = db_query("SELECT * FROM {webform_civicrm_forms}");
682 foreach ($db as $form) {
683 $data = unserialize($form->data);
684 if (!empty($data['case'][1])) {
685 $data['case'][1]['existing_case_status'] = (array) wf_crm_aval($data['case'][1], 'case:1:status_id');
686 $data['case'][1]['case'][1]['client_id'] = 1;
688 drupal_write_record('webform_civicrm_forms', $form, 'nid');
692 return st('Upgrade successful.');
696 * This update will modify all "Existing Contact" fields to enable the "Enforce Permissions" setting.
697 * Earlier versions of this module did not make it sufficiently clear that this setting should almost always be enabled, and contained bugs that were sometimes worked-around by disabling the setting.
698 * If you have configured custom contact permissions that maintain security without needing "Enforce Permissions" to be enabled, please review your webforms and disable it as needed (with care).
700 function webform_civicrm_update_7302() {
701 db_update('webform_component')
702 ->condition('type', 'civicrm_contact')
703 ->expression('extra', 'REPLACE(extra, \'"check_permissions";i:0\', \'"check_permissions";i:1\')')
706 return st('All "Existing Contact" fields have been modified to enable the "Enforce Permissions" setting. In most cases you do not need to further modify this setting. You may wish to review your webforms if you are an advanced user and have configured custom contact permissions.');
710 * Add column for contribution_id to webform_civicrm_submissions table.
712 function webform_civicrm_update_7400() {
718 'description' => 'FK to {civicrm_contribution} id',
720 db_add_field('webform_civicrm_submissions', 'contribution_id', $field);
724 * Move civicrm entity ids into a serialized array for each submission.
726 function webform_civicrm_update_7401(&$batch) {
727 // Initialize batch api and create new field
728 if (!isset($batch['progress'])) {
732 'description' => 'Array of entity ids for this submission',
734 db_add_field('webform_civicrm_submissions', 'civicrm_data', $field);
735 $batch['progress'] = $batch['current_sid'] = 0;
736 $batch['max'] = db_query('SELECT COUNT(sid) FROM {webform_civicrm_submissions}')->fetchField();
739 // Process the next group of submissions.
740 $result = db_query("SELECT * FROM {webform_civicrm_submissions} WHERE sid > {$batch['current_sid']} ORDER BY sid LIMIT 500");
741 foreach ($result as $row) {
743 if ($row->activity_id) {
744 $data['activity'] = array(1 => array('id' => $row->activity_id));
746 if ($row->contribution_id) {
747 $data['contribution'] = array(1 => array('id' => $row->contribution_id));
749 db_update('webform_civicrm_submissions')
750 ->condition('sid', $row->sid)
751 ->fields(array('civicrm_data' => serialize($data)))
754 $batch['current_sid'] = $row->sid;
755 $batch['progress']++;
758 // Update batch progress bar
759 $batch['#finished'] = ($batch['progress'] >= $batch['max']) ? TRUE : ($batch['progress'] / $batch['max']);
761 // Drop old fields when done
762 if ($batch['#finished'] === TRUE) {
763 db_drop_field('webform_civicrm_submissions', 'activity_id');
764 db_drop_field('webform_civicrm_submissions', 'contribution_id');
769 * Upgrade schema to handle multiple activities and cases
771 function webform_civicrm_update_7402() {
772 civicrm_initialize();
773 module_load_include('inc', 'webform', 'includes/webform.components');
774 module_load_include('inc', 'webform_civicrm', 'includes/utils');
775 $forms = db_query("SELECT * FROM {webform_civicrm_forms}");
776 foreach ($forms as $form) {
777 $data = $orig = unserialize($form->data);
778 if (isset($data['activity'][1]['activity'][1])) {
779 $data['activity']['number_of_activity'] = 1;
780 // Fix misnamed fields
781 foreach (array('campaign_id', 'survey_id') as $field) {
782 if (!empty($data['activity'][1]['activity'][1]['activity_' . $field])) {
783 $data['activity'][1]['activity'][1][$field] = $data['activity'][1]['activity'][1]['activity_' . $field];
784 unset($data['activity'][1]['activity'][1]['activity_' . $field]);
787 // Set "file on case" settings if activity and case are both set
788 // Previous behavior was to always file on case
789 if (!empty($data['case'][1]['case'][1]['case_type_id'])) {
790 $data['activity'][1]['case_type_id'] = $data['case'][1]['case'][1]['case_type_id'];
791 $data['activity'][1]['case_status_id'] = (array) wf_crm_aval($data['case'][1]['case'][1], 'status_id');
792 $data['activity'][1]['case_contact_id'] = wf_crm_aval($data['case'][1]['case'][1], 'client_id', 1);
794 // Create hidden field for activity subject to replace the removed "default subject" setting
795 $fid = 'civicrm_1_activity_1_activity_subject';
796 if (!(db_query("SELECT nid FROM {webform_component} WHERE form_key = '$fid' AND nid = {$form->nid}")->fetchField())) {
800 'value' => $data['activity'][1]['activity'][1]['subject'],
802 ) + wf_crm_get_field('activity_subject');
803 webform_component_defaults($component);
804 webform_component_insert($component);
807 if (isset($data['case'][1]['case'][1])) {
808 $data['case']['number_of_case'] = 1;
810 if ($data !== $orig) {
811 db_update('webform_civicrm_forms')
812 ->fields(array('data' => serialize($data)))
813 ->condition('nid', $form->nid)