Used existing function to build query
This commit is contained in:
parent
56881b50a0
commit
8ef7bea74c
@ -59,10 +59,9 @@ class CRM_ConstituentsOnly_BAO_ConstituentsOnly {
|
|||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public static function getConstituentSql($contactIds) {
|
public static function getConstituentSql($contactIds, $returnArray = FALSE) {
|
||||||
return "
|
$sql[] = "
|
||||||
SELECT contact_id, MAX(og_contact_id) AS og_contact_id FROM (
|
SELECT MAX(contact_a.id) AS contact_id, cc.id AS og_contact_id, MAX(cc.sort_name) AS og_sort_name
|
||||||
SELECT MAX(contact_a.id) AS contact_id, cc.id AS og_contact_id
|
|
||||||
FROM civicrm_contact contact_a
|
FROM civicrm_contact contact_a
|
||||||
INNER JOIN civicrm_relationship cr
|
INNER JOIN civicrm_relationship cr
|
||||||
ON (cr.contact_id_a = contact_a.id)
|
ON (cr.contact_id_a = contact_a.id)
|
||||||
@ -71,14 +70,11 @@ class CRM_ConstituentsOnly_BAO_ConstituentsOnly {
|
|||||||
AND cr.is_active = 1
|
AND cr.is_active = 1
|
||||||
INNER JOIN civicrm_contact cc
|
INNER JOIN civicrm_contact cc
|
||||||
ON (cr.contact_id_b = cc.id)
|
ON (cr.contact_id_b = cc.id)
|
||||||
AND contact_a.id <> cc.id
|
|
||||||
AND cc.id IN (" . implode(',', $contactIds) . ")
|
AND cc.id IN (" . implode(',', $contactIds) . ")
|
||||||
AND cc.do_not_trade = 1
|
AND cc.do_not_trade = 1
|
||||||
GROUP BY cc.id
|
GROUP BY cc.id";
|
||||||
|
$sql[] = "
|
||||||
UNION
|
SELECT MAX(contact_a.id) AS contact_id, cc.id AS og_contact_id, MAX(cc.sort_name) AS og_sort_name
|
||||||
|
|
||||||
SELECT MAX(contact_a.id) AS contact_id, cc.id AS og_contact_id
|
|
||||||
FROM civicrm_contact contact_a
|
FROM civicrm_contact contact_a
|
||||||
INNER JOIN civicrm_relationship cr
|
INNER JOIN civicrm_relationship cr
|
||||||
ON (cr.contact_id_b = contact_a.id)
|
ON (cr.contact_id_b = contact_a.id)
|
||||||
@ -87,11 +83,15 @@ class CRM_ConstituentsOnly_BAO_ConstituentsOnly {
|
|||||||
AND cr.is_active = 1
|
AND cr.is_active = 1
|
||||||
INNER JOIN civicrm_contact cc
|
INNER JOIN civicrm_contact cc
|
||||||
ON (cr.contact_id_a = cc.id)
|
ON (cr.contact_id_a = cc.id)
|
||||||
AND contact_a.id <> cc.id
|
|
||||||
AND cc.id IN (" . implode(',', $contactIds) . ")
|
AND cc.id IN (" . implode(',', $contactIds) . ")
|
||||||
AND cc.do_not_trade = 1
|
AND cc.do_not_trade = 1
|
||||||
GROUP BY cc.id
|
GROUP BY cc.id";
|
||||||
) AS temp GROUP BY contact_id
|
if ($returnArray) {
|
||||||
|
return $sql;
|
||||||
|
}
|
||||||
|
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
|
||||||
";
|
";
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -107,24 +107,15 @@ class CRM_ConstituentsOnly_BAO_ConstituentsOnly {
|
|||||||
$sqls[] = "CREATE TEMPORARY TABLE quick_temp_table_1
|
$sqls[] = "CREATE TEMPORARY TABLE quick_temp_table_1
|
||||||
{$joinQuery}
|
{$joinQuery}
|
||||||
";
|
";
|
||||||
|
$queries = self::getConstituentSql(["SELECT id FROM quick_temp_table_1"], TRUE);
|
||||||
$sqls[] = "
|
$sqls[] = "
|
||||||
CREATE TEMPORARY TABLE quick_temp_table_2
|
CREATE TEMPORARY TABLE quick_temp_table_2
|
||||||
SELECT MAX(contact_a.id) AS contact_id, cc.id AS og_contact_id, MAX(cc.sort_name) AS og_sort_name
|
{$queries[0]}
|
||||||
FROM civicrm_contact contact_a
|
";
|
||||||
INNER JOIN civicrm_relationship cr
|
$sqls[] = "
|
||||||
ON (cr.contact_id_a = contact_a.id OR cr.contact_id_b = contact_a.id)
|
INSERT INTO quick_temp_table_2
|
||||||
AND contact_a.is_deleted = 0
|
{$queries[1]}
|
||||||
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) {
|
foreach ($sqls as $sql) {
|
||||||
CRM_Core_DAO::executeQuery($sql);
|
CRM_Core_DAO::executeQuery($sql);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user