Skip to content

Commit

Permalink
Merge pull request #15821 from seamuslee001/dev_core_183_custom_group
Browse files Browse the repository at this point in the history
dev/core#183 Convert Include group / exclude group and include tags /…
  • Loading branch information
eileenmcnaughton authored Nov 20, 2019
2 parents 93a048d + edae5c5 commit cc58f6c
Showing 1 changed file with 80 additions and 39 deletions.
119 changes: 80 additions & 39 deletions CRM/Contact/Form/Search/Custom/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,45 @@ class CRM_Contact_Form_Search_Custom_Group extends CRM_Contact_Form_Search_Custo

protected $_formValues;

protected $_tableName = NULL;
/**
* @var CRM_Utils_SQL_TemTable Object
*/
protected $_xGTable = NULL;

/**
* @var CRM_Utils_SQL_TempTable object
*/
protected $_iGTable = NULL;

/**
* @var string Table Name for xclude Groups
*/
protected $_xGTableName = NULL;

/**
* @var string Table Name for Inclue Groups
*/
protected $_iGTableName = NULL;

/**
* @var CRM_Utils_SQL_TemTable Object
*/
protected $_xTTable = NULL;

/**
* @var CRM_Utils_SQL_TempTable object
*/
protected $_iTTable = NULL;

/**
* @var string Table Name for xclude Groups
*/
protected $_xTTableName = NULL;

/**
* @var string Table Name for Inclue Groups
*/
protected $_iTTableName = NULL;

protected $_where = ' (1) ';

Expand Down Expand Up @@ -227,9 +265,11 @@ public function from() {

$iGroups = $xGroups = $iTags = $xTags = 0;

//define table name
$randomNum = md5(uniqid());
$this->_tableName = "civicrm_temp_custom_{$randomNum}";
//define table names
$this->_xGTable = CRM_Utils_SQL_TempTable::build()->setCategory('xggroup');
$this->_xGTableName = $this->_xGTable->getName();
$this->_iGTable = CRM_Utils_SQL_TempTable::build()->setCategory('iggroup');
$this->_iGTableName = $this->_iGTable->getName();

//block for Group search
$smartGroup = [];
Expand Down Expand Up @@ -265,12 +305,11 @@ public function from() {
$xGroups = 0;
}

$sql = "CREATE TEMPORARY TABLE Xg_{$this->_tableName} ( contact_id int primary key) ENGINE=InnoDB";
CRM_Core_DAO::executeQuery($sql);
$this->_xGTable->createWithColumns("contact_id int primary key");

//used only when exclude group is selected
if ($xGroups != 0) {
$excludeGroup = "INSERT INTO Xg_{$this->_tableName} ( contact_id )
$excludeGroup = "INSERT INTO {$this->_xGTableName} ( contact_id )
SELECT DISTINCT civicrm_group_contact.contact_id
FROM civicrm_group_contact, civicrm_contact
WHERE
Expand All @@ -295,20 +334,16 @@ public function from() {
FROM civicrm_group_contact_cache gcc
WHERE gcc.group_id = {$ssGroup->id}
";
$smartGroupQuery = " INSERT IGNORE INTO Xg_{$this->_tableName}(contact_id) $smartSql";
$smartGroupQuery = " INSERT IGNORE INTO {$this->_xGTableName}(contact_id) $smartSql";
CRM_Core_DAO::executeQuery($smartGroupQuery);
}
}
}

$sql = "CREATE TEMPORARY TABLE Ig_{$this->_tableName} ( id int PRIMARY KEY AUTO_INCREMENT,
contact_id int,
group_names varchar(64)) ENGINE=InnoDB";

CRM_Core_DAO::executeQuery($sql);
$this->_iGTable->createWithColumns("id int PRIMARY KEY AUTO_INCREMENT, contact_id int, group_names varchar(64)");

if ($iGroups) {
$includeGroup = "INSERT INTO Ig_{$this->_tableName} (contact_id, group_names)
$includeGroup = "INSERT INTO {$this->_iGTableName} (contact_id, group_names)
SELECT civicrm_contact.id as contact_id, civicrm_group.title as group_name
FROM civicrm_contact
INNER JOIN civicrm_group_contact
Expand All @@ -317,14 +352,14 @@ public function from() {
ON civicrm_group_contact.group_id = civicrm_group.id";
}
else {
$includeGroup = "INSERT INTO Ig_{$this->_tableName} (contact_id, group_names)
$includeGroup = "INSERT INTO {$this->_iGTableName} (contact_id, group_names)
SELECT civicrm_contact.id as contact_id, ''
FROM civicrm_contact";
}
//used only when exclude group is selected
if ($xGroups != 0) {
$includeGroup .= " LEFT JOIN Xg_{$this->_tableName}
ON civicrm_contact.id = Xg_{$this->_tableName}.contact_id";
$includeGroup .= " LEFT JOIN {$this->_xGTableName}
ON civicrm_contact.id = {$this->_xGTableName}.contact_id";
}

if ($iGroups) {
Expand All @@ -338,7 +373,7 @@ public function from() {

//used only when exclude group is selected
if ($xGroups != 0) {
$includeGroup .= " AND Xg_{$this->_tableName}.contact_id IS null";
$includeGroup .= " AND {$this->_xGTableName}.contact_id IS null";
}

CRM_Core_DAO::executeQuery($includeGroup);
Expand All @@ -362,18 +397,18 @@ public function from() {

//used only when exclude group is selected
if ($xGroups != 0) {
$smartSql .= " AND gcc.contact_id NOT IN (SELECT contact_id FROM Xg_{$this->_tableName})";
$smartSql .= " AND gcc.contact_id NOT IN (SELECT contact_id FROM {$this->_xGTableName})";
}

$smartGroupQuery = " INSERT IGNORE INTO Ig_{$this->_tableName}(contact_id)
$smartGroupQuery = " INSERT IGNORE INTO {$this->_iGTableName}(contact_id)
$smartSql";

CRM_Core_DAO::executeQuery($smartGroupQuery);
$insertGroupNameQuery = "UPDATE IGNORE Ig_{$this->_tableName}
$insertGroupNameQuery = "UPDATE IGNORE {$this->_iGTableName}
SET group_names = (SELECT title FROM civicrm_group
WHERE civicrm_group.id = $values)
WHERE Ig_{$this->_tableName}.contact_id IS NOT NULL
AND Ig_{$this->_tableName}.group_names IS NULL";
WHERE {$this->_iGTableName}.contact_id IS NOT NULL
AND {$this->_iGTableName}.group_names IS NULL";
CRM_Core_DAO::executeQuery($insertGroupNameQuery);
}
}
Expand Down Expand Up @@ -405,12 +440,16 @@ public function from() {
$xTags = 0;
}

$sql = "CREATE TEMPORARY TABLE Xt_{$this->_tableName} ( contact_id int primary key) ENGINE=InnoDB";
CRM_Core_DAO::executeQuery($sql);
$this->_xTTable = CRM_Utils_SQL_TempTable::build()->setCategory('xtgroup');
$this->_xTTableName = $this->_xTTable->getName();
$this->_iTTable = CRM_Utils_SQL_TempTable::build()->setCategory('itgroup');
$this->_iTTableName = $this->_iTTable->getName();

$this->_xTTable->createWithColumns("contact_id int primary key");

//used only when exclude tag is selected
if ($xTags != 0) {
$excludeTag = "INSERT INTO Xt_{$this->_tableName} ( contact_id )
$excludeTag = "INSERT INTO {$this->_xTTableName} ( contact_id )
SELECT DISTINCT civicrm_entity_tag.entity_id
FROM civicrm_entity_tag, civicrm_contact
WHERE
Expand All @@ -421,14 +460,10 @@ public function from() {
CRM_Core_DAO::executeQuery($excludeTag);
}

$sql = "CREATE TEMPORARY TABLE It_{$this->_tableName} ( id int PRIMARY KEY AUTO_INCREMENT,
contact_id int,
tag_names varchar(64)) ENGINE=InnoDB";

CRM_Core_DAO::executeQuery($sql);
$this->_iTTable->createWithColumns("id int PRIMARY KEY AUTO_INCREMENT, contact_id int, tag_names varchar(64)");

if ($iTags) {
$includeTag = "INSERT INTO It_{$this->_tableName} (contact_id, tag_names)
$includeTag = "INSERT INTO {$this->_iTTableName} (contact_id, tag_names)
SELECT civicrm_contact.id as contact_id, civicrm_tag.name as tag_name
FROM civicrm_contact
INNER JOIN civicrm_entity_tag
Expand All @@ -438,15 +473,15 @@ public function from() {
ON civicrm_entity_tag.tag_id = civicrm_tag.id";
}
else {
$includeTag = "INSERT INTO It_{$this->_tableName} (contact_id, tag_names)
$includeTag = "INSERT INTO {$this->_iTTableName} (contact_id, tag_names)
SELECT civicrm_contact.id as contact_id, ''
FROM civicrm_contact";
}

//used only when exclude tag is selected
if ($xTags != 0) {
$includeTag .= " LEFT JOIN Xt_{$this->_tableName}
ON civicrm_contact.id = Xt_{$this->_tableName}.contact_id";
$includeTag .= " LEFT JOIN {$this->_xTTableName}
ON civicrm_contact.id = {$this->_xTTableName}.contact_id";
}
if ($iTags) {
$includeTag .= " WHERE civicrm_entity_tag.tag_id IN($iTags)";
Expand All @@ -457,7 +492,7 @@ public function from() {

//used only when exclude tag is selected
if ($xTags != 0) {
$includeTag .= " AND Xt_{$this->_tableName}.contact_id IS null";
$includeTag .= " AND {$this->_xTTableName}.contact_id IS null";
}

CRM_Core_DAO::executeQuery($includeTag);
Expand Down Expand Up @@ -489,15 +524,21 @@ public function from() {
* Set from statement depending on array sel
*/
$whereitems = [];
$tableNames = [
'Ig' => $this->_iGTableName,
'Xg' => $this->_xGTableName,
'It' => $this->_iTTableName,
'Xt' => $this->_xTTableName,
];
foreach (['Ig', 'It'] as $inc) {
if ($this->_andOr == 1) {
if ($$inc) {
$from .= " INNER JOIN {$inc}_{$this->_tableName} temptable$inc ON (contact_a.id = temptable$inc.contact_id)";
$from .= " INNER JOIN {$tableNames[$inc]} temptable$inc ON (contact_a.id = temptable$inc.contact_id)";
}
}
else {
if ($$inc) {
$from .= " LEFT JOIN {$inc}_{$this->_tableName} temptable$inc ON (contact_a.id = temptable$inc.contact_id)";
$from .= " LEFT JOIN {$tableNames[$inc]} temptable$inc ON (contact_a.id = temptable$inc.contact_id)";
}
}
if ($$inc) {
Expand All @@ -507,7 +548,7 @@ public function from() {
$this->_where = $whereitems ? "(" . implode(' OR ', $whereitems) . ')' : '(1)';
foreach (['Xg', 'Xt'] as $exc) {
if ($$exc) {
$from .= " LEFT JOIN {$exc}_{$this->_tableName} temptable$exc ON (contact_a.id = temptable$exc.contact_id)";
$from .= " LEFT JOIN {$tableNames[$exc]} temptable$exc ON (contact_a.id = temptable$exc.contact_id)";
$this->_where .= " AND temptable$exc.contact_id IS NULL";
}
}
Expand Down

0 comments on commit cc58f6c

Please sign in to comment.