You are here

function webform_civicrm_update_6205 in Webform CiviCRM Integration 7.3

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

Upgrade for CiviCRM 4.1 compatibility.

File

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

Code

function webform_civicrm_update_6205() {
  civicrm_initialize();
  module_load_include('inc', 'webform_civicrm', 'webform_civicrm_utils');
  module_load_include('inc', 'webform', 'includes/webform.components');
  $db = db_query("SELECT * FROM {webform_component} WHERE form_key LIKE 'civicrm_%%_other_tags' OR form_key LIKE 'civicrm_%%_other_group%%'");
  $tag = $group = array();
  foreach ($db as $row) {
    if ($pieces = wf_crm_explode_key($row->form_key)) {
      list(, $c, $ent, $n, $table, $name) = $pieces;
      if ($name == 'groups') {
        if (empty($group[$row->nid][$c])) {
          $group[$row->nid][$c] = array(
            'value' => '',
          );
        }
        continue;
      }
      $type = $name == 'groups_hidden' ? 'group' : 'tag';
      ${$type}[$row->nid][$c] = (array) $row;
    }
  }
  db_query("UPDATE {webform_component} SET form_key = REPLACE(form_key, 'other_groups', 'other_group') WHERE form_key LIKE 'civicrm_%%_other_groups'");
  $db = db_query("SELECT * FROM {webform_civicrm_forms}");
  foreach ($db as $form) {
    $nid = $form->nid;
    $data = unserialize($form->data);
    if (!empty($data['activity'][1])) {
      $data['activity'][1]['details'] = array(
        'view_link' => !empty($data['activity'][1]['add_link']) ? 'view_link' : 0,
      );
      $data['activity'][1]['existing_activity_status'] = empty($data['activity'][1]['existing_activity_status']) ? array() : array(
        $data['activity'][1]['existing_activity_status'],
      );
    }
    foreach ($data['contact'] as &$con) {
      if (empty($con['contact'][1]['contact_sub_type'])) {
        $con['contact'][1]['contact_sub_type'] = array();
      }
      else {
        $con['contact'][1]['contact_sub_type'] = (array) $con['contact'][1]['contact_sub_type'];
      }
    }
    if (!empty($tag[$nid]) || !empty($group[$nid])) {
      $node = node_load($nid);
      $both = wf_crm_aval($tag, $nid, array()) + wf_crm_aval($group, $nid, array());
      foreach ($both as $c => $val) {
        $data['contact'][$c]['number_of_other'] = 1;
        if (!empty($tag[$nid][$c])) {
          $tag_ids = array();
          $tag_names = drupal_explode_tags($tag[$nid][$c]['value']);
          foreach ($tag_names as $t) {
            $result = wf_civicrm_api('tag', 'get', array(
              'name' => $t,
            ));
            if ($tid = wf_crm_aval($result, 'id')) {
              $tag_ids[$tid] = $tid;
            }
          }
          $data['contact'][$c]['other'][1]['tag'] = $tag_ids;
          webform_component_delete($node, $tag[$nid][$c]);
        }
        if (!empty($group[$nid][$c]['form_key'])) {
          if (empty($group[$nid][$c]['value'])) {
            $data['contact'][$c]['other'][1]['group'] = array();
          }
          else {
            $data['contact'][$c]['other'][1]['group'] = drupal_map_assoc(explode(',', $group[$nid][$c]['value']));
          }
          webform_component_delete($node, $group[$nid][$c]);
        }
      }
    }
    $form->data = $data;
    drupal_write_record('webform_civicrm_forms', $form, 'nid');
  }
  return st('Hidden tag and group fields have been removed, those options are now integrated into the CiviCRM tab of webforms.');
}