3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 class CRM_Contact_Form_Search_Criteria
{
20 * @param CRM_Contact_Form_Search_Advanced $form
22 * @throws \CRM_Core_Exception
23 * @throws \CiviCRM_API3_Exception
25 public static function basic(&$form) {
26 $form->addSearchFieldMetadata(['Contact' => self
::getFilteredSearchFieldMetadata('basic')]);
27 $form->addFormFieldsFromMetadata();
28 self
::setBasicSearchFields($form);
29 $form->addElement('hidden', 'hidden_basic', 1);
31 if ($form->_searchOptions
['contactType']) {
32 $contactTypes = CRM_Contact_BAO_ContactType
::getSelectElements();
35 $form->add('select', 'contact_type', ts('Contact Type(s)'), $contactTypes, FALSE,
36 ['id' => 'contact_type', 'multiple' => 'multiple', 'class' => 'crm-select2', 'style' => 'width: 100%;']
41 if ($form->_searchOptions
['groups']) {
42 // multiselect for groups
44 // Arrange groups into hierarchical listing (child groups follow their parents and have indentation spacing in title)
45 $groupHierarchy = CRM_Contact_BAO_Group
::getGroupsHierarchy($form->_group
, NULL, ' ', TRUE);
47 $form->add('select', 'group', ts('Groups'), $groupHierarchy, FALSE,
48 ['id' => 'group', 'multiple' => 'multiple', 'class' => 'crm-select2']
50 $groupOptions = CRM_Core_BAO_OptionValue
::getOptionValuesAssocArrayFromName('group_type');
51 $form->add('select', 'group_type', ts('Group Types'), $groupOptions, FALSE,
52 ['id' => 'group_type', 'multiple' => 'multiple', 'class' => 'crm-select2']
54 $form->add('hidden', 'group_search_selected', 'group');
58 if ($form->_searchOptions
['tags']) {
59 // multiselect for categories
60 $contactTags = CRM_Core_BAO_Tag
::getTags();
63 $form->add('select', 'contact_tags', ts('Select Tag(s)'), $contactTags, FALSE,
64 ['id' => 'contact_tags', 'multiple' => 'multiple', 'class' => 'crm-select2', 'style' => 'width: 100%;']
68 $parentNames = CRM_Core_BAO_Tag
::getTagSet('civicrm_contact');
69 CRM_Core_Form_Tag
::buildQuickForm($form, $parentNames, 'civicrm_contact', NULL, TRUE, FALSE);
71 $used_for = CRM_Core_OptionGroup
::values('tag_used_for');
73 $showAllTagTypes = FALSE;
74 foreach ($used_for as $key => $value) {
75 //check tags for every type and find if there are any defined
76 $tags = CRM_Core_BAO_Tag
::getTagsUsedFor($key, FALSE, TRUE, NULL);
77 // check if there are tags other than contact type, if no - keep checkbox hidden on adv search
78 // we will hide searching contact by attachments tags until it will be implemented in core
79 if (count($tags) && $key != 'civicrm_file' && $key != 'civicrm_contact') {
80 //if tags exists then add type to display in adv search form help text
81 $tagsTypes[] = $value;
82 $showAllTagTypes = TRUE;
85 $tagTypesText = implode(" or ", $tagsTypes);
86 if ($showAllTagTypes) {
87 $form->add('checkbox', 'all_tag_types', ts('Include tags used for %1', [1 => $tagTypesText]));
88 $form->add('hidden', 'tag_types_text', $tagTypesText);
92 //added contact source
93 $form->add('text', 'contact_source', ts('Contact Source'), CRM_Core_DAO
::getAttribute('CRM_Contact_DAO_Contact', 'contact_source'));
96 $form->addElement('text', 'job_title', ts('Job Title'), CRM_Core_DAO
::getAttribute('CRM_Contact_DAO_Contact', 'job_title'));
99 $form->add('number', 'contact_id', ts('Contact ID'), CRM_Core_DAO
::getAttribute('CRM_Contact_DAO_Contact', 'id') +
['min' => 1]);
100 $form->addRule('contact_id', ts('Please enter valid Contact ID'), 'positiveInteger');
103 $form->addElement('text', 'external_identifier', ts('External ID'), CRM_Core_DAO
::getAttribute('CRM_Contact_DAO_Contact', 'external_identifier'));
105 if (CRM_Core_Permission
::check('access deleted contacts') and Civi
::settings()->get('contact_undelete')) {
106 $form->add('checkbox', 'deleted_contacts', ts('Search in Trash') . '<br />' . ts('(deleted contacts)'));
109 // add checkbox for cms users only
110 $form->addYesNo('uf_user', ts('CMS User?'), TRUE);
113 $form->add('text', 'tag_search', ts('All Tags'));
115 // add search profiles
117 // FIXME: This is probably a part of profiles - need to be
118 // FIXME: eradicated from here when profiles are reworked.
119 $types = ['Participant', 'Contribution', 'Membership'];
121 // get component profiles
122 $componentProfiles = [];
123 $componentProfiles = CRM_Core_BAO_UFGroup
::getProfiles($types);
125 $ufGroups = CRM_Core_BAO_UFGroup
::getModuleUFGroup('Search Profile', 1);
126 $accessibleUfGroups = CRM_Core_Permission
::ufGroup(CRM_Core_Permission
::VIEW
);
128 $searchProfiles = [];
129 foreach ($ufGroups as $key => $var) {
130 if (!array_key_exists($key, $componentProfiles) && in_array($key, $accessibleUfGroups)) {
131 $searchProfiles[$key] = $var['title'];
137 ts('Views For Display Contacts'),
139 '0' => ts('- default view -'),
142 ['class' => 'crm-select2']
145 $componentModes = CRM_Contact_Form_Search
::getModeSelect();
146 $form->assign('component_mappings', json_encode(CRM_Contact_Form_Search
::getModeToComponentMapping()));
147 if (count($componentModes) > 1) {
150 ts('Display Results As'),
153 ['class' => 'crm-select2']
159 ts('Search Operator'),
161 CRM_Contact_BAO_Query
::SEARCH_OPERATOR_AND
=> ts('AND'),
162 CRM_Contact_BAO_Query
::SEARCH_OPERATOR_OR
=> ts('OR'),
164 ['allowClear' => FALSE]
167 // add the option to display relationships
168 $rTypes = CRM_Core_PseudoConstant
::relationshipType();
169 $rSelect = ['' => ts('- Select Relationship Type-')];
170 foreach ($rTypes as $rid => $rValue) {
171 if ($rValue['label_a_b'] == $rValue['label_b_a']) {
172 $rSelect[$rid] = $rValue['label_a_b'];
175 $rSelect["{$rid}_a_b"] = $rValue['label_a_b'];
176 $rSelect["{$rid}_b_a"] = $rValue['label_b_a'];
180 $form->addElement('select',
181 'display_relationship_type',
182 ts('Display Results as Relationship'),
184 ['class' => 'crm-select2']
187 // checkboxes for DO NOT phone, email, mail
188 // we take labels from SelectValues
189 $t = CRM_Core_SelectValues
::privacy();
196 'id' => 'privacy_options',
197 'multiple' => 'multiple',
198 'class' => 'crm-select2',
202 $form->addElement('select',
213 2 => ts('Include by Privacy Option(s)'),
215 $form->addRadio('privacy_toggle', ts('Privacy Options'), $options, ['allowClear' => FALSE]);
217 // preferred communication method
218 if (Civi
::settings()->get('civimail_multiple_bulk_emails')) {
219 $form->addSelect('email_on_hold',
220 ['entity' => 'email', 'multiple' => 'multiple', 'label' => ts('Email On Hold'), 'options' => CRM_Core_PseudoConstant
::emailOnHoldOptions()]);
223 $form->add('advcheckbox', 'email_on_hold', ts('Email On Hold'));
226 $form->addSelect('preferred_communication_method',
227 ['entity' => 'contact', 'multiple' => 'multiple', 'label' => ts('Preferred Communication Method'), 'option_url' => NULL, 'placeholder' => ts('- any -')]);
229 //CRM-6138 Preferred Language
230 $form->addSelect('preferred_language', ['class' => 'twenty', 'context' => 'search']);
233 $form->addElement('text', 'phone_numeric', ts('Phone'), CRM_Core_DAO
::getAttribute('CRM_Core_DAO_Phone', 'phone'));
234 $locationType = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
235 $phoneType = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Phone', 'phone_type_id');
236 $form->add('select', 'phone_location_type_id', ts('Phone Location'), ['' => ts('- any -')] +
$locationType, FALSE, ['class' => 'crm-select2']);
237 $form->add('select', 'phone_phone_type_id', ts('Phone Type'), ['' => ts('- any -')] +
$phoneType, FALSE, ['class' => 'crm-select2']);
241 * Get the metadata for fields to be included on the contact search form.
243 * @throws \CiviCRM_API3_Exception
245 public static function getSearchFieldMetadata() {
247 'sort_name' => ['title' => ts('Complete OR Partial Name'), 'template_grouping' => 'basic'],
248 'email' => ['title' => ts('Complete OR Partial Email'), 'entity' => 'Email', 'template_grouping' => 'basic'],
249 'contact_tags' => ['name' => 'contact_tags', 'type' => CRM_Utils_Type
::T_INT
, 'is_pseudofield' => TRUE, 'template_grouping' => 'basic'],
250 'created_date' => ['name' => 'created_date', 'template_grouping' => 'changeLog'],
251 'modified_date' => ['name' => 'modified_date', 'template_grouping' => 'changeLog'],
252 'birth_date' => ['name' => 'birth_date', 'template_grouping' => 'demographic'],
253 'deceased_date' => ['name' => 'deceased_date', 'template_grouping' => 'demographic'],
254 'is_deceased' => ['is_deceased', 'template_grouping' => 'demographic'],
255 'relationship_start_date' => ['name' => 'relationship_start_date', 'template_grouping' => 'relationship'],
256 'relationship_end_date' => ['name' => 'relationship_end_date', 'template_grouping' => 'relationship'],
257 // PseudoRelationship date field.
258 'relation_active_period_date' => [
259 'name' => 'relation_active_period_date',
260 'type' => CRM_Utils_Type
::T_DATE + CRM_Utils_Type
::T_TIME
,
261 'title' => ts('Active Period'),
262 'table_name' => 'civicrm_relationship',
263 'where' => 'civicrm_relationship.start_date',
264 'where_end' => 'civicrm_relationship.end_date',
265 'html' => ['type' => 'SelectDate', 'formatType' => 'activityDateTime'],
266 'template_grouping' => 'relationship',
270 $metadata = civicrm_api3('Relationship', 'getfields', [])['values'];
271 $metadata = array_merge($metadata, civicrm_api3('Contact', 'getfields', [])['values']);
272 foreach ($fields as $fieldName => $field) {
273 $fields[$fieldName] = array_merge(CRM_Utils_Array
::value($fieldName, $metadata, []), $field);
279 * Get search field metadata filtered by the template grouping field.
281 * @param string $filter
284 * @throws \CiviCRM_API3_Exception
286 public static function getFilteredSearchFieldMetadata($filter) {
287 $fields = self
::getSearchFieldMetadata();
288 foreach ($fields as $index => $field) {
289 if ($field['template_grouping'] !== $filter) {
290 unset($fields[$index]);
297 * Defines the fields that can be displayed for the basic search section.
299 * @param CRM_Core_Form $form
301 * @throws \CiviCRM_API3_Exception
303 protected static function setBasicSearchFields($form) {
305 foreach (self
::getFilteredSearchFieldMetadata('basic') as $fieldName => $field) {
306 $searchFields[$fieldName] = $field;
308 $form->assign('basicSearchFields', array_merge(self
::getBasicSearchFields(), $searchFields));
312 * Return list of basic contact fields that can be displayed for the basic search section.
315 public static function getBasicSearchFields() {
316 $userFramework = CRM_Core_Config
::singleton()->userFramework
;
318 // For now an empty array is still left in place for ordering.
320 'email' => ['name' => 'email'],
321 'contact_type' => ['name' => 'contact_type'],
324 'template' => 'CRM/Contact/Form/Search/Criteria/Fields/group.tpl',
326 'contact_tags' => ['name' => 'contact_tags'],
327 'tag_types_text' => ['name' => 'tag_types_text'],
329 'name' => 'tag_search',
330 'help' => ['id' => 'id-all-tags'],
335 'template' => 'CRM/Contact/Form/Search/Criteria/Fields/tag_set.tpl',
338 'name' => 'all_tag_types',
339 'class' => 'search-field__span-3 search-field__checkbox',
340 'help' => ['id' => 'id-all-tag-types'],
343 'name' => 'phone_numeric',
344 'description' => ts('Punctuation and spaces are ignored.'),
346 'phone_location_type_id' => ['name' => 'phone_location_type_id'],
347 'phone_phone_type_id' => ['name' => 'phone_phone_type_id'],
348 'privacy_toggle' => [
349 'name' => 'privacy_toggle',
350 'class' => 'search-field__span-2',
351 'template' => 'CRM/Contact/Form/Search/Criteria/Fields/privacy_toggle.tpl',
353 'preferred_communication_method' => [
354 'name' => 'preferred_communication_method',
355 'template' => 'CRM/Contact/Form/Search/Criteria/Fields/preferred_communication_method.tpl',
357 'contact_source' => [
358 'name' => 'contact_source',
359 'help' => ['id' => 'id-source', 'file' => 'CRM/Contact/Form/Contact'],
361 'job_title' => ['name' => 'job_title'],
362 'preferred_language' => ['name' => 'preferred_language'],
364 'name' => 'contact_id',
365 'help' => ['id' => 'id-contact-id', 'file' => 'CRM/Contact/Form/Contact'],
367 'external_identifier' => [
368 'name' => 'external_identifier',
369 'help' => ['id' => 'id-external-id', 'file' => 'CRM/Contact/Form/Contact'],
373 'description' => ts('Does the contact have a %1 Account?', [$userFramework]),
379 * @param CRM_Core_Form $form
381 public static function location(&$form) {
382 $config = CRM_Core_Config
::singleton();
383 // Build location criteria based on _submitValues if
384 // available; otherwise, use $form->_formValues.
385 $formValues = $form->_submitValues
;
387 if (empty($formValues) && !empty($form->_formValues
)) {
388 $formValues = $form->_formValues
;
391 $form->addElement('hidden', 'hidden_location', 1);
393 $addressOptions = CRM_Core_BAO_Setting
::valueOptions(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
394 'address_options', TRUE, NULL, TRUE
397 $attributes = CRM_Core_DAO
::getAttribute('CRM_Core_DAO_Address');
400 'street_address' => [ts('Street Address'), $attributes['street_address'], NULL, NULL],
401 'supplemental_address_1' => [ts('Supplemental Address 1'), $attributes['supplemental_address_1'], NULL, NULL],
402 'supplemental_address_2' => [ts('Supplemental Address 2'), $attributes['supplemental_address_2'], NULL, NULL],
403 'supplemental_address_3' => [ts('Supplemental Address 3'), $attributes['supplemental_address_3'], NULL, NULL],
404 'city' => [ts('City'), $attributes['city'], NULL, NULL],
405 'postal_code' => [ts('Postal Code'), $attributes['postal_code'], NULL, NULL],
406 'country' => [ts('Country'), $attributes['country_id'], 'country', FALSE],
407 'state_province' => [ts('State/Province'), $attributes['state_province_id'], 'stateProvince', TRUE],
408 'county' => [ts('County'), $attributes['county_id'], 'county', TRUE],
409 'address_name' => [ts('Address Name'), $attributes['address_name'], NULL, NULL],
410 'street_number' => [ts('Street Number'), $attributes['street_number'], NULL, NULL],
411 'street_name' => [ts('Street Name'), $attributes['street_name'], NULL, NULL],
412 'street_unit' => [ts('Apt/Unit/Suite'), $attributes['street_unit'], NULL, NULL],
415 $parseStreetAddress = $addressOptions['street_address_parsing'] ??
0;
416 $form->assign('parseStreetAddress', $parseStreetAddress);
417 foreach ($elements as $name => $v) {
418 list($title, $attributes, $select, $multiSelect) = $v;
421 ['street_number', 'street_name', 'street_unit']
423 if (!$parseStreetAddress) {
427 elseif (!$addressOptions[$name]) {
432 $attributes = $attributes[$name];
436 if ($select == 'stateProvince' ||
$select == 'county') {
437 $element = $form->addChainSelect($name);
440 $selectElements = ['' => ts('- any -')] + CRM_Core_PseudoConstant
::$select();
441 $element = $form->add('select', $name, $title, $selectElements, FALSE, ['class' => 'crm-select2']);
444 $element->setMultiple(TRUE);
448 $form->addElement('text', $name, $title, $attributes);
451 if ($addressOptions['postal_code']) {
452 $attr = ['class' => 'six'] +
(array) CRM_Utils_Array
::value('postal_code', $attributes);
453 $form->addElement('text', 'postal_code_low', NULL, $attr +
['placeholder' => ts('From')]);
454 $form->addElement('text', 'postal_code_high', NULL, $attr +
['placeholder' => ts('To')]);
458 // extend addresses with proximity search
459 if (CRM_Utils_GeocodeProvider
::getUsableClassName()) {
460 $form->addElement('text', 'prox_distance', ts('Find contacts within'), ['class' => 'six']);
461 $form->addElement('select', 'prox_distance_unit', NULL, [
462 'miles' => ts('Miles'),
463 'kilos' => ts('Kilometers'),
465 $form->addRule('prox_distance', ts('Please enter positive number as a distance'), 'numeric');
468 $form->addSelect('world_region', ['entity' => 'address', 'context' => 'search']);
470 // select for location type
471 $locationType = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
472 $form->add('select', 'location_type', ts('Address Location'), $locationType, FALSE, [
474 'class' => 'crm-select2',
475 'placeholder' => ts('Primary'),
478 // custom data extending addresses
479 CRM_Core_BAO_Query
::addCustomFormFields($form, ['Address']);
483 * @param CRM_Core_Form $form
485 public static function activity(&$form) {
486 $form->add('hidden', 'hidden_activity', 1);
487 CRM_Activity_BAO_Query
::buildSearchForm($form);
491 * @param CRM_Core_Form $form
493 * @throws \CiviCRM_API3_Exception
495 public static function changeLog(&$form) {
496 $form->add('hidden', 'hidden_changeLog', 1);
497 $form->addSearchFieldMetadata(['Contact' => self
::getFilteredSearchFieldMetadata('changeLog')]);
498 $form->addFormFieldsFromMetadata();
499 // block for change log
500 $form->addElement('text', 'changed_by', ts('Modified By'), NULL);
504 * @param CRM_Core_Form $form
506 public static function task(&$form) {
507 $form->add('hidden', 'hidden_task', 1);
511 * @param CRM_Core_Form_Search $form
513 * @throws \CiviCRM_API3_Exception
515 public static function relationship(&$form) {
516 $form->add('hidden', 'hidden_relationship', 1);
517 $form->addSearchFieldMetadata(['Relationship' => self
::getFilteredSearchFieldMetadata('relationship')]);
518 $form->addFormFieldsFromMetadata();
519 $form->add('text', 'relation_description', ts('Description'), ['class' => 'twenty']);
520 $allRelationshipType = CRM_Contact_BAO_Relationship
::getContactRelationshipType(NULL, NULL, NULL, NULL, TRUE);
521 $form->add('select', 'relation_type_id', ts('Relationship Type'), ['' => ts('- select -')] +
$allRelationshipType, FALSE, ['multiple' => TRUE, 'class' => 'crm-select2']);
522 $form->addElement('text', 'relation_target_name', ts('Target Contact'), CRM_Core_DAO
::getAttribute('CRM_Contact_DAO_Contact', 'sort_name'));
524 $relStatusOption = [ts('Active'), ts('Inactive'), ts('All')];
525 $form->addRadio('relation_status', ts('Relationship Status'), $relStatusOption);
526 $form->setDefaults(['relation_status' => 0]);
527 // relation permission
528 $allRelationshipPermissions = CRM_Contact_BAO_Relationship
::buildOptions('is_permission_a_b');
529 $form->add('select', 'relation_permission', ts('Permissioned Relationship'),
530 ['' => ts('- select -')] +
$allRelationshipPermissions, FALSE, ['multiple' => TRUE, 'class' => 'crm-select2']);
532 //add the target group
534 $form->add('select', 'relation_target_group', ts('Target Contact(s) in Group'), $form->_group
, FALSE,
535 ['id' => 'relation_target_group', 'multiple' => 'multiple', 'class' => 'crm-select2']
539 // add all the custom searchable fields
540 CRM_Core_BAO_Query
::addCustomFormFields($form, ['Relationship']);
544 * @param CRM_Core_Form_Search $form
546 * @throws \CiviCRM_API3_Exception
548 public static function demographics(&$form) {
549 $form->add('hidden', 'hidden_demographics', 1);
550 $form->addSearchFieldMetadata(['Contact' => self
::getFilteredSearchFieldMetadata('demographic')]);
551 $form->addFormFieldsFromMetadata();
552 // radio button for gender
554 $gender = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'gender_id');
555 foreach ($gender as $key => $var) {
556 $genderOptions[$key] = $form->createElement('radio', NULL,
557 ts('Gender'), $var, $key,
558 ['id' => "civicrm_gender_{$var}_{$key}"]
561 $form->addGroup($genderOptions, 'gender_id', ts('Gender'))->setAttribute('allowClear', TRUE);
563 $form->add('number', 'age_low', ts('Min Age'), ['class' => 'four', 'min' => 0]);
564 $form->addRule('age_low', ts('Please enter a positive integer'), 'positiveInteger');
565 $form->add('number', 'age_high', ts('Max Age'), ['class' => 'four', 'min' => 0]);
566 $form->addRule('age_high', ts('Please enter a positive integer'), 'positiveInteger');
567 $form->add('datepicker', 'age_asof_date', ts('As of'), NULL, FALSE, ['time' => FALSE]);
573 public static function notes(&$form) {
574 $form->add('hidden', 'hidden_notes', 1);
577 2 => ts('Body Only'),
578 3 => ts('Subject Only'),
581 $form->addRadio('note_option', '', $options);
583 $form->addElement('text', 'note', ts('Note Text'), CRM_Core_DAO
::getAttribute('CRM_Contact_DAO_Contact', 'sort_name'));
585 $form->setDefaults(['note_option' => 6]);
589 * Generate the custom Data Fields based for those with is_searchable = 1.
591 * @param CRM_Contact_Form_Search $form
593 * @throws \CiviCRM_API3_Exception
595 public static function custom(&$form) {
596 $form->add('hidden', 'hidden_custom', 1);
597 $extends = array_merge(['Contact', 'Individual', 'Household', 'Organization'],
598 CRM_Contact_BAO_ContactType
::subTypes()
600 $groupDetails = CRM_Core_BAO_CustomGroup
::getGroupDetail(NULL, TRUE,
604 $form->assign('groupTree', $groupDetails);
606 foreach ($groupDetails as $key => $group) {
607 $_groupTitle[$key] = $group['name'];
608 CRM_Core_ShowHideBlocks
::links($form, $group['name'], '', '');
610 foreach ($group['fields'] as $field) {
611 $fieldId = $field['id'];
612 $elementName = 'custom_' . $fieldId;
613 if ($field['data_type'] === 'Date' && $field['is_search_range']) {
614 $form->addDatePickerRange($elementName, $field['label']);
617 CRM_Core_BAO_CustomField
::addQuickFormElement($form, $elementName, $fieldId, FALSE, TRUE);
626 public static function CiviCase(&$form) {
627 //Looks like obsolete code, since CiviCase is a component, but might be used by HRD
628 $form->add('hidden', 'hidden_CiviCase', 1);
629 CRM_Case_BAO_Query
::buildSearchForm($form);