forked from eileenmcnaughton/civicrm_entity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
civicrm_entity.rules.inc
650 lines (626 loc) · 21.1 KB
/
civicrm_entity.rules.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
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
<?php
/**
* @file
* Implement Drupal Rules integration for CiviCRM
*/
/**
* Implements of hook_rules_data_type_info().
*/
function civicrm_entity_rules_data_info() {
if (!civicrm_initialize(TRUE)) {
return;
}
module_load_include('module', 'civicrm_entity', 'civicrm_entity');
$info = array();
$entities = _civicrm_entity_enabled_entities();
foreach ($entities as $drupal_entity => $civicrm_entity) {
$info[$drupal_entity] = array(
'label' => t('CiviCRM @entity', array('@entity' => $civicrm_entity)),
// parent => 'civicrm_entity',
'wrap' => TRUE,
'group' => 'CiviCRM',
// 'ui class' => 'RulesDataUIEntityExportable',
'module' => 'civicrm',
);
$info[$drupal_entity]['property_info'] = _civicrm_entity_getproperties($civicrm_entity);
// I don't think these are being 'kept'.
$fields = civicrm_api($civicrm_entity, 'getfields', array(
'version' => 3,
'action' => 'create',
));
foreach ($fields['values'] as $fieldname => $field_specs) {
if (!empty($field_specs['type']) && $field_specs['type'] == 1) {
// Type is empty for custom fields - we should sort that out
// but skipping for now.
if (CRM_Utils_Array::Value('FKClassName', $field_specs)) {
$fks = _civicrm_entity_chained_fks();
if (array_key_exists($field_specs['FKClassName'], $fks)) {
$fks_entity = $fks[$field_specs['FKClassName']];
$info[$drupal_entity]['property_info'][$fieldname] = array(
'label' => empty($field_specs['title']) ? 'label not defined in CiviCRM schema' : $field_specs['title'],
'type' => 'civicrm_' . $fks_entity,
);
}
}
if ($fieldname == 'entity_id') {
$fks_entity = 'contact';
$info[$drupal_entity]['property_info'][$fieldname] = array(
'label' => empty($field_specs['title']) ? 'label not defined in CiviCRM schema' : $field_specs['title'],
'type' => 'civicrm_' . $fks_entity,
);
}
$info[$drupal_entity]['property_info'][$fieldname] = array(
'label' => empty($field_specs['title']) ? 'label not defined in CiviCRM schema' : $field_specs['title'],
'type' => 'integer',
);
if (!empty($field_specs['options'])) {
$info[$drupal_entity]['property_info'][$fieldname]['type'] = 'list<integer>';
$info[$drupal_entity]['property_info'][$fieldname]['options list'] = '_civicrm_entity_rules_attach_options';
$info[$drupal_entity]['property_info'][$fieldname]['options data'] = $field_specs['options'];
}
}
}
}
return $info;
}
/**
* Implements hook_rules_event_info().
*/
function civicrm_entity_rules_event_info() {
$events = array();
$valid_objects = _civicrm_entity_enabled_entities();
// array('Event' => 'civicrm_event');
if (is_array($valid_objects)) {
foreach ($valid_objects as $entity => $civicrm_entity) {
$entity_name = 'CiviCRM ' . ucwords(str_replace('_', ' ', $civicrm_entity));
// @TODO consider building the entity name into the argument
// rather than calling the same argument for each.
$create_msg = t('Created !entity', array('!entity' => $entity_name));
$events[$entity . '_create'] = array(
'label' => t("!entity has been created", array('!entity' => $entity_name)),
'group' => $entity_name,
'module' => 'civicrm',
'access_callback' => '_civicrm_entity_rules_access',
'variables' => civicrm_entity_rules_events_variables($create_msg, $entity, $civicrm_entity),
);
$update_msg = t('Updated !entity', array('!entity' => $entity_name));
$events[$entity . '_edit'] = array(
'group' => $entity_name,
'module' => 'civicrm',
'access_callback' => '_civicrm_entity_rules_access',
'label' => t("!entity has been updated", array('!entity' => $entity_name)),
'variables' => civicrm_entity_rules_events_variables($update_msg, $entity),
);
$view_msg = t('Viewed !entity', array('!entity' => $entity_name));
$events[$entity . '_view'] = array(
'group' => $entity_name,
'module' => 'civicrm',
'access_callback' => '_civicrm_entity_rules_access',
'label' => t("!entity has been viewed", array('!entity' => $entity_name)),
'variables' => civicrm_entity_rules_events_variables($view_msg, $entity),
);
$delete_msg = t('Deleted !entity', array('!entity' => $entity_name));
$events[$entity . '_delete'] = array(
'group' => $entity_name,
'module' => 'civicrm',
'access_callback' => '_civicrm_entity_rules_access',
'label' => t("!entity has been deleted", array('!entity' => $entity_name)),
'variables' => civicrm_entity_rules_events_variables($delete_msg, $entity),
);
}
}
return $events;
}
/**
* Implements hook_rules_condition_info().
*
* @TODO I don't think the provides array is supported for conditions?
* Remove.
*/
function civicrm_entity_rules_condition_info() {
$conditions = array(
'civicrm_entity_user_exists' => array(
'label' => t('Drupal User Account exists for Contact'),
'module' => 'civicrm',
'access_callback' => '_civicrm_entity_rules_access',
'parameter' => array(
'civicrm_contact' => array(
'label' => t('CiviCRM Contact'),
'type' => 'civicrm_contact',
),
),
'provides' => array(
'civicrm_user' => array(
'label' => t('Drupal User'),
'type' => 'user',
),
),
'group' => t('CiviCRM'),
),
'civicrm_entity_user_creatable' => array(
'label' => t('Drupal User Account can be created for user (this condition creates the user)'),
'module' => 'civicrm',
'access_callback' => '_civicrm_entity_rules_access',
'parameter' => array(
'civicrm_contact' => array(
'label' => t('CiviCRM Contact'),
'type' => 'civicrm_contact',
),
'is_active' => array(
'label' => t('Activate Account'),
'type' => 'boolean',
'default' => 1,
),
'notify' => array(
'label' => t('Send account notification email'),
'type' => 'boolean',
'default' => 1,
),
'signin' => array(
'label' => t('Instant signin'),
'type' => 'boolean',
'default' => FALSE,
'description' => t('Automatically log in as the created user.'),
),
),
'provides' => array(
'civicrm_user' => array(
'label' => t('Drupal User'),
'type' => 'user',
),
),
'group' => t('CiviCRM'),
),
'civicrm_entity_user_exists_or_creatable' => array(
'label' => t('Drupal User Account exists or can be created for user'),
'module' => 'civicrm',
'access_callback' => '_civicrm_entity_rules_access',
'parameter' => array(
'civicrm_contact' => array(
'label' => t('CiviCRM Contact'),
'type' => 'civicrm_contact',
),
),
'provides' => array(
'civicrm_user' => array(
'label' => t('Drupal User'),
'type' => 'user',
),
),
'group' => t('CiviCRM'),
),
'civicrm_entity_query' => array(
'label' => t('Can Fetch CiviCRM entity by property'),
'module' => 'civicrm',
'access_callback' => '_civicrm_entity_rules_access',
'parameter' => array(
'type' => array(
'type' => 'text',
'label' => t('CiviCRM Entity type'),
'options list' => '_civicrm_entity_enabled_entities',
'description' => t('Specifies the type of CiviCRM entity that should be fetched.'),
'restriction' => 'input',
),
'property' => array(
'type' => 'text',
'label' => t('Property'),
'description' => t('The property by which the entity is to be selected.'),
'restriction' => 'input',
),
'value' => array(
'type' => 'unknown',
'label' => t('Value'),
'description' => t('The property value of the entity to be fetched.'),
),
'limit' => array(
'type' => 'integer',
'label' => t('Limit result count'),
'description' => t('Limit the maximum number of fetched entities.'),
'optional' => TRUE,
'default value' => '10',
),
),
'group' => t('CiviCRM'),
'callbacks' => array(
'form_alter' => 'rules_action_type_form_alter',
),
'provides' => array(
'entity_fetched' => array(
'type' => 'list',
'label' => t('Fetched CiviCRM entity list (being ignored?)'),
),
),
'base' => 'civicrm_entity_query',
),
);
//@todo add phone as an entity & add it here, also - I had to remove address as II was less sure about the right approach as I went along
foreach (array('email') as $address_entity) {
$conditions['civicrm_entity_contact_has' . $address_entity] = array(
'group' => t('CiviCRM'),
'label' => t('CiviCRM Contact has ' . ucfirst($address_entity)),
'module' => 'civicrm',
'access_callback' => '_civicrm_entity_rules_access',
'parameter' => array(
'contact' => array(
'type' => 'civicrm_contact',
'label' => t('CiviCRM Contact'),
'description' => t('The CiviCRM Contact to fetch for'),
),
'location' => array(
'type' => 'integer',
'label' => t('Location'),
'description' => t('The property by which the entity is to be selected.'),
'options list' => 'civicrm_entity_get_locations',
),
),
'provides' => array(
'created_' . $address_entity => array(
'type' => 'civicrm_' . $address_entity,
'label' => t('CiviCRM ' . ucfirst($address_entity)),
),
),
'base' => 'civicrm_entity_contact_has_location_element_' . $address_entity,
);
}
// Is contact in group condition
$conditions['civicrm_entity_contact_is_in_group'] = array(
'label' => t('CiviCRM Contact is in Group'),
'access_callback' => '_civicrm_entity_rules_access',
'parameter' => array(
'civicrm_contact' => array(
'label' => t('CiviCRM Contact'),
'type' => 'civicrm_contact',
),
'group_id' => array(
'type' => 'text',
'label' => t('Group'),
'options list' => 'civicrm_entity_rules_assign_contact_group_options_list',
'description' => t('Select the CiviCRM group'),
'restriction' => 'input',
),
),
'provides' => array(
'civicrm_group' => array(
'label' => t('CiviCRM Group'),
'type' => 'civicrm_group',
),
),
'group' => t('CiviCRM'),
);
$conditions['civicrm_entity_contact_is_subtype'] = array(
'label' => t('CiviCRM Contact is of subtype'),
'access_callback' => '_civicrm_entity_rules_access',
'parameter' => array(
'civicrm_contact' => array(
'label' => t('CiviCRM Contact'),
'type' => 'civicrm_contact',
),
'contact_subtype' => array(
'type' => 'text',
'label' => t('Contact Sub Type'),
'options list' => 'civicrm_entity_rules_contact_subtype_options_list',
'description' => t('Select the Contact Sub Type'),
'restriction' => 'input',
),
),
'group' => t('CiviCRM'),
);
return $conditions;
}
/**
* Implements hook_rules_action_info().
*
* @see http://drupalcontrib.org/api/drupal/contributions!rules!rules.api.php/function/hook_rules_action_info/7
*/
function civicrm_entity_rules_action_info() {
$actions = array(
'civicrm_entity_action_load_user' => array(
'label' => t('Load Drupal User Account'),
'module' => 'civicrm',
'access_callback' => '_civicrm_entity_rules_access',
'parameter' => array(
'civicrm_contact' => array(
'label' => t('CiviCRM Contact'),
'type' => 'civicrm_contact',
),
),
'provides' => array(
'civicrm_user' => array(
'label' => t('Loaded Drupal User'),
'type' => 'user',
),
),
'group' => t('CiviCRM'),
'access callback' => '_civicrm_entity_rules_access',
'base' => 'civicrm_entity_action_load_user',
),
'civicrm_entity_action_create_user' => array(
'label' => t('Create Linked Drupal User Account'),
'module' => 'civicrm',
'access_callback' => '_civicrm_entity_rules_access',
'parameter' => array(
'civicrm_contact' => array(
'label' => t('CiviCRM Contact'),
'type' => 'civicrm_contact',
),
'is_active' => array(
'label' => t('Activate Account'),
'type' => 'boolean',
'default' => 1,
),
'notify' => array(
'label' => t('Send account notification email'),
'type' => 'boolean',
'default' => 1,
),
'signin' => array(
'label' => t('Instant signin'),
'type' => 'boolean',
'default' => FALSE,
'description' => t('Automatically log in as the created user.'),
),
'username_format' => array(
'label' => t('Username Format'),
'type' => 'text',
'default' => 'first last',
'options list' => '_civicrm_entity_options_username_format',
'description' => t('Preferred username format'),
),
),
'provides' => array(
'civicrm_user' => array(
'label' => t('Created Drupal User'),
'type' => 'user',
),
),
'group' => t('CiviCRM'),
'base' => 'civicrm_entity_action_create_user',
),
'civicrm_entity_action_load_create_user' => array(
'label' => t('Create or Load Linked Drupal User Account'),
'module' => 'civicrm',
'access_callback' => '_civicrm_entity_rules_access',
'parameter' => array(
'civicrm_contact' => array(
'label' => t('CiviCRM Contact'),
'type' => 'civicrm_contact',
),
'is_active' => array(
'label' => t('Activate Account'),
'type' => 'boolean',
'default' => 1,
),
'notify' => array(
'label' => t('Send account notification email'),
'type' => 'boolean',
'default' => 1,
),
),
'provides' => array(
'civicrm_user' => array(
'label' => t('Created or Loaded Drupal User'),
'type' => 'user',
),
),
'group' => t('CiviCRM'),
'base' => 'civicrm_entity_action_load_create_user',
),
'civicrm_entity_query' => array(
'label' => t('Fetch CiviCRM entity by property'),
'module' => 'civicrm',
'access_callback' => '_civicrm_entity_rules_access',
'parameter' => array(
'type' => array(
'type' => 'text',
'label' => t('CiviCRM Entity type'),
'options list' => '_civicrm_entity_enabled_entities',
'description' => t('Specifies the type of CiviCRM entity that should be fetched.'),
'restriction' => 'input',
),
'property' => array(
'type' => 'text',
'label' => t('Property'),
'description' => t('The property by which the entity is to be selected.'),
'restriction' => 'input',
),
'value' => array(
'type' => 'unknown',
'label' => t('Value'),
'description' => t('The property value of the entity to be fetched.'),
),
'limit' => array(
'type' => 'integer',
'label' => t('Limit result count'),
'description' => t('Limit the maximum number of fetched entities.'),
'optional' => TRUE,
'default value' => '10',
),
),
'group' => t('CiviCRM'),
'callbacks' => array(
'form_alter' => 'rules_action_type_form_alter',
),
'provides' => array(
'entity_fetched' => array(
'type' => 'list',
'label' => t('Fetched CiviCRM entity list'),
),
),
'base' => 'civicrm_entity_query',
),
'civicrm_entity_action_load_create_contact' => array(
'label' => t('Create or Load Linked CiviCRM Contact'),
'module' => 'civicrm',
'access_callback' => '_civicrm_entity_rules_access',
'parameter' => array(
'user' => array(
'label' => t('Drupal User'),
'type' => 'user',
),
'email' => array(
'label' => t('email'),
'type' => 'email',
'optional' => TRUE,
),
),
'provides' => array(
'civicrm_contact' => array(
'label' => t('Created or Loaded CiviCRM Contact'),
'type' => 'civicrm_contact',
),
),
'group' => t('CiviCRM'),
'base' => 'civicrm_entity_action_load_create_contact',
),
);
$actions['civicrm_entity_contact_get_email'] = array(
'group' => t('CiviCRM'),
'label' => t('CiviCRM Contact get Email'),
'module' => 'civicrm',
'access_callback' => '_civicrm_entity_rules_access',
'parameter' => array(
'contact' => array(
'type' => 'civicrm_contact',
'label' => t('CiviCRM Contact'),
'description' => t('The CiviCRM Contact to fetch for'),
),
'location' => array(
'type' => 'integer',
'label' => t('Location'),
'description' => t('The property by which the entity is to be selected.'),
'options list' => 'civicrm_entity_get_locations',
),
),
'provides' => array(
'fetched_email' => array(
'type' => 'text',
'label' => t('Email'),
),
),
'base' => 'civicrm_entity_contact_get_email',
);
$actions['civicrm_entity_contact_send_email'] = array(
'group' => t('CiviCRM'),
'label' => t('CiviCRM Contact Send Email'),
'module' => 'civicrm',
'access_callback' => '_civicrm_entity_rules_access',
'parameter' => array(
'contact' => array(
'type' => 'civicrm_contact',
'label' => t('CiviCRM Contact'),
'description' => t('The CiviCRM Contact to email'),
),
'subject' => array(
'type' => 'text',
'label' => t('Subject'),
'description' => t("The mail\'s subject."),
),
'message_text' => array(
'type' => 'text',
'label' => t('Message'),
'description' => t("The mail\'s message body - text version."),
'optional' => TRUE,
),
'message_html' => array(
'type' => 'text',
'label' => t('Message'),
'description' => t("The mail\'s message body - html version."),
'optional' => TRUE,
),
'from' => array(
'type' => 'text',
'label' => t('From'),
'description' => t("The mail's from address. Leave it empty to use the site-wide configured address."),
'optional' => TRUE,
),
),
'base' => 'civicrm_entity_contact_send_email',
);
$actions['civicrm_entity_rules_action_assign_contact_to_group'] = array(
'label' => t('Assign CiviCRM Contact to Group'),
'group' => t('CiviCRM Contact'),
'parameter' => array(
'contact' => array (
'type' => 'civicrm_contact',
'label' => t('CiviCRM Contact'),
'description' => t('CiviCRM Contact entity'),
'wrapped' => TRUE,
),
'group_id' => array (
'type' => 'list<text>',
'label' => t('CiviCRM Group'),
'description' => t('Choose a group(s) to assign the contact to.'),
'options list' => 'civicrm_entity_rules_assign_contact_group_options_list',
),
),
);
$actions['civicrm_entity_rules_action_unassign_contact_to_group'] = array(
'label' => t('Unassign CiviCRM Contact from Group'),
'group' => t('CiviCRM Contact'),
'parameter' => array(
'contact' => array (
'type' => 'civicrm_contact',
'label' => t('CiviCRM Contact'),
'description' => t('CiviCRM Contact entity'),
'wrapped' => TRUE,
),
'group_id' => array (
'type' => 'list<text>',
'label' => t('CiviCRM Group'),
'description' => t('Choose a group(s) to unassign the contact from.'),
'options list' => 'civicrm_entity_rules_assign_contact_group_options_list',
),
),
);
return $actions;
}
/**
* User access function.
*
* @param String $type
* Ignored.
*
* @param String $name
* Ignored.
*
* @return bool
*/
function civicrm_entity_rules_access($type, $name) {
return user_access('civicrm_entity.rules.administer');
}
/**
* Please document this function.
*
* @param $label
* @param $type
* @param null $civicrm_entity
*
* @return array
*/
function civicrm_entity_rules_events_variables($label, $type, $civicrm_entity = NULL) {
$vars = array();
$default = array(
$type => array(
'type' => $type,
'label' => $label,
'description' => t('CiviCRM Entity - @label', array('@label' => $label)),
),
);
return $default;
}
/**
* Add some access params! @TODO.
*/
function _civicrm_entity_rules_access() {
return TRUE;
}
/**
* Return options list.
* @param $fieldname
* @param $info
* @param $arg3
* @return
*/
function _civicrm_entity_rules_attach_options($fieldname, $info, $arg3) {
return $info['property defaults']['options list'];
}