Compare commits
No commits in common. "5779aee380642adce6a34b3c33991b7e98556a42" and "9fdb1ac12bf6a0019a615bc7ef436c800349b77d" have entirely different histories.
5779aee380
...
9fdb1ac12b
@ -2,87 +2,11 @@
|
|||||||
|
|
||||||
class CRM_ConstituentsOnly_BAO_ConstituentsOnly {
|
class CRM_ConstituentsOnly_BAO_ConstituentsOnly {
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get Suported Report Classes.
|
|
||||||
*
|
|
||||||
* @return array
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public static function getSuportedReportClasses() {
|
|
||||||
return [
|
|
||||||
'CRM_Report_Form_Contact_Summary',
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get sql.
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public static function getCommonSql() {
|
|
||||||
return "
|
|
||||||
SELECT MAX(contact_a.id) AS contact_id
|
|
||||||
FROM civicrm_contact contact_a
|
|
||||||
LEfT JOIN civicrm_relationship cr
|
|
||||||
ON (cr.contact_id_a = contact_a.id)
|
|
||||||
LEFT JOIN civicrm_contact cc
|
|
||||||
ON (cr.contact_id_b = cc.id)
|
|
||||||
AND cc.is_deleted = 0
|
|
||||||
AND (cc.do_not_trade IS NULL OR cc.do_not_trade = 0)
|
|
||||||
AND cr.is_active = 1
|
|
||||||
WHERE contact_a.do_not_trade = 1
|
|
||||||
AND cc.id IS NOT NULL
|
|
||||||
GROUP BY contact_a.id
|
|
||||||
|
|
||||||
UNION
|
|
||||||
SELECT MAX(contact_a.id) AS contact_id
|
|
||||||
FROM civicrm_contact contact_a
|
|
||||||
LEfT JOIN civicrm_relationship cr
|
|
||||||
ON (cr.contact_id_b = contact_a.id)
|
|
||||||
LEFT JOIN civicrm_contact cc
|
|
||||||
ON (cr.contact_id_a = cc.id)
|
|
||||||
AND cc.is_deleted = 0
|
|
||||||
AND (cc.do_not_trade IS NULL OR cc.do_not_trade = 0)
|
|
||||||
AND cr.is_active = 1
|
|
||||||
WHERE contact_a.do_not_trade = 1
|
|
||||||
AND cc.id IS NOT NULL
|
|
||||||
GROUP BY contact_a.id
|
|
||||||
";
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get Constituent Sql.
|
|
||||||
*
|
|
||||||
* @param array $contactIds
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public static function getConstituentSql($contactIds) {
|
|
||||||
return "
|
|
||||||
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
|
|
||||||
";
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Alter Quick search query.
|
* Alter Quick search query.
|
||||||
*
|
*
|
||||||
* @param string $query
|
* @param string $query
|
||||||
*
|
*
|
||||||
* @return string
|
|
||||||
*/
|
*/
|
||||||
public static function getQuickSearchQuery($query) {
|
public static function getQuickSearchQuery($query) {
|
||||||
$joinQuery = str_replace('WHERE ', 'WHERE (cc.do_not_trade = 1) AND ', $query);
|
$joinQuery = str_replace('WHERE ', 'WHERE (cc.do_not_trade = 1) AND ', $query);
|
||||||
@ -139,15 +63,25 @@ class CRM_ConstituentsOnly_BAO_ConstituentsOnly {
|
|||||||
/**
|
/**
|
||||||
* Rebuild search result rows.
|
* Rebuild search result rows.
|
||||||
*
|
*
|
||||||
* @param array $rows
|
* @param string $query
|
||||||
* @param array $headers
|
|
||||||
*
|
*
|
||||||
* @return array
|
|
||||||
*/
|
*/
|
||||||
public static function updateSearchRows($rows, $headers) {
|
public static function updateSearchRows($rows, $headers) {
|
||||||
$contactIds = array_keys($rows);
|
$contactIds = array_keys($rows);
|
||||||
$sql = self::getConstituentSql($contactIds);
|
$sql = "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
|
||||||
|
";
|
||||||
$result = CRM_Core_DAO::executeQuery($sql);
|
$result = CRM_Core_DAO::executeQuery($sql);
|
||||||
$relContactIds = [];
|
$relContactIds = [];
|
||||||
|
|
||||||
@ -197,93 +131,4 @@ class CRM_ConstituentsOnly_BAO_ConstituentsOnly {
|
|||||||
return $rows;
|
return $rows;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Rebuild report result rows.
|
|
||||||
*
|
|
||||||
* @param array $rows
|
|
||||||
* @param CRM_Core_Report $object
|
|
||||||
*
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public static function updateReportRows($rows, &$object) {
|
|
||||||
if (property_exists($object, '_donotTradeRowBuilding')
|
|
||||||
&& $object->_donotTradeRowBuilding
|
|
||||||
) {
|
|
||||||
return $rows;
|
|
||||||
}
|
|
||||||
|
|
||||||
$contactIds = [];
|
|
||||||
foreach ($rows as $id => $row) {
|
|
||||||
if (CRM_Utils_Array::value(
|
|
||||||
'civicrm_contact_do_not_trade_alter', $row) == 'Yes'
|
|
||||||
) {
|
|
||||||
$contactIds[$id] = $row['civicrm_contact_id'];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (empty($contactIds)) {
|
|
||||||
return $rows;
|
|
||||||
}
|
|
||||||
|
|
||||||
$sql = self::getConstituentSql($contactIds);
|
|
||||||
$result = CRM_Core_DAO::executeQuery($sql);
|
|
||||||
$relContactIds = [];
|
|
||||||
while ($result->fetch()) {
|
|
||||||
$relContactIds[$result->og_contact_id] = $result->contact_id;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!empty($relContactIds)) {
|
|
||||||
$object->_donotTradeRowBuilding = TRUE;
|
|
||||||
|
|
||||||
$select = $object->getVar('_select');
|
|
||||||
$from = $object->getVar('_from');
|
|
||||||
$dbAliases = $object->getVar('_aliases');
|
|
||||||
$contactAlias = "`{$dbAliases['civicrm_contact']}`";
|
|
||||||
$query = "
|
|
||||||
{$select}
|
|
||||||
{$from}
|
|
||||||
WHERE {$contactAlias}.id IN (" . implode(',', $relContactIds) . ")
|
|
||||||
";
|
|
||||||
|
|
||||||
$newRows = [];
|
|
||||||
$tempObj = $object;
|
|
||||||
$tempObj->_columnHeaders = $object->_backupHeader;
|
|
||||||
$tempObj->buildRows($query, $newRows);
|
|
||||||
$tempObj->formatDisplay($newRows);
|
|
||||||
foreach ($newRows as $id => $newRow) {
|
|
||||||
$contactId = $newRow['civicrm_contact_id'];
|
|
||||||
while (in_array($contactId, $relContactIds)) {
|
|
||||||
$ogContactId = array_search($contactId, $relContactIds);
|
|
||||||
$ogId = array_search($ogContactId, $contactIds);
|
|
||||||
$newRow['civicrm_contact_sort_name'] .= " ({$rows[$ogId]['civicrm_contact_sort_name']})";
|
|
||||||
$rows[$ogId] = $newRow;
|
|
||||||
unset($relContactIds[$ogContactId]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return $rows;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Update sql for report.
|
|
||||||
*
|
|
||||||
* @param CRM_Core_Report $object
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public static function updateReportSql(&$object) {
|
|
||||||
$where = $object->getVar('_where');
|
|
||||||
$sql = self::getCommonSql();
|
|
||||||
|
|
||||||
$dbAliases = $object->getVar('_aliases');
|
|
||||||
$contactAlias = "`{$dbAliases['civicrm_contact']}`";
|
|
||||||
$searchFrom = "{$contactAlias}.`do_not_trade` IS NULL OR ({$contactAlias}.`do_not_trade` = 0)";
|
|
||||||
$replaceFrom = "IF (
|
|
||||||
{$contactAlias}.`do_not_trade` = 1,
|
|
||||||
{$contactAlias}.id IN ({$sql}),
|
|
||||||
1
|
|
||||||
)";
|
|
||||||
$where = str_replace($searchFrom, $replaceFrom, $where);
|
|
||||||
$object->setVar('_where', $where);
|
|
||||||
$object->_backupHeader = $object->getVar('_columnHeaders');
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -45,17 +45,7 @@ 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'])) {
|
if (CRM_Utils_Array::value('civicrm_contact', $query->_tables) && empty($query->_paramLookup['do_not_trade'])) {
|
||||||
$query->_where[0][] = "
|
$query->_where[0][] = "( contact_a.do_not_trade IS NULL OR contact_a.do_not_trade = 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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -66,13 +56,6 @@ class CRM_ConstituentsOnly_BAO_Query extends CRM_Contact_BAO_Query_Interface {
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public function from($name, $mode, $side) {
|
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";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -130,7 +130,7 @@ function constituentsonly_civicrm_alterSettingsFolders(&$metaDataFolders = NULL)
|
|||||||
*/
|
*/
|
||||||
function constituentsonly_civicrm_queryObjects(&$queryObjects, $type) {
|
function constituentsonly_civicrm_queryObjects(&$queryObjects, $type) {
|
||||||
if ($type == 'Contact') {
|
if ($type == 'Contact') {
|
||||||
$queryObjects[] = new CRM_ConstituentsOnly_BAO_Query();
|
//$queryObjects[] = new CRM_ConstituentsOnly_BAO_Query();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -144,35 +144,10 @@ function constituentsonly_civicrm_contactListQuery(&$query, $queryText, $context
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implements hook_civicrm_alterReportVar().
|
* Implements hook_civicrm_selectWhereClause().
|
||||||
*
|
*
|
||||||
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_alterReportVar
|
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_selectWhereClause
|
||||||
*/
|
*/
|
||||||
function constituentsonly_civicrm_alterReportVar($varType, &$var, &$object) {
|
|
||||||
$reportClassName = get_class($object);
|
|
||||||
$supportedClassNames = CRM_ConstituentsOnly_BAO_ConstituentsOnly::getSuportedReportClasses();
|
|
||||||
if (!in_array($reportClassName, $supportedClassNames)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ('columns' == $varType) {
|
|
||||||
$var['civicrm_contact']['fields']['do_not_trade_alter'] = [
|
|
||||||
'title' => ts('Case ID'),
|
|
||||||
'no_display' => TRUE,
|
|
||||||
'required' => TRUE,
|
|
||||||
'name' => 'do_not_trade',
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($varType == 'sql') {
|
|
||||||
CRM_ConstituentsOnly_BAO_ConstituentsOnly::updateReportSql($object);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($varType == 'rows') {
|
|
||||||
$var = CRM_ConstituentsOnly_BAO_ConstituentsOnly::updateReportRows($var, $object);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function constituentsonly_civicrm_selectWhereClause($entity, &$clauses) {
|
function constituentsonly_civicrm_selectWhereClause($entity, &$clauses) {
|
||||||
if ($entity == 'Contact') {
|
if ($entity == 'Contact') {
|
||||||
$url = CRM_Utils_Array::value('q', $_GET);
|
$url = CRM_Utils_Array::value('q', $_GET);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user