function wf_crm_contact_search in Webform CiviCRM Integration 7.5
Same name and namespace in other branches
- 7.3 contact_component.inc \wf_crm_contact_search()
- 7.4 includes/contact_component.inc \wf_crm_contact_search()
Returns a list of contacts based on component settings.
Parameters
stdClass $node: Node object
array $component: Webform component
array $params: Contact get params (filters)
array $contacts: Existing contact data
string $str: Search string (used during autocomplete)
Return value
array
3 calls to wf_crm_contact_search()
- wf_crm_fill_contact_value in includes/
contact_component.inc - Lookup contact name from ID, verify permissions, and attach as html data.
- wf_crm_webform_ajax::contactAjax in includes/
wf_crm_webform_ajax.inc - Load one or more contacts via ajax
- wf_crm_webform_base::findContact in includes/
wf_crm_webform_base.inc - Find an existing contact based on matching criteria Used to populate a webform existing contact field
File
- includes/
contact_component.inc, line 663
Code
function wf_crm_contact_search($node, $component, $params, $contacts, $str = NULL) {
if (empty($node->webform_civicrm)) {
return [];
}
$limit = $str ? 12 : 500;
$ret = [];
$display_fields = array_values($component['extra']['results_display']);
$search_field = 'display_name';
$sort_field = 'sort_name';
// Search and sort based on the selected display field
if (!in_array('display_name', $display_fields)) {
$search_field = $sort_field = $display_fields[0];
}
$params += [
'rowCount' => $limit,
'sort' => $sort_field,
'return' => $display_fields,
];
if (!empty($params['relationship']['contact'])) {
$c = $params['relationship']['contact'];
$relations = NULL;
if (!empty($contacts[$c]['id'])) {
$relations = wf_crm_find_relations($contacts[$c]['id'], wf_crm_aval($params['relationship'], 'type'));
$params['id'] = [
'IN' => $relations,
];
}
if (!$relations) {
return $ret;
}
}
unset($params['relationship']);
if ($str) {
$str = str_replace(' ', '%', CRM_Utils_Type::escape($str, 'String'));
// The contact api takes a quirky format for display_name and sort_name
if (in_array($search_field, [
'sort_name',
'display_name',
])) {
$params[$search_field] = $str;
}
else {
$params[$search_field] = [
'LIKE' => "%{$str}%",
];
}
}
$result = wf_civicrm_api('contact', 'get', $params);
// Autocomplete results
if ($str) {
foreach (wf_crm_aval($result, 'values', []) as $contact) {
if ($name = wf_crm_format_contact($contact, $display_fields)) {
$ret[] = [
'id' => $contact['id'],
'name' => $name,
];
}
}
if (count($ret) < $limit && $component['extra']['allow_create']) {
// HTML hack to get prompt to show up different than search results
$ret[] = [
'id' => "-{$str}",
'name' => '<em><i>' . filter_xss($component['extra']['none_prompt']) . '</i></em>',
];
}
}
else {
if ($component['extra']['allow_create']) {
$ret['-'] = filter_xss($component['extra']['none_prompt']);
}
foreach (wf_crm_aval($result, 'values', []) as $contact) {
// Select lists will be escaped by FAPI
if ($name = wf_crm_format_contact($contact, $display_fields, FALSE)) {
$ret[$contact['id']] = $name;
}
}
// If we get exactly $limit results, there are probably more - warn that the list is truncated
if (wf_crm_aval($result, 'count') >= $limit) {
watchdog('webform_civicrm', 'Maximum contacts exceeded, list truncated on the webform "@title". The webform_civicrm "@field" field cannot display more than !limit contacts because it is a select list. Recommend switching to autocomplete widget in component settings.', [
'!limit' => $limit,
'@field' => $component['name'],
'@title' => $node->title,
], WATCHDOG_WARNING, l(t('Edit component'), "node/{$node->nid}/webform/components/{$component['cid']}"));
if (wf_crm_admin_access($node) && node_is_page($node)) {
drupal_set_message('<strong>' . t('Maximum contacts exceeded, list truncated.') . '</strong><br>' . t('The field "@field" cannot show more than !limit contacts because it is a select list. Recommend switching to autocomplete widget in <a !link>component settings</a>.', [
'!limit' => $limit,
'@field' => $component['name'],
'!link' => 'href="' . url("node/{$node->nid}/webform/components/{$component['cid']}") . '"',
]), 'warning');
}
}
}
return $ret;
}