-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmautic.php
281 lines (259 loc) · 8.6 KB
/
mautic.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
<?php
require_once 'mautic.civix.php';
$autoload = __DIR__ . '/vendor/autoload.php';
if (file_exists($autoload)) {
require_once $autoload;
}
use CRM_Mautic_ExtensionUtil as E;
/**
* Implements hook_civicrm_config().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_config
*/
function mautic_civicrm_config(&$config) {
_mautic_civix_civicrm_config($config);
}
/**
* Implements hook_civicrm_install().
*/
function mautic_civicrm_install() {
_mautic_civix_civicrm_install();
}
/**
* Implements hook_civicrm_enable().
*/
function mautic_civicrm_enable() {
_mautic_civix_civicrm_enable();
}
/**
* Implementation of hook_civicrm_buildForm.
*
* Add Mautic integration to group settings.
*
* @param string $formName
* @param CRM_Core_Form $form
*/
function mautic_civicrm_buildForm($formName, &$form) {
if ($formName != 'CRM_Group_Form_Edit') {
return;
}
if ($form->isSubmitted()) {
return;
}
// Add Settings Template.
// For Editing a group, then adding to regions other than html-header
// results in duplicate elements.
// For Adding a group, the html-header region inserts the template before
// CRM.$ is loaded.
$region = $form->getAction() == CRM_Core_Action::ADD ? 'page-footer' : 'html-header';
CRM_Core_Region::instance($region)->add(
['template' => 'CRM/Group/MauticSettings.tpl']);
if (($form->getAction() == CRM_Core_Action::ADD) || ($form->getAction() == CRM_Core_Action::UPDATE)) {
// Add form elements to associate group with Mautic Segment.
$segments = CRM_Mautic_Utils::getMauticSegmentOptions();
if ($segments) {
$form->add('select', 'mautic_segment', ts('Mautic Segment'), ['' => '- select -'] + $segments);
$options = [
ts('No integration'),
ts('Sync to a Mautic segment: Contacts in this group will be added or removed from a segment.'),
];
$form->addRadio('mautic_integration_option', '', $options, NULL, '<br/>');
// Prepopulate details if 'edit' action
$groupId = $form->getVar('_id');
if ($form->getAction() == CRM_Core_Action::UPDATE AND !empty($groupId)) {
$mauticDetails = CRM_Mautic_Utils::getGroupsToSync([$groupId]);
$groupDetails = CRM_Utils_Array::value($groupId, $mauticDetails, []);
$defaults['mautic_fixup'] = 1;
if (!empty($groupDetails)) {
$defaults['mautic_segment'] = $groupDetails['segment_id'];
$defaults['mautic_integration_option'] = !empty($groupDetails['segment_id']);
$form->setDefaults($defaults);
}
else {
// defaults for a new group
$defaults['mautic_integration_option'] = 0;
$defaults['is_mautic_update_grouping'] = 0;
$form->setDefaults($defaults);
}
$form->assign('mautic_segment_id' , $groupDetails['segment_id'] ?? 0);
}
}
}
}
/**
* Implements hook_civicrm_validateForm($formName, &$fields, &$files, &$form, &$errors)
*/
function mautic_civicrm_validateForm($formName, &$fields, &$files, &$form, &$errors) {
if ($formName != 'CRM_Group_Form_Edit') {
return;
}
if ($fields['mautic_integration_option'] == 1) {
if (empty($fields['mautic_segment'])) {
$errors['mautic_segment'] = ts('Please specify a Segment');
}
else {
// We need to make sure that this is the only group for this segment.
$otherGroups = CRM_Mautic_Utils::getGroupsToSync([], $fields['mautic_segment'], TRUE);
$thisGroup = $form->getVar('_group');
if ($thisGroup) {
unset($otherGroups[$thisGroup->id]);
}
if (!empty($otherGroups)) {
$otherGroup = reset($otherGroups);
$errors['mautic_segment'] = ts('There is already a CiviCRM group associated with this Segment, called "'
. $otherGroup['civigroup_title'].'"');
}
}
}
}
/**
* Implements hook_civicrm_customPre()
*/
function mautic_civicrm_customPre(string $op, int $groupID, int $entityID, array &$params): void {
// Create a Mautic Segment for an Event if it is so configured.
static $mauticEventGid = NULL;
if (!$mauticEventGid) {
$mauticEventGid = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup',
'Mautic_Event',
'id',
'name'
);
}
if (in_array($op, ['create', 'edit']) && $groupID == $mauticEventGid) {
$createSegmentField = NULL;
$segmentIdField = NULL;
foreach ($params as $idx => &$field) {
if ($field['column_name'] == 'create_segment') {
$createSegmentField = $field;
}
elseif ($field['column_name'] == 'mautic_segment_id') {
$segmentIdField =& $field;
}
}
if (empty($segmentIdField['value']) && !empty($createSegmentField['value'])) {
$title = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event',
$entityID,
'title',
'id'
);
if ($title) {
try {
$suffix = Civi::settings()->get('mautic_event_segment_text');
$position = Civi::settings()->get('mautic_event_segment_text_position');
$segmentName = $position == 'before' ? "$suffix $title" : "$title $suffix";
$segmentName = trim($segmentName);
// Possibly segment with the name already exists.
$segmentParams = [
'name' => $segmentName,
];
$segmentId = CRM_Mautic_Utils::createSegment($segmentParams);
// Add Segment.
$segmentIdField['value'] = $segmentId;
}
catch (Exception $e) {
CRM_Core_Error::debug_var(__FUNCTION__, [
'Error Creating Mautic segment.',
$e->getMessage(),
$segmentParams,
]);
}
}
}
}
}
/**
* Implements hook_civicrm_pageRun().
*
* @param CRM_Core_Page $page
*/
function mautic_civicrm_pageRun(&$page) {
if ($page->getVar('_name') == 'CRM_Group_Page_Group') {
// Manage Groups page at /civicrm/group?reset=1
$js_safe_object = [];
foreach (CRM_Mautic_Utils::getGroupsToSync() as $group_id => $group) {
if ($group['segment_name']) {
$val = strtr(
ts("Sync to segment: %segment_name"),
['%segment_name' => htmlspecialchars($group['segment_name'])]
);
}
else {
$val = ts("Missing segment.");
}
$js_safe_object['id' . $group_id] = $val;
}
$page->assign('mautic_groups', json_encode($js_safe_object));
}
}
/**
* Implements hook_civicrm_navigationMenu().
*/
function mautic_civicrm_navigationMenu(&$menu) {
_mautic_civix_insert_navigation_menu($menu, 'Administer', [
'label' => 'Mautic',
'name' => 'Mautic',
'url' => NULL,
'permission' => 'administer CiviCRM',
'operator' => NULL,
'separator' => NULL,
]);
_mautic_civix_insert_navigation_menu($menu, 'Administer/Mautic', [
'label' => 'Mautic Settings',
'name' => 'Mautic Settings',
'url' => 'civicrm/admin/mautic/settings',
'permission' => 'administer CiviCRM',
'operator' => NULL,
'separator' => 0,
]);
_mautic_civix_insert_navigation_menu($menu, 'Administer/Mautic', [
'label' => 'Connection',
'name' => 'Connection',
'url' => 'civicrm/admin/mautic/connection',
'permission' => 'administer CiviCRM',
'operator' => NULL,
'separator' => 0,
]);
_mautic_civix_insert_navigation_menu($menu, 'Administer/Mautic', [
'label' => 'Push to Mautic',
'name' => 'Push',
'url' => 'civicrm/admin/mautic/pushsync?reset=1',
'permission' => 'administer CiviCRM',
'operator' => NULL,
'separator' => 0,
]);
_mautic_civix_navigationMenu($menu);
}
function mautic_civicrm_entityTypes(&$entityTypes) {
}
/**
* Implements hook_civicrm_alterLogTables().
*
* Exclude tables from logging tables since they hold mostly temp data.
*/
function mautic_civicrm_alterLogTables(&$logTableSpec) {
unset($logTableSpec['civicrm_mauticwebhook']);
}
/**
*
* Implements hook_civicrm_fieldOptions().
*/
function mautic_civicrm_fieldOptions($entity, $field, &$options, $params) {
// Add options for field linking Event to Mautic Segment.
if ($entity == 'Event' && 0 === strpos($field, 'custom_')) {
$fid = CRM_Core_BAO_CustomField::getCustomFieldID('Mautic_Segment', 'Mautic_Event');
if ('custom_' . $fid == $field) {
$segments = CRM_Mautic_Utils::getMauticSegmentOptions();
$options = $segments ? $segments : $options;
}
}
}
/*
* Implementation of hook_civicrm_alterCustomFieldDisplayValue
*
*/
function mautic_civicrm_alterCustomFieldDisplayValue(&$displayValue, $value, $entityId, $fieldInfo) {
if ($fieldInfo['name'] == 'Mautic_Contact_ID') {
$mauticURL = \Civi::settings()->get('mautic_connection_url');
$displayValue = "<a href='{$mauticURL}/s/contacts/view/{$value}' target='_blank'>$value</a>";
}
}