You are here

function wf_crm_component_insert in Webform CiviCRM Integration 7.3

Add a CiviCRM field to a webform

Parameters

$field: array: Webform field info

$enabled: array: Array of enabled fields (reference)

$settings: webform_civicrm configuration for this form

2 calls to wf_crm_component_insert()
webform_civicrm_update_7300 in ./webform_civicrm.install
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
wf_crm_configure_form_submit in ./webform_civicrm_admin.inc
Submission handler, saves CiviCRM options for a Webform node

File

./webform_civicrm_admin.inc, line 1213

Code

function wf_crm_component_insert(&$field, &$enabled, $settings) {
  list(, $c, $ent, $n, $table, $name) = explode('_', $field['form_key'], 6);
  $contact_type = $settings['data']['contact'][$c]['contact'][1]['contact_type'];

  // Replace the # token with set number (or append to the end if no token)
  if ($n > 1 || $table == 'relationship') {
    if (strpos($field['name'], '#') === FALSE) {
      $field['name'] .= " {$n}";
    }
    else {
      $field['name'] = str_replace('#', $n, $field['name']);
    }
  }
  else {
    $field['name'] = str_replace(' #', '', $field['name']);
  }
  if ($name == 'contact_sub_type') {
    list($contact_types, $sub_types) = wf_crm_get_contact_types();
    $field['name'] .= ' ' . $contact_types[$contact_type];
  }

  // Defaults for existing contact field
  if ($name == 'existing') {
    $vals = $enabled + $settings;

    // Set the allow_create flag based on presence of name or email fields
    $field['extra']['allow_create'] = $a = wf_crm_name_field_exists($vals, $c, $contact_type);
    $field['extra']['none_prompt'] = $a ? t('+ Create new contact +') : t('- None Found -');
    if ($c == 1 && $contact_type == 'individual') {

      // Default to hidden field for 1st contact
      $field['extra'] += array(
        'widget' => 'hidden',
        'default' => 'user',
      );
    }
  }

  // A width of 20 is more sensible than Drupal's default of 60
  if (($field['type'] == 'textfield' || $field['type'] == 'email') && empty($field['extra']['width'])) {
    $field['extra']['width'] = 20;
  }

  // Default checksum lifespan
  if ($name == 'cs') {
    require_once 'CRM/Core/BAO/Setting.php';
    $field['value'] = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'checksum_timeout', NULL, 7);
  }

  // Support html_textarea module
  if ($field['type'] == 'html_textarea') {
    $field['value']['format'] = filter_default_format();
    $field['value']['value'] = '';
  }

  // Retrieve option list
  if ($options = wf_crm_field_options($field, 'component_insert', $settings['data'])) {
    $field['extra']['items'] = $options;
    $list = wf_crm_get_list($table, $name);
    if (!isset($field['extra']['aslist']) && empty($field['extra']['multiple'])) {
      $field['extra']['aslist'] = 1;
    }
    if (is_numeric($list)) {

      // Lookup defaults for custom fields
      $dao =& CRM_Core_DAO::executeQuery('SELECT value FROM civicrm_option_value
        WHERE is_default AND is_active AND option_group_id = ' . $list);
      $field['value'] = '';
      while ($dao
        ->fetch()) {
        $field['value'] .= ($field['value'] ? ',' : '') . $dao->value;
      }
    }
  }
  if ($table == 'other' || $field['type'] == 'hidden') {
    unset($field['extra']['description']);
  }

  // Create fieldsets - for contact and contact-specific event fields only
  if (!empty($settings['create_fieldsets']) && $ent != 'activity' && $ent != 'case' && ($ent != 'participant' || wf_crm_aval($settings['data'], 'participant_reg_type') == 'separate')) {
    $sid = 'civicrm_' . $c . '_contact_1_fieldset_fieldset';
    if (!isset($enabled[$sid])) {

      // Create webform fieldset for this contact
      $new_set = array(
        'nid' => $field['nid'],
        'form_key' => $sid,
        'type' => 'fieldset',
        'name' => t('Contact !num', array(
          '!num' => $c,
        )),
        'weight' => $c,
      );
      $new_set += webform_component_invoke('fieldset', 'defaults');
      $enabled[$sid] = webform_component_insert($new_set);
    }
    $field['pid'] = $enabled[$sid];
  }

  // Merge defaults and create webform component
  $field += array(
    'extra' => array(),
  );
  if ($defaults = webform_component_invoke($field['type'], 'defaults')) {
    $field += $defaults;
  }
  if (isset($enabled[$field['form_key']])) {
    $field['cid'] = $enabled[$field['form_key']];
    webform_component_update($field);
  }
  else {
    $enabled[$field['form_key']] = webform_component_insert($field);
  }
}