-
Notifications
You must be signed in to change notification settings - Fork 0
/
oauth_sync.php
executable file
·301 lines (271 loc) · 9.3 KB
/
oauth_sync.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
<?php
require_once 'oauth_sync.civix.php';
use CRM_OauthSync_ExtensionUtil as E;
require_once 'CRM/OauthSync/GenerateSettings.php';
require_once 'CRM/OauthSync/Form/ConnectionSettings.php';
require_once 'CRM/OauthSync/CRM_OauthSync_OAuthHelper.php';
/**
* Implements hook_civicrm_config().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_config
*/
function oauth_sync_civicrm_config(&$config) {
_oauth_sync_civix_civicrm_config($config);
}
/**
* Implements hook_civicrm_xmlMenu().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_xmlMenu
*/
function oauth_sync_civicrm_xmlMenu(&$files) {
_oauth_sync_civix_civicrm_xmlMenu($files);
}
/**
* Implements hook_civicrm_install().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_install
*/
function oauth_sync_civicrm_install() {
_oauth_sync_civix_civicrm_install();
}
/**
* Implements hook_civicrm_postInstall().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_postInstall
*/
function oauth_sync_civicrm_postInstall() {
_oauth_sync_civix_civicrm_postInstall();
}
/**
* Implements hook_civicrm_uninstall().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_uninstall
*/
function oauth_sync_civicrm_uninstall() {
_oauth_sync_civix_civicrm_uninstall();
}
/**
* Implements hook_civicrm_enable().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_enable
*/
function oauth_sync_civicrm_enable() {
_oauth_sync_civix_civicrm_enable();
}
/**
* Implements hook_civicrm_disable().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_disable
*/
function oauth_sync_civicrm_disable() {
_oauth_sync_civix_civicrm_disable();
}
/**
* Implements hook_civicrm_upgrade().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_upgrade
*/
function oauth_sync_civicrm_upgrade($op, CRM_Queue_Queue $queue = NULL) {
return _oauth_sync_civix_civicrm_upgrade($op, $queue);
}
/**
* Implements hook_civicrm_managed().
*
* Generate a list of entities to create/deactivate/delete when this module
* is installed, disabled, uninstalled.
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_managed
*/
function oauth_sync_civicrm_managed(&$entities) {
_oauth_sync_civix_civicrm_managed($entities);
}
/**
* Implements hook_civicrm_caseTypes().
*
* Generate a list of case-types.
*
* Note: This hook only runs in CiviCRM 4.4+.
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_caseTypes
*/
function oauth_sync_civicrm_caseTypes(&$caseTypes) {
_oauth_sync_civix_civicrm_caseTypes($caseTypes);
}
/**
* Implements hook_civicrm_angularModules().
*
* Generate a list of Angular modules.
*
* Note: This hook only runs in CiviCRM 4.5+. It may
* use features only available in v4.6+.
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_angularModules
*/
function oauth_sync_civicrm_angularModules(&$angularModules) {
_oauth_sync_civix_civicrm_angularModules($angularModules);
}
/**
* Implements hook_civicrm_alterSettingsFolders().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_alterSettingsFolders
*/
function oauth_sync_civicrm_alterSettingsFolders(&$metaDataFolders = NULL) {
_oauth_sync_civix_civicrm_alterSettingsFolders($metaDataFolders);
}
/**
* Implements hook_civicrm_entityTypes().
*
* Declare entity types provided by this module.
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_entityTypes
*/
function oauth_sync_civicrm_entityTypes(&$entityTypes) {
_oauth_sync_civix_civicrm_entityTypes($entityTypes);
}
/**
* Implements hook_civicrm_fieldOptions()
*
* Used to keep the list of sync groups up to date
*
* @param string $entity the name of the entity
* @param string $field the field we are looking up
* @param array $options the list of options as a reference to modify
* @param array $params params sent to the lookup field
*/
function oauth_sync_civicrm_fieldOptions($entity, $field, &$options, $params) {
// print_r($field);
// if($entity == 'Group') {
// print $field;
// print "\n";
// print_r($params);
// print "\n";
// print_r($options);
// print "\n";
// CRM_Core_BAO_CustomField::getOptionGroupDefault()
// print CRM_Core_BAO_CustomField::getCustomFieldID("JIRA_Groups", "JIRA_Sync_Settings");
// print "\n";
// }
// $field_id = CRM_Core_BAO_CustomField::getCustomFieldID($field);
// $field = CRM_Core_BAO_CustomField::getFieldObject($field_id);
// print_r($field);
// print_r($options);
// if ($entity == 'Group' && (substr($field,0, 6) == 'custom')) {
// if($field->option_group_id == 'jira_sync_group_options') {
// print_r($field);
// $field_id = CRM_Core_BAO_CustomField::getCustomFieldID($field);
// $field = CRM_Core_BAO_CustomField::getFieldObject($field_id);
// print_r($field);
// print_r($options);
// print 'aaa';
// }
// }
}
// --- Functions below this ship commented out. Uncomment as required. ---
/**
* Implements hook_civicrm_preProcess().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_preProcess
*
function oauth_sync_civicrm_preProcess($formName, &$form) {
} // */
/**
* Implements hook_civicrm_navigationMenu().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_navigationMenu
*
function oauth_sync_civicrm_navigationMenu(&$menu) {
_oauth_sync_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,
));
_oauth_sync_civix_navigationMenu($menu);
} // */
/**
* Implementation of hook_civicrm_pre
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_pre
*/
function oauth_sync_civicrm_pre( $op, $objectName, $id, &$params ) {
// TODO: handle user deletion (plugins must handle their own email matching changes)
}
/**
* Implementation of hook_civicrm_post
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_post
*/
function oauth_sync_civicrm_post( $op, $objectName, $objectId, &$objectRef ) {
// handle change to sync groups
if ($objectName == 'Group' ) {
if ($op == 'edit' || $op == 'create') {
// check if any of our prefixed methods have changed
// we don't need to check for removals as that just means we don't sync them next time a sync action
// is performed.
$customFields = CRM_Core_BAO_CustomValueTable::getEntityValues($objectId, $objectName, NULL, TRUE);
foreach (CRM_OauthSync_OAuthHelper::getHelperArray() as $helper) {
$prefix = $helper->settingsPrefix;
$remoteGroup = CRM_OauthSync_SyncHelper::getInstance($prefix)->getRemoteGroup($objectId, $customFields);
if ($remoteGroup != null) {
// we have groups
CRM_OauthSync_SyncHelper::getInstance($prefix)->syncGroup($objectId, $remoteGroup, false);
}
}
}
} elseif($objectName == 'GroupContact') {
// $localGroupId = CRM_Contact_BAO_GroupContact::getGroupId($objectId);
// this seems to be a bug in civicrm but its giving us the groupId rather than the group contact id
$localGroupId = $objectId;
if($op == 'create' || $op == 'edit') {
foreach (CRM_OauthSync_OAuthHelper::getHelperArray() as $helper) {
$prefix = $helper->settingsPrefix;
$syncHelper = CRM_OauthSync_SyncHelper::getInstance($prefix);
$remoteGroups = $syncHelper->getRemoteGroupsIncludingParents($localGroupId, true, false);
foreach($remoteGroups as $remoteGroup) {
# we don't need to remove any users here
$emptyArray = array();
$toAddRemote = $objectRef;
// add the remote members
CRM_Utils_Hook::singleton()->invoke(
array('remoteGroupName', 'toRemove', 'toAdd'),
$remoteGroup,
$emptyArray,
$toAddRemote,
CRM_Utils_Hook::$_nullObject,
CRM_Utils_Hook::$_nullObject,
CRM_Utils_Hook::$_nullObject,
'civicrm_oauthsync_' . $prefix . '_update_remote_users'
);
}
}
} elseif ($op == 'delete') {
foreach (CRM_OauthSync_OAuthHelper::getHelperArray() as $helper) {
$prefix = $helper->settingsPrefix;
$syncHelper = CRM_OauthSync_SyncHelper::getInstance($prefix);
// if this hook was triggered by a server side delete don't send it back to the server
if($syncHelper->protectedDeleteInProgress) {
continue;
}
$remoteGroups = $syncHelper->getRemoteGroupsIncludingParents($localGroupId, true, false);
foreach($remoteGroups as $remoteGroup) {
# we don't need to remove any users here
$emptyArray = array();
$toRemoveRemote = $objectRef;
// add the remote members
CRM_Utils_Hook::singleton()->invoke(
array('remoteGroupName', 'toRemove', 'toAdd'),
$remoteGroup,
$toRemoveRemote,
$emptyArray,
CRM_Utils_Hook::$_nullObject,
CRM_Utils_Hook::$_nullObject,
CRM_Utils_Hook::$_nullObject,
'civicrm_oauthsync_' . $prefix . '_update_remote_users'
);
}
}
}
}
}