From d7570a7fc5436e40dc3b1c36e95260d734f333b1 Mon Sep 17 00:00:00 2001 From: eileen Date: Thu, 2 May 2019 18:25:31 +1200 Subject: [PATCH] [REF] premlinary tidy up of code as tested - ready for actual fix --- CRM/Contact/BAO/Query.php | 61 +++++++++++++++------ tests/phpunit/CRM/Contact/BAO/QueryTest.php | 2 +- 2 files changed, 44 insertions(+), 19 deletions(-) diff --git a/CRM/Contact/BAO/Query.php b/CRM/Contact/BAO/Query.php index e5f9b4c604..b7f353ad7f 100644 --- a/CRM/Contact/BAO/Query.php +++ b/CRM/Contact/BAO/Query.php @@ -3002,25 +3002,13 @@ class CRM_Contact_BAO_Query { $regularGroupIDs[] = trim($id); } } + $hasNonSmartGroups = count($regularGroupIDs); $isNotOp = ($op == 'NOT IN' || $op == '!='); - $statii = []; - $gcsValues = $this->getWhereValues('group_contact_status', $grouping); - if ($gcsValues && - is_array($gcsValues[2]) - ) { - foreach ($gcsValues[2] as $k => $v) { - if ($v) { - $statii[] = "'" . CRM_Utils_Type::escape($k, 'String') . "'"; - } - } - } - else { - $statii[] = "'Added'"; - } + $statusJoinClause = $this->getGroupStatusClause($grouping); $groupClause = []; - if (count($regularGroupIDs) || empty($value)) { + if ($hasNonSmartGroups || empty($value)) { // include child groups IDs if any $childGroupIds = (array) CRM_Contact_BAO_Group::getChildGroupIds($regularGroupIDs); foreach ($childGroupIds as $key => $id) { @@ -3063,8 +3051,8 @@ class CRM_Contact_BAO_Query { } $groupClause[] = "( {$clause} )"; - if ($statii) { - $joinClause[] = "{$gcTable}.status IN (" . implode(', ', $statii) . ")"; + if ($statusJoinClause) { + $joinClause[] = "{$gcTable}.$statusJoinClause"; } $this->_tables[$gcTable] = $this->_whereTables[$gcTable] = " LEFT JOIN civicrm_group_contact {$gcTable} ON (" . implode(' AND ', $joinClause) . ")"; } @@ -3101,7 +3089,7 @@ class CRM_Contact_BAO_Query { list($qillop, $qillVal) = CRM_Contact_BAO_Query::buildQillForFieldValue('CRM_Contact_DAO_Group', 'id', $value, $op); $this->_qill[$grouping][] = ts("Group(s) %1 %2", [1 => $qillop, 2 => $qillVal]); if (strpos($op, 'NULL') === FALSE) { - $this->_qill[$grouping][] = ts("Group Status %1", [1 => implode(' ' . ts('or') . ' ', $statii)]); + $this->_qill[$grouping][] = ts("Group Status %1", [1 => implode(' ' . ts('or') . ' ', $this->getSelectedGroupStatuses($grouping))]); } } @@ -6974,4 +6962,41 @@ AND displayRelType.is_active = 1 ]; } + /** + * Get the clause for group status. + * + * @param int $grouping + * + * @return string + */ + protected function getGroupStatusClause($grouping) { + $statuses = $this->getSelectedGroupStatuses($grouping); + return "status IN (" . implode(', ', $statuses) . ")"; + } + + /** + * Get an array of the statuses that have been selected. + * + * @param string $grouping + * + * @return array + */ + protected function getSelectedGroupStatuses($grouping) { + $statuses = []; + $gcsValues = $this->getWhereValues('group_contact_status', $grouping); + if ($gcsValues && + is_array($gcsValues[2]) + ) { + foreach ($gcsValues[2] as $k => $v) { + if ($v) { + $statuses[] = "'" . CRM_Utils_Type::escape($k, 'String') . "'"; + } + } + } + else { + $statuses[] = "'Added'"; + } + return $statuses; + } + } diff --git a/tests/phpunit/CRM/Contact/BAO/QueryTest.php b/tests/phpunit/CRM/Contact/BAO/QueryTest.php index 632e389793..d3d42b9484 100644 --- a/tests/phpunit/CRM/Contact/BAO/QueryTest.php +++ b/tests/phpunit/CRM/Contact/BAO/QueryTest.php @@ -629,7 +629,7 @@ civicrm_relationship.is_active = 1 AND */ public function testGetByGroupWithStatus() { $groupID = $this->groupCreate(); - $this->groupContactCreate($groupID , 3); + $this->groupContactCreate($groupID, 3); $groupContactID = $this->callAPISuccessGetSingle('GroupContact', ['group_id' => $groupID, 'options' => ['limit' => 1]])['id']; $this->callAPISuccess('GroupContact', 'create', ['id' => $groupContactID, 'status' => 'Removed']); $queryObj = new CRM_Contact_BAO_Query([['group', '=', $groupID, 0, 0], ['group_contact_status', 'IN', ['Removed' => 1], 0, 0]]); -- 2.25.1