(NFC) (dev/core#878) Simplify copyright header (templates/*)
[civicrm-core.git] / CRM / Contact / Form / Search / Criteria.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
f299f7db 6 | Copyright CiviCRM LLC (c) 2004-2020 |
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
ca5cec67 31 * @copyright CiviCRM LLC https://civicrm.org/licensing
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'],
80b5c9f1 266 'created_date' => ['name' => 'created_date', 'template_grouping' => 'changeLog'],
267 'modified_date' => ['name' => 'modified_date', 'template_grouping' => 'changeLog'],
bca2ad39 268 'birth_date' => ['name' => 'birth_date', 'template_grouping' => 'demographic'],
269 'deceased_date' => ['name' => 'deceased_date', 'template_grouping' => 'demographic'],
95c2e666 270 'is_deceased' => ['is_deceased', 'template_grouping' => 'demographic'],
41b8dd1d 271 'relationship_start_date' => ['name' => 'relationship_start_date', 'template_grouping' => 'relationship'],
272 'relationship_end_date' => ['name' => 'relationship_end_date', 'template_grouping' => 'relationship'],
7d8f464a 273 // PseudoRelationship date field.
274 'relation_active_period_date' => [
275 'name' => 'relation_active_period_date',
276 'type' => CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME,
277 'title' => ts('Active Period'),
278 'table_name' => 'civicrm_relationship',
279 'where' => 'civicrm_relationship.start_date',
280 'where_end' => 'civicrm_relationship.end_date',
281 'html' => ['type' => 'SelectDate', 'formatType' => 'activityDateTime'],
282 'template_grouping' => 'relationship',
283 ],
8c9caddc 284 ];
7d8f464a 285
41b8dd1d 286 $metadata = civicrm_api3('Relationship', 'getfields', [])['values'];
287 $metadata = array_merge($metadata, civicrm_api3('Contact', 'getfields', [])['values']);
8c9caddc 288 foreach ($fields as $fieldName => $field) {
289 $fields[$fieldName] = array_merge(CRM_Utils_Array::value($fieldName, $metadata, []), $field);
290 }
291 return $fields;
292 }
293
bca2ad39 294 /**
295 * Get search field metadata filtered by the template grouping field.
296 *
297 * @param string $filter
298 *
299 * @return array
300 * @throws \CiviCRM_API3_Exception
301 */
302 public static function getFilteredSearchFieldMetadata($filter) {
303 $fields = self::getSearchFieldMetadata();
304 foreach ($fields as $index => $field) {
305 if ($field['template_grouping'] !== $filter) {
306 unset($fields[$index]);
307 }
308 }
309 return $fields;
310 }
311
a39762fd
MD
312 /**
313 * Defines the fields that can be displayed for the basic search section.
314 *
315 * @param CRM_Core_Form $form
bca2ad39 316 *
317 * @throws \CiviCRM_API3_Exception
a39762fd
MD
318 */
319 protected static function setBasicSearchFields($form) {
4b7b1909 320 $searchFields = [];
bca2ad39 321 foreach (self::getFilteredSearchFieldMetadata('basic') as $fieldName => $field) {
322 $searchFields[$fieldName] = $field;
4b7b1909 323 }
324 $form->assign('basicSearchFields', array_merge(self::getBasicSearchFields(), $searchFields));
723e3e6b 325 }
a39762fd 326
723e3e6b 327 /**
328 * Return list of basic contact fields that can be displayed for the basic search section.
329 *
330 */
331 public static function getBasicSearchFields() {
332 $userFramework = CRM_Core_Config::singleton()->userFramework;
333 return [
f73329c4 334 // For now an empty array is still left in place for ordering.
335 'sort_name' => [],
a39762fd
MD
336 'email' => ['name' => 'email'],
337 'contact_type' => ['name' => 'contact_type'],
338 'group' => [
339 'name' => 'group',
340 'template' => 'CRM/Contact/Form/Search/Criteria/Fields/group.tpl',
341 ],
342 'contact_tags' => ['name' => 'contact_tags'],
343 'tag_types_text' => ['name' => 'tag_types_text'],
344 'tag_search' => [
345 'name' => 'tag_search',
346 'help' => ['id' => 'id-all-tags'],
347 ],
348 'tag_set' => [
349 'name' => 'tag_set',
350 'is_custom' => TRUE,
351 'template' => 'CRM/Contact/Form/Search/Criteria/Fields/tag_set.tpl',
352 ],
353 'all_tag_types' => [
354 'name' => 'all_tag_types',
355 'class' => 'search-field__span-3 search-field__checkbox',
69078420 356 'help' => ['id' => 'id-all-tag-types'],
a39762fd
MD
357 ],
358 'phone_numeric' => [
359 'name' => 'phone_numeric',
360 'description' => ts('Punctuation and spaces are ignored.'),
361 ],
362 'phone_location_type_id' => ['name' => 'phone_location_type_id'],
363 'phone_phone_type_id' => ['name' => 'phone_phone_type_id'],
364 'privacy_toggle' => [
365 'name' => 'privacy_toggle',
366 'class' => 'search-field__span-2',
367 'template' => 'CRM/Contact/Form/Search/Criteria/Fields/privacy_toggle.tpl',
368 ],
369 'preferred_communication_method' => [
370 'name' => 'preferred_communication_method',
371 'template' => 'CRM/Contact/Form/Search/Criteria/Fields/preferred_communication_method.tpl',
372 ],
373 'contact_source' => [
374 'name' => 'contact_source',
375 'help' => ['id' => 'id-source', 'file' => 'CRM/Contact/Form/Contact'],
376 ],
377 'job_title' => ['name' => 'job_title'],
378 'preferred_language' => ['name' => 'preferred_language'],
379 'contact_id' => [
380 'name' => 'contact_id',
381 'help' => ['id' => 'id-contact-id', 'file' => 'CRM/Contact/Form/Contact'],
382 ],
383 'external_identifier' => [
384 'name' => 'external_identifier',
385 'help' => ['id' => 'id-external-id', 'file' => 'CRM/Contact/Form/Contact'],
386 ],
387 'uf_user' => [
388 'name' => 'uf_user',
389 'description' => ts('Does the contact have a %1 Account?', [$userFramework]),
390 ],
723e3e6b 391 ];
a39762fd
MD
392 }
393
86538308 394 /**
12fbb7b3 395 * @param CRM_Core_Form $form
86538308 396 */
00be9182 397 public static function location(&$form) {
12fbb7b3 398 $config = CRM_Core_Config::singleton();
6a488035
TO
399 // Build location criteria based on _submitValues if
400 // available; otherwise, use $form->_formValues.
401 $formValues = $form->_submitValues;
402
403 if (empty($formValues) && !empty($form->_formValues)) {
404 $formValues = $form->_formValues;
405 }
406
407 $form->addElement('hidden', 'hidden_location', 1);
408
409 $addressOptions = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
410 'address_options', TRUE, NULL, TRUE
411 );
412
413 $attributes = CRM_Core_DAO::getAttribute('CRM_Core_DAO_Address');
414
be2fb01f
CW
415 $elements = [
416 'street_address' => [ts('Street Address'), $attributes['street_address'], NULL, NULL],
417 'supplemental_address_1' => [ts('Supplemental Address 1'), $attributes['supplemental_address_1'], NULL, NULL],
418 'supplemental_address_2' => [ts('Supplemental Address 2'), $attributes['supplemental_address_2'], NULL, NULL],
419 'supplemental_address_3' => [ts('Supplemental Address 3'), $attributes['supplemental_address_3'], NULL, NULL],
420 'city' => [ts('City'), $attributes['city'], NULL, NULL],
421 'postal_code' => [ts('Postal Code'), $attributes['postal_code'], NULL, NULL],
422 'country' => [ts('Country'), $attributes['country_id'], 'country', FALSE],
423 'state_province' => [ts('State/Province'), $attributes['state_province_id'], 'stateProvince', TRUE],
424 'county' => [ts('County'), $attributes['county_id'], 'county', TRUE],
425 'address_name' => [ts('Address Name'), $attributes['address_name'], NULL, NULL],
426 'street_number' => [ts('Street Number'), $attributes['street_number'], NULL, NULL],
427 'street_name' => [ts('Street Name'), $attributes['street_name'], NULL, NULL],
428 'street_unit' => [ts('Apt/Unit/Suite'), $attributes['street_unit'], NULL, NULL],
429 ];
6a488035
TO
430
431 $parseStreetAddress = CRM_Utils_Array::value('street_address_parsing', $addressOptions, 0);
432 $form->assign('parseStreetAddress', $parseStreetAddress);
433 foreach ($elements as $name => $v) {
434 list($title, $attributes, $select, $multiSelect) = $v;
435
436 if (in_array($name,
be2fb01f 437 ['street_number', 'street_name', 'street_unit']
353ffa53 438 )) {
6a488035
TO
439 if (!$parseStreetAddress) {
440 continue;
441 }
442 }
443 elseif (!$addressOptions[$name]) {
444 continue;
445 }
446
447 if (!$attributes) {
448 $attributes = $attributes[$name];
449 }
450
451 if ($select) {
c927c151
CW
452 if ($select == 'stateProvince' || $select == 'county') {
453 $element = $form->addChainSelect($name);
6a488035
TO
454 }
455 else {
be2fb01f
CW
456 $selectElements = ['' => ts('- any -')] + CRM_Core_PseudoConstant::$select();
457 $element = $form->add('select', $name, $title, $selectElements, FALSE, ['class' => 'crm-select2']);
6a488035
TO
458 }
459 if ($multiSelect) {
460 $element->setMultiple(TRUE);
461 }
462 }
463 else {
464 $form->addElement('text', $name, $title, $attributes);
465 }
466
467 if ($addressOptions['postal_code']) {
be2fb01f
CW
468 $attr = ['class' => 'six'] + (array) CRM_Utils_Array::value('postal_code', $attributes);
469 $form->addElement('text', 'postal_code_low', NULL, $attr + ['placeholder' => ts('From')]);
470 $form->addElement('text', 'postal_code_high', NULL, $attr + ['placeholder' => ts('To')]);
6a488035
TO
471 }
472 }
473
474 // extend addresses with proximity search
4882d275 475 if (CRM_Utils_GeocodeProvider::getUsableClassName()) {
be2fb01f
CW
476 $form->addElement('text', 'prox_distance', ts('Find contacts within'), ['class' => 'six']);
477 $form->addElement('select', 'prox_distance_unit', NULL, [
12fbb7b3 478 'miles' => ts('Miles'),
21dfd5f5 479 'kilos' => ts('Kilometers'),
be2fb01f 480 ]);
12fbb7b3
CW
481 $form->addRule('prox_distance', ts('Please enter positive number as a distance'), 'numeric');
482 }
6a488035 483
be2fb01f 484 $form->addSelect('world_region', ['entity' => 'address', 'context' => 'search']);
6a488035 485
d8a8bb0b 486 // select for location type
b2b0530a 487 $locationType = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id');
be2fb01f 488 $form->add('select', 'location_type', ts('Address Location'), $locationType, FALSE, [
d8a8bb0b
CW
489 'multiple' => TRUE,
490 'class' => 'crm-select2',
491 'placeholder' => ts('Primary'),
be2fb01f 492 ]);
6a488035 493
86a0d21e 494 // custom data extending addresses
be2fb01f 495 CRM_Core_BAO_Query::addCustomFormFields($form, ['Address']);
6a488035
TO
496 }
497
86538308 498 /**
c490a46a 499 * @param CRM_Core_Form $form
86538308 500 */
00be9182 501 public static function activity(&$form) {
6a488035
TO
502 $form->add('hidden', 'hidden_activity', 1);
503 CRM_Activity_BAO_Query::buildSearchForm($form);
504 }
505
86538308 506 /**
c490a46a 507 * @param CRM_Core_Form $form
80b5c9f1 508 *
509 * @throws \CiviCRM_API3_Exception
86538308 510 */
00be9182 511 public static function changeLog(&$form) {
6a488035 512 $form->add('hidden', 'hidden_changeLog', 1);
80b5c9f1 513 $form->addSearchFieldMetadata(['Contact' => self::getFilteredSearchFieldMetadata('changeLog')]);
514 $form->addFormFieldsFromMetadata();
6a488035
TO
515 // block for change log
516 $form->addElement('text', 'changed_by', ts('Modified By'), NULL);
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
e2ce074e 608 *
609 * @throws \CiviCRM_API3_Exception
6a488035 610 */
00be9182 611 public static function custom(&$form) {
6a488035 612 $form->add('hidden', 'hidden_custom', 1);
be2fb01f 613 $extends = array_merge(['Contact', 'Individual', 'Household', 'Organization'],
6a488035
TO
614 CRM_Contact_BAO_ContactType::subTypes()
615 );
616 $groupDetails = CRM_Core_BAO_CustomGroup::getGroupDetail(NULL, TRUE,
617 $extends
618 );
619
620 $form->assign('groupTree', $groupDetails);
621
622 foreach ($groupDetails as $key => $group) {
623 $_groupTitle[$key] = $group['name'];
624 CRM_Core_ShowHideBlocks::links($form, $group['name'], '', '');
625
6a488035
TO
626 foreach ($group['fields'] as $field) {
627 $fieldId = $field['id'];
628 $elementName = 'custom_' . $fieldId;
e2ce074e 629 if ($field['data_type'] === 'Date' && $field['is_search_range']) {
630 $form->addDatePickerRange($elementName, $field['label']);
0b77ccc2
CW
631 }
632 else {
633 CRM_Core_BAO_CustomField::addQuickFormElement($form, $elementName, $fieldId, FALSE, TRUE);
634 }
6a488035
TO
635 }
636 }
6a488035
TO
637 }
638
86538308
EM
639 /**
640 * @param $form
641 */
00be9182 642 public static function CiviCase(&$form) {
6a488035
TO
643 //Looks like obsolete code, since CiviCase is a component, but might be used by HRD
644 $form->add('hidden', 'hidden_CiviCase', 1);
645 CRM_Case_BAO_Query::buildSearchForm($form);
646 }
96025800 647
6a488035 648}