This repository has been archived by the owner on May 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
CiviGroupSync.drush.inc
326 lines (305 loc) · 10.6 KB
/
CiviGroupSync.drush.inc
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
<?php
/**
* CiviGroupSync Drush Command File
* Copyright (C) 2014 Evi Vanoost ([email protected])
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
function CiviGroupSync_drush_command() {
$items = array ();
// Default options for all CGS commands
$defopts = array (
'print' => "Output the resulting group to terminal",
'test' => "Does not commit the changes"
);
// Default arguments for all CGS commands
$defargs = array (
'sourceid' => 'Source Group ID or ID\'s separated with a comma',
'targetid' => 'Target Group ID or ID\'s separated with a comma'
);
$defitem = array (
'arguments' => $defargs,
'required-arguments' => TRUE,
'options' => $defopts
);
// the key in the $items array is the name of the command.
$items ['civigroup-sync'] = array_merge ( array (
'description' => "Sync group(s), deletes entities in the target that are not in the source(s)"
), $defitem );
$items ['civigroup-merge'] = array (
'description' => "Merge group(s), union of source(s) and target(s))"
) + $defitem;
$items ['civigroup-empty'] = array (
'description' => "Empties the group(s), deletes entities from a group)",
'arguments' => array (
'targetid' => 'Target Group ID or ID\'s separated with a comma'
)
) + $defitem;
$items ['civigroup-user-create'] = array (
'description' => "Create Drupal users from a CiviCRM group",
'arguments' => array (
'sourceid' => 'Source Group ID or ID\'s separated with a comma'
)
) + $defitem;
return $items;
}
function CiviGroupSync_user_search($email, $name) {
// We use a DB query while looking for the uid to keep things speedy.
$uids = array ();
if (drush_drupal_major_version () >= 7) {
$uid_query = db_query ( "SELECT uid, name FROM {users} WHERE mail = :mail OR name = :name", array (
':mail' => $email,
':name' => $name
) );
} else {
$uid_query = db_query ( "SELECT uid, name FROM {users} WHERE mail = '%s' OR name = '%s'", $email, $name );
}
while ( $uid = drush_db_fetch_object ( $uid_query ) ) {
$uids [$uid->uid] = $uid->name;
}
return (count ( $uids ));
}
function drush_CiviGroupSync_civigroup_user_create($groupids) {
$groups = CiviGroupSync_split ( $groupids );
if (empty ( $groups )) {
drush_set_error ( "Error validating groups for user create" );
return false;
}
civicrm_initialize ();
$final_group = array ();
foreach ( $groups as $groupid ) {
// Get all the entities from these groups
$final_group += CiviGroupSync_getGroupContactDetails ( $groupid );
}
foreach ( $final_group as $entity ) {
if ($error = user_validate_mail ( $entity ['email'] )) {
drush_print ( $entity ['email'] . ": " . $error );
continue;
} elseif ($error = user_validate_name ( $entity ['nick_name'] )) {
drush_print ( $entity ['nick_name'] . ": " . $error );
continue;
} elseif (CiviGroupSync_user_search ( $entity ['email'], $entity ['nick_name'] ) != 0) {
if ($print = drush_get_option ( 'print' )) {
drush_print ( "A user account for " . $entity ['nick_name'] . " already exists" );
}
continue;
}
$new_user = array (
'name' => $entity ['nick_name'],
'mail' => $entity ['email'],
'pass' => user_password ( 10 ),
'status' => 1,
'init' => $entity ['email']
);
if (! drush_get_option ( 'test' )) {
if ($account = user_save ( null, $new_user )) {
if ($print = drush_get_option ( 'print' )) {
drush_print ( "Account created: " . print_r ( $account, 1 ) );
}
_user_mail_notify ( 'register_admin_created', $account );
} else {
drush_print ( "Could not create a new user account with the name " . $entity ['nick_name'] . "." );
}
} else {
drush_print ( "Account created (test): " . $entity ['nick_name'] );
}
}
}
function drush_CiviGroupSync_civigroup_sync($sourceids, $targetids) {
$sources = CiviGroupSync_split ( $sourceids );
$targets = CiviGroupSync_split ( $targetids );
if (empty ( $sources ) || empty ( $targets )) {
drush_set_error ( "Error validating groups for merge" );
return false;
}
civicrm_initialize ();
$final_group = array ();
// For each of the sources
foreach ( $sources as $sourceid ) {
// Get all the entities from this source group
$final_group = array_merge ( CiviGroupSync_getGroupContact ( $sourceid ), $final_group );
}
// Make sure we only get unique values
$final_group = array_unique ( $final_group );
foreach ( $targets as $targetid ) {
// Get the current target group
$this_group = CiviGroupSync_getGroupContact ( $targetid );
// Find out if there are any entities in our target that are to be deleted
$tobedeleted = array_diff ( $this_group, $final_group );
// Delete entities to be deleted
foreach ( $tobedeleted as $target_entity ) {
CiviGroupSync_deleteGroupContact ( $targetid, $target_entity );
}
// Remove any source entities that are already in the group
$this_final_group = array_diff ( $final_group, $this_group );
foreach ( $this_final_group as $target_entity ) {
// Create a contact in the group for each contact ID we have left
CiviGroupSync_createGroupContact ( $targetid, $target_entity );
}
// If we need to print out the result, fetch the group and print it
if ($print = drush_get_option ( 'print' )) {
drush_print ( "Resulting Group: " . json_encode ( CiviGroupSync_getGroupContact ( $targetid ) ) );
}
}
}
function CiviGroupSync_getGroupContact($groupid) {
$return = array ();
if ($result = CiviGroupSync_getGroupContactDetails ( $groupid )) {
foreach ( $result as $entity ) {
$return [] = $entity ["id"];
}
}
return $return;
}
function CiviGroupSync_getGroupContactDetails($groupid) {
// First find out the type of group
try {
// entity=GroupContact&action=get&group_id=4
$result = civicrm_api ( 'Group', 'get', array (
'id' => $groupid,
'version' => 3
) );
} catch ( CiviCRM_API3_Exception $e ) {
$errorMessage = $e->getMessage ();
$errorCode = $e->getErrorCode ();
$errorData = $e->getExtraParams ();
drush_log ( dt ( "!error", array (
'!error' => $errorData
), 'error' ) );
return;
}
if ($result ['count'] != 1) {
drush_print ( "Invalid group specified" );
return false;
}
// This is probably more expensive than doing it the other way:
// entity=Contact&action=get&debug=1&sequential=1&json=1&group=2
try {
// entity=GroupContact&action=get&group_id=4
$result = civicrm_api ( 'Contact', 'get', array (
'group' => $groupid,
'version' => 3,
'return' => 'nick_name,email',
'options' => array (
'limit' => 0
)
) );
} catch ( CiviCRM_API3_Exception $e ) {
$errorMessage = $e->getMessage ();
$errorCode = $e->getErrorCode ();
$errorData = $e->getExtraParams ();
drush_log ( dt ( "!error", array (
'!error' => $errorData
), 'error' ) );
return;
}
return $result ['values'];
}
function CiviGroupSync_createGroupContact($groupid, $contactid) {
if (drush_get_option ( 'print' )) {
drush_print ( "Adding User ID $contactid to Group ID $groupid" );
}
if (drush_get_option ( 'test' )) {
return true;
}
try {
// entity=GroupContact&action=create&group_id=4&contact_id=
$result = civicrm_api ( 'GroupContact', 'create', array (
'contact_id' => $contactid,
'group_id' => $groupid,
'version' => 3
) );
} catch ( CiviCRM_API3_Exception $e ) {
$errorMessage = $e->getMessage ();
$errorCode = $e->getErrorCode ();
$errorData = $e->getExtraParams ();
drush_log ( dt ( "!error", array (
'!error' => $errorData
), 'error' ) );
return false;
}
return true;
}
function CiviGroupSync_deleteGroupContact($groupid, $contactid) {
if (drush_get_option ( 'test' )) {
drush_print ( "Removing User ID $contactid from Group ID $groupid" );
}
if (drush_get_option ( 'test' )) {
return true;
}
try {
// entity=GroupContact&action=create&group_id=4&contact_id=
$result = civicrm_api ( 'GroupContact', 'delete', array (
'contact_id' => $contactid,
'group_id' => $groupid,
'version' => 3
) );
} catch ( CiviCRM_API3_Exception $e ) {
$errorMessage = $e->getMessage ();
$errorCode = $e->getErrorCode ();
$errorData = $e->getExtraParams ();
drush_log ( dt ( "!error", array (
'!error' => $errorData
), 'error' ) );
return false;
}
return true;
}
function drush_CiviGroupSync_civigroup_merge($sourceids, $targetids) {
$sources = CiviGroupSync_split ( $sourceids );
$targets = CiviGroupSync_split ( $targetids );
if (empty ( $sources ) || empty ( $targets )) {
drush_set_error ( "Error validating groups for merge" );
return false;
}
civicrm_initialize ();
$final_group = array ();
// For each of the sources
foreach ( $sources as $sourceid ) {
// Get all the entities from this source group
$final_group = array_merge ( CiviGroupSync_getGroupContact ( $sourceid ), $final_group );
}
// Make sure we only get unique values
$final_group = array_unique ( $final_group );
// For each of the targets, "merge" them with the final group and save them
foreach ( $targets as $targetid ) {
// First we need to fetch the entities already in this group (so we don't add an already existing entity)
$this_group = CiviGroupSync_getGroupContact ( $targetid );
// Filter out the already existing entities
$this_final_group = array_diff ( $final_group, $this_group );
// Create a contact in the group for each contact ID we have left
foreach ( $this_final_group as $contactid ) {
CiviGroupSync_createGroupContact ( $targetid, $contactid );
}
// If we need to print out the result, fetch the group and print it
if ($print = drush_get_option ( 'print' )) {
drush_print ( "Resulting Group:" . json_encode ( CiviGroupSync_getGroupContact ( $targetid ) ) );
}
}
}
function drush_CiviGroupSync_civigroup_empty($targetid) {
// TODO: Implement deleting the contents of a group
}
function CiviGroupSync_split($groups) {
$groups = explode ( ",", $groups );
foreach ( $groups as $groupid ) {
if (! is_numeric ( $groupid )) {
drush_set_error ( "Invalid Group ID specified" );
return false;
}
}
return $groups;
}
?>