Merge pull request #15638 from eileenmcnaughton/vangelis
[civicrm-core.git] / CRM / Contact / Form / Search / Criteria.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
6b83d5bd 31 * @copyright CiviCRM LLC (c) 2004-2019
6a488035
TO
32 */
33class CRM_Contact_Form_Search_Criteria {
69078420 34
8a4f27dc 35 /**
8c9caddc 36 * @param CRM_Contact_Form_Search_Advanced $form
37 *
38 * @throws \CRM_Core_Exception
bca2ad39 39 * @throws \CiviCRM_API3_Exception
8a4f27dc 40 */
00be9182 41 public static function basic(&$form) {
bca2ad39 42 $form->addSearchFieldMetadata(['Contact' => self::getFilteredSearchFieldMetadata('basic')]);
8c9caddc 43 $form->addFormFieldsFromMetadata();
a39762fd 44 self::setBasicSearchFields($form);
6a488035
TO
45 $form->addElement('hidden', 'hidden_basic', 1);
46
47 if ($form->_searchOptions['contactType']) {
46b3417a 48 $contactTypes = CRM_Contact_BAO_ContactType::getSelectElements();
6a488035
TO
49
50 if ($contactTypes) {
51 $form->add('select', 'contact_type', ts('Contact Type(s)'), $contactTypes, FALSE,
be2fb01f 52 ['id' => 'contact_type', 'multiple' => 'multiple', 'class' => 'crm-select2', 'style' => 'width: 100%;']
6a488035
TO
53 );
54 }
55 }
56
57 if ($form->_searchOptions['groups']) {
58 // multiselect for groups
59 if ($form->_group) {
60 // Arrange groups into hierarchical listing (child groups follow their parents and have indentation spacing in title)
f828fa2c 61 $groupHierarchy = CRM_Contact_BAO_Group::getGroupsHierarchy($form->_group, NULL, '&nbsp;&nbsp;', TRUE);
6a488035
TO
62
63 $form->add('select', 'group', ts('Groups'), $groupHierarchy, FALSE,
be2fb01f 64 ['id' => 'group', 'multiple' => 'multiple', 'class' => 'crm-select2']
6a488035
TO
65 );
66 $groupOptions = CRM_Core_BAO_OptionValue::getOptionValuesAssocArrayFromName('group_type');
67 $form->add('select', 'group_type', ts('Group Types'), $groupOptions, FALSE,
be2fb01f 68 ['id' => 'group_type', 'multiple' => 'multiple', 'class' => 'crm-select2']
6a488035 69 );
353ffa53 70 $form->add('hidden', 'group_search_selected', 'group');
6a488035
TO
71 }
72 }
73
74 if ($form->_searchOptions['tags']) {
75 // multiselect for categories
76 $contactTags = CRM_Core_BAO_Tag::getTags();
77
78 if ($contactTags) {
a39762fd 79 $form->add('select', 'contact_tags', ts('Select Tag(s)'), $contactTags, FALSE,
be2fb01f 80 ['id' => 'contact_tags', 'multiple' => 'multiple', 'class' => 'crm-select2', 'style' => 'width: 100%;']
6a488035
TO
81 );
82 }
83
84 $parentNames = CRM_Core_BAO_Tag::getTagSet('civicrm_contact');
4dff5e17 85 CRM_Core_Form_Tag::buildQuickForm($form, $parentNames, 'civicrm_contact', NULL, TRUE, FALSE);
f828fa2c 86
6a488035 87 $used_for = CRM_Core_OptionGroup::values('tag_used_for');
be2fb01f 88 $tagsTypes = [];
ab8a593e 89 $showAllTagTypes = FALSE;
6a488035
TO
90 foreach ($used_for as $key => $value) {
91 //check tags for every type and find if there are any defined
92 $tags = CRM_Core_BAO_Tag::getTagsUsedFor($key, FALSE, TRUE, NULL);
93 // check if there are tags other than contact type, if no - keep checkbox hidden on adv search
94 // we will hide searching contact by attachments tags until it will be implemented in core
95 if (count($tags) && $key != 'civicrm_file' && $key != 'civicrm_contact') {
96 //if tags exists then add type to display in adv search form help text
1836ab9e 97 $tagsTypes[] = $value;
4eeb9a5b 98 $showAllTagTypes = TRUE;
6a488035
TO
99 }
100 }
101 $tagTypesText = implode(" or ", $tagsTypes);
102 if ($showAllTagTypes) {
be2fb01f 103 $form->add('checkbox', 'all_tag_types', ts('Include tags used for %1', [1 => $tagTypesText]));
353ffa53 104 $form->add('hidden', 'tag_types_text', $tagTypesText);
6a488035
TO
105 }
106 }
107
6a488035
TO
108 //added contact source
109 $form->add('text', 'contact_source', ts('Contact Source'), CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'contact_source'));
110
111 //added job title
112 $form->addElement('text', 'job_title', ts('Job Title'), CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'job_title'));
113
6a488035 114 //added internal ID
be2fb01f 115 $form->add('number', 'contact_id', ts('Contact ID'), CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'id') + ['min' => 1]);
5a0739f2 116 $form->addRule('contact_id', ts('Please enter valid Contact ID'), 'positiveInteger');
6a488035
TO
117
118 //added external ID
119 $form->addElement('text', 'external_identifier', ts('External ID'), CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'external_identifier'));
120
89595c92 121 if (CRM_Core_Permission::check('access deleted contacts') and Civi::settings()->get('contact_undelete')) {
6a488035
TO
122 $form->add('checkbox', 'deleted_contacts', ts('Search in Trash') . '<br />' . ts('(deleted contacts)'));
123 }
124
125 // add checkbox for cms users only
8a4f27dc 126 $form->addYesNo('uf_user', ts('CMS User?'), TRUE);
6a488035
TO
127
128 // tag all search
129 $form->add('text', 'tag_search', ts('All Tags'));
130
131 // add search profiles
132
133 // FIXME: This is probably a part of profiles - need to be
134 // FIXME: eradicated from here when profiles are reworked.
be2fb01f 135 $types = ['Participant', 'Contribution', 'Membership'];
6a488035
TO
136
137 // get component profiles
be2fb01f 138 $componentProfiles = [];
6a488035
TO
139 $componentProfiles = CRM_Core_BAO_UFGroup::getProfiles($types);
140
141 $ufGroups = CRM_Core_BAO_UFGroup::getModuleUFGroup('Search Profile', 1);
142 $accessibleUfGroups = CRM_Core_Permission::ufGroup(CRM_Core_Permission::VIEW);
143
be2fb01f 144 $searchProfiles = [];
6a488035
TO
145 foreach ($ufGroups as $key => $var) {
146 if (!array_key_exists($key, $componentProfiles) && in_array($key, $accessibleUfGroups)) {
147 $searchProfiles[$key] = $var['title'];
148 }
149 }
150
3c0ec132 151 $form->add('select',
6a488035 152 'uf_group_id',
e2b3c0e3 153 ts('Views For Display Contacts'),
be2fb01f 154 [
317fceb4 155 '0' => ts('- default view -'),
be2fb01f 156 ] + $searchProfiles,
3c0ec132 157 FALSE,
be2fb01f 158 ['class' => 'crm-select2']
6a488035
TO
159 );
160
161 $componentModes = CRM_Contact_Form_Search::getModeSelect();
e30af20d 162 $form->assign('component_mappings', json_encode(CRM_Contact_Form_Search::getModeToComponentMapping()));
6a488035 163 if (count($componentModes) > 1) {
3c0ec132 164 $form->add('select',
6a488035
TO
165 'component_mode',
166 ts('Display Results As'),
3c0ec132 167 $componentModes,
ae1f4229 168 FALSE,
be2fb01f 169 ['class' => 'crm-select2']
6a488035
TO
170 );
171 }
172
3c0ec132 173 $form->addRadio(
6a488035
TO
174 'operator',
175 ts('Search Operator'),
be2fb01f 176 [
eda34f9b
MW
177 CRM_Contact_BAO_Query::SEARCH_OPERATOR_AND => ts('AND'),
178 CRM_Contact_BAO_Query::SEARCH_OPERATOR_OR => ts('OR'),
be2fb01f
CW
179 ],
180 ['allowClear' => FALSE]
6a488035
TO
181 );
182
183 // add the option to display relationships
184 $rTypes = CRM_Core_PseudoConstant::relationshipType();
be2fb01f 185 $rSelect = ['' => ts('- Select Relationship Type-')];
6a488035
TO
186 foreach ($rTypes as $rid => $rValue) {
187 if ($rValue['label_a_b'] == $rValue['label_b_a']) {
188 $rSelect[$rid] = $rValue['label_a_b'];
189 }
190 else {
191 $rSelect["{$rid}_a_b"] = $rValue['label_a_b'];
192 $rSelect["{$rid}_b_a"] = $rValue['label_b_a'];
193 }
194 }
195
196 $form->addElement('select',
197 'display_relationship_type',
198 ts('Display Results as Relationship'),
aae123ba 199 $rSelect,
be2fb01f 200 ['class' => 'crm-select2']
6a488035
TO
201 );
202
203 // checkboxes for DO NOT phone, email, mail
204 // we take labels from SelectValues
205 $t = CRM_Core_SelectValues::privacy();
206 $form->add('select',
207 'privacy_options',
208 ts('Privacy'),
209 $t,
210 FALSE,
be2fb01f 211 [
6a488035
TO
212 'id' => 'privacy_options',
213 'multiple' => 'multiple',
02ddf039 214 'class' => 'crm-select2',
be2fb01f 215 ]
6a488035
TO
216 );
217
218 $form->addElement('select',
219 'privacy_operator',
220 ts('Operator'),
be2fb01f 221 [
6ea503d4 222 'OR' => ts('OR'),
6a488035 223 'AND' => ts('AND'),
be2fb01f 224 ]
6a488035
TO
225 );
226
be2fb01f 227 $options = [
6a488035
TO
228 1 => ts('Exclude'),
229 2 => ts('Include by Privacy Option(s)'),
be2fb01f
CW
230 ];
231 $form->addRadio('privacy_toggle', ts('Privacy Options'), $options, ['allowClear' => FALSE]);
6a488035
TO
232
233 // preferred communication method
38056b36
AS
234 if (Civi::settings()->get('civimail_multiple_bulk_emails')) {
235 $form->addSelect('email_on_hold',
be2fb01f 236 ['entity' => 'email', 'multiple' => 'multiple', 'label' => ts('Email On Hold'), 'options' => CRM_Core_PseudoConstant::emailOnHoldOptions()]);
38056b36
AS
237 }
238 else {
239 $form->add('advcheckbox', 'email_on_hold', ts('Email On Hold'));
240 }
6a488035 241
7cc09daf 242 $form->addSelect('preferred_communication_method',
be2fb01f 243 ['entity' => 'contact', 'multiple' => 'multiple', 'label' => ts('Preferred Communication Method'), 'option_url' => NULL, 'placeholder' => ts('- any -')]);
6a488035
TO
244
245 //CRM-6138 Preferred Language
be2fb01f 246 $form->addSelect('preferred_language', ['class' => 'twenty', 'context' => 'search']);
6a488035
TO
247
248 // Phone search
d79be26c 249 $form->addElement('text', 'phone_numeric', ts('Phone'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_Phone', 'phone'));
b2b0530a 250 $locationType = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id');
b4f964d9 251 $phoneType = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id');
be2fb01f
CW
252 $form->add('select', 'phone_location_type_id', ts('Phone Location'), ['' => ts('- any -')] + $locationType, FALSE, ['class' => 'crm-select2']);
253 $form->add('select', 'phone_phone_type_id', ts('Phone Type'), ['' => ts('- any -')] + $phoneType, FALSE, ['class' => 'crm-select2']);
6a488035
TO
254 }
255
8c9caddc 256 /**
257 * Get the metadata for fields to be included on the contact search form.
d7cc9ca9 258 *
259 * @throws \CiviCRM_API3_Exception
8c9caddc 260 */
261 public static function getSearchFieldMetadata() {
262 $fields = [
263 'sort_name' => ['title' => ts('Complete OR Partial Name'), 'template_grouping' => 'basic'],
d7cc9ca9 264 'email' => ['title' => ts('Complete OR Partial Email'), 'entity' => 'Email', 'template_grouping' => 'basic'],
265 'contact_tags' => ['name' => 'contact_tags', 'type' => CRM_Utils_Type::T_INT, 'is_pseudofield' => TRUE, 'template_grouping' => 'basic'],
bca2ad39 266 'birth_date' => ['name' => 'birth_date', 'template_grouping' => 'demographic'],
267 'deceased_date' => ['name' => 'deceased_date', 'template_grouping' => 'demographic'],
95c2e666 268 'is_deceased' => ['is_deceased', 'template_grouping' => 'demographic'],
41b8dd1d 269 'relationship_start_date' => ['name' => 'relationship_start_date', 'template_grouping' => 'relationship'],
270 'relationship_end_date' => ['name' => 'relationship_end_date', 'template_grouping' => 'relationship'],
7d8f464a 271 // PseudoRelationship date field.
272 'relation_active_period_date' => [
273 'name' => 'relation_active_period_date',
274 'type' => CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME,
275 'title' => ts('Active Period'),
276 'table_name' => 'civicrm_relationship',
277 'where' => 'civicrm_relationship.start_date',
278 'where_end' => 'civicrm_relationship.end_date',
279 'html' => ['type' => 'SelectDate', 'formatType' => 'activityDateTime'],
280 'template_grouping' => 'relationship',
281 ],
8c9caddc 282 ];
7d8f464a 283
41b8dd1d 284 $metadata = civicrm_api3('Relationship', 'getfields', [])['values'];
285 $metadata = array_merge($metadata, civicrm_api3('Contact', 'getfields', [])['values']);
8c9caddc 286 foreach ($fields as $fieldName => $field) {
287 $fields[$fieldName] = array_merge(CRM_Utils_Array::value($fieldName, $metadata, []), $field);
288 }
289 return $fields;
290 }
291
bca2ad39 292 /**
293 * Get search field metadata filtered by the template grouping field.
294 *
295 * @param string $filter
296 *
297 * @return array
298 * @throws \CiviCRM_API3_Exception
299 */
300 public static function getFilteredSearchFieldMetadata($filter) {
301 $fields = self::getSearchFieldMetadata();
302 foreach ($fields as $index => $field) {
303 if ($field['template_grouping'] !== $filter) {
304 unset($fields[$index]);
305 }
306 }
307 return $fields;
308 }
309
a39762fd
MD
310 /**
311 * Defines the fields that can be displayed for the basic search section.
312 *
313 * @param CRM_Core_Form $form
bca2ad39 314 *
315 * @throws \CiviCRM_API3_Exception
a39762fd
MD
316 */
317 protected static function setBasicSearchFields($form) {
4b7b1909 318 $searchFields = [];
bca2ad39 319 foreach (self::getFilteredSearchFieldMetadata('basic') as $fieldName => $field) {
320 $searchFields[$fieldName] = $field;
4b7b1909 321 }
322 $form->assign('basicSearchFields', array_merge(self::getBasicSearchFields(), $searchFields));
723e3e6b 323 }
a39762fd 324
723e3e6b 325 /**
326 * Return list of basic contact fields that can be displayed for the basic search section.
327 *
328 */
329 public static function getBasicSearchFields() {
330 $userFramework = CRM_Core_Config::singleton()->userFramework;
331 return [
f73329c4 332 // For now an empty array is still left in place for ordering.
333 'sort_name' => [],
a39762fd
MD
334 'email' => ['name' => 'email'],
335 'contact_type' => ['name' => 'contact_type'],
336 'group' => [
337 'name' => 'group',
338 'template' => 'CRM/Contact/Form/Search/Criteria/Fields/group.tpl',
339 ],
340 'contact_tags' => ['name' => 'contact_tags'],
341 'tag_types_text' => ['name' => 'tag_types_text'],
342 'tag_search' => [
343 'name' => 'tag_search',
344 'help' => ['id' => 'id-all-tags'],
345 ],
346 'tag_set' => [
347 'name' => 'tag_set',
348 'is_custom' => TRUE,
349 'template' => 'CRM/Contact/Form/Search/Criteria/Fields/tag_set.tpl',
350 ],
351 'all_tag_types' => [
352 'name' => 'all_tag_types',
353 'class' => 'search-field__span-3 search-field__checkbox',
69078420 354 'help' => ['id' => 'id-all-tag-types'],
a39762fd
MD
355 ],
356 'phone_numeric' => [
357 'name' => 'phone_numeric',
358 'description' => ts('Punctuation and spaces are ignored.'),
359 ],
360 'phone_location_type_id' => ['name' => 'phone_location_type_id'],
361 'phone_phone_type_id' => ['name' => 'phone_phone_type_id'],
362 'privacy_toggle' => [
363 'name' => 'privacy_toggle',
364 'class' => 'search-field__span-2',
365 'template' => 'CRM/Contact/Form/Search/Criteria/Fields/privacy_toggle.tpl',
366 ],
367 'preferred_communication_method' => [
368 'name' => 'preferred_communication_method',
369 'template' => 'CRM/Contact/Form/Search/Criteria/Fields/preferred_communication_method.tpl',
370 ],
371 'contact_source' => [
372 'name' => 'contact_source',
373 'help' => ['id' => 'id-source', 'file' => 'CRM/Contact/Form/Contact'],
374 ],
375 'job_title' => ['name' => 'job_title'],
376 'preferred_language' => ['name' => 'preferred_language'],
377 'contact_id' => [
378 'name' => 'contact_id',
379 'help' => ['id' => 'id-contact-id', 'file' => 'CRM/Contact/Form/Contact'],
380 ],
381 'external_identifier' => [
382 'name' => 'external_identifier',
383 'help' => ['id' => 'id-external-id', 'file' => 'CRM/Contact/Form/Contact'],
384 ],
385 'uf_user' => [
386 'name' => 'uf_user',
387 'description' => ts('Does the contact have a %1 Account?', [$userFramework]),
388 ],
723e3e6b 389 ];
a39762fd
MD
390 }
391
86538308 392 /**
12fbb7b3 393 * @param CRM_Core_Form $form
86538308 394 */
00be9182 395 public static function location(&$form) {
12fbb7b3 396 $config = CRM_Core_Config::singleton();
6a488035
TO
397 // Build location criteria based on _submitValues if
398 // available; otherwise, use $form->_formValues.
399 $formValues = $form->_submitValues;
400
401 if (empty($formValues) && !empty($form->_formValues)) {
402 $formValues = $form->_formValues;
403 }
404
405 $form->addElement('hidden', 'hidden_location', 1);
406
407 $addressOptions = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
408 'address_options', TRUE, NULL, TRUE
409 );
410
411 $attributes = CRM_Core_DAO::getAttribute('CRM_Core_DAO_Address');
412
be2fb01f
CW
413 $elements = [
414 'street_address' => [ts('Street Address'), $attributes['street_address'], NULL, NULL],
415 'supplemental_address_1' => [ts('Supplemental Address 1'), $attributes['supplemental_address_1'], NULL, NULL],
416 'supplemental_address_2' => [ts('Supplemental Address 2'), $attributes['supplemental_address_2'], NULL, NULL],
417 'supplemental_address_3' => [ts('Supplemental Address 3'), $attributes['supplemental_address_3'], NULL, NULL],
418 'city' => [ts('City'), $attributes['city'], NULL, NULL],
419 'postal_code' => [ts('Postal Code'), $attributes['postal_code'], NULL, NULL],
420 'country' => [ts('Country'), $attributes['country_id'], 'country', FALSE],
421 'state_province' => [ts('State/Province'), $attributes['state_province_id'], 'stateProvince', TRUE],
422 'county' => [ts('County'), $attributes['county_id'], 'county', TRUE],
423 'address_name' => [ts('Address Name'), $attributes['address_name'], NULL, NULL],
424 'street_number' => [ts('Street Number'), $attributes['street_number'], NULL, NULL],
425 'street_name' => [ts('Street Name'), $attributes['street_name'], NULL, NULL],
426 'street_unit' => [ts('Apt/Unit/Suite'), $attributes['street_unit'], NULL, NULL],
427 ];
6a488035
TO
428
429 $parseStreetAddress = CRM_Utils_Array::value('street_address_parsing', $addressOptions, 0);
430 $form->assign('parseStreetAddress', $parseStreetAddress);
431 foreach ($elements as $name => $v) {
432 list($title, $attributes, $select, $multiSelect) = $v;
433
434 if (in_array($name,
be2fb01f 435 ['street_number', 'street_name', 'street_unit']
353ffa53 436 )) {
6a488035
TO
437 if (!$parseStreetAddress) {
438 continue;
439 }
440 }
441 elseif (!$addressOptions[$name]) {
442 continue;
443 }
444
445 if (!$attributes) {
446 $attributes = $attributes[$name];
447 }
448
449 if ($select) {
c927c151
CW
450 if ($select == 'stateProvince' || $select == 'county') {
451 $element = $form->addChainSelect($name);
6a488035
TO
452 }
453 else {
be2fb01f
CW
454 $selectElements = ['' => ts('- any -')] + CRM_Core_PseudoConstant::$select();
455 $element = $form->add('select', $name, $title, $selectElements, FALSE, ['class' => 'crm-select2']);
6a488035
TO
456 }
457 if ($multiSelect) {
458 $element->setMultiple(TRUE);
459 }
460 }
461 else {
462 $form->addElement('text', $name, $title, $attributes);
463 }
464
465 if ($addressOptions['postal_code']) {
be2fb01f
CW
466 $attr = ['class' => 'six'] + (array) CRM_Utils_Array::value('postal_code', $attributes);
467 $form->addElement('text', 'postal_code_low', NULL, $attr + ['placeholder' => ts('From')]);
468 $form->addElement('text', 'postal_code_high', NULL, $attr + ['placeholder' => ts('To')]);
6a488035
TO
469 }
470 }
471
472 // extend addresses with proximity search
4882d275 473 if (CRM_Utils_GeocodeProvider::getUsableClassName()) {
be2fb01f
CW
474 $form->addElement('text', 'prox_distance', ts('Find contacts within'), ['class' => 'six']);
475 $form->addElement('select', 'prox_distance_unit', NULL, [
12fbb7b3 476 'miles' => ts('Miles'),
21dfd5f5 477 'kilos' => ts('Kilometers'),
be2fb01f 478 ]);
12fbb7b3
CW
479 $form->addRule('prox_distance', ts('Please enter positive number as a distance'), 'numeric');
480 }
6a488035 481
be2fb01f 482 $form->addSelect('world_region', ['entity' => 'address', 'context' => 'search']);
6a488035 483
d8a8bb0b 484 // select for location type
b2b0530a 485 $locationType = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id');
be2fb01f 486 $form->add('select', 'location_type', ts('Address Location'), $locationType, FALSE, [
d8a8bb0b
CW
487 'multiple' => TRUE,
488 'class' => 'crm-select2',
489 'placeholder' => ts('Primary'),
be2fb01f 490 ]);
6a488035 491
86a0d21e 492 // custom data extending addresses
be2fb01f 493 CRM_Core_BAO_Query::addCustomFormFields($form, ['Address']);
6a488035
TO
494 }
495
86538308 496 /**
c490a46a 497 * @param CRM_Core_Form $form
86538308 498 */
00be9182 499 public static function activity(&$form) {
6a488035
TO
500 $form->add('hidden', 'hidden_activity', 1);
501 CRM_Activity_BAO_Query::buildSearchForm($form);
502 }
503
86538308 504 /**
c490a46a 505 * @param CRM_Core_Form $form
86538308 506 */
00be9182 507 public static function changeLog(&$form) {
6a488035
TO
508 $form->add('hidden', 'hidden_changeLog', 1);
509
510 // block for change log
511 $form->addElement('text', 'changed_by', ts('Modified By'), NULL);
512
be2fb01f
CW
513 $dates = [1 => ts('Added'), 2 => ts('Modified')];
514 $form->addRadio('log_date', NULL, $dates, ['allowClear' => TRUE]);
6a488035 515
672decaf 516 CRM_Core_Form_Date::buildDateRange($form, 'log_date', 1, '_low', '_high', ts('From:'), FALSE, FALSE);
6a488035
TO
517 }
518
86538308 519 /**
c490a46a 520 * @param CRM_Core_Form $form
86538308 521 */
00be9182 522 public static function task(&$form) {
6a488035
TO
523 $form->add('hidden', 'hidden_task', 1);
524 }
525
86538308 526 /**
6e83b317 527 * @param CRM_Core_Form_Search $form
41b8dd1d 528 *
529 * @throws \CiviCRM_API3_Exception
86538308 530 */
00be9182 531 public static function relationship(&$form) {
6a488035 532 $form->add('hidden', 'hidden_relationship', 1);
41b8dd1d 533 $form->addSearchFieldMetadata(['Relationship' => self::getFilteredSearchFieldMetadata('relationship')]);
534 $form->addFormFieldsFromMetadata();
6e83b317 535 $form->add('text', 'relation_description', ts('Description'), ['class' => 'twenty']);
6a488035 536 $allRelationshipType = CRM_Contact_BAO_Relationship::getContactRelationshipType(NULL, NULL, NULL, NULL, TRUE);
be2fb01f 537 $form->add('select', 'relation_type_id', ts('Relationship Type'), ['' => ts('- select -')] + $allRelationshipType, FALSE, ['multiple' => TRUE, 'class' => 'crm-select2']);
6a488035 538 $form->addElement('text', 'relation_target_name', ts('Target Contact'), CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'sort_name'));
5ea73a9f 539 // relation status
be2fb01f 540 $relStatusOption = [ts('Active'), ts('Inactive'), ts('All')];
6a488035 541 $form->addRadio('relation_status', ts('Relationship Status'), $relStatusOption);
be2fb01f 542 $form->setDefaults(['relation_status' => 0]);
5ea73a9f 543 // relation permission
f871c3a9
AS
544 $allRelationshipPermissions = CRM_Contact_BAO_Relationship::buildOptions('is_permission_a_b');
545 $form->add('select', 'relation_permission', ts('Permissioned Relationship'),
be2fb01f 546 ['' => ts('- select -')] + $allRelationshipPermissions, FALSE, ['multiple' => TRUE, 'class' => 'crm-select2']);
6a488035
TO
547
548 //add the target group
549 if ($form->_group) {
550 $form->add('select', 'relation_target_group', ts('Target Contact(s) in Group'), $form->_group, FALSE,
be2fb01f 551 ['id' => 'relation_target_group', 'multiple' => 'multiple', 'class' => 'crm-select2']
6a488035
TO
552 );
553 }
eea5db81 554
6a488035 555 // add all the custom searchable fields
be2fb01f 556 CRM_Core_BAO_Query::addCustomFormFields($form, ['Relationship']);
6a488035
TO
557 }
558
86538308 559 /**
ac241c34 560 * @param CRM_Core_Form_Search $form
bca2ad39 561 *
562 * @throws \CiviCRM_API3_Exception
86538308 563 */
00be9182 564 public static function demographics(&$form) {
6a488035 565 $form->add('hidden', 'hidden_demographics', 1);
bca2ad39 566 $form->addSearchFieldMetadata(['Contact' => self::getFilteredSearchFieldMetadata('demographic')]);
567 $form->addFormFieldsFromMetadata();
6a488035 568 // radio button for gender
be2fb01f 569 $genderOptions = [];
26cf88b5 570 $gender = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id');
6a488035
TO
571 foreach ($gender as $key => $var) {
572 $genderOptions[$key] = $form->createElement('radio', NULL,
573 ts('Gender'), $var, $key,
be2fb01f 574 ['id' => "civicrm_gender_{$var}_{$key}"]
6a488035
TO
575 );
576 }
b847e6e7 577 $form->addGroup($genderOptions, 'gender_id', ts('Gender'))->setAttribute('allowClear', TRUE);
6a488035 578
ac241c34 579 $form->add('number', 'age_low', ts('Min Age'), ['class' => 'four', 'min' => 0]);
c4a7c967 580 $form->addRule('age_low', ts('Please enter a positive integer'), 'positiveInteger');
ac241c34 581 $form->add('number', 'age_high', ts('Max Age'), ['class' => 'four', 'min' => 0]);
c4a7c967 582 $form->addRule('age_high', ts('Please enter a positive integer'), 'positiveInteger');
ac241c34 583 $form->add('datepicker', 'age_asof_date', ts('As of'), NULL, FALSE, ['time' => FALSE]);
6a488035
TO
584 }
585
86538308
EM
586 /**
587 * @param $form
588 */
00be9182 589 public static function notes(&$form) {
6a488035
TO
590 $form->add('hidden', 'hidden_notes', 1);
591
be2fb01f 592 $options = [
6a488035
TO
593 2 => ts('Body Only'),
594 3 => ts('Subject Only'),
595 6 => ts('Both'),
be2fb01f 596 ];
6a488035
TO
597 $form->addRadio('note_option', '', $options);
598
599 $form->addElement('text', 'note', ts('Note Text'), CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'sort_name'));
600
be2fb01f 601 $form->setDefaults(['note_option' => 6]);
6a488035
TO
602 }
603
604 /**
5a409b50 605 * Generate the custom Data Fields based for those with is_searchable = 1.
6a488035 606 *
5a409b50 607 * @param CRM_Contact_Form_Search $form
6a488035 608 */
00be9182 609 public static function custom(&$form) {
6a488035 610 $form->add('hidden', 'hidden_custom', 1);
be2fb01f 611 $extends = array_merge(['Contact', 'Individual', 'Household', 'Organization'],
6a488035
TO
612 CRM_Contact_BAO_ContactType::subTypes()
613 );
614 $groupDetails = CRM_Core_BAO_CustomGroup::getGroupDetail(NULL, TRUE,
615 $extends
616 );
617
618 $form->assign('groupTree', $groupDetails);
619
620 foreach ($groupDetails as $key => $group) {
621 $_groupTitle[$key] = $group['name'];
622 CRM_Core_ShowHideBlocks::links($form, $group['name'], '', '');
623
6a488035
TO
624 foreach ($group['fields'] as $field) {
625 $fieldId = $field['id'];
626 $elementName = 'custom_' . $fieldId;
0b77ccc2
CW
627 if ($field['data_type'] == 'Date' && $field['is_search_range']) {
628 CRM_Core_Form_Date::buildDateRange($form, $elementName, 1, '_from', '_to', ts('From:'), FALSE);
629 }
630 else {
631 CRM_Core_BAO_CustomField::addQuickFormElement($form, $elementName, $fieldId, FALSE, TRUE);
632 }
6a488035
TO
633 }
634 }
6a488035
TO
635 }
636
86538308
EM
637 /**
638 * @param $form
639 */
00be9182 640 public static function CiviCase(&$form) {
6a488035
TO
641 //Looks like obsolete code, since CiviCase is a component, but might be used by HRD
642 $form->add('hidden', 'hidden_CiviCase', 1);
643 CRM_Case_BAO_Query::buildSearchForm($form);
644 }
96025800 645
6a488035 646}