Compare commits

..

No commits in common. "8ef7bea74cd016a90c8172ca93efead1a4073c85" and "425d0431c5482781cbacfe51ed3c46a5424cca8d" have entirely different histories.

View File

@ -59,39 +59,21 @@ class CRM_ConstituentsOnly_BAO_ConstituentsOnly {
*
* @return string
*/
public static function getConstituentSql($contactIds, $returnArray = FALSE) {
$sql[] = "
SELECT MAX(contact_a.id) AS contact_id, cc.id AS og_contact_id, MAX(cc.sort_name) AS og_sort_name
FROM civicrm_contact contact_a
INNER JOIN civicrm_relationship cr
ON (cr.contact_id_a = contact_a.id)
AND contact_a.is_deleted = 0
AND (contact_a.do_not_trade = 0 OR contact_a.do_not_trade IS NULL)
AND cr.is_active = 1
INNER JOIN civicrm_contact cc
ON (cr.contact_id_b = cc.id)
AND cc.id IN (" . implode(',', $contactIds) . ")
AND cc.do_not_trade = 1
GROUP BY cc.id";
$sql[] = "
SELECT MAX(contact_a.id) AS contact_id, cc.id AS og_contact_id, MAX(cc.sort_name) AS og_sort_name
FROM civicrm_contact contact_a
INNER JOIN civicrm_relationship cr
ON (cr.contact_id_b = contact_a.id)
AND contact_a.is_deleted = 0
AND (contact_a.do_not_trade = 0 OR contact_a.do_not_trade IS NULL)
AND cr.is_active = 1
INNER JOIN civicrm_contact cc
ON (cr.contact_id_a = cc.id)
AND cc.id IN (" . implode(',', $contactIds) . ")
AND cc.do_not_trade = 1
GROUP BY cc.id";
if ($returnArray) {
return $sql;
}
public static function getConstituentSql($contactIds) {
return "
SELECT contact_id, MAX(og_contact_id) AS og_contact_id, MAX(og_sort_name) AS og_sort_name
FROM (" . implode(' UNION ', $sql) . ") AS temp GROUP BY contact_id
SELECT MAX(contact_a.id) AS contact_id, cc.id AS og_contact_id
FROM civicrm_contact contact_a
INNER JOIN civicrm_relationship cr
ON (cr.contact_id_a = contact_a.id OR cr.contact_id_b = contact_a.id)
AND contact_a.is_deleted = 0
AND (contact_a.do_not_trade = 0 OR contact_a.do_not_trade IS NULL)
AND cr.is_active = 1
INNER JOIN civicrm_contact cc
ON (cr.contact_id_a = cc.id OR cr.contact_id_b = cc.id)
AND contact_a.id <> cc.id
AND cc.id IN (" . implode(',', $contactIds) . ")
AND cc.do_not_trade = 1
GROUP BY cc.id
";
}
@ -107,15 +89,24 @@ class CRM_ConstituentsOnly_BAO_ConstituentsOnly {
$sqls[] = "CREATE TEMPORARY TABLE quick_temp_table_1
{$joinQuery}
";
$queries = self::getConstituentSql(["SELECT id FROM quick_temp_table_1"], TRUE);
$sqls[] = "
CREATE TEMPORARY TABLE quick_temp_table_2
{$queries[0]}
";
$sqls[] = "
INSERT INTO quick_temp_table_2
{$queries[1]}
SELECT MAX(contact_a.id) AS contact_id, cc.id AS og_contact_id, MAX(cc.sort_name) AS og_sort_name
FROM civicrm_contact contact_a
INNER JOIN civicrm_relationship cr
ON (cr.contact_id_a = contact_a.id OR cr.contact_id_b = contact_a.id)
AND contact_a.is_deleted = 0
AND (contact_a.do_not_trade = 0 OR contact_a.do_not_trade IS NULL)
AND cr.is_active = 1
INNER JOIN civicrm_contact cc
ON (cr.contact_id_a = cc.id OR cr.contact_id_b = cc.id)
AND contact_a.id <> cc.id
AND cc.id IN (SELECT id FROM quick_temp_table_1)
AND cc.do_not_trade = 1
GROUP BY cc.id
";
foreach ($sqls as $sql) {
CRM_Core_DAO::executeQuery($sql);
}
@ -156,6 +147,7 @@ class CRM_ConstituentsOnly_BAO_ConstituentsOnly {
public static function updateSearchRows($rows, $headers) {
$contactIds = array_keys($rows);
$sql = self::getConstituentSql($contactIds);
$result = CRM_Core_DAO::executeQuery($sql);
$relContactIds = [];