function _webform_edit_civicrm_contact in Webform CiviCRM Integration 7.5
Same name and namespace in other branches
- 7.3 contact_component.inc \_webform_edit_civicrm_contact()
- 7.4 includes/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 = [];
$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, $node);
$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'], [
'alias' => TRUE,
'query' => [
'admin' => 1,
],
]) . '"';
$settings = '{
queryParam: "str",
hintText: "' . t('Choose @type', [
'@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] = [
'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'] = [
'#type' => 'select',
'#title' => t('Form Widget'),
'#default_value' => $component['extra']['widget'],
'#options' => [
'autocomplete' => t('Autocomplete'),
'select' => t('Select List'),
'hidden' => t('Static'),
'textfield' => t('Enter Contact ID'),
],
'#weight' => -9,
'#parents' => [
'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'] = [
'#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' => [
'extra',
'search_prompt',
],
];
$form['display']['none_prompt'] = [
'#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' => [
'extra',
'none_prompt',
],
];
$form['display']['results_display'] = [
'#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' => [
'extra',
'results_display',
],
];
$form['display']['show_hidden_contact'] = [
'#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' => [
t('No'),
t('Yes'),
],
'#default_value' => $component['extra']['show_hidden_contact'],
'#parents' => [
'extra',
'show_hidden_contact',
],
'#weight' => -5,
];
$form['field_handling'] = [
'#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'] = [
'#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' => [
'' => '- ' . t('None') . ' -',
] + wf_crm_contact_fields($node, $c),
'#parents' => [
'extra',
'no_autofill',
],
];
$form['field_handling']['hide_fields'] = [
'#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' => [
'' => '- ' . t('None') . ' -',
] + wf_crm_contact_fields($node, $c),
'#parents' => [
'extra',
'hide_fields',
],
];
$form['field_handling']['hide_method'] = [
'#type' => 'select',
'#title' => t('Locked fields should be'),
'#default_value' => $component['extra']['hide_method'],
'#options' => [
'hide' => t('Hidden'),
'disable' => t('Disabled'),
],
'#parents' => [
'extra',
'hide_method',
],
];
$form['field_handling']['no_hide_blank'] = [
'#type' => 'checkbox',
'#title' => t("Don't lock fields that are empty"),
'#default_value' => $component['extra']['no_hide_blank'],
'#parents' => [
'extra',
'no_hide_blank',
],
];
$form['field_handling']['submit_disabled'] = [
'#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' => [
'extra',
'submit_disabled',
],
];
$form['validation']['unique'] = [
'#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' => [
'extra',
'unique',
],
];
$form['extra']['allow_create'] = [
'#type' => 'hidden',
'#value' => $allow_create,
];
$form['defaults'] = [
'#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'] = [
'#type' => 'select',
'#title' => t('Set default contact from'),
'#options' => [
'contact_id' => t('Specified Contact'),
],
'#empty_option' => t('- None -'),
'#default_value' => $component['extra']['default'],
'#parents' => [
'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'] = [
'#type' => 'select',
'#title' => t('Relationship to Contact'),
'#default_value' => $to,
'#parents' => [
'extra',
'default_relationship_to',
],
];
$form['defaults']['default_relationship'] = [
'#type' => 'select',
'#multiple' => TRUE,
'#title' => t('Specify Relationship(s)'),
'#options' => [
'-' => '-',
],
'#required' => TRUE,
'#default_value' => $component['extra']['default_relationship'],
'#parents' => [
'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'] = [
'#type' => 'select',
'#title' => t('Specify Custom Field'),
'#options' => $custom_ref_options,
'#required' => TRUE,
'#default_value' => $component['extra']['default_custom_ref'],
'#parents' => [
'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'] = [
'#type' => 'select',
'#title' => t('For Case'),
'#default_value' => $dc,
'#parents' => [
'extra',
'default_for_case',
],
];
$form['defaults']['default_case_roles'] = [
'#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' => [
'extra',
'default_case_roles',
],
];
$case_type = [];
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'] = [
'#type' => 'textfield',
'#title' => t('Contact'),
'#id' => 'default-contact-id',
'#parents' => [
'extra',
'default_contact_id',
],
];
$cid = $component['extra']['default_contact_id'];
if ($cid) {
if ($name = wf_crm_contact_access($component, [
'check_permissions' => 1,
], $cid)) {
$form['defaults']['default_contact_id']['#default_value'] = $cid;
$form['defaults']['default_contact_id']['#attributes'] = [
'data-civicrm-name' => $name,
'data-civicrm-id' => $cid,
];
}
}
$form['defaults']['allow_url_autofill'] = [
'#type' => 'checkbox',
'#title' => t('Use contact id from URL'),
'#default_value' => $component['extra']['allow_url_autofill'],
'#parents' => [
'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).', [
'%arg' => "cid{$c}=123",
]),
];
if ($c > 1) {
$form['defaults']['dupes_allowed'] = [
'#type' => 'checkbox',
'#title' => t('Allow Duplicate Autofill'),
'#default_value' => $component['extra']['dupes_allowed'],
'#parents' => [
'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'] = [
'#type' => 'checkbox',
'#title' => t('Randomize'),
'#default_value' => $component['extra']['randomize'],
'#parents' => [
'extra',
'randomize',
],
'#description' => t('Pick a contact at random if more than one meets criteria.'),
];
$form['filters'] = [
'#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' => [
'extra',
'filters',
],
'#tree' => TRUE,
'#collapsible' => TRUE,
];
if (!empty($sub_types[$contact_type])) {
$form['filters']['contact_sub_type'] = [
'#type' => 'select',
'#title' => t('Type of @contact', [
'@contact' => $contact_types[$contact_type],
]),
'#options' => [
t('- Any -'),
] + $sub_types[$contact_type],
'#default_value' => $component['extra']['filters']['contact_sub_type'],
];
}
$form['filters']['group'] = [
'#type' => 'select',
'#multiple' => TRUE,
'#title' => t('Groups'),
'#options' => [
'' => '- ' . t('None') . ' -',
] + wf_crm_apivalues('group_contact', 'getoptions', [
'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 = [];
$form['filters']['tag'] = [
'#type' => 'select',
'#multiple' => TRUE,
'#title' => t('Tags'),
'#options' => [
'' => '- ' . t('None') . ' -',
] + wf_crm_get_tags('contact'),
'#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'] = [
'#type' => 'select',
'#title' => t('Relationships to'),
'#options' => [
'' => '- ' . t('None') . ' -',
],
'#default_value' => wf_crm_aval($component['extra']['filters'], 'relationship:contact'),
];
$form['filters']['relationship']['type'] = [
'#type' => 'select',
'#multiple' => TRUE,
'#title' => t('Specify Relationship(s)'),
'#options' => [
'' => '- ' . 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, []);
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][] = [
'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][] = [
'key' => '',
'value' => '- ' . t('No relationship types defined for @a to @b', [
'@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'] = [
'#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;
}