-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathregistration.module
2322 lines (2125 loc) · 74 KB
/
registration.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
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @file
* Module file for registrations.
*/
module_load_include('inc', 'registration', 'includes/registration.field');
module_load_include('inc', 'registration', 'includes/registration.forms');
define('REGISTRATION_STATE_ONE', 1);
define('REGISTRATION_INSUFFICIENT_SPACES_MESSAGE', 'Insufficient spaces remaining.');
/**
* If user has access to create registrations for his account.
*
* @see registration_access_people()
*/
define('REGISTRATION_REGISTRANT_TYPE_ME', 'registration_registrant_type_me');
/**
* If user has access to create registrations for other users.
*
* @see registration_access_people()
*/
define('REGISTRATION_REGISTRANT_TYPE_USER', 'registration_registrant_type_user');
/**
* If user has access to create registrations for people identified by email.
*
* @see registration_access_people()
*/
define('REGISTRATION_REGISTRANT_TYPE_ANON', 'registration_registrant_type_anon');
/**
* Implements hook_entity_info().
*/
function registration_entity_info() {
$entities = array(
'registration' => array(
'label' => t('Registration'),
'plural label' => t('Registrations'),
'controller class' => 'EntityPlusController',
'entity class' => 'Registration',
'metadata controller class' => 'RegistrationMetadataController',
'base table' => 'registration',
'fieldable' => TRUE,
'entity keys' => array(
'id' => 'registration_id',
'bundle' => 'type',
),
'access callback' => 'registration_access',
'bundle keys' => array(
'bundle' => 'name',
),
'bundles' => array(),
'view modes' => array(
'full' => array(
'label' => t('Full Registration'),
'custom settings' => FALSE,
),
),
'uri callback' => 'entity_class_uri',
'token type' => 'registration',
'module' => 'registration',
'label callback' => 'entity_class_label',
'entity cache' => TRUE,
'field cache' => FALSE,
),
'registration_type' => array(
'label' => t('Registration type'),
'entity class' => 'RegistrationType',
'controller class' => 'RegistrationTypeController',
'base table' => 'registration_type',
'fieldable' => FALSE,
'bundle of' => 'registration',
'exportable' => TRUE,
'entity keys' => array(
'id' => 'id',
'name' => 'name',
'label' => 'label',
),
'access callback' => 'registration_type_access',
'module' => 'registration',
// Enable the entity API's admin UI.
'admin ui' => array(
'path' => 'admin/structure/registration/registration_types',
'file' => 'registration_type.admin.inc',
'file path' => backdrop_get_path('module', 'registration') . '/includes',
'controller class' => 'RegistrationTypeUIController',
),
'entity cache' => TRUE,
'field cache' => FALSE,
),
'registration_state' => array(
'label' => t('Registration State'),
'plural label' => t('Registration states'),
'controller class' => 'RegistrationStateController',
'entity class' => 'RegistrationState',
'base table' => 'registration_state',
'fieldable' => FALSE,
'entity keys' => array(
'id' => 'registration_state_id',
'label' => 'label',
'name' => 'name',
),
'bundles' => array(
'registration_state' => array(
'label' => 'Registration States',
),
),
'admin ui' => array(
'path' => 'admin/structure/registration/registration_states',
'file' => 'registration.forms.inc',
'file path' => backdrop_get_path('module', 'registration') . '/includes',
'controller class' => 'RegistrationStatesUIController',
),
'token type' => 'registration_state',
'module' => 'registration',
'access callback' => 'registration_state_access',
'exportable' => TRUE,
'entity cache' => TRUE,
'field cache' => FALSE,
),
);
return $entities;
}
/**
* Implements hook_entity_info_alter().
*/
function registration_entity_info_alter(&$entity_info) {
// @todo: we're testing to ensure the schema exists; needed because running gui
// install profile was hitting this BEFORE the schema was installed.
if (backdrop_get_schema('registration')) {
// We are adding the info about the registration types via a hook to avoid a
// recursion issue as loading the model types requires the entity info as well.
// Need to do a class_exists check for updates from earlier versions
// of registration module.
if (class_exists('RegistrationTypeController')) {
foreach (registration_get_types() as $type => $info) {
$entity_info['registration']['bundles'][$type] = array(
'label' => $info->label,
'admin' => array(
'path' => 'admin/structure/registration/registration_types/manage/%registration_type',
'real path' => 'admin/structure/registration/registration_types/manage/' . $type,
'bundle argument' => 5,
'access arguments' => array('administer registration types'),
),
);
}
}
}
}
/**
* Implements hook_block_info().
*/
function registration_block_info() {
$blocks['form'] = array(
'info' => t('Registration form'),
'cache' => DRUPAL_NO_CACHE,
);
return $blocks;
}
/**
* Implements hook_block_configure().
*/
function registration_block_configure($delta = '', $settings = array()) {
$form = array();
if ($delta == 'form') {
$info = entity_get_info();
$entity_options = array();
foreach ($info as $machine => $type) {
$entity_options[$machine] = $type['label'];
}
$form['entity_type'] = array(
'#type' => 'select',
'#title' => t('Entity Type'),
'#options' => $entity_options,
'#default_value' => isset($settings['entity_type']) ? $settings['entity_type'] : '',
);
$form['entity_id'] = array(
'#type' => 'number',
'#title' => t('Entity ID'),
'#min' => 0,
'#default_value' => isset($settings['entity_id']) ? $settings['entity_id'] : '',
);
}
return $form;
}
/**
* Implements hook_block_view().
*/
function registration_block_view($delta = '', $settings = array(), $contexts = array()) {
$block = array();
switch ($delta) {
case 'form':
$entity_type = $settings['entity_type'];
$entity = entity_load($entity_type, $settings['entity_id']);
if (registration_register_page_access($entity_type, $entity)) {
$block['subject'] = t('Register Now');
$block['content'] = registration_register_page($entity_type, $entity);
}
break;
}
return $block;
}
/**
* Implements hook_menu().
*/
function registration_menu() {
$items['admin/structure/registration'] = array(
'title' => 'Registration',
'description' => 'Administer Registration items, such as types, states, etc.',
'page callback' => 'system_admin_menu_block_page',
'access arguments' => array('administer registration'),
'file' => 'system.admin.inc',
'file path' => backdrop_get_path('module', 'system'),
);
$items['registration/%registration'] = array(
'title callback' => 'registration_page_title',
'title arguments' => array(1),
'page callback' => 'registration_view',
'page arguments' => array(1),
'access callback' => 'registration_own_access',
'access arguments' => array('view', 1),
);
$items['registration/%registration/view'] = array(
'title' => 'View',
'page callback' => 'registration_view',
'page arguments' => array(1),
'access callback' => 'registration_own_access',
'access arguments' => array('view', 1),
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items['registration/%registration/edit'] = array(
'title' => 'Edit',
'page callback' => 'backdrop_get_form',
'page arguments' => array('registration_form', 1),
'access callback' => 'registration_own_access',
'access arguments' => array('update', 1),
'weight' => 10,
'type' => MENU_LOCAL_TASK,
);
$items['registration/%registration/delete'] = array(
'title' => 'Delete',
'page callback' => 'backdrop_get_form',
'page arguments' => array('registration_delete_confirm', 1),
'access callback' => 'registration_own_access',
'access arguments' => array('delete', 1),
'type' => MENU_CALLBACK,
);
$items['registration/%registration/%/%'] = array(
'title' => 'Registration Access Check',
'page callback' => 'registration_access_check_redirect',
'page arguments' => array(1, 2),
'access callback' => 'registration_own_access',
'access arguments' => array(2, 1, 3),
);
// Entity local tasks.
foreach (registration_get_registration_instances() as $instance) {
$type = $instance['entity_type'];
if (!in_array($type, array('registration', 'registration_type'))) {
$hide_register_tab = isset($instance['settings']['hide_register_tab']) && $instance['settings']['hide_register_tab'];
$items[$type . '/%entity_plus_object/register'] = array(
'load arguments' => array($type),
'title' => 'Register',
'page callback' => 'registration_register_page',
'page arguments' => array(0, 1),
'access callback' => 'registration_register_page_access',
'access arguments' => array(0, 1),
'type' => $hide_register_tab ? MENU_CALLBACK : MENU_LOCAL_TASK,
);
$items[$type . '/%entity_plus_object/registrations'] = array(
'load arguments' => array($type),
'title' => 'Manage Registrations',
'page callback' => 'registration_registrations_page',
'page arguments' => array(0, 1),
'access callback' => 'registration_administer_registrations_access',
'access arguments' => array(0, 1),
'type' => MENU_LOCAL_TASK,
);
$items[$type . '/%entity_plus_object/registrations/list'] = array(
'load arguments' => array($type),
'title' => 'Registrations',
'page callback' => 'registration_registrations_page',
'page arguments' => array(0, 1),
'access callback' => 'registration_administer_registrations_access',
'access arguments' => array(0, 1),
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items[$type . '/%entity_plus_object/registrations/settings'] = array(
'load arguments' => array($type),
'title' => 'Settings',
'page callback' => 'registration_entity_settings_page',
'page arguments' => array(0, 1),
'access callback' => 'registration_administer_registrations_access',
'access arguments' => array(0, 1),
'weight' => 9,
'type' => MENU_LOCAL_TASK,
);
$items[$type . '/%entity_plus_object/registrations/broadcast'] = array(
'load arguments' => array($type),
'title' => 'Email Registrants',
'page callback' => 'backdrop_get_form',
'page arguments' => array(
'registration_registrations_broadcast_form',
0,
1,
),
'access callback' => 'registration_administer_registrations_access',
'access arguments' => array(0, 1),
'weight' => 10,
'type' => MENU_LOCAL_TASK,
);
}
}
if (module_exists('devel')) {
$items['registration/%registration/devel'] = array(
'title' => 'Devel',
'page callback' => 'devel_load_object',
'page arguments' => array('node', 1),
'access arguments' => array('access devel information'),
'type' => MENU_LOCAL_TASK,
'file path' => backdrop_get_path('module', 'devel'),
'file' => 'devel.pages.inc',
'weight' => 100,
);
$items['registration/%registration/devel/load'] = array(
'title' => 'Load',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
}
return $items;
}
/**
* Hash function used for enabling anonymous access to registrations.
*
* Similar to user_pass_rehash().
*/
function registration_anonymous_access_hash($registration, $options = array(), $name = "", $type = "") {
return backdrop_hmac_base64($registration->created . $registration->id(), backdrop_get_hash_salt() . $registration->anon_mail);
}
/**
* Get a full link to view an anonymous registration, with access hash.
*
* For backwards-compatibility with longstanding issue queue solution.
*/
function registration_anonymous_link_get($registration, $options = array(), $name = "", $type = "") {
// Create a link that can be used to access an anonymously created event from anywhere.
$path = entity_uri('registration', $registration);
$url = url($path['path'] . '/view/' . registration_anonymous_access_hash($registration), array('absolute' => TRUE));
return $url;
}
/**
* Helper function to run a hashed URL through a permissions check first.
*/
function registration_access_check_redirect($registration, $function) {
backdrop_goto('registration/' . $registration->id() . '/' . $function);
}
/**
* Define our own view/update/delete access.
*
* Check access based on the entity permissions for all users
* Also check that an anonymous user created the registration via session token.
*/
function registration_own_access($action, $registration, $hash = NULL) {
if (entity_access($action, 'registration', $registration)) {
// Only check session information if this is an anonymous user.
if (!user_is_anonymous()) {
// They have access to the registration and they aren't anonymous.
return TRUE;
}
// Anonymous has access, and we are anonymous. Check for a valid hash.
$in_session = FALSE;
// If we were not handed a hash, check for it in the session or a query
// parameter.
if (!$hash) {
// If they made it or validated already this session:
if (isset($_SESSION['registration_ids'][$registration->registration_id])) {
$in_session = TRUE;
$hash = $_SESSION['registration_ids'][$registration->registration_id];
}
// If there isn't a Session or this registration isn't in it, see if a
// hash is provided in the query param to verify this person is the one
// registered. (For backwards compatibility with patch from
// https://www.drupal.org/node/2224159).
else {
global $_SESSION;
$params = backdrop_get_query_parameters();
if (isset($params['registration_hash'])) {
$hash = $params['registration_hash'];
}
}
}
if ($hash === registration_anonymous_access_hash($registration)) {
if (!$in_session) {
$_SESSION['registration_ids'][$registration->registration_id] = $hash;
}
return TRUE;
}
}
return FALSE;
}
/**
* Implements hook_config_info().
*/
function registration_config_info() {
$prefixes['registration.settings'] = array(
'label' => t('Registration settings'),
'group' => t('Configuration'),
);
return $prefixes;
}
/**
* Implements hook_admin_paths().
*/
function registration_admin_paths() {
$paths = array(
'registration/*' => TRUE,
'registration/*/edit' => TRUE,
'registration/*/delete' => TRUE,
);
if (config_get('registration.settings', 'registration_paths_administrative')) {
foreach (registration_get_registration_instances() as $instance) {
$type = $instance['entity_type'];
if (!in_array($type, array('registration', 'registration_type'))) {
$paths[$type . '/*/registrations'] = TRUE;
$paths[$type . '/*/registrations/list'] = TRUE;
$paths[$type . '/*/registrations/settings'] = TRUE;
$paths[$type . '/*/registrations/broadcast'] = TRUE;
}
}
}
return $paths;
}
/**
* Implements hook_permission().
*/
function registration_permission() {
$permissions = array(
'administer registration types' => array(
'title' => t('Administer registration types'),
'description' => t('Manage registration types, fields, and display settings.'),
'restrict access' => TRUE,
),
'administer registration states' => array(
'title' => t('Administer registration states'),
'description' => t('Manage registration states, fields, and display settings.'),
'restrict access' => TRUE,
),
'administer registration' => array(
'title' => t('Administer registration'),
'description' => t('View, edit, delete, and manage all registrations, regardless of type.'),
'restrict access' => TRUE,
),
);
foreach (registration_get_types() as $type_info) {
$permissions += registration_permission_list($type_info);
}
return $permissions;
}
/**
* Builds permissions for a registration type.
*
* @param object $info
* Information about a registration type.
*
* @return array
* An array of permission names and descriptions keyed by permission name.
*/
function registration_permission_list($info) {
$type = $info->name;
$label = $info->label;
return array(
"administer $type registration" => array(
'title' => t('%type_name: Administer settings', array('%type_name' => $label)),
'description' => t('Allow changing registration settings for all entities of this type.'),
),
"administer own $type registration" => array(
'title' => t('%type_name: Administer own settings', array('%type_name' => $label)),
'description' => t('Allow changing registration settings for entities which a user has edit access.'),
),
"view $type registration" => array(
'title' => t('%type_name: View all registrations', array('%type_name' => $label)),
),
"view own $type registration" => array(
'title' => t('%type_name: View own registrations', array('%type_name' => $label)),
),
"create $type registration" => array(
'title' => t('%type_name: Create new registration', array('%type_name' => $label)),
),
"update own $type registration" => array(
'title' => t('%type_name: Edit own registrations', array('%type_name' => $label)),
),
"update any $type registration" => array(
'title' => t('%type_name: Edit any registrations', array('%type_name' => $label)),
),
"delete own $type registration" => array(
'title' => t('%type_name: Delete own registrations', array('%type_name' => $label)),
),
"delete any $type registration" => array(
'title' => t('%type_name: Delete any registrations', array('%type_name' => $label)),
),
"create $type registration self" => array(
'title' => t('%type_name: Register self', array('%type_name' => $label)),
),
"create $type registration other users" => array(
'title' => t('%type_name: Register other accounts', array('%type_name' => $label)),
'description' => t("Register other users by username. Note that giving this permission to Anonymous is not recommended, as it can expose a user's registration status to a site visitor who knows their username."),
),
"create $type registration other anonymous" => array(
'title' => t('%type_name: Register other people', array('%type_name' => $label)),
),
"edit $type registration state" => array(
'title' => t('%type_name: Edit registration state', array('%type_name' => $label)),
),
);
}
/**
* Implements hook_user_cancel().
*/
function registration_user_cancel($edit, $account, $method) {
if (isset($account->uid)) {
if ($method == 'user_cancel_reassign') {
db_update('registration')
->fields(array(
'author_uid' => 0,
))
->condition('author_uid', $account->uid)
->execute();
db_update('registration')
->fields(array(
'user_uid' => NULL,
))
->condition('user_uid', $account->uid)
->execute();
}
}
}
/**
* Implements hook_user_delete().
*/
function registration_user_delete($account) {
if (isset($account->uid)) {
$query = new EntityFieldQuery();
$result = $query
->entityCondition('entity_type', 'registration')
->propertyCondition('author_uid', $account->uid)
->execute();
if ($result) {
registration_delete_multiple(array_keys($result['registration']));
}
// Users associated with {registration}.user_uid do not own the
// registration. Simply disassociate the user with the registration.
db_update('registration')
->fields(array(
'user_uid' => NULL,
))
->condition('user_uid', $account->uid)
->execute();
// Delete entries in the cache as well.
cache_clear_all('*', 'cache_entity_registration', TRUE);
}
}
/**
* Display a registration.
*
* @param object $registration
* A fully loaded registration object.
*
* @return array
* Renderable elements.
*/
function registration_view(Registration $registration, $view_mode = 'full') {
return $registration->view($view_mode);
}
/**
* Title callback: Generate a title for a registration entity.
*
* Callback for hook_menu() within system_themes_page().
*
* @param @registration
* A fully loaded registration object.
*
* @return string
*/
function registration_page_title(Registration $registration) {
return $registration->label();
}
/**
* Access callback: for registration_register_page().
*
* Check if user has access to register for a host entity.
*
* @param string $entity_type
* The host entity type.
* @param object $entity
* The host entity.
*
* @return bool
* Whether a user can create a new registration for a host entity.
*
* @see registration_register_page()
* @see registration_menu()
*/
function registration_register_page_access($entity_type, $entity) {
list($entity_id) = entity_extract_ids($entity_type, $entity);
$entities = array($entity_id => $entity);
if (module_exists('translation')) {
$translation_entity = entity_load($entity_type, $entity->tnid);
$entities[$entity->tnid] = $translation_entity;
}
foreach ($entities as $entity_id => $entity) {
if ($type = registration_get_entity_registration_type($entity_type, $entity)) {
$registration = entity_get_controller('registration')->create(array(
'entity_type' => $entity_type,
'entity_id' => $entity_id,
'type' => $type,
));
if (entity_access('create', 'registration', $registration)) {
$settings = registration_entity_settings($entity_type, $entity_id);
if ($settings['status']) {
return TRUE;
}
}
}
}
return FALSE;
}
/**
* Access callback: for registration_registrations_page().
*
* Check if user has access to administer registrations for a host entity.
*
* @param string $entity_type
* The host entity type.
* @param object $entity
* The host entity.
* @param object $account = NULL
* An account for which to check access. If NULL is provided the current
* user is used.
*
* @return bool
* Whether a user can view registrations for a host entity.
*
* @see registration_registrations_page()
* @see registration_menu()
*/
function registration_administer_registrations_access($entity_type, $entity, $account = NULL) {
$registration_type = registration_get_entity_registration_type($entity_type, $entity);
if ($registration_type) {
if (user_access("administer $registration_type registration", $account)) {
return TRUE;
}
elseif (user_access("administer own $registration_type registration", $account) && entity_access('update', $entity_type, $entity, $account)) {
return TRUE;
}
}
return FALSE;
}
/**
* Page callback: Add a new registration to a host entity.
*
* @param string $entity_type
* The host entity type.
* @param object $entity
* The host entity.
*
* @return array
* A render array
*
* @see registration_register_access()
* @see registration_menu()
*/
function registration_register_page($entity_type, $entity) {
list($entity_id) = entity_extract_ids($entity_type, $entity);
if (registration_status($entity_type, $entity_id)) {
$registration_type = registration_get_entity_registration_type($entity_type, $entity);
$registration = entity_get_controller('registration')->create(array(
'entity_type' => $entity_type,
'entity_id' => $entity_id,
'type' => $registration_type,
));
return backdrop_get_form('registration_form', $registration);
}
elseif (module_exists('translation')) {
$translation_entity = entity_load($entity_type, $entity->tnid);
if (registration_status($entity_type, $entity->tnid)) {
$registration_type = registration_get_entity_registration_type($entity_type, $translation_entity);
$registration = entity_get_controller('registration')->create(array(
'entity_type' => $entity_type,
'entity_id' => $entity->tnid,
'type' => $registration_type,
));
return backdrop_get_form('registration_form', $registration);
}
}
else {
return t('Sorry, registrations are no longer available for %name',
array('%name' => entity_label($entity_type, $entity)));
}
}
/**
* Page callback: Show a list of registrations for a host entity.
*
* @param string $entity_type
* The host entity type.
* @param object $entity
* The host entity.
*
* @return array
* A render array
*
* @see registration_administer_registrations_access()
* @see registration_menu()
*/
function registration_registrations_page($entity_type, $entity) {
$header = array(
array(
'data' => t('id'),
'field' => 'registration_id',
'type' => 'property',
'specifier' => 'registration_id',
),
array(
'data' => t('Email'),
),
array(
'data' => t('User'),
'field' => 'user_uid',
'type' => 'property',
'specifier' => 'user_uid',
),
array(
'data' => t('Created By'),
'field' => 'author_uid',
'type' => 'property',
'specifier' => 'author_uid',
),
array(
'data' => t('Count'),
'field' => 'count',
'type' => 'property',
'specifier' => 'count',
),
array(
'data' => t('Created'),
'field' => 'created',
'sort' => 'desc',
'type' => 'property',
'specifier' => 'created',
),
array(
'data' => t('State'),
'field' => 'state',
'type' => 'property',
'specifier' => 'state',
),
array('data' => t('Actions')),
);
list($entity_id) = entity_extract_ids($entity_type, $entity);
$label = entity_label($entity_type, $entity);
$query = new EntityFieldQuery();
$result = $query
->entityCondition('entity_type', 'registration')
->propertyCondition('entity_id', $entity_id)
->propertyCondition('entity_type', $entity_type)
->pager(20)
->tableSort($header)
->execute();
if (!empty($result['registration'])) {
$registrations = registration_load_multiple(array_keys($result['registration']));
$rows = array();
foreach ($registrations as $registration) {
$wrapper = entity_metadata_wrapper('registration', $registration);
$author = $wrapper->author->value();
$user = $wrapper->user->value();
$state = $wrapper->state->value();
$author_col = '';
if ($registration->author_uid) {
$author_col = theme('username', array('account' => $author));
}
$user_col = '';
if ($registration->user_uid) {
$user_col = theme('username', array('account' => $user));
}
$actions = array();
if (entity_access('view', 'registration', $registration)) {
$actions[] = l(t('View'), 'registration/' . $registration->registration_id);
}
if (entity_access('update', 'registration', $registration)) {
$actions[] = l(t('Edit'), 'registration/' . $registration->registration_id . '/edit', array('query' => backdrop_get_destination()));
}
if (entity_access('delete', 'registration', $registration)) {
$actions[] = l(t('Delete'), 'registration/' . $registration->registration_id . '/delete', array('query' => backdrop_get_destination()));
}
$rows[] = array(
l($registration->registration_id, 'registration/' . $registration->registration_id),
l($wrapper->mail->value(), 'mailto:' . $wrapper->mail->value()),
$user_col,
$author_col,
$registration->count,
format_date($registration->created),
($state ? filter_xss_admin(entity_label('registration_state', $state)) : ''),
implode(' | ', $actions),
);
}
$settings = registration_entity_settings($entity_type, $entity_id);
$table = array(
'header' => $header,
'rows' => $rows,
);
if ($settings['capacity'] != 0) {
$table['caption'] = t('List of registrations for %title. !count of !capacity spaces are filled.', array(
'%title' => $label,
'!count' => '<strong>' . registration_event_count($entity_type, $entity_id) . '</strong>',
'!capacity' => '<strong>' . $settings['capacity'] . '</strong>',
));
}
else {
$table['caption'] = t('List of registrations for %title. !count spaces are filled.', array(
'%title' => $label,
'!count' => '<strong>' . registration_event_count($entity_type, $entity_id) . '</strong>',
));
}
$out = theme('table', $table) . theme('pager');
}
else {
$out = t('There are no registrants for %name',
array('%name' => $label));
}
return $out;
}
/**
* Page callback for entity registration settings.
*
* @param $entity_type
* @param $entity
*
* @return array
* Registration entity settings form.
*/
function registration_entity_settings_page($entity_type, $entity) {
list($entity_id) = entity_extract_ids($entity_type, $entity);
$settings = registration_entity_settings($entity_type, $entity_id);
return backdrop_get_form('registration_entity_settings_form', $settings, $entity_type, $entity_id);
}
/**
* Determines if a host entity has spaces remaining.
*
* @param string $entity_type
* The host entity type.
* @param int $entity_id
* The host entity ID.
* @param int $spaces
* (optional) Used if validating a new registration. The number of spaces
* attempting to fill.
* @param int $registration_id
* The registration ID. Used to exclude specified registration from count.
*
* @return bool
*
* @see registration_status()
*/
function registration_has_room($entity_type, $entity_id, $spaces = 1, $registration_id = NULL, $reset = FALSE) {
$settings = registration_entity_settings($entity_type, $entity_id, $reset);
$capacity = $settings['capacity'];
if ($capacity) {
$count = registration_event_count($entity_type, $entity_id, $registration_id, $reset) + $spaces;
if (($capacity - $count) < 0) {
return FALSE;
}
}
return TRUE;
}
/**
* Determines current number of spaces filled for a host entity.
*
* @param string $entity_type
* The host entity type.
* @param int $entity_id
* The host entity ID.
* @param int $registration_id
* The registration ID. If specified, exclude identified registration from count.
*
* @return int
* The number of spaces remaining for a host entity.
*
* @see registration_has_room()
*/
function registration_event_count($entity_type, $entity_id, $registration_id = NULL, $reset = FALSE) {
$count = &backdrop_static(__FUNCTION__ . '_' . $entity_type . '_' . $entity_id . '_' . $registration_id, FALSE);
if (!$count || $reset) {
$query = db_select('registration', 'r');
$query->addExpression('sum(count)', 'count');
$query->condition('entity_id', $entity_id);
$query->condition('entity_type', $entity_type);
if ($registration_id != NULL) {
$query->condition('registration_id', $registration_id, '<>');
}
$active_held_states = array_merge(registration_get_active_states(), registration_get_held_states());
if (!empty($active_held_states)) {
$query->condition('state', $active_held_states, 'IN');
}
$result = $query->execute();
$count = $result->fetchField();
$count = ($count == '') ? 0 : $count;
}
// Allow other mods to override count.
$settings = registration_entity_settings($entity_type, $entity_id);
$context = array(
'entity_type' => $entity_type,
'entity_id' => $entity_id,
'registration_id' => $registration_id,
'settings' => $settings,
);
backdrop_alter('registration_event_count', $count, $context);
return $count;
}
/**
* Implements hook_entity_insert().
*/
function registration_entity_insert($entity, $entity_type) {
$registration_type = registration_get_entity_registration_type($entity_type, $entity);
if ($registration_type !== FALSE) {
registration_entity_set_default_settings($entity_type, $entity);
}
}
/**
* Implements hook_entity_update().
*/
function registration_entity_update($entity, $entity_type) {
$registration_type = registration_get_entity_registration_type($entity_type, $entity);
if ($registration_type !== FALSE) {
list($entity_id) = entity_extract_ids($entity_type, $entity);
$settings = registration_entity_settings($entity_type, $entity_id);
// no settings yet, try to set defaults
if (!$settings) {
registration_entity_set_default_settings($entity_type, $entity);
}
}