You are here

function webform_civicrm_update_7300 in Webform CiviCRM Integration 7.5

Same name and namespace in other branches
  1. 7.3 webform_civicrm.install \webform_civicrm_update_7300()
  2. 7.4 webform_civicrm.install \webform_civicrm_update_7300()

Note: There are differences in how contact references and relationships work in the 3.x branch. Read the upgrade instructions at http://drupal.org/node/1615380

File

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

Code

function webform_civicrm_update_7300() {
  module_load_include('inc', 'webform_civicrm', 'includes/wf_crm_admin_form');
  module_load_include('inc', 'webform_civicrm', 'includes/contact_component');
  module_load_include('inc', 'webform', 'includes/webform.components');
  civicrm_initialize();
  $fields = wf_crm_get_fields();
  $db = db_query("SELECT * FROM {webform_civicrm_forms}");
  foreach ($db as $form) {
    $form = (array) $form;
    $form['data'] = unserialize($form['data']);
    $data =& $form['data'];
    $node = node_load($form['nid']);
    $enabled = wf_crm_enabled_fields($node);

    // Add existing contact field
    $field = $fields['contact_existing'];
    $field['nid'] = $form['nid'];
    $field['form_key'] = 'civicrm_1_contact_1_contact_existing';
    $field['weight'] = -255;
    $field['extra']['default'] = $form['contact_matching'] ? 'user' : '';
    $field['extra']['hide_fields'] = [];
    wf_crm_admin_form::insertComponent($field, $enabled, $form);

    // Activity target
    if (!empty($data['activity'][1]['activity'])) {
      foreach ($data['contact'] as $c => $contact) {
        if (!empty($contact['activity_target'])) {
          $data['activity'][1]['activity'][1]['target_contact_id'][$c] = $c;
        }
      }
    }

    // Update relationship type ids
    foreach ($data['contact'] as $c => &$contact) {
      if (!empty($contact['relationship'])) {
        foreach ($contact['relationship'] as $n => $rel) {
          if (!empty($rel['relationship_type_id'])) {
            $r_types = wf_crm_field_options([
              'form_key' => "civicrm_{$c}_contact_{$n}_relationship_relationship_type_id",
            ], 'config_form', $data);
            $type = $rel['relationship_type_id'];
            list($r, $a_b) = explode('_', $type);
            if (!isset($r_types[$type]) && isset($r_types[$r . '_r'])) {
              $contact['relationship'][$n]['relationship_type_id'] = $r . '_r';
            }

            // Change current employer rel
            if ($r == 'ce') {

              // Get regular employer rel type
              foreach (wf_crm_get_relationship_types() as $e_type => $r) {
                if ($r['name_a_b'] == 'Employee of') {
                  break;
                }
              }
              $contact['relationship'][$n]['relationship_type_id'] = $e_type . '_' . $a_b;
              $i = $a_b == 'a' ? $c : $n;
              $e = $a_b == 'a' ? $n : $c;
              $data['contact'][$i]['contact'][1]['employer_id'] = $e;
              if ($n == 1 && empty($enabled["civicrm_{$c}_contact_1_contact_existing"])) {

                // Add existing contact field
                $field = $fields['contact_existing'];
                $field['name'] = $a_b == 'a' ? t('Employee') : t('Employer');
                $field['nid'] = $form['nid'];
                $field['weight'] = -255;
                $field['form_key'] = "civicrm_{$c}_contact_1_contact_existing";
                $field['extra']['widget'] = 'hidden';
                $field['extra']['default'] = 'relationship';
                $field['extra']['default_relationship'] = [
                  $e_type . '_' . $a_b,
                ];
                $field['extra']['hide_fields'] = [];
                wf_crm_admin_form::insertComponent($field, $enabled, $form);
              }
            }
          }
        }
      }
    }

    // Activity assignee & case manager
    foreach ([
      'activity' => 'assignee_contact_id',
      'case' => 'creator_id',
    ] as $table => $name) {
      if (!empty($data[$table][1][$table][1][$name])) {
        $val =& $data[$table][1][$table][1][$name];
        if ($val[0] == '#') {
          $val = substr($val, 1);
        }
        else {

          // Create contact
          $c = count($data['contact']) + 1;
          $data['contact'][$c] = [
            'contact' => [
              1 => [
                'contact_type' => 'individual',
                'contact_sub_type' => [],
              ],
            ],
          ];

          // Set existing contact id
          $field = $fields['contact_existing'] + _webform_defaults_civicrm_contact();
          $field['nid'] = $form['nid'];
          $field['form_key'] = 'civicrm_' . $c . '_contact_1_contact_existing';
          $field['weight'] = 255;
          $field['name'] = $table == 'case' ? st('Case Manager') : st('Activity Assignee');
          $field['extra']['widget'] = 'hidden';
          $field['extra']['default'] = 'contact_id';
          $field['extra']['default_contact_id'] = $val;
          $val = $c;
          webform_component_insert($field);
        }
      }
    }

    // Update some fields
    foreach ($enabled as $key => $field_id) {
      list(, $c, $ent, $n, $table, $name) = wf_crm_explode_key($key);
      $field = $node->webform['components'][$field_id];

      // Convert ContactRef fields to real contacts
      if (wf_crm_aval($fields, "{$table}_{$name}:data_type") == 'ContactReference') {

        // No big changes if selecting from webform contacts, just strip the # symbols
        if (empty($field['extra']['civicrm_group'])) {
          $field['value'] = str_replace('#', '', $field['value']);

          // Hidden fields are no longer needed for referencing a webform contact
          if ($field['type'] == 'hidden') {
            $data[$ent][$c][$table][$n][$name] = $field['value'];
            webform_component_delete($node, $field);
            continue;
          }
          if (!empty($field['extra']['items'])) {
            $old = wf_crm_str2array($field['extra']['items']);
            $new = [];
            foreach ($old as $k => $v) {
              $new[substr($k, 1)] = $v;
            }
            $field['extra']['items'] = wf_crm_array2str($new);
          }
        }
        elseif ($field['type'] === 'select') {
          $group = $field['extra']['civicrm_group'];

          // Get a sample from the group for a hint of this contact's type
          $result = wf_civicrm_api('contact', 'get', [
            'rowCount' => 1,
            'group' => [
              $group => 1,
            ],
            'return.contact_type' => 1,
          ]);
          $type = 'individual';
          if (!empty($result['values'])) {
            $result = array_pop($result['values']);
            $type = strtolower($result['contact_type']);
          }

          // Create contact
          $c = count($data['contact']) + 1;
          $data['contact'][$c] = [
            'contact' => [
              1 => [
                'contact_type' => $type,
                'contact_sub_type' => [],
              ],
            ],
          ];
          $data[$ent][$c][$table][$n][$name] = $c;

          // Change this field type from select to civicrm_contact
          $field['form_key'] = 'civicrm_' . $c . '_contact_1_contact_existing';
          $field['type'] = 'civicrm_contact';
          $field['extra'] = $fields['contact_existing']['extra'];
          $field['extra']['widget'] = 'select';
          $field['extra']['none_prompt'] = t('- None Found -');
          $default = _webform_defaults_civicrm_contact();
          $field['extra']['filters'] = $default['extra']['filters'];
          $field['extra']['filters']['group'] = [
            $group,
          ];
        }
        webform_component_update($field);
      }
      elseif ($name == 'relationship_type_id') {
        $r_types = wf_crm_field_options($field, '', $data);
        if (!empty($field['extra']['items'])) {
          $old = wf_crm_str2array($field['extra']['items']);
          $new = [];
          foreach ($old as $k => $v) {
            if (!isset($r_types[$k])) {
              list($r, $a_b) = explode('_', $k);
              if (isset($r_types[$r . '_r'])) {
                $k = $r . '_r';
              }
              else {
                continue;
              }
            }
            $new[$k] = $v;
          }
          $field['extra']['items'] = wf_crm_array2str($new);
        }

        // Default value
        if (!empty($field['value'])) {
          list($r, $a_b) = explode('_', $field['value']);
          if (isset($r_types[$r . '_r'])) {
            $field['value'] = $r . '_r';
          }
        }
        webform_component_update($field);
      }
    }
    drupal_write_record('webform_civicrm_forms', $form, 'nid');
  }
  db_drop_field('webform_civicrm_forms', 'contact_matching');
  $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!');
  drupal_set_message($msg, 'warning');
  return $msg;
}