-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustomfieldfilter.php
More file actions
142 lines (132 loc) · 4.54 KB
/
customfieldfilter.php
File metadata and controls
142 lines (132 loc) · 4.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
require_once 'customfieldfilter.civix.php';
use CRM_Customfieldfilter_ExtensionUtil as E;
/**
* Implements hook_civicrm_config().
*
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_config/
*/
function customfieldfilter_civicrm_config(&$config): void {
_customfieldfilter_civix_civicrm_config($config);
}
/**
* Implements hook_civicrm_install().
*
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_install
*/
function customfieldfilter_civicrm_install(): void {
_customfieldfilter_civix_civicrm_install();
}
/**
* Implements hook_civicrm_enable().
*
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_enable
*/
function customfieldfilter_civicrm_enable(): void {
_customfieldfilter_civix_civicrm_enable();
}
/**
* Hook implementation to modify contact summary page
*/
function customfieldfilter_civicrm_pageRun(&$page) {
$pageName = $page->getVar('_name');
if ($pageName == 'CRM_Contact_Page_View_CustomData') {
$groupId = $page->getVar('_groupId');
// Add contact ID and group info to the page for JavaScript access
$contactId = $page->getVar('_contactId');
$page->assign('collapsibleFilters', FALSE);
$page->assign('customFieldFilters_' . $groupId, FALSE);
if ($contactId && $groupId) {
$enabledGroups = CRM_Customfieldfilter_Utils::getEnabledGroups();
if (in_array($groupId, $enabledGroups)) {
$settings = Civi::settings()->get('customfieldfilter_settings') ?: [];
if ($settings['collapsible_filters']) {
$page->assign('collapsibleFilters', TRUE);
}
$enabledFields = CRM_Customfieldfilter_Utils::getEnabledFieldsForGroup($groupId);
if (!empty($enabledFields)) {
$page->assign('customFieldFilters_' . $groupId, TRUE);
$controller = new CRM_Core_Controller_Simple('CRM_Customfieldfilter_Form_Filter',
ts('Custom Filter'), NULL
);
$controller->setEmbedded(TRUE);
$controller->set('groupId', $groupId);
$controller->run();
}
}
}
}
}
/**
* Implements hook_civicrm_alterMenu().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_alterMenu
*/
function customfieldfilter_civicrm_alterMenu(&$items) {
$items['civicrm/ajax/multirecordfieldlist']['page_callback'] = 'CRM_Customfieldfilter_Utils::getMultiRecordFieldList';
}
/**
* Implements hook_civicrm_tabs().
*
* Add filter controls to contact summary tabs
*/
function customfieldfilter_civicrm_tabset($tabsetName, &$tabs, $context) {
if ($tabsetName !== 'civicrm/contact/view') {
return;
}
// Check if any custom field filtering is enabled
$config = CRM_Customfieldfilter_Utils::getFilterConfiguration();;
if (empty($config)) {
return;
}
foreach ($tabs as $key => &$tab) {
// Update the position of custom data tabs if configured
if (strpos($tab['id'], 'custom_') === 0) {
$groupId = str_replace('custom_', '', $tab['id']);
if (in_array($groupId, array_keys($config))) {
if (!empty($config[$groupId]['weight'])) {
$tab['weight'] = $config[$groupId]['weight'];
}
}
}
}
}
/**
* Implements hook_civicrm_navigationMenu().
*
* Add menu item for settings
*/
function customfieldfilter_civicrm_navigationMenu(&$menu) {
_customfieldfilter_civix_insert_navigation_menu($menu, 'Administer/Customize Data and Screens', [
'label' => E::ts('Custom Field Filter Settings'),
'name' => 'custom_field_filter_settings',
'url' => 'civicrm/admin/customfieldfilter?reset=1',
'permission' => 'administer CiviCRM',
'operator' => 'OR',
'separator' => 0,
'icon' => 'crm-i fa-filter',
]);
_customfieldfilter_civix_navigationMenu($menu);
}
function customfieldfilter_civicrm_customValueTableFilter($tableName, $params, &$additionalFilter) {
if (!empty($params) && is_array($params)) {
$additionalFilter = ' AND (1) ';
$additionalClauses = [];
foreach ($params as $fName => $fValue) {
if (str_starts_with($fName, 'custom_filter_') && !empty($fValue)) {
$columnName = substr($fName, 14);
if (is_array($fValue)) {
// implode array with IN clause with each value escaped
$fValue = "'" . implode("','", $fValue) . "'";
$additionalClauses[] = "{$columnName} IN ( $fValue )";
}
else {
$additionalClauses[] = "{$columnName} LIKE '%" . CRM_Utils_Type::escape($fValue, 'String') . "%'";
}
}
}
if (!empty($additionalClauses)) {
$additionalFilter .= ' AND ' . implode(' AND ', $additionalClauses);
}
}
}