You are here

function _webform_edit_civicrm_contact in Webform CiviCRM Integration 7.4

Same name and namespace in other branches
  1. 7.5 includes/contact_component.inc \_webform_edit_civicrm_contact()
  2. 7.3 contact_component.inc \_webform_edit_civicrm_contact()

Implements _webform_edit_component().

File

includes/contact_component.inc, line 63

Code

function _webform_edit_civicrm_contact($component) {
  civicrm_initialize();
  $form = array();
  $node = node_load($component['nid']);
  if (empty($node->webform_civicrm) && node_is_page($node)) {
    drupal_set_message(t('CiviCRM processing is not enabled for this webform.'), 'error');
    return $form;
  }
  list($contact_types, $sub_types) = wf_crm_get_contact_types();
  $data = $node->webform_civicrm['data'];
  $enabled = wf_crm_enabled_fields($node);
  list(, $c, ) = explode('_', $component['form_key'], 3);
  $contact_type = $data['contact'][$c]['contact'][1]['contact_type'];
  wf_crm_update_existing_component($component, $enabled, $data);
  $allow_create = $component['extra']['allow_create'];

  // Load scripts & css
  $form['#attached']['js'][] = wf_crm_tokeninput_path();
  $form['#attached']['js'][] = drupal_get_path('module', 'webform_civicrm') . '/js/webform_civicrm_admin.js';
  CRM_Core_Resources::singleton()
    ->addCoreResources();
  $form['#suffix'] = wf_crm_admin_help::helpTemplate();
  $callback_path = '"' . url('webform-civicrm/js/' . $node->nid . '-' . $component['cid'], array(
    'alias' => TRUE,
    'query' => array(
      'admin' => 1,
    ),
  )) . '"';
  $settings = '{
    queryParam: "str",
    hintText: "' . t('Choose @type', array(
    '@type' => $contact_types[$contact_type],
  )) . '",
    noResultsText: "' . t('Not found') . '",
    searchingText: "' . t('Searching...') . '",
    tokenLimit: 1,
    prePopulate: prep
  }';
  $js = '
  jQuery(function($) {
    var prep = wfCiviContact.init(' . $callback_path . ');
    $("#default-contact-id").tokenInput(' . $callback_path . ', ' . $settings . ');
  });';
  $form['#attached']['js'][$js] = array(
    'type' => 'inline',
  );
  $form['#attached']['css'][] = drupal_get_path('module', 'webform_civicrm') . '/css/token-input.css';
  $form['#attached']['css'][] = drupal_get_path('module', 'webform_civicrm') . '/css/webform_civicrm_admin.css';
  $form['#attached']['js'][] = drupal_get_path('module', 'webform_civicrm') . '/js/contact_component.js';
  $form['display']['widget'] = array(
    '#type' => 'select',
    '#title' => t('Form Widget'),
    '#default_value' => $component['extra']['widget'],
    '#options' => array(
      'autocomplete' => t('Autocomplete'),
      'select' => t('Select List'),
      'hidden' => t('Static'),
      'textfield' => t('Enter Contact ID'),
    ),
    '#weight' => -9,
    '#parents' => array(
      'extra',
      'widget',
    ),
  );
  $status = $allow_create ? t('<strong>Contact Creation: Enabled</strong> - this contact has name/email fields on the webform.') : t('<strong>Contact Creation: Disabled</strong> - no name/email fields for this contact on the webform.');
  $form['display']['#description'] = '<div class="messages ' . ($allow_create ? 'status' : 'warning') . '">' . $status . ' ' . wf_crm_admin_help::helpIcon('contact_creation', t('Contact Creation')) . '</div>';
  $form['display']['search_prompt'] = array(
    '#type' => 'textfield',
    '#title' => t('Search Prompt'),
    '#default_value' => $component['extra']['search_prompt'],
    '#description' => t('Text the user will see before selecting a contact.'),
    '#size' => 60,
    '#maxlength' => 1024,
    '#weight' => -7,
    '#parents' => array(
      'extra',
      'search_prompt',
    ),
  );
  $form['display']['none_prompt'] = array(
    '#type' => 'textfield',
    '#title' => $allow_create ? t('Create Prompt') : t('Not Found Prompt'),
    '#default_value' => $component['extra']['none_prompt'],
    '#description' => $allow_create ? t('This text should prompt the user to create a new contact.') : t('This text should tell the user that no search results were found.'),
    '#size' => 60,
    '#maxlength' => 1024,
    '#weight' => -6,
    '#parents' => array(
      'extra',
      'none_prompt',
    ),
  );
  $form['display']['results_display'] = array(
    '#type' => 'select',
    '#multiple' => TRUE,
    '#title' => t("Contact Display Field(s)"),
    '#required' => TRUE,
    '#default_value' => $component['extra']['results_display'],
    '#options' => wf_crm_results_display_options($contact_type),
    '#parents' => array(
      'extra',
      'results_display',
    ),
  );
  $form['display']['show_hidden_contact'] = array(
    '#type' => 'radios',
    '#title' => t('Display Contact Name'),
    '#description' => t('If enabled, this static element will show the contact that has been pre-selected (or else the Create/Not Found Prompt if set). Otherwise the element will not be visible.'),
    '#options' => array(
      t('No'),
      t('Yes'),
    ),
    '#default_value' => $component['extra']['show_hidden_contact'],
    '#parents' => array(
      'extra',
      'show_hidden_contact',
    ),
    '#weight' => -5,
  );
  $form['field_handling'] = array(
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#title' => t('Contact Field Handling'),
    '#description' => t('By default, all contact data will be autopopulated on the form for this existing contact. You can modify that behavior here.'),
  );
  $form['field_handling']['no_autofill'] = array(
    '#type' => 'select',
    '#multiple' => TRUE,
    '#title' => t('Skip Autofilling of'),
    '#description' => t('Which fields should <em>not</em> be autofilled for this contact?'),
    '#default_value' => $component['extra']['no_autofill'],
    '#options' => array(
      '' => '- ' . t('None') . ' -',
    ) + wf_crm_contact_fields($node, $c),
    '#parents' => array(
      'extra',
      'no_autofill',
    ),
  );
  $form['field_handling']['hide_fields'] = array(
    '#type' => 'select',
    '#multiple' => TRUE,
    '#title' => t('Fields to Lock'),
    '#description' => t('Prevent editing by disabling or hiding fields when a contact already exists.'),
    '#default_value' => $component['extra']['hide_fields'],
    '#options' => array(
      '' => '- ' . t('None') . ' -',
    ) + wf_crm_contact_fields($node, $c),
    '#parents' => array(
      'extra',
      'hide_fields',
    ),
  );
  $form['field_handling']['hide_method'] = array(
    '#type' => 'select',
    '#title' => t('Locked fields should be'),
    '#default_value' => $component['extra']['hide_method'],
    '#options' => array(
      'hide' => t('Hidden'),
      'disable' => t('Disabled'),
    ),
    '#parents' => array(
      'extra',
      'hide_method',
    ),
  );
  $form['field_handling']['no_hide_blank'] = array(
    '#type' => 'checkbox',
    '#title' => t("Don't lock fields that are empty"),
    '#default_value' => $component['extra']['no_hide_blank'],
    '#parents' => array(
      'extra',
      'no_hide_blank',
    ),
  );
  $form['field_handling']['submit_disabled'] = array(
    '#type' => 'checkbox',
    '#title' => t("Submit disabled field value(s)"),
    '#description' => t('Store disabled field value(s) in webform submissions.'),
    '#default_value' => $component['extra']['submit_disabled'],
    '#parents' => array(
      'extra',
      'submit_disabled',
    ),
  );
  $form['validation']['unique'] = array(
    '#type' => 'checkbox',
    '#title' => t('Unique'),
    '#return_value' => 1,
    '#description' => t('Require this field to be unique for every submission. The same contact may not be entered twice.'),
    '#weight' => 1,
    '#default_value' => $component['extra']['unique'],
    '#parents' => array(
      'extra',
      'unique',
    ),
  );
  $form['extra']['allow_create'] = array(
    '#type' => 'hidden',
    '#value' => $allow_create,
  );
  $form['defaults'] = array(
    '#type' => 'fieldset',
    '#title' => t('Default value'),
    '#description' => t('Should the form be pre-populated with an existing contact?<ul><li>Any filters set below will restrict this default.</li><li>If more than one contact meets the criteria, the first match will be picked. If multiple existing contact fields exist on the webform, each will select a different contact.</li></ul>'),
    '#collapsible' => TRUE,
  );
  $form['defaults']['default'] = array(
    '#type' => 'select',
    '#title' => t('Set default contact from'),
    '#options' => array(
      'contact_id' => t('Specified Contact'),
    ),
    '#empty_option' => t('- None -'),
    '#default_value' => $component['extra']['default'],
    '#parents' => array(
      'extra',
      'default',
    ),
  );
  if ($c == 1 && $contact_type == 'individual') {
    $form['defaults']['default']['#options']['user'] = t('Current User');
  }
  elseif ($c > 1) {
    $form['defaults']['default']['#options']['relationship'] = t('Relationship to...');
    $to = $component['extra']['default_relationship_to'];
    $form['defaults']['default_relationship_to'] = array(
      '#type' => 'select',
      '#title' => t('Relationship to Contact'),
      '#default_value' => $to,
      '#parents' => array(
        'extra',
        'default_relationship_to',
      ),
    );
    $form['defaults']['default_relationship'] = array(
      '#type' => 'select',
      '#multiple' => TRUE,
      '#title' => t('Specify Relationship(s)'),
      '#options' => array(
        '-' => '-',
      ),
      '#required' => TRUE,
      '#default_value' => $component['extra']['default_relationship'],
      '#parents' => array(
        'extra',
        'default_relationship',
      ),
    );
  }
  $custom_ref_options = wf_crm_get_custom_ref_options();
  if ($custom_ref_options) {
    $form['defaults']['default']['#options']['custom_ref'] = t('Custom reference field');
    $form['defaults']['default_custom_ref'] = array(
      '#type' => 'select',
      '#title' => t('Specify Custom Field'),
      '#options' => $custom_ref_options,
      '#required' => TRUE,
      '#default_value' => $component['extra']['default_custom_ref'],
      '#parents' => array(
        'extra',
        'default_custom_ref',
      ),
    );
  }
  $case_count = isset($data['case']['number_of_case']) ? $data['case']['number_of_case'] : 0;
  if ($case_count > 0) {
    $form['defaults']['default']['#options']['case_roles'] = t('Case roles');
    $dc = $component['extra']['default_relationship_to'];
    $form['defaults']['default_for_case'] = array(
      '#type' => 'select',
      '#title' => t('For Case'),
      '#default_value' => $dc,
      '#parents' => array(
        'extra',
        'default_for_case',
      ),
    );
    $form['defaults']['default_case_roles'] = array(
      '#type' => 'select',
      '#title' => t('Specify Case Role(s)'),
      '#options' => wf_crm_get_case_roles_options(),
      '#required' => TRUE,
      '#default_value' => $component['extra']['default_case_roles'],
      '#parents' => array(
        'extra',
        'default_case_roles',
      ),
    );
    $case_type = array();
    for ($i = 1; $i <= $case_count; $i++) {
      $form['defaults']['default_for_case']['#options'][$i] = 'Case ' . $i;
    }
  }
  $form['defaults']['default']['#options']['auto'] = t('Auto - From Filters');
  $form['defaults']['default_contact_id'] = array(
    '#type' => 'textfield',
    '#title' => t('Contact'),
    '#id' => 'default-contact-id',
    '#parents' => array(
      'extra',
      'default_contact_id',
    ),
  );
  $cid = $component['extra']['default_contact_id'];
  if ($cid) {
    if ($name = wf_crm_contact_access($component, array(
      'check_permissions' => 1,
    ), $cid)) {
      $form['defaults']['default_contact_id']['#default_value'] = $cid;
      $form['defaults']['default_contact_id']['#attributes'] = array(
        'data-civicrm-name' => $name,
        'data-civicrm-id' => $cid,
      );
    }
  }
  $form['defaults']['allow_url_autofill'] = array(
    '#type' => 'checkbox',
    '#title' => t('Use contact id from URL'),
    '#default_value' => $component['extra']['allow_url_autofill'],
    '#parents' => array(
      'extra',
      'allow_url_autofill',
    ),
    '#description' => t('If the url contains e.g. %arg, it will be used to pre-populate this contact (takes precidence over other default values).', array(
      '%arg' => "cid{$c}=123",
    )),
  );
  if ($c > 1) {
    $form['defaults']['dupes_allowed'] = array(
      '#type' => 'checkbox',
      '#title' => t('Allow Duplicate Autofill'),
      '#default_value' => $component['extra']['dupes_allowed'],
      '#parents' => array(
        'extra',
        'dupes_allowed',
      ),
      '#description' => t('Check this box to allow a contact to be selected even if they already autofilled a prior field on the form. (For example, if contact 1 was autofilled with Bob Smith, should this field also be allowed to select Bob Smith or should it pick a different contact?)'),
    );
  }
  $form['defaults']['randomize'] = array(
    '#type' => 'checkbox',
    '#title' => t('Randomize'),
    '#default_value' => $component['extra']['randomize'],
    '#parents' => array(
      'extra',
      'randomize',
    ),
    '#description' => t('Pick a contact at random if more than one meets criteria.'),
  );
  $form['filters'] = array(
    '#type' => 'fieldset',
    '#title' => t('Filters'),
    '#description' => t('Only contacts meeting filter criteria will be available as select options or default value.<br />Note: Filters only apply to how a contact is chosen on the form, they do not affect how a contact is saved.'),
    '#parents' => array(
      'extra',
      'filters',
    ),
    '#tree' => TRUE,
    '#collapsible' => TRUE,
  );
  if (!empty($sub_types[$contact_type])) {
    $form['filters']['contact_sub_type'] = array(
      '#type' => 'select',
      '#title' => t('Type of @contact', array(
        '@contact' => $contact_types[$contact_type],
      )),
      '#options' => array(
        t('- Any -'),
      ) + $sub_types[$contact_type],
      '#default_value' => $component['extra']['filters']['contact_sub_type'],
    );
  }
  $form['filters']['group'] = array(
    '#type' => 'select',
    '#multiple' => TRUE,
    '#title' => t('Groups'),
    '#options' => array(
      '' => '- ' . t('None') . ' -',
    ) + wf_crm_apivalues('group_contact', 'getoptions', array(
      'field' => 'group_id',
    )),
    '#default_value' => $component['extra']['filters']['group'],
    '#description' => t('Listed contacts must be members of at least one of the selected groups (leave blank to not filter by group).'),
  );
  $tags = array();
  $form['filters']['tag'] = array(
    '#type' => 'select',
    '#multiple' => TRUE,
    '#title' => t('Tags'),
    '#options' => array(
      '' => '- ' . t('None') . ' -',
    ) + CRM_Core_BAO_Tag::getTags("civicrm_contact", $tags, NULL, '- '),
    '#default_value' => $component['extra']['filters']['tag'],
    '#description' => t('Listed contacts must be have at least one of the selected tags (leave blank to not filter by tag).'),
  );
  if ($c > 1) {
    $form['filters']['relationship']['contact'] = array(
      '#type' => 'select',
      '#title' => t('Relationships to'),
      '#options' => array(
        '' => '- ' . t('None') . ' -',
      ),
      '#default_value' => wf_crm_aval($component['extra']['filters'], 'relationship:contact'),
    );
    $form['filters']['relationship']['type'] = array(
      '#type' => 'select',
      '#multiple' => TRUE,
      '#title' => t('Specify Relationship(s)'),
      '#options' => array(
        '' => '- ' . t('Any relation') . ' -',
      ),
      '#default_value' => wf_crm_aval($component['extra']['filters'], 'relationship:type'),
    );

    // Fill relationship data for defaults and filters
    $all_relationship_types = array_fill(1, $c - 1, array());
    for ($i = 1; $i < $c; ++$i) {
      $form['defaults']['default_relationship_to']['#options'][$i] = $form['filters']['relationship']['contact']['#options'][$i] = wf_crm_contact_label($i, $data, 'plain');
      $rtypes = wf_crm_get_contact_relationship_types($contact_type, $data['contact'][$i]['contact'][1]['contact_type'], $data['contact'][$c]['contact'][1]['contact_sub_type'], $data['contact'][$i]['contact'][1]['contact_sub_type']);
      foreach ($rtypes as $k => $v) {
        $all_relationship_types[$i][] = array(
          'key' => $k,
          'value' => $v . ' ' . wf_crm_contact_label($i, $data, 'plain'),
        );
        $form['defaults']['default_relationship']['#options'][$k] = $form['filters']['relationship']['type']['#options'][$k] = $v . ' ' . wf_crm_contact_label($i, $data, 'plain');
      }
      if (!$rtypes) {
        $all_relationship_types[$i][] = array(
          'key' => '',
          'value' => '- ' . t('No relationship types defined for @a to @b', array(
            '@a' => $contact_types[$contact_type],
            '@b' => $contact_types[$data['contact'][$i]['contact'][1]['contact_type']],
          )) . ' -',
        );
      }
    }
    $form['#attributes']['data-reltypes'] = json_encode($all_relationship_types);
  }
  $form['filters']['check_permissions'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enforce Permissions'),
    '#default_value' => $component['extra']['filters']['check_permissions'],
    '#description' => t('Only show contacts the acting user has permission to see in CiviCRM.') . '<br />' . t('WARNING: Keeping this option enabled is highly recommended unless you are effectively controlling access by another method.'),
  );
  wf_crm_contact_component_form_help($form);
  return $form;
}