Skip to content

Commit

Permalink
dev/core#1735 Advanced Search et al: ignore the is_searchable as a cr…
Browse files Browse the repository at this point in the history
…iteria for displaying a custom field

Also provides an alternative getCustomGroupDetail() function without the $searchable
argument, and removes the pass-by-ref on $extends, in an attempt to de-uglify some
other code. Also replaces some calls by getAll().
  • Loading branch information
mlutfy committed May 28, 2024
1 parent 14a2478 commit 9d46d48
Show file tree
Hide file tree
Showing 11 changed files with 26 additions and 34 deletions.
3 changes: 1 addition & 2 deletions CRM/Case/XMLProcessor/Report.php
Original file line number Diff line number Diff line change
Expand Up @@ -959,8 +959,7 @@ public static function printCaseReport() {

// Retrieve custom values for cases.
$customValues = CRM_Core_BAO_CustomValueTable::getEntityValues($caseID, 'Case');
$extends = ['Case'];
$groupTree = CRM_Core_BAO_CustomGroup::getGroupDetail(NULL, NULL, $extends);
$groupTree = CRM_Core_BAO_CustomGroup::getAll(['extends' => ['Case']]);
$caseCustomFields = [];
foreach ($groupTree as $gid => $group_values) {
foreach ($group_values['fields'] as $id => $field_values) {
Expand Down
4 changes: 1 addition & 3 deletions CRM/Contact/Form/Search/Advanced.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,7 @@ public function buildQuickForm() {
CRM_Contact_BAO_ContactType::basicTypes(),
CRM_Contact_BAO_ContactType::subTypes()
);
$groupDetails = CRM_Core_BAO_CustomGroup::getGroupDetail(NULL, TRUE,
$extends
);
$groupDetails = CRM_Core_BAO_CustomGroup::getAll(['extends' => $extends]);
// if no searchable fields unset panel
if (empty($groupDetails)) {
unset($paneNames[ts('Custom Fields')]);
Expand Down
5 changes: 1 addition & 4 deletions CRM/Contact/Form/Search/Criteria.php
Original file line number Diff line number Diff line change
Expand Up @@ -607,10 +607,7 @@ public static function custom(&$form) {
CRM_Contact_BAO_ContactType::basicTypes(),
CRM_Contact_BAO_ContactType::subTypes()
);
$groupDetails = CRM_Core_BAO_CustomGroup::getGroupDetail(NULL, TRUE,
$extends
);

$groupDetails = CRM_Core_BAO_CustomGroup::getAll(['extends' => $extends]);
$form->assign('groupTree', $groupDetails);

foreach ($groupDetails as $key => $group) {
Expand Down
3 changes: 1 addition & 2 deletions CRM/Contribute/BAO/ContributionRecur.php
Original file line number Diff line number Diff line change
Expand Up @@ -648,8 +648,7 @@ public static function copyCustomValues($recurId, $targetContributionId) {
}

// copy custom data
$extends = ['Contribution'];
$groupTree = CRM_Core_BAO_CustomGroup::getGroupDetail(NULL, NULL, $extends);
$groupTree = CRM_Core_BAO_CustomGroup::getAll(['extends' => ['Contribution']]);
if ($groupTree) {
foreach ($groupTree as $groupID => $group) {
$table[$groupTree[$groupID]['table_name']] = ['entity_id'];
Expand Down
19 changes: 13 additions & 6 deletions CRM/Core/BAO/CustomGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -937,6 +937,18 @@ public static function getTitle($id) {
return self::getAll()[$id]['title'] ?? NULL;
}

/**
* Get custom group details for a group. Legacy function for backwards compatibility.
* @deprecated Legacy function
*
* @see CRM_Core_BAO_CustomGroup::getAll()
* for a better alternative.
*/
public static function &getGroupDetail($groupId = NULL, $searchable = FALSE, &$extends = NULL, $inSelector = NULL) {
CRM_Core_Error::deprecatedFunctionWarning('getCustomGroupDetail');
return self::getCustomGroupDetail($groupId, $extends, $inSelector);
}

/**
* @deprecated Legacy function
*
Expand All @@ -945,26 +957,21 @@ public static function getTitle($id) {
*
* @param int $groupId
* Group id whose details are needed.
* @param bool $searchable
* Is this field searchable.
* @param array $extends
* Which table does it extend if any.
* @param bool $inSelector
*
* @return array
* array consisting of all group and field details
*/
public static function &getGroupDetail($groupId = NULL, $searchable = NULL, &$extends = NULL, $inSelector = NULL) {
public static function &getCustomGroupDetail($groupId = NULL, $extends = NULL, $inSelector = NULL) {
$groupFilters = [
'is_active' => TRUE,
];
$fieldFilters = [];
if ($groupId) {
$groupFilters['id'] = $groupId;
}
if ($searchable) {
$fieldFilters['is_searchable'] = TRUE;
}
if ($inSelector) {
$groupFilters['is_multiple'] = TRUE;
$fieldFilters['in_selector'] = TRUE;
Expand Down
2 changes: 1 addition & 1 deletion CRM/Core/BAO/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class CRM_Core_BAO_Query {
* @param array $extends
*/
public static function addCustomFormFields(&$form, $extends) {
$groupDetails = CRM_Core_BAO_CustomGroup::getGroupDetail(NULL, TRUE, $extends);
$groupDetails = CRM_Core_BAO_CustomGroup::getAll(['extends' => $extends]);
if ($groupDetails) {
foreach ($groupDetails as $group) {
if (empty($group['fields'])) {
Expand Down
2 changes: 1 addition & 1 deletion CRM/Custom/Form/Preview.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function preProcess() {
// Group preview
else {
$this->_groupId = CRM_Utils_Request::retrieve('gid', 'Positive', $this, TRUE);
$groupTree = CRM_Core_BAO_CustomGroup::getGroupDetail($this->_groupId);
$groupTree = CRM_Core_BAO_CustomGroup::getCustomGroupDetail($this->_groupId);
$this->_groupTree = CRM_Core_BAO_CustomGroup::formatGroupTree($groupTree, TRUE, $this);
$this->assign('preview_type', 'group');
}
Expand Down
4 changes: 1 addition & 3 deletions CRM/Event/BAO/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,7 @@ public static function del($id) {
*/
public static function self_hook_civicrm_pre(\Civi\Core\Event\PreEvent $event) {
if ($event->action === 'delete' && $event->id) {

$extends = ['Event'];
$groupTree = CRM_Core_BAO_CustomGroup::getGroupDetail(NULL, NULL, $extends);
$groupTree = CRM_Core_BAO_CustomGroup::getAll(['extends' => ['Event']]);
// @todo is this custom field loop necessary? The cascade delete on the
// db foreign key should do it already.
foreach ($groupTree as $values) {
Expand Down
2 changes: 1 addition & 1 deletion CRM/Profile/Page/MultipleRecordFieldsListing.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ public function browse(): array {
}
$this->assign('reachedMax', $reached);
// custom group info : this consists of the field title of group fields
$groupDetail = CRM_Core_BAO_CustomGroup::getGroupDetail($customGroupId, NULL, CRM_Core_DAO::$_nullObject, TRUE);
$groupDetail = CRM_Core_BAO_CustomGroup::getCustomGroupDetail($customGroupId, NULL, TRUE);
// field ids of fields in_selector for the custom group id provided
$fieldIDs = array_keys($groupDetail[$customGroupId]['fields']);
// field labels for headers
Expand Down
2 changes: 1 addition & 1 deletion settings/Search.setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@
'title' => ts('Quicksearch options'),
'is_domain' => '1',
'is_contact' => 0,
'description' => ts("Which fields can be searched on in the menubar quicksearch box? Don't see your custom field here? Make sure it is marked as Searchable."),
'description' => ts("Which fields can be searched on in the menubar quicksearch box?"),
'help_text' => NULL,
'settings_pages' => ['search' => ['weight' => 90]],
],
Expand Down
14 changes: 4 additions & 10 deletions tests/phpunit/CRM/Core/BAO/CustomGroupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,9 @@ public function testRetrieve(): void {
}

/**
* Test getGroupDetail().
* Test getCustomGroupDetail().
*/
public function testGetGroupDetail(): void {
public function testGetCustomGroupDetail(): void {
$customGroupTitle = 'My Custom Group';
$groupParams = [
'title' => $customGroupTitle,
Expand Down Expand Up @@ -291,23 +291,17 @@ public function testGetGroupDetail(): void {
$customField2 = $this->customFieldCreate($field2Params + ['custom_group_id' => $customGroupId]);
$field2Id = $customField2['id'];

$emptyTree = CRM_Core_BAO_CustomGroup::getGroupDetail(99);
$emptyTree = CRM_Core_BAO_CustomGroup::getCustomGroupDetail(99);
$this->assertCount(0, $emptyTree, 'Check that no custom Group matches id=99');

$groupTree = CRM_Core_BAO_CustomGroup::getGroupDetail($customGroupId);
$groupTree = CRM_Core_BAO_CustomGroup::getCustomGroupDetail($customGroupId);
$this->assertCount(1, $groupTree);
$this->assertCount(2, $groupTree[$customGroupId]['fields']);
//check values of custom group
$this->assertAttributesEquals($groupParams, $groupTree[$customGroupId]);
//check values of custom fields
$this->assertAttributesEquals($field1Params, $groupTree[$customGroupId]['fields'][$field1Id]);
$this->assertAttributesEquals($field2Params, $groupTree[$customGroupId]['fields'][$field2Id]);

$searchableTree = CRM_Core_BAO_CustomGroup::getGroupDetail($customGroupId, TRUE);
$this->assertCount(1, $searchableTree[$customGroupId]['fields']);
$this->assertAttributesEquals($groupParams, $searchableTree[$customGroupId]);
// only searchable field should be returned
$this->assertAttributesEquals($field2Params, $searchableTree[$customGroupId]['fields'][$field2Id]);
}

/**
Expand Down

0 comments on commit 9d46d48

Please sign in to comment.