-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfieldlookup.php
319 lines (290 loc) · 11.1 KB
/
fieldlookup.php
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
<?php
require_once 'fieldlookup.civix.php';
use CRM_Fieldlookup_ExtensionUtil as E;
/**
* Generate paths on the fly for each chain-select lookup. This is not a great way to do this - ideally
* we'd have a single route and a URL param - but I'm trying not to rewrite the
* Civi chain-select code, which hard-codes assumptions that you're using it for state/county purposes.
* @param array $items
*/
function fieldlookup_civicrm_alterMenu(&$items) {
// We have an issue where this is called before the autoloader can find the FieldLookupGroup API, so we look using the DAO.
$dao = new CRM_Fieldlookup_DAO_FieldLookupGroup();
$dao->find();
while ($dao->fetch()) {
$items["civicrm/ajax/chainselect/{$dao->field_2_name}"] = [
'page_callback' => 'CRM_Fieldlookup_AJAX::chainSelectJSON',
];
}
}
function fieldlookup_civicrm_buildForm($formName, &$form) {
// For now, we're just working on multi-record custom field forms. Will expand this later.
if ($formName != 'CRM_Contact_Form_CustomData') {
return;
}
// We no longer use this JS - will remove at a later date.
// CRM_Core_Resources::singleton()->addScriptFile('fieldlookup', 'js/fieldlookup.js');
// Collect a list of Select fields, so we can check for field lookups.
foreach ($form->_elements as $element) {
if ($element instanceof HTML_QuickForm_select) {
preg_match('/(custom_\d+)/', $element->_attributes['name'], $matches);
$selectFields[$matches[1]] = $element->_attributes['name'];
}
}
// Are any of these select fields also lookup fields?
if ($selectFields) {
$lookupGroupsRaw = civicrm_api3('FieldLookupGroup', 'get', [
'sequential' => 1,
'field_1_name' => ['IN' => array_flip($selectFields)],
'lookup_type' => 'chain-select',
]);
}
// If any of these ARE lookup fields, configure them as chain selects.
$lookupGroups = [];
foreach ($lookupGroupsRaw['values'] as $rawGroup) {
$lookupGroups[$selectFields[$rawGroup['field_2_name']]] = [
'chain-parent' => $rawGroup['field_1_name'],
'chain-child' => $rawGroup['field_2_name'],
];
}
foreach ($lookupGroups as $key => $lookupGroup) {
$settings = [
'control_field' => $lookupGroup['chain-parent'],
'control-field-name' => $selectFields[$lookupGroup['chain-parent']],
'data-callback' => "civicrm/ajax/chainselect/{$lookupGroup['chain-child']}",
];
// OK, CRM_Core_Form::addChainSelect() hard-codes assumptions about being for state/county, so
// let's use our own variant.
fieldlookup_addChainSelect($key, $settings, $form);
}
}
/**
* Create a chain-select target field. Stolen from CRM_Core_Form because it hard-codes
* references to $form->_chainSelects, which is private and preProcessChainSelectFields()
* assumes this is a state/country or county/country chain select.
*
* @param string $elementName
* @param array $settings
*/
function fieldlookup_addChainSelect($elementName, array $settings, &$form) {
$controlElement = $form->_elements[$form->_elementIndex[$settings['control-field-name']]];
$targetElement = $form->_elements[$form->_elementIndex[$elementName]];
$props = $settings += [
'control_field' => NULL,
'data-callback' => NULL,
'label' => $targetElement->_label,
'data-entry-prompt' => "Choose $controlElement->_label first",
'data-none-prompt' => ts('- N/A -'),
'placeholder' => empty($settings['required']) ? ts('- none -') : ts('- select -'),
];
CRM_Utils_Array::remove($props, 'label', 'required', 'control_field', 'context', 'control-field-name');
$props['data-select-prompt'] = $props['placeholder'];
$props['data-name'] = $elementName;
// Add the 'crm-chain-select-control' class and data-target to the control field.
$controlElement->setAttribute('data-target', $elementName);
$controlElement->_attributes['class'] .= ' crm-chain-select-control';
// Now update the target field attributes.
$props['class'] = $targetElement->_attributes['class'] . ' crm-chain-select-target';
$targetElement->_attributes = array_merge($targetElement->_attributes, $props);
// If the control field already has a value, pre-filter.
$controlFieldDefaultValue = $form->_defaultValues[$settings['control-field-name']] ?? FALSE;
if ($controlFieldDefaultValue) {
// We can't use data-api-field since you need "Administer CiviCRM" so let's calculate it with preg_match.
preg_match('/(custom_\d+)/', $targetElement->_attributes['name'], $matches);
$targetField = $matches[1];
$options = CRM_Fieldlookup_AJAX::chainSelect($targetField, $form->_defaultValues[$settings['control-field-name']] ?? FALSE);
foreach ($options as $k => $option) {
$filteredOptions[$k] = [
'attr' => [
'value' => $option['key'],
],
'text' => $option['value'],
];
}
$targetElement->_options = $filteredOptions;
}
}
function fieldlookup_civicrm_post_callback(Civi\Core\Event\PostEvent $event) {
$op = $event->action;
$objectName = $event->entity;
$id = $event->id;
$object = $event->object;
// Some entities have a post hook but no API, that's bad news. Skip them.
$validEntities = CRM_Fieldlookup_SelectValues::getEntities();
if (!in_array($objectName, $validEntities)) {
return;
}
if (CRM_Core_Transaction::isActive()) {
CRM_Core_Transaction::addCallback(CRM_Core_Transaction::PHASE_POST_COMMIT, 'findNoncustomFieldReverseLookups', [$op, $objectName, $id, $object]);
}
else {
findNoncustomFieldReverseLookups($op, $objectName, $id, $object);
}
}
/**
* Identifies reverse lookups triggered by non-custom fields.
*/
function findNoncustomFieldReverseLookups($op, $objectName, $id, $object) {
if ($op == 'delete') {
return;
}
// Check for reverse lookups.
$fields = array_keys((array) $object);
$fieldLookupGroups = CRM_Fieldlookup_BAO_FieldLookup::getReverseLookupGroups($fields, $objectName);
foreach ($fieldLookupGroups as $lookupGroup) {
// If reverse lookups are found.
// Handle different foreign keys on the other table.
if ($lookupGroup['table_1_fk'] ?? FALSE) {
$foreignKey = $lookupGroup['table_1_fk'];
$entityId = $object->$foreignKey;
// If a foreign key is specified but missing, we're not doing any field lookups. E.g. for addresses in a LocBlock.
if (!$entityId) {
return;
}
}
else {
$entityId = $id;
}
$field1Name = $lookupGroup['field_1_name'];
$field1Value = $object->$field1Name;
if ($field1Value) {
doReverseLookup($lookupGroup, $field1Value, $entityId);
}
}
}
function fieldlookup_civicrm_custom_callback(Civi\Core\Event\GenericHookEvent $event) {
$values = $event->getHookValues();
$op = $values[0];
$groupId = $values[1];
$entityId = $values[2];
$params = $values[3];
if (CRM_Core_Transaction::isActive()) {
CRM_Core_Transaction::addCallback(CRM_Core_Transaction::PHASE_POST_COMMIT, 'findCustomFieldReverseLookups', [$op, $groupId, $entityId, &$params]);
}
else {
findCustomFieldReverseLookups($op, $groupId, $entityId, $params);
}
}
/**
* Identifies reverse lookups triggered by custom fields.
*/
function findCustomFieldReverseLookups($op, $groupId, $entityId, $params) {
if ($op == 'delete') {
return;
}
// Check for reverse lookups.
$customFieldIds = CRM_Utils_Array::collect('custom_field_id', $params);
$customFieldNames = [];
foreach ($customFieldIds as $fieldId) {
$customFieldNames[] = 'custom_' . $fieldId;
}
$fieldLookupGroups = CRM_Fieldlookup_BAO_FieldLookup::getReverseLookupGroups($customFieldNames);
foreach ($fieldLookupGroups as $lookupGroup) {
// If reverse lookups are found.
$field1Name = $lookupGroup['field_1_name'];
if (strpos($field1Name, 'custom_') === 0) {
$customFieldId = substr($field1Name, 7);
}
$key = array_search($customFieldId, array_column($params, 'custom_field_id'));
$field1Value = $params[$key]['value'];
doReverseLookup($lookupGroup, $field1Value, $entityId);
}
}
/**
* Handles reverse lookups.
*
* @param $lookupGroup array
* @param $field1Value string
* @param $entityId int
*/
function doReverseLookup($lookupGroup, $field1Value, $entityId) {
// Find the fieldLookup that matches.
$params = [
'sequential' => 1,
'field_lookup_group_id' => $lookupGroup['id'],
'field_1_value' => [$lookupGroup['lookup_operator'] => $field1Value],
];
// Handle the "reverse between" case.
if ($lookupGroup['lookup_operator'] == 'BETWEEN') {
$params['field_1_value'] = ['<=' => $field1Value];
$params['field_1_value_2'] = ['>=' => $field1Value];
}
$fieldLookup = civicrm_api3('FieldLookup', 'get', $params)['values'];
if ($fieldLookup) {
setField2($lookupGroup, $entityId, $fieldLookup[0]['field_2_value']);
}
}
/**
* Given a fieldLookupGroup record, entity ID and value, set the field 2 value.
*/
function setField2($fieldLookup, $entityId, $field2Value) {
$field2Name = $fieldLookup['field_2_name'];
$field2Entity = $fieldLookup['field_2_entity'];
// Check to see if the field has the same value; if so we won't overwrite it.
$existingValue = civicrm_api3($field2Entity, 'get', [
'sequential' => 1,
'return' => [$field2Name],
'id' => $entityId,
])['values'][0][$field2Name] ?? NULL;
if ($existingValue !== $field2Value) {
// Fill in the reverse lookup here.
civicrm_api3($field2Entity, 'create', [
'id' => $entityId,
$field2Name => $field2Value,
]);
}
}
/**
* Implements hook_civicrm_config().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_config
*/
function fieldlookup_civicrm_config(&$config) {
_fieldlookup_civix_civicrm_config($config);
if (isset(Civi::$statics[__FUNCTION__])) {
return;
}
Civi::$statics[__FUNCTION__] = 1;
// We want to run last, to avoid unpleasant interactions with other extensions using the post hook.
Civi::dispatcher()->addListener('hook_civicrm_post', 'fieldlookup_civicrm_post_callback', -10);
Civi::dispatcher()->addListener('hook_civicrm_custom', 'fieldlookup_civicrm_custom_callback', -10);
}
/**
* Implements hook_civicrm_install().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_install
*/
function fieldlookup_civicrm_install() {
_fieldlookup_civix_civicrm_install();
}
/**
* Implements hook_civicrm_enable().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_enable
*/
function fieldlookup_civicrm_enable() {
_fieldlookup_civix_civicrm_enable();
}
// --- Functions below this ship commented out. Uncomment as required. ---
/**
* Implements hook_civicrm_preProcess().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_preProcess
*
// */
/**
* Implements hook_civicrm_navigationMenu().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_navigationMenu
*
function fieldlookup_civicrm_navigationMenu(&$menu) {
_fieldlookup_civix_insert_navigation_menu($menu, 'Mailings', array(
'label' => E::ts('New subliminal message'),
'name' => 'mailing_subliminal_message',
'url' => 'civicrm/mailing/subliminal',
'permission' => 'access CiviMail',
'operator' => 'OR',
'separator' => 0,
));
_fieldlookup_civix_navigationMenu($menu);
} // */