| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 5 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2020 | |
| 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 | +--------------------------------------------------------------------+ |
| 26 | */ |
| 27 | |
| 28 | /** |
| 29 | * |
| 30 | * @package CRM |
| 31 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
| 32 | */ |
| 33 | class CRM_Contact_Form_Search_Criteria { |
| 34 | |
| 35 | /** |
| 36 | * @param CRM_Contact_Form_Search_Advanced $form |
| 37 | * |
| 38 | * @throws \CRM_Core_Exception |
| 39 | * @throws \CiviCRM_API3_Exception |
| 40 | */ |
| 41 | public static function basic(&$form) { |
| 42 | $form->addSearchFieldMetadata(['Contact' => self::getFilteredSearchFieldMetadata('basic')]); |
| 43 | $form->addFormFieldsFromMetadata(); |
| 44 | self::setBasicSearchFields($form); |
| 45 | $form->addElement('hidden', 'hidden_basic', 1); |
| 46 | |
| 47 | if ($form->_searchOptions['contactType']) { |
| 48 | $contactTypes = CRM_Contact_BAO_ContactType::getSelectElements(); |
| 49 | |
| 50 | if ($contactTypes) { |
| 51 | $form->add('select', 'contact_type', ts('Contact Type(s)'), $contactTypes, FALSE, |
| 52 | ['id' => 'contact_type', 'multiple' => 'multiple', 'class' => 'crm-select2', 'style' => 'width: 100%;'] |
| 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) |
| 61 | $groupHierarchy = CRM_Contact_BAO_Group::getGroupsHierarchy($form->_group, NULL, ' ', TRUE); |
| 62 | |
| 63 | $form->add('select', 'group', ts('Groups'), $groupHierarchy, FALSE, |
| 64 | ['id' => 'group', 'multiple' => 'multiple', 'class' => 'crm-select2'] |
| 65 | ); |
| 66 | $groupOptions = CRM_Core_BAO_OptionValue::getOptionValuesAssocArrayFromName('group_type'); |
| 67 | $form->add('select', 'group_type', ts('Group Types'), $groupOptions, FALSE, |
| 68 | ['id' => 'group_type', 'multiple' => 'multiple', 'class' => 'crm-select2'] |
| 69 | ); |
| 70 | $form->add('hidden', 'group_search_selected', 'group'); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | if ($form->_searchOptions['tags']) { |
| 75 | // multiselect for categories |
| 76 | $contactTags = CRM_Core_BAO_Tag::getTags(); |
| 77 | |
| 78 | if ($contactTags) { |
| 79 | $form->add('select', 'contact_tags', ts('Select Tag(s)'), $contactTags, FALSE, |
| 80 | ['id' => 'contact_tags', 'multiple' => 'multiple', 'class' => 'crm-select2', 'style' => 'width: 100%;'] |
| 81 | ); |
| 82 | } |
| 83 | |
| 84 | $parentNames = CRM_Core_BAO_Tag::getTagSet('civicrm_contact'); |
| 85 | CRM_Core_Form_Tag::buildQuickForm($form, $parentNames, 'civicrm_contact', NULL, TRUE, FALSE); |
| 86 | |
| 87 | $used_for = CRM_Core_OptionGroup::values('tag_used_for'); |
| 88 | $tagsTypes = []; |
| 89 | $showAllTagTypes = FALSE; |
| 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 |
| 97 | $tagsTypes[] = $value; |
| 98 | $showAllTagTypes = TRUE; |
| 99 | } |
| 100 | } |
| 101 | $tagTypesText = implode(" or ", $tagsTypes); |
| 102 | if ($showAllTagTypes) { |
| 103 | $form->add('checkbox', 'all_tag_types', ts('Include tags used for %1', [1 => $tagTypesText])); |
| 104 | $form->add('hidden', 'tag_types_text', $tagTypesText); |
| 105 | } |
| 106 | } |
| 107 | |
| 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 | |
| 114 | //added internal ID |
| 115 | $form->add('number', 'contact_id', ts('Contact ID'), CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'id') + ['min' => 1]); |
| 116 | $form->addRule('contact_id', ts('Please enter valid Contact ID'), 'positiveInteger'); |
| 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 | |
| 121 | if (CRM_Core_Permission::check('access deleted contacts') and Civi::settings()->get('contact_undelete')) { |
| 122 | $form->add('checkbox', 'deleted_contacts', ts('Search in Trash') . '<br />' . ts('(deleted contacts)')); |
| 123 | } |
| 124 | |
| 125 | // add checkbox for cms users only |
| 126 | $form->addYesNo('uf_user', ts('CMS User?'), TRUE); |
| 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. |
| 135 | $types = ['Participant', 'Contribution', 'Membership']; |
| 136 | |
| 137 | // get component profiles |
| 138 | $componentProfiles = []; |
| 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 | |
| 144 | $searchProfiles = []; |
| 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 | |
| 151 | $form->add('select', |
| 152 | 'uf_group_id', |
| 153 | ts('Views For Display Contacts'), |
| 154 | [ |
| 155 | '0' => ts('- default view -'), |
| 156 | ] + $searchProfiles, |
| 157 | FALSE, |
| 158 | ['class' => 'crm-select2'] |
| 159 | ); |
| 160 | |
| 161 | $componentModes = CRM_Contact_Form_Search::getModeSelect(); |
| 162 | $form->assign('component_mappings', json_encode(CRM_Contact_Form_Search::getModeToComponentMapping())); |
| 163 | if (count($componentModes) > 1) { |
| 164 | $form->add('select', |
| 165 | 'component_mode', |
| 166 | ts('Display Results As'), |
| 167 | $componentModes, |
| 168 | FALSE, |
| 169 | ['class' => 'crm-select2'] |
| 170 | ); |
| 171 | } |
| 172 | |
| 173 | $form->addRadio( |
| 174 | 'operator', |
| 175 | ts('Search Operator'), |
| 176 | [ |
| 177 | CRM_Contact_BAO_Query::SEARCH_OPERATOR_AND => ts('AND'), |
| 178 | CRM_Contact_BAO_Query::SEARCH_OPERATOR_OR => ts('OR'), |
| 179 | ], |
| 180 | ['allowClear' => FALSE] |
| 181 | ); |
| 182 | |
| 183 | // add the option to display relationships |
| 184 | $rTypes = CRM_Core_PseudoConstant::relationshipType(); |
| 185 | $rSelect = ['' => ts('- Select Relationship Type-')]; |
| 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'), |
| 199 | $rSelect, |
| 200 | ['class' => 'crm-select2'] |
| 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, |
| 211 | [ |
| 212 | 'id' => 'privacy_options', |
| 213 | 'multiple' => 'multiple', |
| 214 | 'class' => 'crm-select2', |
| 215 | ] |
| 216 | ); |
| 217 | |
| 218 | $form->addElement('select', |
| 219 | 'privacy_operator', |
| 220 | ts('Operator'), |
| 221 | [ |
| 222 | 'OR' => ts('OR'), |
| 223 | 'AND' => ts('AND'), |
| 224 | ] |
| 225 | ); |
| 226 | |
| 227 | $options = [ |
| 228 | 1 => ts('Exclude'), |
| 229 | 2 => ts('Include by Privacy Option(s)'), |
| 230 | ]; |
| 231 | $form->addRadio('privacy_toggle', ts('Privacy Options'), $options, ['allowClear' => FALSE]); |
| 232 | |
| 233 | // preferred communication method |
| 234 | if (Civi::settings()->get('civimail_multiple_bulk_emails')) { |
| 235 | $form->addSelect('email_on_hold', |
| 236 | ['entity' => 'email', 'multiple' => 'multiple', 'label' => ts('Email On Hold'), 'options' => CRM_Core_PseudoConstant::emailOnHoldOptions()]); |
| 237 | } |
| 238 | else { |
| 239 | $form->add('advcheckbox', 'email_on_hold', ts('Email On Hold')); |
| 240 | } |
| 241 | |
| 242 | $form->addSelect('preferred_communication_method', |
| 243 | ['entity' => 'contact', 'multiple' => 'multiple', 'label' => ts('Preferred Communication Method'), 'option_url' => NULL, 'placeholder' => ts('- any -')]); |
| 244 | |
| 245 | //CRM-6138 Preferred Language |
| 246 | $form->addSelect('preferred_language', ['class' => 'twenty', 'context' => 'search']); |
| 247 | |
| 248 | // Phone search |
| 249 | $form->addElement('text', 'phone_numeric', ts('Phone'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_Phone', 'phone')); |
| 250 | $locationType = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id'); |
| 251 | $phoneType = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id'); |
| 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']); |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Get the metadata for fields to be included on the contact search form. |
| 258 | * |
| 259 | * @throws \CiviCRM_API3_Exception |
| 260 | */ |
| 261 | public static function getSearchFieldMetadata() { |
| 262 | $fields = [ |
| 263 | 'sort_name' => ['title' => ts('Complete OR Partial Name'), 'template_grouping' => 'basic'], |
| 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'], |
| 266 | 'created_date' => ['name' => 'created_date', 'template_grouping' => 'changeLog'], |
| 267 | 'modified_date' => ['name' => 'modified_date', 'template_grouping' => 'changeLog'], |
| 268 | 'birth_date' => ['name' => 'birth_date', 'template_grouping' => 'demographic'], |
| 269 | 'deceased_date' => ['name' => 'deceased_date', 'template_grouping' => 'demographic'], |
| 270 | 'is_deceased' => ['is_deceased', 'template_grouping' => 'demographic'], |
| 271 | 'relationship_start_date' => ['name' => 'relationship_start_date', 'template_grouping' => 'relationship'], |
| 272 | 'relationship_end_date' => ['name' => 'relationship_end_date', 'template_grouping' => 'relationship'], |
| 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 | ], |
| 284 | ]; |
| 285 | |
| 286 | $metadata = civicrm_api3('Relationship', 'getfields', [])['values']; |
| 287 | $metadata = array_merge($metadata, civicrm_api3('Contact', 'getfields', [])['values']); |
| 288 | foreach ($fields as $fieldName => $field) { |
| 289 | $fields[$fieldName] = array_merge(CRM_Utils_Array::value($fieldName, $metadata, []), $field); |
| 290 | } |
| 291 | return $fields; |
| 292 | } |
| 293 | |
| 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 | |
| 312 | /** |
| 313 | * Defines the fields that can be displayed for the basic search section. |
| 314 | * |
| 315 | * @param CRM_Core_Form $form |
| 316 | * |
| 317 | * @throws \CiviCRM_API3_Exception |
| 318 | */ |
| 319 | protected static function setBasicSearchFields($form) { |
| 320 | $searchFields = []; |
| 321 | foreach (self::getFilteredSearchFieldMetadata('basic') as $fieldName => $field) { |
| 322 | $searchFields[$fieldName] = $field; |
| 323 | } |
| 324 | $form->assign('basicSearchFields', array_merge(self::getBasicSearchFields(), $searchFields)); |
| 325 | } |
| 326 | |
| 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 [ |
| 334 | // For now an empty array is still left in place for ordering. |
| 335 | 'sort_name' => [], |
| 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', |
| 356 | 'help' => ['id' => 'id-all-tag-types'], |
| 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 | ], |
| 391 | ]; |
| 392 | } |
| 393 | |
| 394 | /** |
| 395 | * @param CRM_Core_Form $form |
| 396 | */ |
| 397 | public static function location(&$form) { |
| 398 | $config = CRM_Core_Config::singleton(); |
| 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 | |
| 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 | ]; |
| 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, |
| 437 | ['street_number', 'street_name', 'street_unit'] |
| 438 | )) { |
| 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) { |
| 452 | if ($select == 'stateProvince' || $select == 'county') { |
| 453 | $element = $form->addChainSelect($name); |
| 454 | } |
| 455 | else { |
| 456 | $selectElements = ['' => ts('- any -')] + CRM_Core_PseudoConstant::$select(); |
| 457 | $element = $form->add('select', $name, $title, $selectElements, FALSE, ['class' => 'crm-select2']); |
| 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']) { |
| 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')]); |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | // extend addresses with proximity search |
| 475 | if (CRM_Utils_GeocodeProvider::getUsableClassName()) { |
| 476 | $form->addElement('text', 'prox_distance', ts('Find contacts within'), ['class' => 'six']); |
| 477 | $form->addElement('select', 'prox_distance_unit', NULL, [ |
| 478 | 'miles' => ts('Miles'), |
| 479 | 'kilos' => ts('Kilometers'), |
| 480 | ]); |
| 481 | $form->addRule('prox_distance', ts('Please enter positive number as a distance'), 'numeric'); |
| 482 | } |
| 483 | |
| 484 | $form->addSelect('world_region', ['entity' => 'address', 'context' => 'search']); |
| 485 | |
| 486 | // select for location type |
| 487 | $locationType = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id'); |
| 488 | $form->add('select', 'location_type', ts('Address Location'), $locationType, FALSE, [ |
| 489 | 'multiple' => TRUE, |
| 490 | 'class' => 'crm-select2', |
| 491 | 'placeholder' => ts('Primary'), |
| 492 | ]); |
| 493 | |
| 494 | // custom data extending addresses |
| 495 | CRM_Core_BAO_Query::addCustomFormFields($form, ['Address']); |
| 496 | } |
| 497 | |
| 498 | /** |
| 499 | * @param CRM_Core_Form $form |
| 500 | */ |
| 501 | public static function activity(&$form) { |
| 502 | $form->add('hidden', 'hidden_activity', 1); |
| 503 | CRM_Activity_BAO_Query::buildSearchForm($form); |
| 504 | } |
| 505 | |
| 506 | /** |
| 507 | * @param CRM_Core_Form $form |
| 508 | * |
| 509 | * @throws \CiviCRM_API3_Exception |
| 510 | */ |
| 511 | public static function changeLog(&$form) { |
| 512 | $form->add('hidden', 'hidden_changeLog', 1); |
| 513 | $form->addSearchFieldMetadata(['Contact' => self::getFilteredSearchFieldMetadata('changeLog')]); |
| 514 | $form->addFormFieldsFromMetadata(); |
| 515 | // block for change log |
| 516 | $form->addElement('text', 'changed_by', ts('Modified By'), NULL); |
| 517 | } |
| 518 | |
| 519 | /** |
| 520 | * @param CRM_Core_Form $form |
| 521 | */ |
| 522 | public static function task(&$form) { |
| 523 | $form->add('hidden', 'hidden_task', 1); |
| 524 | } |
| 525 | |
| 526 | /** |
| 527 | * @param CRM_Core_Form_Search $form |
| 528 | * |
| 529 | * @throws \CiviCRM_API3_Exception |
| 530 | */ |
| 531 | public static function relationship(&$form) { |
| 532 | $form->add('hidden', 'hidden_relationship', 1); |
| 533 | $form->addSearchFieldMetadata(['Relationship' => self::getFilteredSearchFieldMetadata('relationship')]); |
| 534 | $form->addFormFieldsFromMetadata(); |
| 535 | $form->add('text', 'relation_description', ts('Description'), ['class' => 'twenty']); |
| 536 | $allRelationshipType = CRM_Contact_BAO_Relationship::getContactRelationshipType(NULL, NULL, NULL, NULL, TRUE); |
| 537 | $form->add('select', 'relation_type_id', ts('Relationship Type'), ['' => ts('- select -')] + $allRelationshipType, FALSE, ['multiple' => TRUE, 'class' => 'crm-select2']); |
| 538 | $form->addElement('text', 'relation_target_name', ts('Target Contact'), CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'sort_name')); |
| 539 | // relation status |
| 540 | $relStatusOption = [ts('Active'), ts('Inactive'), ts('All')]; |
| 541 | $form->addRadio('relation_status', ts('Relationship Status'), $relStatusOption); |
| 542 | $form->setDefaults(['relation_status' => 0]); |
| 543 | // relation permission |
| 544 | $allRelationshipPermissions = CRM_Contact_BAO_Relationship::buildOptions('is_permission_a_b'); |
| 545 | $form->add('select', 'relation_permission', ts('Permissioned Relationship'), |
| 546 | ['' => ts('- select -')] + $allRelationshipPermissions, FALSE, ['multiple' => TRUE, 'class' => 'crm-select2']); |
| 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, |
| 551 | ['id' => 'relation_target_group', 'multiple' => 'multiple', 'class' => 'crm-select2'] |
| 552 | ); |
| 553 | } |
| 554 | |
| 555 | // add all the custom searchable fields |
| 556 | CRM_Core_BAO_Query::addCustomFormFields($form, ['Relationship']); |
| 557 | } |
| 558 | |
| 559 | /** |
| 560 | * @param CRM_Core_Form_Search $form |
| 561 | * |
| 562 | * @throws \CiviCRM_API3_Exception |
| 563 | */ |
| 564 | public static function demographics(&$form) { |
| 565 | $form->add('hidden', 'hidden_demographics', 1); |
| 566 | $form->addSearchFieldMetadata(['Contact' => self::getFilteredSearchFieldMetadata('demographic')]); |
| 567 | $form->addFormFieldsFromMetadata(); |
| 568 | // radio button for gender |
| 569 | $genderOptions = []; |
| 570 | $gender = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id'); |
| 571 | foreach ($gender as $key => $var) { |
| 572 | $genderOptions[$key] = $form->createElement('radio', NULL, |
| 573 | ts('Gender'), $var, $key, |
| 574 | ['id' => "civicrm_gender_{$var}_{$key}"] |
| 575 | ); |
| 576 | } |
| 577 | $form->addGroup($genderOptions, 'gender_id', ts('Gender'))->setAttribute('allowClear', TRUE); |
| 578 | |
| 579 | $form->add('number', 'age_low', ts('Min Age'), ['class' => 'four', 'min' => 0]); |
| 580 | $form->addRule('age_low', ts('Please enter a positive integer'), 'positiveInteger'); |
| 581 | $form->add('number', 'age_high', ts('Max Age'), ['class' => 'four', 'min' => 0]); |
| 582 | $form->addRule('age_high', ts('Please enter a positive integer'), 'positiveInteger'); |
| 583 | $form->add('datepicker', 'age_asof_date', ts('As of'), NULL, FALSE, ['time' => FALSE]); |
| 584 | } |
| 585 | |
| 586 | /** |
| 587 | * @param $form |
| 588 | */ |
| 589 | public static function notes(&$form) { |
| 590 | $form->add('hidden', 'hidden_notes', 1); |
| 591 | |
| 592 | $options = [ |
| 593 | 2 => ts('Body Only'), |
| 594 | 3 => ts('Subject Only'), |
| 595 | 6 => ts('Both'), |
| 596 | ]; |
| 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 | |
| 601 | $form->setDefaults(['note_option' => 6]); |
| 602 | } |
| 603 | |
| 604 | /** |
| 605 | * Generate the custom Data Fields based for those with is_searchable = 1. |
| 606 | * |
| 607 | * @param CRM_Contact_Form_Search $form |
| 608 | * |
| 609 | * @throws \CiviCRM_API3_Exception |
| 610 | */ |
| 611 | public static function custom(&$form) { |
| 612 | $form->add('hidden', 'hidden_custom', 1); |
| 613 | $extends = array_merge(['Contact', 'Individual', 'Household', 'Organization'], |
| 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 | |
| 626 | foreach ($group['fields'] as $field) { |
| 627 | $fieldId = $field['id']; |
| 628 | $elementName = 'custom_' . $fieldId; |
| 629 | if ($field['data_type'] === 'Date' && $field['is_search_range']) { |
| 630 | $form->addDatePickerRange($elementName, $field['label']); |
| 631 | } |
| 632 | else { |
| 633 | CRM_Core_BAO_CustomField::addQuickFormElement($form, $elementName, $fieldId, FALSE, TRUE); |
| 634 | } |
| 635 | } |
| 636 | } |
| 637 | } |
| 638 | |
| 639 | /** |
| 640 | * @param $form |
| 641 | */ |
| 642 | public static function CiviCase(&$form) { |
| 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 | } |
| 647 | |
| 648 | } |