From 9c9908e9c241d322e94871fe80509270ab417022 Mon Sep 17 00:00:00 2001 From: highfalutin Date: Sat, 17 Nov 2018 09:48:12 +1300 Subject: [PATCH] Minor Contact BAO code cleanup --- CRM/Contact/BAO/Query.php | 18 ++++++++++-------- tests/phpunit/CRM/Contact/BAO/QueryTest.php | 6 ++++++ 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/CRM/Contact/BAO/Query.php b/CRM/Contact/BAO/Query.php index 1572a81e6b..eb4ab50d6a 100644 --- a/CRM/Contact/BAO/Query.php +++ b/CRM/Contact/BAO/Query.php @@ -4206,6 +4206,13 @@ civicrm_relationship.start_date > {$today} } } + /** + * Add relationship permission criteria to where clause. + * + * @param string $grouping + * @param array $where Array to add "where" criteria to, in case you are generating a temp table. + * Not the main query. + */ public function addRelationshipPermissionClauses($grouping, &$where) { $relPermission = $this->getWhereValues('relation_permission', $grouping); if ($relPermission) { @@ -4216,14 +4223,9 @@ civicrm_relationship.start_date > {$today} $where[$grouping][] = "(civicrm_relationship.is_permission_a_b IN (" . implode(",", $relPermission[2]) . "))"; $allRelationshipPermissions = CRM_Contact_BAO_Relationship::buildOptions('is_permission_a_b'); - $relQill = ''; - foreach ($relPermission[2] as $rel) { - if (!empty($relQill)) { - $relQill .= ' OR '; - } - $relQill .= ts($allRelationshipPermissions[$rel]); - } - $this->_qill[$grouping][] = ts('Permissioned Relationships') . ' - ' . $relQill; + + $relPermNames = array_intersect_key($allRelationshipPermissions, array_flip($relPermission[2])); + $this->_qill[$grouping][] = ts('Permissioned Relationships') . ' - ' . implode(' OR ', $relPermNames); } } diff --git a/tests/phpunit/CRM/Contact/BAO/QueryTest.php b/tests/phpunit/CRM/Contact/BAO/QueryTest.php index 755233f9b6..64f1140a7f 100644 --- a/tests/phpunit/CRM/Contact/BAO/QueryTest.php +++ b/tests/phpunit/CRM/Contact/BAO/QueryTest.php @@ -510,6 +510,12 @@ class CRM_Contact_BAO_QueryTest extends CiviUnitTestCase { $this->assertContains('INNER JOIN civicrm_rel_temp_', $sql, "Query appears to use temporary table of compiled relationships?", TRUE); } + public function testRelationshipPermissionClause() { + $params = [['relation_type_id', 'IN', ['1_b_a'], 0, 0], ['relation_permission', 'IN', [2], 0, 0]]; + $sql = CRM_Contact_BAO_Query::getQuery($params); + $this->assertContains('(civicrm_relationship.is_permission_a_b IN (2))', $sql); + } + /** * Test Relationship Clause */ -- 2.25.1