-
Notifications
You must be signed in to change notification settings - Fork 0
/
invite.module
952 lines (852 loc) · 26.1 KB
/
invite.module
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
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
<?php
/**
* @file
*/
const INVITE_SESSION_CODE = 'invite_code';
const INVITE_SESSION_FAILED = 'invite_failed';
const INVITE_SESSION_ADMIN = 'invite_admin_filter';
/**
* Validity constants.
*/
const INVITE_VALID = 1;
const INVITE_WITHDRAWN = 2;
const INVITE_USED = 3;
const INVITE_EXPIRED = 4;
// @TODO
// - "canceled" is obsolete with "status", remove it.
/**
* Registration settings form values.
*/
define('USER_REGISTER_INVITATION_ONLY', 3);
/**
* Implements hook_entity_info().
*/
function invite_entity_info() {
$return = array(
'invite' => array(
'label' => t('Invite'),
'entity class' => 'Invite',
'controller class' => 'InviteController',
'base table' => 'invite',
'fieldable' => TRUE,
'entity keys' => array(
'id' => 'iid',
'bundle' => 'type',
),
'bundle keys' => array(
'bundle' => 'type',
),
'bundles' => array(),
'load hook' => 'invite_load',
'view modes' => array(
'full' => array(
'label' => t('Default'),
'custom settings' => FALSE,
),
),
'label callback' => 'entity_class_label',
'uri callback' => 'entity_class_uri',
'module' => 'invite',
'access callback' => 'invite_access',
'metadata controller class' => 'InviteMetadataController',
),
);
$return['invite_type'] = array(
'label' => t('Invite Type'),
'entity class' => 'InviteType',
'controller class' => 'InviteTypeController',
'base table' => 'invite_type',
'fieldable' => FALSE,
'bundle of' => 'invite',
'exportable' => TRUE,
'entity keys' => array(
'id' => 'itid',
'name' => 'type',
'label' => 'label',
),
'module' => 'invite',
// Enable the entity API's admin UI.
'admin ui' => array(
'path' => 'admin/structure/invite-types',
'file' => 'includes/invite.admin.inc',
'controller class' => 'InviteTypeUIController',
),
'access callback' => 'invite_type_access',
);
return $return;
}
/**
* Implements hook_entity_info_alter().
*/
function invite_entity_info_alter(&$entity_info) {
foreach (invite_get_types() as $type => $info) {
$entity_info['invite']['bundles'][$type] = array(
'label' => $info->label,
'admin' => array(
'path' => 'admin/structure/invite-types/manage/%invite_type',
'real path' => 'admin/structure/invite-types/manage/' . $type,
'bundle argument' => 4,
),
);
}
}
/**
* Implements hook_block_info().
*/
function invite_block_info() {
$blocks = array();
foreach (invite_get_types() as $type => $info) {
$blocks['invite_add_' . $type] = array(
'info' => t('Create invite @type.', array('@type' => $info->label)),
);
}
return $blocks;
}
/**
* Implements hook_block_view().
*/
function invite_block_view($delta) {
$type = str_replace('invite_add_', '', $delta);
// Return empty block for users who have no appropriate permissions.
if (!(user_access('create any invite entities') || user_access('create ' . $type . ' entity'))) {
return NULL;
}
module_load_include('inc', 'invite', 'includes/invite.admin');
$invite_type = invite_get_types($type);
$invite = entity_create('invite', array('type' => $type));
$block['subject'] = t('Create @name', array('@name' => entity_label('invite_type', $invite_type)));
$block['content'] = drupal_get_form('invite_form', $invite);
return $block;
}
/**
* Implements hook_menu().
*/
function invite_menu() {
$items = array();
$items['invite/add'] = array(
'title' => 'Add Invite',
'page callback' => 'invite_admin_add_page',
'access arguments' => array('create any invite entities'),
'file' => 'includes/invite.admin.inc',
'type' => MENU_LOCAL_ACTION,
'tab_parent' => 'invite',
'tab_root' => 'invite',
);
$invite_uri = 'invite/%invite';
$invite_uri_argument_position = 1;
$items[$invite_uri] = array(
'title callback' => 'entity_label',
'title arguments' => array('invite', $invite_uri_argument_position),
'page callback' => 'invite_view',
'page arguments' => array($invite_uri_argument_position),
'access callback' => 'entity_access',
'access arguments' => array('view', 'invite', $invite_uri_argument_position),
'file' => 'includes/invite.pages.inc',
);
$items[$invite_uri . '/view'] = array(
'title' => 'View',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items[$invite_uri . '/delete'] = array(
'title' => 'Delete invite',
'page callback' => 'drupal_get_form',
'page arguments' => array('invite_delete_form', $invite_uri_argument_position),
'access callback' => 'entity_access',
'access arguments' => array('edit', 'invite', $invite_uri_argument_position),
'file' => 'includes/invite.admin.inc',
);
$items[$invite_uri . '/edit'] = array(
'title' => 'Edit',
'page callback' => 'backdrop_get_form',
'page arguments' => array('invite_form', $invite_uri_argument_position),
'access callback' => 'entity_access',
'access arguments' => array('edit', 'invite', $invite_uri_argument_position),
'file' => 'includes/invite.admin.inc',
'type' => MENU_LOCAL_TASK,
'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
);
foreach (invite_get_types() as $type => $info) {
$items['invite/add/' . $type] = array(
'title' => 'Add invite',
'page callback' => 'invite_add',
'page arguments' => array(2),
'access callback' => 'invite_access',
'access arguments' => array('create', $type),
'file' => 'includes/invite.admin.inc',
);
}
// Admin menu items.
$items['admin/config/people/invite'] = array(
'title' => 'Invite',
'description' => 'Modify invitation settings.',
'page callback' => 'backdrop_get_form',
'page arguments' => array('invite_settings_form'),
'access arguments' => array('administer invitations'),
'type' => MENU_NORMAL_ITEM,
'weight' => 10,
'file' => 'includes/invite.admin.inc',
);
$items['admin/config/people/invite/common'] = array(
'title' => 'Invite',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => 10,
);
// Frontend menu items.
$weight = 20;
foreach (invite_get_types() as $type => $info) {
$items['user/%/invites/' . $type] = array(
'title' => $info->label,
'page callback' => 'invite_add',
'page arguments' => array(3),
'access callback' => 'invite_access',
'access arguments' => array('create', $type),
'file' => 'includes/invite.admin.inc',
'type' => MENU_LOCAL_TASK,
'weight' => $weight++,
);
}
$items['invite/accept/%invite_by_code'] = array(
'page callback' => 'invite_accept',
'page arguments' => array(2),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
'file' => 'includes/invite.pages.inc',
);
$items['invite/withdraw/%invite_by_code'] = array(
'page callback' => 'backdrop_get_form',
'page arguments' => array('invite_withdraw_form', 2),
'access callback' => 'invite_withdraw_access',
'access arguments' => array(2),
'type' => MENU_CALLBACK,
'file' => 'includes/invite.pages.inc',
);
$items['invite/resend/%invite_by_code'] = array(
'title' => 'Resend invitation',
'page callback' => 'invite_resend',
'page arguments' => array(2),
'access callback' => 'invite_resend_access',
'access arguments' => array(2),
'type' => MENU_CALLBACK,
'file' => 'includes/invite.pages.inc',
);
if (config_get('invite.settings','invite_version_updated')) {
$items['admin/config/people/invite/migrate'] = array(
'title' => 'Migrate',
'description' => 'Migrate version 2.x invites.',
'page callback' => 'backdrop_get_form',
'page arguments' => array('invite_migrate_form'),
'access arguments' => array('administer invitations'),
'type' => MENU_LOCAL_TASK,
'weight' => 20,
'file' => 'includes/invite.admin.inc',
);
}
return $items;
}
/**
* Alter Account Settings form.
*
* Add new registration mode 'by invitation only'. By prepending the
* option value with a numeric value, other modules still work as
* expected, as long as they are using the non-strict PHP comparison
* operator (since '1-inviteonly' == 1 yields TRUE). To determine the real
* setting use invite_user_registration_by_invite_only().
*
* However, setting the new mode is only allowed if no other module
* has overridden the menu access handler for the user registration form.
*
* @param array $form
* Array with form.
* @param array $form_state
* Array with form state.
* @param string $form_id
* The ID of form.
*/
function invite_form_user_admin_settings_alter(&$form, &$form_state, $form_id) {
$item = menu_get_item('user/register');
if (in_array($item['access_callback'], array('user_register_access', 'invite_user_register_access'))) {
$form['registration_cancellation']['user_register']['#options'][USER_REGISTER_INVITATION_ONLY] = t('Invitees only');
}
// Clear menu cache on submit to allow our custom access handler to snap in.
$form['#submit'][] = 'menu_rebuild';
}
/**
* Implements hook_menu_alter().
*
* Override the user/register menu access handler with a custom
* implementation.
*/
function invite_menu_alter(&$items) {
if (invite_user_registration_by_invite_only()) {
$items['user/register']['access callback'] = 'invite_user_register_access';
}
}
/**
* Determine if user registration mode is set to invite only.
*/
function invite_user_registration_by_invite_only() {
/* The default for the user_register setting is actually the Backdrop constant
* USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL, which is defined as the
* value 2.
*/
return (config_get('invite.settings','user_register') == USER_REGISTER_INVITATION_ONLY);
}
/**
* Access callback; determine access to user registration form.
*/
function invite_user_register_access() {
$invite = invite_load_from_session();
if ((empty($invite) || invite_validate($invite) != INVITE_VALID) && !user_access('administer users')) {
return FALSE;
}
// Let the default handler take care of standard conditions.
return user_register_access();
}
/**
* Checks the status of an invite.
*
* @param object $invite
* An invite object as returned by invite_load().
*
* @return int
* The constant corresponding to the status of the invite.
*/
function invite_validate($invite) {
if (!$invite || $invite->canceled != 0) {
return INVITE_WITHDRAWN;
}
elseif ($invite->joined != 0) {
return INVITE_USED;
}
elseif ($invite->expiry < REQUEST_TIME) {
return INVITE_EXPIRED;
}
else {
return INVITE_VALID;
}
}
/**
* Access callback for invite type.
*
* @param object $invite
* Either an invite entity or in case $op is 'create' either an entity or
* an invite type name.
*
* @return bool
* TRUE/FALSE for access.
*
* @see entity_access()
*/
function invite_access($op, $invite = NULL, $account = NULL, $entity_type = NULL) {
global $user;
if (!isset($account)) {
$account = $user;
}
switch ($op) {
case 'create':
$type = isset($invite) && is_object($invite) ? $invite->type : $invite;
return user_access('administer invite entities', $account)
|| user_access('create any invite entities', $account)
|| user_access('create ' . $type . ' entity');
case 'view':
return user_access('administer invite entities', $account)
|| user_access('view any invite entities', $account)
|| ($invite->uid == $account->uid);
case 'edit':
return user_access('administer invite entities')
|| user_access('edit any invite entities')
|| isset($invite) && (user_access('edit own invite entities') && ($invite->uid == $account->uid));
case 'withdraw':
return user_access('withdraw invitations')
|| isset($invite) && (user_access('edit own invite entities') && ($invite->invitee == $account->uid));
}
return TRUE;
}
/**
* Access callback for withdraw confirmation form.
*
* @param Invite $invite
* The invitations to be withdrawn.
*
* @return bool
* Is user have access or no.
*/
function invite_withdraw_access($invite) {
global $user;
if ($invite->status == INVITE_WITHDRAWN) {
backdrop_set_message(t('The invitation has already been withdrawn.'), 'error');
return FALSE;
}
if (!$invite->joined) {
$permission = 'withdraw own invitations';
}
else {
$permission = 'withdraw own accepted invitations';
}
if ((($invite->uid == $user->uid || $invite->invitee == $user->uid) && user_access($permission)) || user_access('administer invitations')) {
return TRUE;
}
else {
return FALSE;
}
}
/**
* Access callback for resend invitation.
*
* @param Invite $invite
* The invitations to be resent.
*
* @return bool
* Is user have access or no.
*/
function invite_resend_access($invite) {
global $user;
$is_valid = $invite->joined == 0 && $invite->canceled == 0;
$access_own = user_access('resend own invitations') && $invite->uid == $user->uid;
$access_any = user_access('resend any invitations');
$is_expired = $invite->expiry < REQUEST_TIME || user_access('resend unexpired invitations');
return $is_valid && $is_expired && ($access_own || $access_any);
}
/**
* Access callback for task Type.
*/
function invite_type_access($op, $entity = NULL) {
return user_access('administer invite types');
}
/**
* Implements hook_permission().
*/
function invite_permission() {
$permissions = array(
'administer invitations' => array(
'title' => t('Administer invitations'),
'description' => t('Administer all invitations'),
'restrict access' => TRUE,
),
'administer invite types' => array(
'title' => t('Administer invite types'),
'description' => t('Allows users to configure invite types and their fields.'),
'restrict access' => TRUE,
),
'create any invite entities' => array(
'title' => t('Create any invites'),
'description' => t('Allows users to create invites of any type.'),
'restrict access' => FALSE,
),
'view any invite entities' => array(
'title' => t('View any invites'),
'description' => t('Allows users to view invites.'),
'restrict access' => TRUE,
),
'edit any invite entities' => array(
'title' => t('Edit any invites'),
'description' => t('Allows users to edit any invites.'),
'restrict access' => TRUE,
),
'edit own invite entities' => array(
'title' => t('Edit own invites'),
'description' => t('Allows users to edit own invites.'),
'restrict access' => FALSE,
),
'withdraw own invitations' => array(
'title' => t('Withdraw own invitations'),
'description' => t('Allows users to withdraw own invitations.'),
'restrict access' => FALSE,
),
'withdraw own accepted invitations' => array(
'title' => t('Withdraw own accepted invitations'),
'description' => t('Allows users to edit own invites.'),
'restrict access' => FALSE,
),
'resend own invitations' => array(
'title' => t('Resend own invitations'),
'description' => t('Allows users to resend their invitations that have expired. Add the "resend unexpired invitations" permission to allow them to resend their invitations that have not expired yet.'),
'restrict access' => FALSE,
),
'resend any invitations' => array(
'title' => t('Resend any invitations'),
'description' => t('Allows users to resend any invitation that have expired. Add the "resend unexpired invitations" permission to allow them to resend the invitations that have not expired yet.'),
'restrict access' => FALSE,
),
'resend unexpired invitations' => array(
'title' => t('Resend unexpired invitations'),
'description' => t('Allows users to resend invitations that have not expired yet. This needs to be used in relation with the "resend own invitations" or "resend any invitations" permission.'),
'restrict access' => FALSE,
),
);
foreach (invite_get_types() as $invite_type) {
$permissions['create ' . $invite_type->type . ' entity'] = array(
'title' => t('%name create new invites.', array('%name' => $invite_type->label)),
'description' => '',
'restrict access' => FALSE,
);
}
return $permissions;
}
/**
* Loads Invite by registration code.
*
* @param string $reg_code
*
* @return Invite
*/
function invite_by_code_load($reg_code) {
$invites = invite_load_multiple(FALSE, array('reg_code' => $reg_code));
return reset($invites);
}
/**
* Loads Invite using information stored in session.
*
* @return bool|Invite
* Return invite or FALSE.
*/
function invite_load_from_session() {
if (isset($_SESSION[INVITE_SESSION_CODE])) {
$invite = invite_by_code_load($_SESSION[INVITE_SESSION_CODE]);
}
// Legacy url support (user/register/regcode).
elseif (arg(0) == 'user' && arg(1) == 'register' && $reg_code = arg(2)) {
if ($invite = invite_by_code_load($reg_code)) {
$_SESSION[INVITE_SESSION_CODE] = $invite->reg_code;
}
}
if (!empty($invite)) {
return $invite;
}
else {
return FALSE;
}
}
/**
* Loads Invite by Id.
*
* @param int $iid
* Invite entity ID.
* @param bool $reset
* Whether to reset the internal cache for the requested entity type.
*
* @return Invite
*/
function invite_load($iid, $reset = FALSE) {
$invites = invite_load_multiple(array($iid), array(), $reset);
return reset($invites);
}
/**
* Load multiple invites based on certain conditions.
*
* @param array|bool $iids
* Array with ID's or bool.
* @param array $conditions
* Array with conditions.
* @param bool $reset
* Whether to reset the internal cache for the requested entity type.
*
* @return mixed
* An array of entity objects indexed by their ids. When no results are
* found, an empty array is returned.
*/
function invite_load_multiple($iids, $conditions = array(), $reset = FALSE) {
return entity_load('invite', $iids, $conditions, $reset);
}
/**
* Saves Invite.
*
* @param Invite $invite
*
* @return bool
*/
function invite_save($invite) {
return entity_plus_save('invite', $invite);
}
/**
* Delete single invite.
*
* @param Invite $invite
*/
function invite_delete($invite) {
/* TODO: Double check that this works correctly. There currently is no
* entity_delete() function in Backdrop.
*/
// Maybe just use entity_delete_multiple with a single element array? Hmm.
// $id = entity_plus_id('invite', $invite);
$invite->delete();
// entity_delete('invite', );
}
/**
* Delete multiple invites.
*
* @param array $invite_ids
*/
function invite_delete_multiple($invite_ids) {
entity_delete_multiple('invite', $invite_ids);
}
/**
* Implements hook_insert().
*
* @param Invite $entity
*/
function invite_invite_insert($entity) {
if ($entity->sendInvite()) {
if ($entity->sendNotification) {
backdrop_set_message(t('Invitation has been sent.'));
}
}
else {
if ($entity->sendNotification) {
backdrop_set_message(t('Failed to send a message.'), 'error');
}
}
}
/**
* Callback for getting properties of an Invite.
*
* @param Invite $entity
* @param array $options
* @param $name
* @param $entity_type
*
* @return string
* String with URL.
*/
function invite_metadata_entity_get_properties($entity, array $options, $name, $entity_type) {
$invite = entity_metadata_wrapper('invite', $entity);
switch ($name) {
case 'invite_accept_link':
$result = url('invite/accept/' . $invite->reg_code->value(), array('absolute' => TRUE));
break;
case 'invite_withdraw_link':
$result = url('invite/withdraw/' . $invite->reg_code->value(), array('absolute' => TRUE));
break;
case 'invite_resend_link':
$result = url('invite/resend/' . $invite->reg_code->value(), array('absolute' => TRUE));
break;
default:
$result = '';
}
return $result;
}
/**
* Load Invite Type.
*
* @param string $invite_type_name
*
* @return array|mixed
*/
function invite_type_load($invite_type_name) {
return invite_get_types($invite_type_name);
}
/**
* List of Invite Types.
*
* @param string $type_name
*
* @return array|mixed
*/
function invite_get_types($type_name = NULL) {
$types = entity_load_multiple_by_name('invite_type', isset($type_name) ? array($type_name) : FALSE);
return isset($type_name) ? reset($types) : $types;
}
/**
* Saves Invite Type entity.
*
* @param InviteType $invite_type
*/
function invite_type_save($invite_type) {
entity_plus_save('invite_type', $invite_type);
}
/**
* Deletes single Invite type.
*
* @param InviteType $invite_type
*/
function invite_type_delete($invite_type) {
// TODO: CHECK THIS AS ABOVE.
$invite_type->delete();
// entity_delete('invite_type', entity_id('invite_type', $invite_type));
}
/**
* Delete multiple Invite types.
*
* @param mixed $invite_type_ids
* ID' of invite types.
*/
function invite_type_delete_multiple($invite_type_ids) {
entity_delete_multiple('invite_type', $invite_type_ids);
}
/**
* Implements hook_views_api().
*/
function invite_views_api() {
return array(
'api' => '3.0',
'path' => backdrop_get_path('module', 'invite') . '/views',
);
}
/**
* Implements hook_views_data_alter().
*
* - Allow views filter selection based on status text.
*/
function invite_views_data_alter(&$data) {
$data['invite']['status']['field']['handler'] = 'views_handler_field_invite_status';
$data['invite']['status']['filter']['handler'] = 'views_handler_filter_in_operator';
$data['invite']['status']['filter']['options callback'] = 'invite_get_status_options';
}
/**
* Helper function to return the list of status option.
*/
function invite_get_status_options() {
return array(
INVITE_VALID => t('Valid'),
INVITE_WITHDRAWN => t('Withdrawn'),
INVITE_USED => t('Joined'),
INVITE_EXPIRED => t('Expired'),
);
}
/**
* Implements hook_user_login().
*/
function invite_user_login($edit, $account) {
$invite = invite_load_from_session();
if ($invite && $invite->status() == INVITE_VALID) {
$invite->invitee = $account->uid;
$invite->joined = REQUEST_TIME;
$invite->status = INVITE_USED;
entity_plus_save('invite', $invite);
if (isset($_SESSION)) {
unset($_SESSION[INVITE_SESSION_CODE]);
}
}
}
/**
* Implements hook_user_insert().
*/
function invite_user_insert(&$edit, $account, $category) {
$invite = invite_load_from_session();
if ($invite && $invite->status() == INVITE_VALID) {
$invite->invitee = $account->uid;
$invite->joined = REQUEST_TIME;
$invite->status = INVITE_USED;
entity_plus_save('invite', $invite);
if (isset($_SESSION)) {
unset($_SESSION[INVITE_SESSION_CODE]);
}
}
}
/**
* Implements hook_invite_update().
*/
function invite_invite_update($invite) {
// When invite has been accepted, trigger accept hook.
if (isset($invite->original) && !$invite->original->joined && $invite->joined) {
$account = $invite->invitee();
module_invoke_all('invite_accept', $invite, $account);
if (module_exists('rules')) {
rules_invoke_event('invite_accept', $invite, $account);
}
}
}
/**
* Implements hook_user_presave().
*/
function invite_user_presave(&$edit, $account, $category) {
if ($account->is_new && !empty($account->mail)) {
$invite = invite_load_from_session();
if ($invite) {
$roles = invite_target_roles($invite, $edit);
if ($roles) {
if (!isset($edit['roles']) || !is_array($edit['roles'])) {
$edit['roles'] = array();
}
foreach ($roles as $role) {
$edit['roles'][$role] = $role;
}
}
if (!config_get('invite.settings','invite_require_approval')) {
$edit['status'] = 1;
}
else {
$edit['status'] = 0;
}
}
}
}
/**
* Determine target roles based on the roles of an inviter.
*
* @param Invite $invite
* An invitation object.
* @param $account
* The user for whom the role is being determined.
*
* @return array
* Array of target roles for an invited user.
*/
function invite_target_roles($invite, $account) {
$targets = array();
$config = config('invite.settings');
$invite = entity_metadata_wrapper('invite', $invite);
$inviter = user_load($invite->inviter->uid->value());
// Add roles of inviter.
$roles = array_intersect($inviter->roles, user_roles(TRUE, 'send invitations'));
dpm($roles); // Should be an associative array with machine name => label
// Add a dummy entry to retrieve the default target role setting.
$roles['default'] = 'default';
// Map to configured target roles.
foreach ($roles as $rid => $role) {
// TODO: WTF? How am I supposed to do this in the settings file?
// Well, you do it in the .install file.
$target = $config->get('invite_target_role_' . $rid, BACKDROP_AUTHENTICATED_ROLE);
if ($target != BACKDROP_AUTHENTICATED_ROLE) {
$targets[$target] = $target;
}
}
backdrop_alter('invite_target_roles', $targets, $invite, $account);
return $targets;
}
/**
* Implements hook_cron().
*
* Updates status of invites to INVITE_EXPIRED if expiry time has past.
*/
function invite_cron() {
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'invite')
->propertyCondition('expiry', time(), "<")
->propertyCondition('status', INVITE_VALID, "=");
$results = $query->execute();
if (!empty($results) && !empty($results['invite'])) {
$invites = entity_load('invite', array_keys($results['invite']));
foreach ($invites as $invite) {
$invite->status = INVITE_EXPIRED;
entity_plus_save('invite', $invite);
}
}
}
/**
* Implements hook_autoload_info().
*
* @return array
*/
function invite_autoload_info() {
return array(
'InviteController' => 'includes/invite.controller.inc',
'InviteTypeController' => 'includes/invite.controller.inc',
'InviteTypeUIController' => 'includes/invite.controller.inc',
'Invite' => 'includes/invite.controller.inc',
'InviteType' => 'includes/invite.controller.inc',
'InviteMetadataController' => 'includes/invite.info.inc',
'InviteExpireTest' => 'tests/InviteExpireTest.test',
);
}
/**
* Implements hook_config_info().
*/
function invite_config_info() {
$prefixes['invite.settings'] = array(
'label' => t('Invite module settings'),
'group' => t('Invite'),
);
return $prefixes;
}