From e573074111c0c1650ca72eb94d07c153b0f8324e Mon Sep 17 00:00:00 2001 From: Civiware Solutions Date: Fri, 28 Sep 2018 22:46:03 +0100 Subject: [PATCH] fixed searcg query --- CRM/ConstituentsOnly/BAO/Query.php | 19 ++++++++++++++++++- constituentsonly.php | 2 +- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/CRM/ConstituentsOnly/BAO/Query.php b/CRM/ConstituentsOnly/BAO/Query.php index 8b99bce..560049f 100644 --- a/CRM/ConstituentsOnly/BAO/Query.php +++ b/CRM/ConstituentsOnly/BAO/Query.php @@ -45,7 +45,17 @@ class CRM_ConstituentsOnly_BAO_Query extends CRM_Contact_BAO_Query_Interface { } if (CRM_Utils_Array::value('civicrm_contact', $query->_tables) && empty($query->_paramLookup['do_not_trade'])) { - $query->_where[0][] = "( contact_a.do_not_trade IS NULL OR contact_a.do_not_trade = 0)"; + $query->_where[0][] = " + IF (contact_a.do_not_trade = 1, + CASE + WHEN (cr.id IS NULL OR cc1.is_deleted = 1 OR cc1.do_not_trade = 1) + THEN 0 + ELSE 1 + END, + 1) + "; + $query->_tables['civicrm_do_not_trade'] = 1; + $query->_whereTables['civicrm_do_not_trade'] = 1; } } @@ -56,6 +66,13 @@ class CRM_ConstituentsOnly_BAO_Query extends CRM_Contact_BAO_Query_Interface { * */ public function from($name, $mode, $side) { + if ($name == 'civicrm_do_not_trade') { + return " LEFT JOIN civicrm_relationship cr + ON (cr.contact_id_a = contact_a.id OR cr.contact_id_b = contact_a.id) + LEFT JOIN civicrm_contact cc1 + ON (cr.contact_id_a = cc1.id OR cr.contact_id_b = cc1.id) + AND contact_a.id <> cc1.id"; + } } /** diff --git a/constituentsonly.php b/constituentsonly.php index 6c34bfe..502b2f3 100644 --- a/constituentsonly.php +++ b/constituentsonly.php @@ -130,7 +130,7 @@ function constituentsonly_civicrm_alterSettingsFolders(&$metaDataFolders = NULL) */ function constituentsonly_civicrm_queryObjects(&$queryObjects, $type) { if ($type == 'Contact') { - //$queryObjects[] = new CRM_ConstituentsOnly_BAO_Query(); + $queryObjects[] = new CRM_ConstituentsOnly_BAO_Query(); } }