Compare commits
2 Commits
e34dbaa077
...
9fdb1ac12b
Author | SHA1 | Date | |
---|---|---|---|
9fdb1ac12b | |||
e3e27bbcb6 |
134
CRM/ConstituentsOnly/BAO/ConstituentsOnly.php
Normal file
134
CRM/ConstituentsOnly/BAO/ConstituentsOnly.php
Normal file
@ -0,0 +1,134 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
class CRM_ConstituentsOnly_BAO_ConstituentsOnly {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Alter Quick search query.
|
||||||
|
*
|
||||||
|
* @param string $query
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static function getQuickSearchQuery($query) {
|
||||||
|
$joinQuery = str_replace('WHERE ', 'WHERE (cc.do_not_trade = 1) AND ', $query);
|
||||||
|
$sqls[] = "CREATE TEMPORARY TABLE quick_temp_table_1
|
||||||
|
{$joinQuery}
|
||||||
|
";
|
||||||
|
|
||||||
|
$sqls[] = "
|
||||||
|
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
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
$startPoint = stripos($query, 'from');
|
||||||
|
$endPoint = stripos($query, 'union');
|
||||||
|
$firstQuery = substr($query, $startPoint, ($endPoint - $startPoint));
|
||||||
|
$firstQuery = substr($firstQuery, stripos($firstQuery, 'select'));
|
||||||
|
$firstQuery = substr($firstQuery, 0, stripos($firstQuery, 'where'));
|
||||||
|
$firstQuery = str_replace(
|
||||||
|
'sort_name, ',
|
||||||
|
"CONCAT(cc.sort_name, ' (', og_sort_name, ')'), ",
|
||||||
|
$firstQuery
|
||||||
|
);
|
||||||
|
|
||||||
|
$strToReplace = "
|
||||||
|
UNION
|
||||||
|
(
|
||||||
|
$firstQuery
|
||||||
|
INNER JOIN quick_temp_table_2 temp ON temp.contact_id = cc.id
|
||||||
|
)
|
||||||
|
) t
|
||||||
|
";
|
||||||
|
|
||||||
|
$replace = 'WHERE (cc.do_not_trade IS NULL OR cc.do_not_trade = 0) AND ';
|
||||||
|
$query = str_replace('WHERE ', $replace, $query);
|
||||||
|
$query = str_replace(') t', $strToReplace, $query);
|
||||||
|
return $query;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Rebuild search result rows.
|
||||||
|
*
|
||||||
|
* @param string $query
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static function updateSearchRows($rows, $headers) {
|
||||||
|
$contactIds = array_keys($rows);
|
||||||
|
$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);
|
||||||
|
$relContactIds = [];
|
||||||
|
|
||||||
|
while ($result->fetch()) {
|
||||||
|
$relContactIds[$result->og_contact_id] = $result->contact_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!empty($relContactIds)) {
|
||||||
|
$formValues = ['contact_id' => $relContactIds];
|
||||||
|
|
||||||
|
$url = CRM_Utils_Array::value('q', $_GET);
|
||||||
|
$returnProperties = NULL;
|
||||||
|
$request = $_REQUEST;
|
||||||
|
if (!empty($_REQUEST['has_js'])) {
|
||||||
|
$qfKey = $_REQUEST['qfKey'];
|
||||||
|
$formValuesFromSession = CRM_Core_Session::singleton()->get("CRM_Contact_Controller_Search_{$qfKey}");
|
||||||
|
if (!empty($formValuesFromSession['formValues'])) {
|
||||||
|
$request = $formValuesFromSession['formValues'];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (in_array($url, ['civicrm/contact/search/builder']) || !empty($request['uf_group_id'])) {
|
||||||
|
foreach ($headers as $header) {
|
||||||
|
if (!empty($header['sort'])) {
|
||||||
|
$returnProperties[$header['sort']] = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$returnProperties['contact_sub_type'] = 1;
|
||||||
|
$returnProperties['contact_type'] = 1;
|
||||||
|
}
|
||||||
|
$queryParams = CRM_Contact_BAO_Query::convertFormValues($formValues);
|
||||||
|
$selector = new CRM_Contact_Selector(
|
||||||
|
'',
|
||||||
|
$queryParams,
|
||||||
|
$queryParams,
|
||||||
|
$returnProperties
|
||||||
|
);
|
||||||
|
list($select, $from, $where, $having) = $selector->getQuery()->query();
|
||||||
|
$newRows = $selector->getRows('', '', '', '');
|
||||||
|
foreach ($relContactIds as $ogContactId => $cid) {
|
||||||
|
if (empty($newRows[$cid])) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$newRows[$cid]['sort_name'] .= "({$rows[$ogContactId]['sort_name']})";
|
||||||
|
$rows[$ogContactId] = $newRows[$cid];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $rows;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -140,8 +140,7 @@ function constituentsonly_civicrm_queryObjects(&$queryObjects, $type) {
|
|||||||
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_contactListQuery
|
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_contactListQuery
|
||||||
*/
|
*/
|
||||||
function constituentsonly_civicrm_contactListQuery(&$query, $queryText, $context, $id) {
|
function constituentsonly_civicrm_contactListQuery(&$query, $queryText, $context, $id) {
|
||||||
$replace = 'WHERE (cc.do_not_trade IS NULL OR cc.do_not_trade = 0) AND ';
|
$query = CRM_ConstituentsOnly_BAO_ConstituentsOnly::getQuickSearchQuery($query);
|
||||||
$query = str_replace('WHERE ', $replace, $query);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -158,3 +157,14 @@ function constituentsonly_civicrm_selectWhereClause($entity, &$clauses) {
|
|||||||
$clauses['do_not_trade'] = ' = 0';
|
$clauses['do_not_trade'] = ' = 0';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Implements hook_civicrm_searchColumns().
|
||||||
|
*
|
||||||
|
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_searchColumns
|
||||||
|
*/
|
||||||
|
function constituentsonly_civicrm_searchColumns($objectName, &$headers, &$rows, &$selector) {
|
||||||
|
if (in_array($objectName, ['contact']) && !empty($rows)) {
|
||||||
|
$rows = CRM_ConstituentsOnly_BAO_ConstituentsOnly::updateSearchRows($rows, $headers);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user