-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustomtabgrouping.php
More file actions
408 lines (368 loc) · 11.4 KB
/
customtabgrouping.php
File metadata and controls
408 lines (368 loc) · 11.4 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
<?php
require_once 'customtabgrouping.civix.php';
use CRM_Customtabgrouping_ExtensionUtil as E;
/**
* Implements hook_civicrm_config().
*
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_config/
*/
function customtabgrouping_civicrm_config(&$config): void {
_customtabgrouping_civix_civicrm_config($config);
}
/**
* Implements hook_civicrm_enable().
*
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_enable
*/
function customtabgrouping_civicrm_enable(): void {
_customtabgrouping_civix_civicrm_enable();
}
/**
* Implements hook_civicrm_install().
*/
function customtabgrouping_civicrm_install() {
_customtabgrouping_civix_civicrm_install();
}
/**
* Implements hook_civicrm_uninstall().
*/
function customtabgrouping_civicrm_uninstall() {
_customtabgrouping_civix_civicrm_uninstall();
}
/**
* Implements hook_civicrm_buildForm().
*
* Add tab name field to custom group form.
*/
function customtabgrouping_civicrm_buildForm($formName, &$form) {
if ($formName == 'CRM_Custom_Form_Group') {
// Add the tab name field
$form->add(
'text',
'tab_name',
E::ts('Tab Name'),
['class' => 'huge', 'placeholder' => E::ts('Enter tab name (e.g., "Other Stuff")')],
FALSE
);
// Add the tab group order field
$form->add(
'text',
'tab_group_order',
E::ts('Order within Tab'),
[
'class' => 'four',
'placeholder' => E::ts('1'),
'size' => 3,
'maxlength' => 10
],
FALSE
);
$layoutWidth = CRM_Customtabgrouping_Utils::getLayoutWidth();
$form->add('select', 'layout_width', E::ts('Layout Width'), $layoutWidth, FALSE);
// Layout Float Field
$floatStyle = CRM_Customtabgrouping_Utils::getLayoutFloatStyle();
$form->add('select', 'layout_float', E::ts('Layout Float'), $floatStyle, FALSE);
// Set default values if editing existing group
if ($form->getAction() == CRM_Core_Action::UPDATE) {
$groupId = $form->getVar('_id');
if ($groupId) {
try {
$customGroup = civicrm_api3('CustomGroup', 'getsingle', [
'id' => $groupId,
'return' => ['tab_name', 'tab_group_order', 'layout_width', 'layout_float'],
]);
$defaults = [];
if (!empty($customGroup['tab_name'])) {
$defaults['tab_name'] = $customGroup['tab_name'];
}
if (!empty($customGroup['tab_group_order'])) {
$defaults['tab_group_order'] = $customGroup['tab_group_order'];
}
if (!empty($customGroup['layout_width'])) {
$defaults['layout_width'] = $customGroup['layout_width'];
}
if (!empty($customGroup['layout_float'])) {
$defaults['layout_float'] = $customGroup['layout_float'];
}
if (!empty($defaults)) {
$form->setDefaults($defaults);
}
} catch (Exception $e) {
// Group not found or no tab_name, continue
}
}
}
else {
// Set default value for new groups
$form->setDefaults(
[
'tab_group_order' => 1,
'layout_width' => '100',
'layout_float' => 'none',
]
);
}
// Add help text and styling via template
CRM_Core_Region::instance('page-body')->add([
'template' => 'CRM/Customtabgrouping/CustomGroupFormExtra.tpl',
]);
}
}
/**
* Implements hook_civicrm_postProcess().
*
* Save the tab name when custom group is saved.
*/
function customtabgrouping_civicrm_postProcess($formName, &$form) {
if ($formName == 'CRM_Custom_Form_Group') {
$values = $form->exportValues();
$groupId = $form->getVar('_id');
if ($groupId) {
$tabName = $values['tab_name'] ?? NULL;
$tabGroupOrder = $values['tab_group_order'] ?? 1;
$layoutWidth = $values['layout_width'] ?? '100';
$layoutFloat = $values['layout_float'] ?? 'none';
// Ensure tab_group_order is an integer
$tabGroupOrder = (int) $tabGroupOrder;
if ($tabGroupOrder < 1) {
$tabGroupOrder = 1;
}
CRM_Core_DAO::executeQuery("
UPDATE civicrm_custom_group
SET tab_name = %1, tab_group_order = %2, layout_width = %3, layout_float = %4
WHERE id = %5
", [
1 => [$tabName, 'String'],
2 => [$tabGroupOrder, 'Integer'],
3 => [$layoutWidth, 'String'],
4 => [$layoutFloat, 'String'],
5 => [$groupId, 'Integer'],
]);
// Clear cache
//CRM_Core_BAO_Cache::deleteGroup('contact fields');
}
}
}
/**
* Implements hook_civicrm_tabs().
*
* Group custom field groups with the same tab_name into a single tab.
*/
function customtabgrouping_civicrm_tabset($tabsetName, &$tabs, $context) {
if ($tabsetName !== 'civicrm/contact/view') {
return;
}
// Get contact ID from context
$contactId = $context['contact_id'] ?? NULL;
if (empty($contactId)) {
return;
}
// Get contact type
try {
$contact = civicrm_api3('Contact', 'getsingle', [
'id' => $contactId,
'return' => ['contact_type', 'contact_sub_type'],
]);
$contactType = $contact['contact_type'];
$contactSubType = $contact['contact_sub_type'] ?? [];
}
catch (Exception $e) {
return;
}
// Build the extends criteria based on contact type
$extendsTypes = ['Contact']; // Always include generic Contact
$extendsSubTypes = [];
// Add specific contact type
if (!empty($contactType)) {
$extendsTypes[] = $contactType;
}
// Add contact sub-types if any
if (!empty($contactSubType)) {
if (is_array($contactSubType)) {
$extendsSubTypes = array_merge($extendsSubTypes, $contactSubType);
}
else {
$extendsSubTypes[] = $contactSubType;
}
}
// Get all custom groups with tab_name set for this contact type
try {
$inputParams = [
'extends' => ['IN' => $extendsTypes],
'is_active' => 1,
'options' => ['limit' => 0],
'tab_name' => ['IS NOT NULL' => 1, '!=' => '' ],
];
if (!empty($extendsSubTypes)) {
$inputParams['extends_sub_type'] = ['IN' => $extendsSubTypes];
}
$customGroups = civicrm_api3('CustomGroup', 'get', $inputParams);
}
catch (Exception $e) {
return;
}
$groupedTabs = [];
$customGroupsToRemove = [];
// Group custom groups by tab_name
foreach ($customGroups['values'] as $group) {
// Check if custom group has a tab_name column value
$result = CRM_Core_DAO::executeQuery("
SELECT tab_name, tab_group_order FROM civicrm_custom_group WHERE id = %1
", [
1 => [$group['id'], 'Integer'],
]);
$tabName = NULL;
$tabGroupOrder = 1;
if ($result->fetch()) {
$tabName = $result->tab_name;
$tabGroupOrder = $result->tab_group_order ?? 1;
}
// Only process groups with tab_name set
if (!empty($tabName)) {
if (!isset($groupedTabs[$tabName])) {
$groupedTabs[$tabName] = [];
}
$group['tab_group_order'] = $tabGroupOrder;
$groupedTabs[$tabName][] = $group;
$customGroupsToRemove[] = 'custom_' . $group['id'];
}
}
// Remove individual custom group tabs that are being grouped
foreach ($tabs as $key => $tab) {
if (in_array($tab['id'], $customGroupsToRemove)) {
unset($tabs[$key]);
}
}
// Add grouped tabs
foreach ($groupedTabs as $tabName => $groups) {
// Sort groups by tab_group_order
usort($groups, function($a, $b) {
$orderA = $a['tab_group_order'] ?? 1;
$orderB = $b['tab_group_order'] ?? 1;
return $orderA <=> $orderB;
});
// Generate a unique tab ID based on the tab name
$tabId = 'grouped_custom_' . md5($tabName);
// Calculate weight (use the average weight of grouped items)
$totalWeight = 0;
foreach ($groups as $group) {
$totalWeight += CRM_Utils_Array::value('weight', $group, 100);
}
$avgWeight = count($groups) > 0 ? $totalWeight / count($groups) : 100;
// Build group IDs parameter (sorted by tab_group_order)
$groupIds = array_map(function ($g) {
return $g['id'];
}, $groups);
$tabs[] = [
'id' => $tabId,
'title' => $tabName,
'weight' => $avgWeight,
'icon' => 'crm-i fa-list-alt',
'url' => CRM_Utils_System::url('civicrm/contact/view/custom-grouped', [
'reset' => 1,
'cid' => $contactId,
'group_ids' => implode(',', $groupIds),
]),
'count' => count($groups),
];
}
}
/**
* Implements hook_civicrm_entityTypes().
*/
function customtabgrouping_civicrm_entityTypes(&$entityTypes) {
$civiVersion = CRM_Utils_System::version();
$membershipType = 'CRM_Core_DAO_CustomGroup';
if (version_compare($civiVersion, '5.75.0') >= 0) {
$membershipType = 'CustomGroup';
}
$entityTypes[$membershipType]['fields_callback'][]
= function ($class, &$fields) {
$fields['tab_name'] = [
'name' => 'tab_name',
'type' => CRM_Utils_Type::T_STRING,
'title' => ts('Tab Name'),
'description' => 'tab name.',
'localizable' => 0,
'maxlength' => 128,
'size' => CRM_Utils_Type::HUGE,
'import' => TRUE,
'where' => 'civicrm_custom_group.tab_name',
'export' => TRUE,
'table_name' => 'civicrm_custom_group',
'entity' => 'CustomGroup',
'bao' => 'CRM_Core_BAO_CustomGroup',
'localizable' => 1,
'html' => [
'type' => 'Text',
'label' => ts("Tab Name."),
],
];
$fields['tab_group_order'] = [
'name' => 'tab_group_order',
'type' => CRM_Utils_Type::T_INT,
'title' => ts('Custom Group Order in Tab'),
'description' => 'Custom Group Order in Tab',
'localizable' => 0,
'maxlength' => 10,
'size' => CRM_Utils_Type::T_INT,
'import' => TRUE,
'where' => 'civicrm_custom_group.tab_group_order',
'export' => TRUE,
'table_name' => 'civicrm_custom_group',
'entity' => 'CustomGroup',
'bao' => 'CRM_Core_BAO_CustomGroup',
'localizable' => 1,
'html' => [
'type' => 'Text',
'label' => ts("Custom Group Order in Tab"),
],
];
$fields['layout_width'] = [
'name' => 'layout_width',
'type' => CRM_Utils_Type::T_STRING,
'title' => ts('Layout Width'),
'description' => 'layout width.',
'localizable' => 0,
'maxlength' => 10,
'size' => CRM_Utils_Type::HUGE,
'import' => TRUE,
'where' => 'civicrm_custom_group.layout_width',
'export' => TRUE,
'table_name' => 'civicrm_custom_group',
'entity' => 'CustomGroup',
'bao' => 'CRM_Core_BAO_CustomGroup',
'localizable' => 1,
'html' => [
'type' => 'Select',
'label' => ts("Layout Width"),
],
'pseudoconstant' => [
'callback' => 'CRM_Customtabgrouping_Utils::getLayoutWidth',
],
];
$fields['layout_float'] = [
'name' => 'layout_float',
'type' => CRM_Utils_Type::T_STRING,
'title' => ts('Layout Float'),
'description' => 'The float option allows more flexible positioning of custom field groups',
'localizable' => 0,
'maxlength' => 10,
'size' => CRM_Utils_Type::HUGE,
'import' => TRUE,
'where' => 'civicrm_custom_group.layout_float',
'export' => TRUE,
'table_name' => 'civicrm_custom_group',
'entity' => 'CustomGroup',
'bao' => 'CRM_Core_BAO_CustomGroup',
'localizable' => 1,
'html' => [
'type' => 'Select',
'label' => ts("Layout Float"),
],
'pseudoconstant' => [
'callback' => 'CRM_Customtabgrouping_Utils::getLayoutFloatStyle',
],
];
};
}