protected $_aclFrom = NULL;
protected $_aclWhere = NULL;
+ protected $_datesTable = NULL, $_xgTable = NULL, $_igTable = NULL;
+
/**
* Class constructor.
*
*/
public function from() {
//define table name
- $randomNum = md5(uniqid());
- $this->_tableName = "civicrm_temp_custom_{$randomNum}";
+ $this->_datesTable = CRM_Utils_SQL_TempTable::build()->setCategory('dates')->getName();
+ $this->_xgTable = CRM_Utils_SQL_TempTable::build()->setCategory('xg')->getName();
+ $this->_igTable = CRM_Utils_SQL_TempTable::build()->setCategory('ig')->getName();
//grab the contacts added in the date range first
- $sql = "CREATE TEMPORARY TABLE dates_{$this->_tableName} ( id int primary key, date_added date ) ENGINE=HEAP";
+ $sql = "CREATE TEMPORARY TABLE {$this->_datesTable} ( id int primary key, date_added date ) ENGINE=HEAP";
if ($this->_debug > 0) {
print "-- Date range query: <pre>";
print "$sql;";
$endDateFix = "AND date_added <= '" . substr($endDate, 0, 10) . " 23:59:00'";
}
- $dateRange = "INSERT INTO dates_{$this->_tableName} ( id, date_added )
+ $dateRange = "INSERT INTO {$this->_datesTable} ( id, date_added )
SELECT
civicrm_contact.id,
min(civicrm_log.modified_date) AS date_added
$xGroups = 0;
}
- $sql = "DROP TEMPORARY TABLE IF EXISTS Xg_{$this->_tableName}";
+ $sql = "DROP TEMPORARY TABLE IF EXISTS {$this->_xgTable}";
CRM_Core_DAO::executeQuery($sql, CRM_Core_DAO::$_nullArray);
- $sql = "CREATE TEMPORARY TABLE Xg_{$this->_tableName} ( contact_id int primary key) ENGINE=HEAP";
+ $sql = "CREATE TEMPORARY TABLE {$this->_xgTable} ( contact_id int primary key) ENGINE=HEAP";
CRM_Core_DAO::executeQuery($sql, CRM_Core_DAO::$_nullArray);
//used only when exclude group is selected
if ($xGroups != 0) {
- $excludeGroup = "INSERT INTO Xg_{$this->_tableName} ( contact_id )
+ $excludeGroup = "INSERT INTO {$this->_xgTable} ( contact_id )
SELECT DISTINCT civicrm_group_contact.contact_id
- FROM civicrm_group_contact, dates_{$this->_tableName} AS d
+ FROM civicrm_group_contact, {$this->_datesTable} AS d
WHERE
d.id = civicrm_group_contact.contact_id AND
civicrm_group_contact.status = 'Added' AND
SELECT contact_id FROM civicrm_group_contact
WHERE civicrm_group_contact.group_id = {$values} AND civicrm_group_contact.status = 'Removed')";
- $smartGroupQuery = " INSERT IGNORE INTO Xg_{$this->_tableName}(contact_id) $smartSql";
+ $smartGroupQuery = " INSERT IGNORE INTO {$this->_xgTable}(contact_id) $smartSql";
CRM_Core_DAO::executeQuery($smartGroupQuery, CRM_Core_DAO::$_nullArray);
}
}
}
- $sql = "DROP TEMPORARY TABLE IF EXISTS Ig_{$this->_tableName}";
+ $sql = "DROP TEMPORARY TABLE IF EXISTS {$this->_igTable}";
CRM_Core_DAO::executeQuery($sql, CRM_Core_DAO::$_nullArray);
- $sql = "CREATE TEMPORARY TABLE Ig_{$this->_tableName}
+ $sql = "CREATE TEMPORARY TABLE {$this->_igTable}
( id int PRIMARY KEY AUTO_INCREMENT,
contact_id int,
group_names varchar(64)) ENGINE=HEAP";
CRM_Core_DAO::executeQuery($sql, CRM_Core_DAO::$_nullArray);
- $includeGroup = "INSERT INTO Ig_{$this->_tableName} (contact_id, group_names)
+ $includeGroup = "INSERT INTO {$this->_igTable} (contact_id, group_names)
SELECT d.id as contact_id, civicrm_group.name as group_name
- FROM dates_{$this->_tableName} AS d
+ FROM {$this->_datesTable} AS d
INNER JOIN civicrm_group_contact
ON civicrm_group_contact.contact_id = d.id
LEFT JOIN civicrm_group
//used only when exclude group is selected
if ($xGroups != 0) {
- $includeGroup .= " LEFT JOIN Xg_{$this->_tableName}
- ON d.id = Xg_{$this->_tableName}.contact_id";
+ $includeGroup .= " LEFT JOIN {$this->_xgTable}
+ ON d.id = {$this->_xgTable}.contact_id";
}
$includeGroup .= " WHERE
civicrm_group_contact.status = 'Added' AND
//used only when exclude group is selected
if ($xGroups != 0) {
- $includeGroup .= " AND Xg_{$this->_tableName}.contact_id IS null";
+ $includeGroup .= " AND {$this->_xgTable}.contact_id IS null";
}
if ($this->_debug > 0) {
$smartSql .= " AND contact_a.id IN (
SELECT id AS contact_id
- FROM dates_{$this->_tableName} )";
+ FROM {$this->_datesTable} )";
$smartSql .= " AND contact_a.id NOT IN (
SELECT contact_id FROM civicrm_group_contact
//used only when exclude group is selected
if ($xGroups != 0) {
- $smartSql .= " AND contact_a.id NOT IN (SELECT contact_id FROM Xg_{$this->_tableName})";
+ $smartSql .= " AND contact_a.id NOT IN (SELECT contact_id FROM {$this->_xgTable})";
}
$smartGroupQuery = " INSERT IGNORE INTO
- Ig_{$this->_tableName}(contact_id)
+ {$this->_igTable}(contact_id)
$smartSql";
CRM_Core_DAO::executeQuery($smartGroupQuery, CRM_Core_DAO::$_nullArray);
print "$smartGroupQuery;";
print "</pre>";
}
- $insertGroupNameQuery = "UPDATE IGNORE Ig_{$this->_tableName}
+ $insertGroupNameQuery = "UPDATE IGNORE {$this->_igTable}
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->_igTable}.contact_id IS NOT NULL
+ AND {$this->_igTable}.group_names IS NULL";
CRM_Core_DAO::executeQuery($insertGroupNameQuery, CRM_Core_DAO::$_nullArray);
if ($this->_debug > 0) {
print "-- Smart group query: <pre>";
/* We need to join to this again to get the date_added value */
- $from .= " INNER JOIN dates_{$this->_tableName} d ON (contact_a.id = d.id) {$this->_aclFrom}";
+ $from .= " INNER JOIN {$this->_datesTable} d ON (contact_a.id = d.id) {$this->_aclFrom}";
// Only include groups in the search query of one or more Include OR Exclude groups has been selected.
// CRM-6356
if ($this->_groups) {
- $from .= " INNER JOIN Ig_{$this->_tableName} temptable1 ON (contact_a.id = temptable1.contact_id)";
+ $from .= " INNER JOIN {$this->_igTable} temptable1 ON (contact_a.id = temptable1.contact_id)";
}
return $from;
public function __destruct() {
//drop the temp. tables if they exist
- if (!empty($this->_includeGroups)) {
- $sql = "DROP TEMPORARY TABLE IF EXISTS Ig_{$this->_tableName}";
+ if ($this->_igTable && !empty($this->_includeGroups)) {
+ $sql = "DROP TEMPORARY TABLE IF EXISTS {$this->_igTable}";
CRM_Core_DAO::executeQuery($sql, CRM_Core_DAO::$_nullArray);
}
- if (!empty($this->_excludeGroups)) {
- $sql = "DROP TEMPORARY TABLE IF EXISTS Xg_{$this->_tableName}";
+ if ($this->_xgTable && !empty($this->_excludeGroups)) {
+ $sql = "DROP TEMPORARY TABLE IF EXISTS {$this->_xgTable}";
CRM_Core_DAO::executeQuery($sql, CRM_Core_DAO::$_nullArray);
}
}