You are here

function wf_crm_configure_form in Webform CiviCRM Integration 7.3

Same name and namespace in other branches
  1. 7.5 includes/wf_crm_admin_form.inc \wf_crm_configure_form()
  2. 7.4 includes/wf_crm_admin_form.inc \wf_crm_configure_form()

Drupal form builder callback Form to configure CiviCRM options for a Webform

1 string reference to 'wf_crm_configure_form'
webform_civicrm_menu in ./webform_civicrm.module
Implements hook_menu().

File

./webform_civicrm_admin.inc, line 14

Code

function wf_crm_configure_form($form, &$form_state, $node) {
  $form = array();
  $form_state['storage']['nid'] = $node->nid;

  // Display confirmtion message before deleting fields
  if (!empty($form_state['storage']['msg'])) {
    $form['#prefix'] = $form_state['storage']['msg'];
    $form['cancel'] = $form['disable'] = $form['delete'] = array(
      '#type' => 'submit',
    );
    $form['delete']['#value'] = t('Remove Fields and Save Settings');
    $form['disable']['#value'] = t('Leave Fields and Save Settings');
    $form['cancel']['#value'] = t('Cancel (go back)');
    return $form;
  }
  $form['#attached']['js'][] = drupal_get_path('module', 'webform_civicrm') . '/' . 'webform_civicrm_admin.js';
  $form['#attached']['css'][] = drupal_get_path('module', 'webform_civicrm') . '/webform_civicrm_admin.css';
  civicrm_initialize();
  require_once 'CRM/Core/BAO/Setting.php';
  $config = CRM_Core_Config::singleton();
  $fields = wf_crm_get_fields();
  $sets = wf_crm_get_fields('sets');

  // Initialize form on first view
  if (empty($form_state['values'])) {

    // For an existing civi-webform, load settings
    if (isset($node->webform_civicrm)) {
      $settings = $node->webform_civicrm;
    }
    else {
      $settings = array(
        'data' => array(
          'contact' => array(
            1 => array(
              'contact' => array(
                1 => array(
                  'contact_type' => 'individual',
                  'contact_sub_type' => array(),
                ),
              ),
            ),
          ),
        ),
        'confirm_subscription' => 1,
        'create_fieldsets' => 1,
        'new_contact_source' => check_plain($node->title),
        'civicrm_1_contact_1_contact_first_name' => 'create_civicrm_webform_element',
        'civicrm_1_contact_1_contact_last_name' => 'create_civicrm_webform_element',
        'civicrm_1_contact_1_contact_existing' => 'create_civicrm_webform_element',
      );
    }

    // Warn of incompatible modules
    $incompatibilities = array(
      'form_builder_webform' => t('Form builder Webform UI'),
      'webform_alt_ui' => t('Webform Alternate UI'),
    );
    foreach ($incompatibilities as $module => $label) {
      if (module_exists($module)) {
        drupal_set_message(t('The module %module is not compatible with Webform CiviCRM Integration. Please disable it before continuing.', array(
          '%module' => $label,
        )), 'error');
      }
    }
  }
  else {
    $settings = wf_crm_aval($form_state['storage'], 'vals', $form_state['values']);
    wf_crm_process_form_settings($settings);
    unset($form_state['storage']['vals']);
  }

  // Merge in existing fields
  $existing = array_keys(wf_crm_enabled_fields($node, NULL, TRUE));
  $settings += array_fill_keys($existing, 'create_civicrm_webform_element');
  $tokens = '<strong>' . t('Contact 1 Tokens:') . '</strong> [' . implode('], [', wf_crm_get_fields('tokens')) . '].';
  list($contact_types, $sub_types) = wf_crm_get_contact_types();
  $data = $settings['data'];
  $contacts = count($data['contact']);
  $form['#attached']['js'][] = array(
    'data' => array(
      'webform_civicrm' => array(
        'rTypes' => wf_crm_get_relationship_types(),
      ),
    ),
    'type' => 'setting',
  );

  // Sort fields by set
  foreach ($fields as $fid => $field) {
    list($group) = explode('_', $fid, 2);
    $sets[$group]['fields'][$fid] = $field;
  }
  $form['nid'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable CiviCRM Processing'),
    '#default_value' => !empty($settings['nid']),
    '#return_value' => $node->nid,
    '#description' => t('Contacts will be created or updated when users submit this webform.'),
    '#prefix' => '<a id="web-civi-help" class="module-link module-link-help" href="http://drupal.org/node/1615380" target="_blank">' . t('Webform CiviCRM Help') . '</a>',
  );
  $form['number_of_contacts'] = array(
    '#type' => 'select',
    '#title' => t('Number of Contacts'),
    '#default_value' => $contacts,
    '#options' => drupal_map_assoc(range(1, 30)),
  );
  $form['change_form_settings'] = array(
    '#type' => 'button',
    '#value' => t('Change Form Settings'),
    '#prefix' => '<div id="no-js-button-wrapper" class="messages warning">',
    '#suffix' => '<div>' . t('You have Javascript disabled. You will need to click this button after changing any option to see the result.') . '</div></div>',
  );
  $form['webform_civicrm'] = array(
    '#type' => 'vertical_tabs',
  );

  // Contact settings
  foreach ($data['contact'] as $n => $c) {
    $form['contact_' . $n] = array(
      '#type' => 'fieldset',
      '#title' => t('Contact !num', array(
        '!num' => $n,
      )),
      '#description' => $n > 1 ? NULL : t('Primary contact. Usually assumed to be the person filling out the form.') . '<br />' . t('Check the "Existing Contact" box to autofill with the current user (or another contact).'),
      '#group' => 'webform_civicrm',
      '#attributes' => array(
        'class' => array(
          'contact-icon-' . $c['contact'][1]['contact_type'],
        ),
      ),
    );
    $form['contact_' . $n][$n . '_contact_type'] = array(
      '#type' => 'select',
      '#title' => t('Contact Type'),
      '#default_value' => $c['contact'][1]['contact_type'],
      '#options' => $contact_types,
      '#prefix' => '<div class="contact-type-select">',
      '#suffix' => '</div>',
    );
    wf_crm_ajax_item($form, 'contact_' . $n, $n . '_contact_type', 'contact_subtype_wrapper');

    // Contact sub-type
    $fid = 'civicrm_' . $n . '_contact_1_contact_contact_sub_type';
    if (!empty($sub_types[$c['contact'][1]['contact_type']])) {
      $field = wf_crm_configure_form_item($fid, $fields['contact_contact_sub_type'], $settings);
      $field['#title'] .= ' ' . $contact_types[$c['contact'][1]['contact_type']];
      $form['contact_' . $n]['contact_subtype_wrapper'][$fid] = $field;
      wf_crm_ajax_item($form, 'contact_' . $n . ':contact_subtype_wrapper', $fid, 'contact_custom_wrapper');
    }
    else {
      $form['contact_' . $n]['contact_subtype_wrapper'][$fid] = array(
        '#type' => 'value',
        '#value' => array(),
      );
    }
    $form['contact_' . $n]['contact_subtype_wrapper']['clear'] = array(
      '#markup' => '<div class="clearfix"> </div>',
    );
    foreach ($sets as $sid => $set) {
      if ($set['entity_type'] != 'contact') {
        continue;
      }
      if ($sid == 'relationship' && !($set['max_instances'] = $n - 1)) {
        continue;
      }
      if (!empty($set['contact_type']) && $set['contact_type'] != $c['contact'][1]['contact_type']) {
        continue;
      }
      if (!empty($set['sub_types'])) {
        if (!array_intersect($c['contact'][1]['contact_sub_type'], $set['sub_types'])) {
          continue;
        }
        $pos =& $form['contact_' . $n]['contact_subtype_wrapper']['contact_custom_wrapper'];
        $path = 'contact_' . $n . ':contact_subtype_wrapper:contact_custom_wrapper';
      }
      elseif (!empty($set['contact_type']) || $sid == 'contact') {
        $pos =& $form['contact_' . $n]['contact_subtype_wrapper'];
        $path = 'contact_' . $n . ':contact_subtype_wrapper';
      }
      else {
        $pos =& $form['contact_' . $n];
        $path = 'contact_' . $n;
      }
      if (!empty($set['max_instances'])) {
        if (!isset($c['number_of_' . $sid])) {
          $c['number_of_' . $sid] = 0;
        }
        $set['label'] = $sid == 'relationship' ? t('Relationship') : $set['label'];
        $selector = array(
          '#type' => 'select',
          '#default_value' => $c['number_of_' . $sid],
          '#prefix' => '<div class="number-of">',
          '#suffix' => '</div>',
        );
        if ($set['max_instances'] > 1) {
          $selector['#options'] = range(0, $set['max_instances']);
          $selector['#title'] = t('Number of %type Fields', array(
            '%type' => $set['label'],
          ));
        }
        else {
          $selector['#options'] = array(
            t('No'),
            t('Yes'),
          );
          $selector['#title'] = t('Enable %type Fields', array(
            '%type' => $set['label'],
          ));
        }
        $pos['contact_' . $n . '_number_of_' . $sid] = $selector;
        wf_crm_ajax_item($form, $path, 'contact_' . $n . '_number_of_' . $sid, $n . $sid . '_wrapper');
      }
      else {
        $c['number_of_' . $sid] = 1;
      }
      for ($i = 1; $i <= $c['number_of_' . $sid]; ++$i) {
        $fsid = 'civicrm_' . $n . $sid . $i . '_fieldset';
        $fieldset = array(
          '#type' => 'fieldset',
          '#title' => $set['label'],
          '#attributes' => array(
            'id' => $fsid,
            'class' => array(
              'web-civi-checkbox-set',
            ),
          ),
          'js_select' => wf_crm_js_select($fsid),
        );
        if (isset($set['max_instances']) && $set['max_instances'] > 1 || $sid == 'relationship') {
          $fieldset['#title'] .= ' ' . $i;
          if (in_array($sid, array(
            'email',
            'address',
            'phone',
            'website',
          )) && $i == 1) {
            $fieldset['#title'] .= ' ' . t('(primary)');
          }
        }
        foreach ($set['fields'] as $fid => $field) {
          if ($fid == 'contact_contact_sub_type' || $fid == 'address_master_id' && $contacts == 1 || isset($field['contact_type']) && $field['contact_type'] != $c['contact'][1]['contact_type']) {
            continue;
          }
          $fid = 'civicrm_' . $n . '_contact_' . $i . '_' . $fid;
          $fieldset[$fid] = wf_crm_configure_form_item($fid, $field, $settings);
        }
        if (isset($set['max_instances'])) {
          $pos[$n . $sid . '_wrapper'][$n . $sid . $i . '_fieldset'] = $fieldset;
        }
        else {
          $pos[$n . $sid . $i . '_fieldset'] = $fieldset;
        }
      }
    }
  }

  // Configure messages
  $form['prefix'] = array(
    '#type' => 'fieldset',
    '#title' => t('Introduction Text'),
    '#description' => t('This text will appear at the top of the form. You may configure separate messages for known contacts (logged in users, or users following a hashed link from civimail) and unknown (anonymous) users.'),
    '#group' => 'webform_civicrm',
    '#attributes' => array(
      'class' => array(
        'civi-icon-text',
      ),
    ),
  );
  $form['prefix']['prefix_known'] = array(
    '#type' => 'textarea',
    '#title' => t('Introduction text for known contacts'),
    '#default_value' => wf_crm_aval($settings, 'prefix_known'),
    '#description' => $tokens,
  );
  $form['prefix']['prefix_unknown'] = array(
    '#type' => 'textarea',
    '#title' => t('Introduction text for unknown contacts'),
    '#default_value' => wf_crm_aval($settings, 'prefix_unknown'),
    '#description' => t('No tokens available for unknown contacts.'),
  );
  $form['st_message'] = array(
    '#type' => 'fieldset',
    '#title' => t('"Not You?" Message'),
    '#description' => t('Prompt for users who are logged in as, or following a hashed link for, someone else.'),
    '#group' => 'webform_civicrm',
    '#attributes' => array(
      'class' => array(
        'civi-icon-message',
      ),
    ),
  );
  $form['st_message']['toggle_message'] = array(
    '#type' => 'checkbox',
    '#title' => t('Display message to known contacts?'),
    '#default_value' => !empty($settings['message']),
  );
  $form['st_message']['message'] = array(
    '#type' => 'textfield',
    '#title' => t('Text (displayed as a status message)'),
    '#default_value' => wf_crm_aval($settings, 'message', t("You are viewing this form as [display name]. Please {click here if that's not you}.")),
    '#size' => 100,
    '#maxlength' => 255,
    '#description' => t('Enclose your "not you" link text in curly brackets {like this}.') . '<p>' . $tokens . '</p>',
  );

  // Case and activity settings
  $case_enabled = in_array('CiviCase', $config->enableComponents, TRUE) && ($case_types = wf_crm_get_options('case_type'));
  $form['act'] = array(
    '#type' => 'fieldset',
    '#title' => $case_enabled ? t('Activity/Case') : t('Activity'),
    '#group' => 'webform_civicrm',
    '#attributes' => array(
      'class' => array(
        'civi-icon-activity',
      ),
    ),
  );
  if ($case_enabled) {
    $form['act']['case_type_id'] = array(
      '#type' => 'select',
      '#title' => t('CiviCase Type'),
      '#description' => t('Is this activity part of a case?'),
      '#options' => array(
        t('- No Case -'),
      ) + $case_types,
    );
    wf_crm_ajax_item($form, 'act', 'case_type_id', 'case');
  }
  $case_type = $activity_type = NULL;
  $campaign_act_types = array();
  if ($case_enabled && !empty($data['case'][1]['case'][1]['case_type_id'])) {
    $case_type = $form['act']['case_type_id']['#default_value'] = $data['case'][1]['case'][1]['case_type_id'];
    $case_type_name = $case_types[$case_type];
    $form['act']['case']['case_fields'] = array(
      '#type' => 'fieldset',
      '#title' => $case_type_name . ' ' . t('Case'),
    );
    $form['act']['case']['case_fields']['existing_case_status'] = array(
      '#type' => 'select',
      '#title' => t('Update Existing Case'),
      '#description' => t('If any of the above options are selected and a %type case with that status already exists for the client, it will be autofilled and updated.', array(
        '%type' => $case_type_name,
      )) . '<br />' . t('Note: a case can also be autofilled by passing "caseid" in the url.'),
      '#options' => wf_crm_get_options('case_status'),
      '#default_value' => wf_crm_aval($data, 'case:1:existing_case_status', array()),
      '#multiple' => TRUE,
    );
    require_once 'CRM/Case/XMLProcessor/Process.php';
    $case_info = new CRM_Case_XMLProcessor_Process();
    $act_types = $case_info
      ->get($case_type_name, 'ActivityTypes');
  }
  else {
    $act_types = wf_crm_get_options('activity_type');

    // Include campaign types
    if (in_array('CiviCampaign', $config->enableComponents, TRUE)) {
      $campaign_act_types = wf_crm_get_options('activity_type', 'CiviCampaign');
      $act_types += $campaign_act_types;
      asort($act_types);
    }
  }
  $form['act']['case']['activity_type_id'] = array(
    '#type' => 'select',
    '#title' => t('Activity Type'),
    '#options' => array(
      t('- No Activity -'),
    ) + $act_types,
  );
  wf_crm_ajax_item($form, 'act:case', 'activity_type_id', 'activity_fields');
  if (!empty($data['activity'][1]['activity'][1]['activity_type_id']) && isset($act_types[$data['activity'][1]['activity'][1]['activity_type_id']])) {
    $form['act']['case']['activity_fields']['#type'] = 'fieldset';
    $activity_type = $form['act']['case']['activity_type_id']['#default_value'] = $settings['activity_type_id'] = $data['activity'][1]['activity'][1]['activity_type_id'];
    $form['act']['case']['activity_fields']['#title'] = $act_types[$activity_type] . ' ' . t('Activity');
    $form['act']['case']['activity_fields']['existing_activity_status'] = array(
      '#type' => 'select',
      '#title' => t('Update Existing Activity'),
      '#description' => t('If any of the above options are selected and a %type activity with that status already exists for !ent, it will be autofilled and updated.', array(
        '!ent' => $case_type ? t('the case') : t('contact 1'),
        '%type' => $act_types[$settings['activity_type_id']],
      )) . '<br />' . t('Note: an activity can also be autofilled by passing "aid" in the url.'),
      '#options' => wf_crm_get_options('activity_status'),
      '#default_value' => wf_crm_aval($data, 'activity:1:existing_activity_status', array()),
      '#multiple' => TRUE,
    );
    $form['act']['case']['activity_fields']['activity_subject'] = array(
      '#type' => 'textfield',
      '#title' => t('Default Activity Subject'),
      '#maxlength' => 255,
      '#required' => TRUE,
      '#description' => t('You can override this default by allowing users to enter a subject (enable "Activity Subject" field below)'),
      '#default_value' => wf_crm_aval($data, 'activity:1:activity:1:subject', check_plain($node->title)),
    );
    $form['act']['case']['activity_fields']['activity_details'] = array(
      '#type' => 'checkboxes',
      '#options' => array(
        'entire_result' => t('Include <em>entire</em> webform submission in activity details'),
        'view_link' => t('Include link to <em>view</em> webform submission in activity details'),
        'edit_link' => t('Include link to <em>edit</em> webform submission in activity details'),
      ),
      '#default_value' => wf_crm_aval($data, 'activity:1:details', array(
        'view_link',
      ), TRUE),
    );
  }
  foreach ($sets as $sid => $set) {
    $sid .= '_fieldset';
    $ent = $set['entity_type'];
    if ($ent == 'activity' && ($type = $activity_type) || $ent == 'case' && ($type = $case_type)) {
      if (!empty($set['sub_types']) && !in_array($type, $set['sub_types']) || empty($set['fields'])) {
        continue;
      }
      $form['act']['case'][$ent . '_fields'][$sid] = array(
        '#type' => 'fieldset',
        '#title' => $set['label'],
        '#attributes' => array(
          'id' => $sid,
          'class' => array(
            'web-civi-checkbox-set',
          ),
        ),
        'js_select' => wf_crm_js_select($sid),
      );
      foreach ($set['fields'] as $fid => $field) {
        if ($fid == 'activity_assignee_contact_id' && CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'activity_assignee_notification')) {
          $field['extra']['description'] = t('A copy of this activity will be emailed to the assignee.');
        }
        if ($fid !== 'activity_survey_id') {
          $complete_fid = "civicrm_1_{$ent}_1_{$fid}";
          $form_item = wf_crm_configure_form_item($complete_fid, $field, $settings);
          $form['act']['case'][$ent . '_fields'][$sid][$complete_fid] = $form_item;

          // If the activity type is part of CiviCampaign
          // then attach an AJAX select box for the survey/petition ID
          if (isset($campaign_act_types[$activity_type]) && $fid === 'activity_campaign_id') {
            $pathstr = 'act:case:activity_fields:' . $sid;
            wf_crm_ajax_item($form, $pathstr, $complete_fid, 'campaign_fields');
            $survey_fid = 'activity_survey_id';
            $complete_survey_fid = "civicrm_1_{$ent}_1_{$survey_fid}";
            $survey_form_item = wf_crm_configure_form_item($complete_survey_fid, $sets['activity']['fields']['activity_survey_id'], $settings);
            $form['act']['case']['activity_fields'][$sid]['campaign_fields'][$complete_survey_fid] = $survey_form_item;
          }
        }
      }
    }
  }

  // Event participant settings
  if (in_array('CiviEvent', $config->enableComponents, TRUE)) {
    $form['event'] = array(
      '#type' => 'fieldset',
      '#title' => t('Event Registration'),
      '#group' => 'webform_civicrm',
      '#attributes' => array(
        'class' => array(
          'civi-icon-participant',
        ),
      ),
    );
    $reg_type = wf_crm_aval($data, 'participant_reg_type');
    $form['event']['participant_reg_type'] = array(
      '#type' => 'select',
      '#title' => t('Registration Method'),
      '#default_value' => $reg_type,
      '#options' => array(
        t('- None -'),
        'all' => t('Register all contacts for the same event(s)'),
        'separate' => t('Register each contact separately'),
      ),
    );
    $form['event']['event_type'] = array(
      '#type' => 'select',
      '#title' => t('Show Events of Type'),
      '#options' => array(
        'any' => t('- Any Type -'),
      ) + wf_crm_get_options('event_type'),
      '#default_value' => wf_crm_aval($data, 'reg_options:event_type', 'any'),
      '#prefix' => '<div id="event-reg-options-wrapper"><div class="web-civi-checkbox-set">',
      '#parents' => array(
        'reg_options',
        'event_type',
      ),
      '#tree' => TRUE,
    );
    $form['event']['show_past_events'] = array(
      '#type' => 'checkbox',
      '#title' => t('Show Past Events'),
      '#default_value' => (bool) wf_crm_aval($data, 'reg_options:show_past_events'),
      '#suffix' => '</div>',
      '#parents' => array(
        'reg_options',
        'show_past_events',
      ),
      '#tree' => TRUE,
    );
    $form['event']['reg_options'] = array(
      '#prefix' => '<div class="clearfix"> </div>',
      '#suffix' => '</div>',
      '#type' => 'fieldset',
      '#title' => t('Registration Options'),
      '#collapsible' => TRUE,
      '#tree' => TRUE,
    );
    $form['event']['reg_options']['show_remaining'] = array(
      '#type' => 'select',
      '#title' => t('Show Remaining Space in Events'),
      '#default_value' => wf_crm_aval($data, 'reg_options:show_remaining', 0),
      '#description' => t('Display a message at the top of the form for each event with a registration limit or past end date.'),
      '#options' => array(
        t('Never'),
        'always' => t('Always'),
        '0_full' => t('When full - 0 spaces left'),
      ),
    );
    foreach (array(
      5,
      10,
      20,
      50,
      100,
      200,
      500,
      1000,
    ) as $num) {
      $form['event']['reg_options']['show_remaining']['#options'][$num] = t('When under !num spaces left', array(
        '!num' => $num,
      ));
    }
    $form['event']['reg_options']['validate'] = array(
      '#type' => 'checkbox',
      '#title' => t('Prevent Registration for Past/Full Events'),
      '#default_value' => (bool) wf_crm_aval($data, 'reg_options:validate'),
      '#description' => t('Will not allow the form to be submitted if user registers for an event that is ended or full.'),
    );
    $form['event']['reg_options']['block_form'] = array(
      '#type' => 'checkbox',
      '#title' => t('Block Form Access when Event(s) are Full/Ended'),
      '#default_value' => (bool) wf_crm_aval($data, 'reg_options:block_form'),
      '#description' => t('Hide webform if all the events for the form are full or ended.'),
    );
    wf_crm_ajax_item($form, 'event', 'participant_reg_type', 'participants');
    wf_crm_ajax_item($form, 'event', 'event_type', 'participants');
    wf_crm_ajax_item($form, 'event', 'show_past_events', 'participants');
    for ($n = 1; $reg_type && ($n <= $contacts && $reg_type != 'all' || $n == 1); ++$n) {
      $form['event']['participants'][$n] = array(
        '#type' => 'fieldset',
        '#title' => $reg_type == 'all' ? t('Registration') : t('Contact !num', array(
          '!num' => $n,
        )),
      );
      if (!($num = wf_crm_aval($data, "participant:{$n}:number_of_participant")) || $n > 1 && $reg_type == 'all') {
        $num = 0;
      }
      $form['event']['participants'][$n]['participant_' . $n . '_number_of_participant'] = array(
        '#type' => 'select',
        '#title' => $reg_type == 'all' ? t('Number of Event Sets') : t('Number of Event Sets for Contact !num', array(
          '!num' => $n,
        )),
        '#default_value' => $num,
        '#options' => range(0, $sets['participant']['max_instances']),
        '#prefix' => '<div class="number-of">',
        '#suffix' => '</div>',
      );
      wf_crm_ajax_item($form, "event:participants:{$n}", 'participant_' . $n . '_number_of_participant', 'div');
      $particpant_extensions = array(
        1 => 'role_id',
        2 => 'event_id',
        3 => 'event_type',
      );
      for ($e = 1; $e <= $num; ++$e) {
        $fs = "participant_{$n}_event_{$e}_fieldset";
        $form['event']['participants'][$n]['div'][$fs] = array(
          '#type' => 'fieldset',
          '#title' => t('Event !num', array(
            '!num' => $e,
          )),
          '#attributes' => array(
            'id' => $fs,
          ),
        );
        foreach ($sets as $sid => $set) {
          if ($set['entity_type'] == 'participant') {
            $sid = 'civicrm_' . $n . '_participant_' . $e . '_' . $sid . '_fieldset';
            $class = 'web-civi-checkbox-set';
            if (!empty($set['sub_types'])) {
              $role_id = wf_crm_aval($data, "participant:{$n}:particpant:{$e}:role_id", '');
              $event_id = wf_crm_aval($data, "participant:{$n}:particpant:{$e}:event_id", '');
              $event_type = wf_crm_aval($data, 'reg_options:event_type', '');
              if ($event_id && $event_id !== 'create_civicrm_webform_element') {
                list($event_id, $event_type) = explode('-', $event_id);
              }
              $ext = $particpant_extensions[$set['extension_of']];
              if (!in_array(${$ext}, $set['sub_types'])) {
                $class .= ' hidden';
              }
              $class .= ' extends-condition ' . str_replace('_', '', $ext) . '-' . implode('-', $set['sub_types']);
            }
            $form['event']['participants'][$n]['div'][$fs][$sid] = array(
              '#type' => 'fieldset',
              '#title' => $set['label'],
              '#attributes' => array(
                'id' => $sid,
                'class' => array(
                  $class,
                ),
              ),
              'js_select' => wf_crm_js_select($sid),
            );
            foreach ($set['fields'] as $fid => $field) {
              $id = 'civicrm_' . $n . '_participant_' . $e . '_' . $fid;
              $item = wf_crm_configure_form_item($id, $field, $settings);
              if ($fid == 'participant_event_id' || $fid == 'participant_role_id') {
                $item['#attributes']['onchange'] = "wfCiviAdmin.participantConditional('#{$fs}');";
                $item['#attributes']['class'] = array(
                  $fid,
                );
                ${$fid} = wf_crm_aval($item, '#default_value');
              }
              $form['event']['participants'][$n]['div'][$fs][$sid][$id] = $item;
            }
          }
        }
      }
    }
  }

  // Configure additional options
  $form['options'] = array(
    '#type' => 'fieldset',
    '#title' => t('Additional Options'),
    '#group' => 'webform_civicrm',
    '#attributes' => array(
      'class' => array(
        'civi-icon-prefs',
      ),
    ),
    '#description' => '<p>' . t('To have this form auto-filled for anonymous users, enable the contact 1 "existing contact" field and send the following link from CiviMail:') . '<br /><code>' . url('node/' . $node->nid, array(
      'absolute' => TRUE,
      'query' => array(
        'cid1' => '',
      ),
    )) . '{contact.contact_id}&amp;{contact.checksum}</code></p>',
  );
  $form['options']['create_fieldsets'] = array(
    '#type' => 'checkbox',
    '#title' => t('Create Fieldsets'),
    '#default_value' => (bool) $settings['create_fieldsets'],
    '#description' => t('Create a fieldset around each contact. Provides visual organization of your form. Also allows the contact clone feature to work.'),
  );
  $form['options']['confirm_subscription'] = array(
    '#type' => 'checkbox',
    '#title' => t('Confirm Subscriptions'),
    '#default_value' => (bool) $settings['confirm_subscription'],
    '#description' => t('Recommended. Send a confirmation email before adding contacts to publicly subscribable mailing list groups.') . '<br />' . t('Your public mailing lists:') . ' <em>',
  );
  if ($ml = wf_crm_get_options('mailing_lists')) {
    if (count($ml) > 4) {
      $ml = array_slice($ml, 0, 3);
      $ml[] = t('etc.');
    }
    $form['options']['confirm_subscription']['#description'] .= implode(', ', $ml) . '</em>';
  }
  else {
    $form['options']['confirm_subscription']['#description'] .= t('none') . '</em>';
  }
  $form['options']['block_unknown_users'] = array(
    '#type' => 'checkbox',
    '#title' => t('Block Unknown Users'),
    '#default_value' => !empty($settings['block_unknown_users']),
    '#description' => t('Only allow users to see this form if they are logged in or following a personalized link from CiviMail.'),
  );
  $form['options']['new_contact_source'] = array(
    '#type' => 'textfield',
    '#title' => t('New Contact Source'),
    '#maxlength' => 255,
    '#size' => 30,
    '#default_value' => $settings['new_contact_source'],
    '#description' => t('Optional "source" label for any new contact created by this webform.'),
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save Settings'),
  );
  return $form;
}