-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathregistration.forms.inc
1190 lines (1077 loc) · 42.8 KB
/
registration.forms.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
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
* Form definitions and callbacks for Registration.
*/
/**
* Form callback: create or edit a registration.
*
* @param Registration $registration
* The registration object to edit or for a create form an empty registration
* object with a node defined.
*/
function registration_form($form, &$form_state, Registration $registration) {
$wrapper = entity_metadata_wrapper('registration', $registration);
$user = $wrapper->user->value();
$host = $wrapper->entity->value();
$state = $wrapper->state->value();
$type = $wrapper->getBundle();
$form_state['registration'] = $registration;
$settings = registration_entity_settings($registration->entity_type, $registration->entity_id);
if (!registration_has_room($registration->entity_type, $registration->entity_id)) {
if ($settings['settings']['registration_waitlist_enable'] == 1 && $settings['settings']['registration_waitlist_message_enable'] == 1) {
backdrop_set_message(t($settings['settings']['registration_waitlist_message']), 'warning');
}
}
$who_options = registration_access_people($registration);
// Default value for who is registering.
$who_default = NULL;
if (isset($registration->registration_id)) {
$who_default = $registration->registrant_type($GLOBALS['user']);
}
elseif (count($who_options) == 1) {
$keys = array_keys($who_options);
$who_default = reset($keys);
}
// Show a message if there's only one option as we're going to hide the form.
if (count($who_options) == 1 && !user_is_anonymous()) {
$form['who_message'] = array(
'#markup' => '<div class="registration-who-msg">' .
t('You are registering: %who', array('%who' => current($who_options))) .
'</div>',
);
}
$form['who_is_registering'] = array(
'#type' => 'select',
'#title' => t('This registration is for:'),
'#options' => $who_options,
'#default_value' => $who_default,
'#required' => TRUE,
'#access' => (count($who_options) > 1),
);
$form['user'] = array(
'#type' => 'textfield',
'#title' => t('User'),
'#default_value' => ($user ? $user->name : ''),
'#maxlength' => 60,
'#size' => 30,
'#description' => t('Select a user by typing their username to get a list of matches.'),
'#autocomplete_path' => 'user/autocomplete',
'#access' => isset($who_options[REGISTRATION_REGISTRANT_TYPE_USER]),
'#states' => array(
'visible' => array(
':input[name="who_is_registering"]' => array('value' => REGISTRATION_REGISTRANT_TYPE_USER),
),
'required' => array(
':input[name="who_is_registering"]' => array('value' => REGISTRATION_REGISTRANT_TYPE_USER),
),
),
);
$form['anon_mail'] = array(
'#type' => 'textfield',
'#title' => t('Email'),
'#description' => t('The email to associate with this registration.'),
'#default_value' => isset($registration->anon_mail) ? $registration->anon_mail : '',
'#size' => 40,
'#maxlength' => 255,
'#access' => isset($who_options[REGISTRATION_REGISTRANT_TYPE_ANON]),
'#required' => (user_is_anonymous()) ? TRUE : FALSE,
'#states' => array(
'visible' => array(
':input[name="who_is_registering"]' => array('value' => REGISTRATION_REGISTRANT_TYPE_ANON),
),
'required' => array(
':input[name="who_is_registering"]' => array('value' => REGISTRATION_REGISTRANT_TYPE_ANON),
),
),
);
$settings = registration_entity_settings($registration->entity_type, $registration->entity_id);
$capacity = $settings['capacity'];
$limit = isset($settings['settings']['maximum_spaces']) ? $settings['settings']['maximum_spaces'] : 1;
// Just in case it was unset:
$settings['settings']['maximum_spaces'] = $limit;
$remaining = $capacity - registration_event_count($registration->entity_type, $registration->entity_id);
if ($capacity && $limit) {
$description = t('The number of spaces you wish to reserve. @spaces_remaining spaces remaining. You may register up to @max spaces.',
array(
'@spaces_remaining' => $remaining,
'@max' => min($limit, $remaining),
)
);
}
elseif ($capacity) {
$description = t('The number of spaces you wish to reserve. @spaces_remaining spaces remaining.', array('@spaces_remaining' => $remaining));
}
elseif ($limit) {
$description = t('The number of spaces you wish to reserve. You may register up to @max spaces.', array('@max' => $limit));
}
else {
$description = t('The number of spaces you wish to reserve.');
}
$form['count'] = array(
'#type' => 'number',
'#min' => 1,
'#step' => 1,
'#maxlength' => 4,
'#title' => t('Spaces'),
'#description' => $description,
'#default_value' => isset($registration->count) ? $registration->count : 1,
'#size' => backdrop_strlen($remaining),
'#access' => (isset($settings['settings']['maximum_spaces']) && $settings['settings']['maximum_spaces'] == 1) ? FALSE : TRUE,
);
$default_state = registration_get_default_state($type);
$states = registration_get_states_options(array('show_on_form' => TRUE));
// Ensure default state is in options or it won't be set.
if (!isset($states[$default_state->name])) {
$states[$default_state->name] = t('@state', array('@state' => entity_label('registration_state', $default_state)));
}
$form['state'] = array(
'#type' => 'select',
'#title' => t('State'),
'#description' => t('State of this registration'),
'#default_value' => ($state ? $state->id() : $default_state->id()),
'#options' => $states,
'#access' => !empty($states) && user_access('edit ' . $registration->type . ' registration state'),
);
field_attach_form('registration', $registration, $form, $form_state);
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save Registration'),
);
// Add a delete button for existing registration:
if (isset($registration->registration_id) && entity_access('delete', 'registration', $registration)) {
$form['actions']['delete'] = array(
'#type' => 'submit',
'#value' => t('Delete'),
'#submit' => array('registration_form_delete_submit'),
"#limit_validation_errors" => array(),
);
}
if ($host && entity_plus_access('view', $registration->entity_type, $host)) {
$uri = entity_uri($registration->entity_type, $host);
if (isset($uri['path'])) {
$form['actions']['cancel'] = array(
'#type' => 'link',
'#title' => t('Cancel'),
'#href' => $uri['path'],
);
}
}
$form['#registration_settings'] = $settings;
// @todo find out why this is necessary.
$form['#submit'][] = 'registration_form_submit';
if ($settings['send_confirmation']) {
// Ensure ours comes after the default.
$form['#submit'][] = 'registration_send_confirmation';
}
return $form;
}
/**
* Validation callback for registration_form().
*/
function registration_form_validate($form, &$form_state) {
$registration = $form_state['registration'];
$count = $form_state['values']['count'];
$label = entity_label($registration->entity_type,
entity_load($registration->entity_type, $registration->entity_id));
// Test status on new registrations.
if (isset($registration->is_new) && $registration->is_new) {
$errors = array();
$registration_status = registration_status($registration->entity_type, $registration->entity_id, TRUE, $count, $registration->registration_id, $errors);
if (!$registration_status) {
form_set_error('', t('Sorry, unable to register for %label due to: %errors.',
array('%label' => $label, '%errors' => implode(', ', $errors))
));
}
}
// Only check capacity for existing registrations that are active.
elseif (in_array($registration->state, registration_get_active_states())) {
$has_room = registration_has_room($registration->entity_type, $registration->entity_id, $count, $registration->registration_id);
if (!$has_room) {
form_set_error('', t('Sorry, unable to register for %label due to: insufficient spaces remaining.',
array('%label' => $label)));
}
}
$settings = registration_entity_settings($registration->entity_type, $registration->entity_id);
$allow_multiple = !empty($settings['settings']['multiple_registrations']) && $settings['settings']['multiple_registrations'];
// Validate according to who's registering.
switch ($form_state['values']['who_is_registering']) {
case REGISTRATION_REGISTRANT_TYPE_ANON:
$form_state['values']['anon_mail'] = trim($form_state['values']['anon_mail']);
if (!valid_email_address($form_state['values']['anon_mail'])) {
form_set_error('anon_mail', t('The email address is invalid.'));
}
if (!$allow_multiple && registration_is_registered($registration, $form_state['values']['anon_mail'])) {
form_set_error('anon_mail', t('%mail is already registered for this event.',
array('%mail' => $form_state['values']['anon_mail'])));
}
break;
case REGISTRATION_REGISTRANT_TYPE_ME:
global $user;
if (!$allow_multiple && registration_is_registered($registration, NULL, $user->uid)) {
form_set_error('user', t('You are already registered for this event.'));
}
break;
case REGISTRATION_REGISTRANT_TYPE_USER:
if (empty($form_state['values']['user'])) {
form_set_error('user', t('User name is required.'));
}
$user = user_load_by_name($form_state['values']['user']);
if ($user) {
if (!$allow_multiple && registration_is_registered($registration, NULL, $user->uid)) {
if (user_access('access user profiles')) {
form_set_error('user', t('%user is already registered for this event.', array('%user' => $user->name)));
}
else {
form_set_error('user', t('Registration Failed.'));
}
}
}
else {
if (user_access('access user profiles')) {
form_set_error('user', t('%user is not a valid user.', array('%user' => $form_state['values']['user'])));
}
else {
form_set_error('user', t('Registration Failed.'));
}
}
break;
}
// Notify field widgets to validate their data.
field_attach_form_validate('registration', $registration, $form, $form_state);
}
/**
* Submit callback for registration_form().
*/
function registration_form_submit($form, &$form_state) {
$registration = $form_state['registration'];
// Set the registration's author uid:
if (!isset($registration->registration_id)) {
$registration->author_uid = $GLOBALS['user']->uid;
}
$registration->count = $form_state['values']['count'];
if (!empty($form_state['values']['state'])) {
$registration->state = $form_state['values']['state'];
}
switch ($form_state['values']['who_is_registering']) {
case REGISTRATION_REGISTRANT_TYPE_ANON:
$registration->user_uid = NULL;
$registration->anon_mail = trim($form_state['values']['anon_mail']);
break;
case REGISTRATION_REGISTRANT_TYPE_ME:
global $user;
$registration->user_uid = $user->uid;
$registration->anon_mail = '';
break;
case REGISTRATION_REGISTRANT_TYPE_USER:
if ($reg_user = user_load_by_name($form_state['values']['user'])) {
$registration->user_uid = $reg_user->uid;
$registration->anon_mail = '';
}
break;
}
// Notify field widgets.
field_attach_submit('registration', $registration, $form, $form_state);
// Save the registration and redirect.
if (registration_save($registration)) {
$form_state['registration'] = $registration;
$reg_config = registration_entity_settings($registration->entity_type, $registration->entity_id);
if (!empty($reg_config['settings']['confirmation']) && strlen($reg_config['settings']['confirmation'])) {
backdrop_set_message(t($reg_config['settings']['confirmation']));
}
$wrapper = entity_metadata_wrapper('registration', $registration);
$host = $wrapper->entity->value();
// Set redirect to configured value, if there is one.
if (!empty($reg_config['settings']['confirmation_redirect']) && strlen($reg_config['settings']['confirmation_redirect'])) {
$form_state['redirect'] = $reg_config['settings']['confirmation_redirect'];
}
else {
// Redirect to registration:
if (entity_access('view', 'registration', $registration)) {
$uri = entity_uri('registration', $registration);
$form_state['redirect'] = $uri['path'];
}
// Redirect to host entity:
else {
if (entity_access('view', $registration->entity_type, $host)) {
$uri = entity_uri($registration->entity_type, $host);
$form_state['redirect'] = $uri['path'];
}
}
}
// Make sure to Start a Session and save the registration info so we
// can check it for anonymous user access.
backdrop_session_start();
$_SESSION['registration_ids'][$registration->id()] = $wrapper->anon_access_hash->value();
}
else {
backdrop_set_message(t('There was a problem submitting your registration.'));
}
}
/**
* Submit handler for registration_form().
*/
function registration_send_confirmation($form, &$form_state) {
// Grab registration entity settings for confirmation email.
$entity_type = $form['#registration_settings']['entity_type'];
$entity_id = $form['#registration_settings']['entity_id'];
$settings = registration_entity_settings($entity_type, $entity_id);
// Send the confirmation email.
if ($settings['send_confirmation']) {
// Find an email address to send the confirmation.
if (isset($form_state['values']['anon_mail'])) {
$email = trim($form_state['values']['anon_mail']);
}
else {
global $user;
$email = $user->mail;
}
if (isset($email) && !empty($email) && valid_email_address($email)) {
$registration = $form_state['registration']; // Nice shorthand.
$entity = entity_load($entity_type, $entity_id);
$label = entity_label($entity_type, $entity);
$subject = t('Registration confirmation for !label', array('!label' => $label));
$context = array(
$entity_type => $entity,
'registration' => $registration,
);
if ($registration->user_uid) {
$account = user_load($registration->user_uid);
$context['user'] = $account;
}
$message = token_replace($settings['confirmation_template'], $context, array('clear' => TRUE));
$params = array(
'subject' => $subject,
'message' => $message,
);
if (!empty($settings['settings']['from_address'])) {
$params['from'] = $settings['settings']['from_address'];
}
global $language;
// Send the confirmation email.
backdrop_mail('registration', 'confirmation', $email, $language, $params);
}
}
}
/**
* Button submit function: handle the 'Delete' button on the node form.
*/
function registration_form_delete_submit($form, &$form_state) {
$destination = array();
if (isset($_GET['destination'])) {
$destination = backdrop_get_destination();
unset($_GET['destination']);
}
$registration = $form_state['registration'];
$form_state['redirect'] = array(
'registration/' .
$registration->registration_id . '/delete',
array('query' => $destination),
);
}
/**
* Menu callback -- ask for confirmation of node deletion.
*/
function registration_delete_confirm($form, &$form_state, $registration) {
// Always provide entity id in the same form key as in the entity edit form.
$form['registration'] = array(
'#type' => 'value',
'#value' => $registration,
);
return confirm_form($form,
t('Are you sure you want to delete registration %title?',
array('%title' => $registration->registration_id)
),
'registration/' . $registration->registration_id,
t('This action cannot be undone.'),
t('Delete'),
t('Cancel')
);
}
/**
* Execute node deletion.
*/
function registration_delete_confirm_submit($form, &$form_state) {
if ($form_state['values']['confirm']) {
$registration = $form_state['values']['registration'];
registration_delete_multiple(array($registration->registration_id));
watchdog('registration', 'Registration %id deleted.', array('%id' => $registration->registration_id));
backdrop_set_message(t('Registration %id has been deleted.', array('%id' => $registration->registration_id)));
$wrapper = entity_metadata_wrapper('registration', $registration);
$host = $wrapper->entity->value();
$uri = entity_uri($registration->entity_type, $host);
$form_state['redirect'] = $uri['path'];
}
}
/**
* Return a form for sending a broadcast email to participants.
*/
function registration_registrations_broadcast_form($form, &$form_state, $entity_type, $entity) {
// We'll need this info when we submit the form.
list($entity_id) = entity_extract_ids($entity_type, $entity);
$form_state['entity'] = array(
'entity_id' => $entity_id,
'entity_type' => $entity_type,
);
$form['subject'] = array(
'#type' => 'textfield',
'#title' => t('Subject'),
'#description' => t('The subject of the message.'),
'#required' => TRUE,
'#size' => 40,
'#maxlength' => 40,
);
$form['message'] = array(
'#type' => 'textarea',
'#title' => t('Message'),
'#description' => t(
'Enter the message you want to send to %name registrants. Tokens are supported, E.g., [node:title].',
array('%name' => entity_label($entity_type, $entity))
),
'#required' => TRUE,
'#cols' => 60,
'#rows' => 5,
);
// Message preview:
if (isset($form_state['registration_broadcast_preview'])) {
$form['preview'] = array(
'#type' => 'textarea',
'#title' => t('Message preview'),
'#value' => $form_state['registration_broadcast_preview'],
'#resizable' => FALSE,
'#disabled' => TRUE,
);
}
// Add token support:
$form['token_tree'] = array(
'#theme' => 'token_tree_link',
'#token_types' => array(
$entity_type,
'registration',
),
'#global_types' => FALSE,
);
$form['actions'] = array('#type' => 'actions');
$form['actions']['preview'] = array(
'#type' => 'submit',
'#value' => t('Preview'),
'#weight' => 10,
'#submit' => array('registration_broadcast_preview'),
);
$form['actions']['send'] = array(
'#type' => 'submit',
'#value' => t('Send'),
);
return $form;
}
/**
* @todo Please document this function.
* @see http://drupal.org/node/1354
*/
function registration_broadcast_preview($form, &$form_state) {
$entity_type = $form_state['entity']['entity_type'];
$entity_id = $form_state['entity']['entity_id'];
$message_template = $form_state['values']['message'];
$entity = entity_load($entity_type, $entity_id);
// Select one registration at random.
$registration = array();
$registration_type = registration_get_entity_registration_type($entity_type, $entity);
$query = "SELECT registration_id FROM {registration} WHERE type = :type LIMIT 1";
$registration_id = db_query($query, array(':type' => $registration_type))->fetchField();
if ($registration_id) {
$registration = entity_load('registration', $registration_id);
}
$data = array(
$entity_type => $entity,
'registration' => $registration,
);
$message = token_replace($message_template, $data);
$form_state['registration_broadcast_preview'] = $message;
$form_state['rebuild'] = TRUE;
}
/**
* Submit handler for registration_registrations_broadcast_form.
*/
function registration_registrations_broadcast_form_submit($form, &$form_state) {
registration_send_broadcast(
$form_state['entity']['entity_type'],
$form_state['entity']['entity_id'],
$form_state['values']['subject'],
$form_state['values']['message']
);
}
/**
* Return a form for an entity's registration settings.
*
* @param array $form
* @param array $form_state
* @param object $entity_type
* @param object $entity
*
* @return array $form
*
* @see hook_registration_entity_settings()
*/
function registration_entity_settings_form($form, &$form_state, $settings, $entity_type = NULL, $entity_id = NULL) {
if ($entity_id) {
// We'll need this info when we submit the form:
$form_state['entity'] = array(
'entity_id' => $entity_id,
'entity_type' => $entity_type,
);
}
$form['status'] = array(
'#type' => 'checkbox',
'#title' => t('Enable'),
'#description' => t('Check to enable registrations.'),
'#default_value' => isset($settings['status']) ? $settings['status'] : -1,
);
$form['capacity'] = array(
'#type' => 'textfield',
'#title' => t('Capacity'),
'#description' => t('The maximum number of registrants. Leave at 0 for no limit.'),
'#size' => 5,
'#maxlength' => 10,
'#required' => TRUE,
'#default_value' => isset($settings['capacity']) ? $settings['capacity'] : 0,
);
$form['scheduling'] = array(
'#type' => 'fieldset',
'#title' => t('Scheduling'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#tree' => TRUE,
);
$form['scheduling']['open'] = array(
'#type' => 'date_popup',
'#date_format' => 'Y-m-d H:i:s O',
'#title' => t('Open Date'),
'#description' => t('When to automatically open registrations. (This uses the !timezone.)', array(
'!timezone' => l(t('site default time zone'), 'admin/config/regional/settings'),
)),
'#default_value' => !empty($settings['open']) ? $settings['open'] : '',
);
$form['scheduling']['close'] = array(
'#type' => 'date_popup',
'#date_format' => 'Y-m-d H:i:s O',
'#title' => t('Close Date'),
'#description' => t('When to automatically close registrations. (This uses the !timezone.)', array(
'!timezone' => l(t('site default time zone'), 'admin/config/regional/settings'),
)),
'#default_value' => !empty($settings['close']) ? $settings['close'] : '',
);
$form['confirmation'] = array(
'#type' => 'fieldset',
'#title' => t('Confirmation'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#tree' => TRUE,
);
$form['confirmation']['send_confirmation'] = array(
'#type' => 'checkbox',
'#title' => t('Send confirmation email'),
'#default_value' => isset($settings['send_confirmation']) ? $settings['send_confirmation'] : -1,
);
$form['confirmation']['confirmation_settings'] = array(
'#type' => 'fieldset',
'#title' => t('Settings'),
'#collapsible' => FALSE,
'#states' => array(
'visible' => array(
':input[name="confirmation[send_confirmation]"]' => array('checked' => TRUE),
),
),
);
$form['confirmation']['confirmation_settings']['confirmation_subject'] = array(
'#type' => 'textfield',
'#title' => 'Reminder subject',
'#default_value' => t('Registration Confirmation for [entity:label]'),
'#size' => 128,
'#disabled' => TRUE,
);
$form['confirmation']['confirmation_settings']['confirmation_template'] = array(
'#type' => 'textarea',
'#title' => t('Template'),
'#default_value' => isset($settings['confirmation_template']) ? $settings['confirmation_template'] : '',
'#description' => t('The confirmation message sent to registrants. Tokens are supported, for example: [node:title].'),
);
// Add token support:
$form['confirmation']['confirmation_settings']['token_tree'] = array(
'#theme' => 'token_tree_link',
'#token_types' => array($entity_type, 'registration'),
'#global_types' => FALSE,
);
$form['reminder'] = array(
'#type' => 'fieldset',
'#title' => t('Reminder'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#tree' => TRUE,
);
$form['reminder']['send_reminder'] = array(
'#type' => 'checkbox',
'#title' => t('Send Reminder'),
'#description' => t('If checked, a reminder will be sent to registrants on the following date.'),
'#default_value' => isset($settings['send_reminder']) ? $settings['send_reminder'] : -1,
);
$form['reminder']['reminder_settings'] = array(
'#type' => 'fieldset',
'#title' => t('Settings'),
'#collapsible' => FALSE,
'#states' => array(
'visible' => array(
':input[name="reminder[send_reminder]"]' => array('checked' => TRUE),
),
),
);
$form['reminder']['reminder_settings']['reminder_date'] = array(
'#type' => 'date_popup',
'#date_format' => 'Y-m-d H:i:s O',
'#title' => t('Reminder Date'),
'#description' => t('When to send reminders. (This uses the !timezone.)', array(
'!timezone' => l(t('site default time zone'), 'admin/config/regional/settings'),
)),
'#default_value' => !empty($settings['reminder_date']) ? $settings['reminder_date'] : '',
);
$form['reminder']['reminder_settings']['reminder_subject'] = array(
'#type' => 'textfield',
'#title' => 'Reminder subject',
'#default_value' => t('Reminder for [entity:label]'),
'#size' => 128,
'#disabled' => TRUE,
);
$form['reminder']['reminder_settings']['reminder_template'] = array(
'#type' => 'textarea',
'#title' => t('Template'),
'#default_value' => isset($settings['reminder_template']) ? $settings['reminder_template'] : '',
'#description' => t('The reminder message sent to registrants. Tokens are supported, for example: [node:title].'),
);
// Add token support:
$form['reminder']['reminder_settings']['token_tree'] = array(
'#theme' => 'token_tree_link',
'#token_types' => array(
$entity_type,
'registration',
),
'#global_types' => FALSE,
);
$form['settings'] = array(
'#type' => 'fieldset',
'#title' => t('Additional Settings'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#tree' => TRUE,
);
$form['settings']['maximum_spaces'] = array(
'#type' => 'textfield',
'#title' => t('Spaces allowed'),
'#size' => 5,
'#maxlength' => 10,
'#required' => TRUE,
'#description' => t('The maximum number of spaces allowed for each registrations. For no limit, use 0. (Default is 1)'),
'#default_value' => isset($settings['settings']['maximum_spaces']) ? $settings['settings']['maximum_spaces'] : 1,
);
$form['settings']['multiple_registrations'] = array(
'#type' => 'checkbox',
'#title' => t('Allow multiple registrations'),
'#description' => t('If selected, each person can create multiple registrations for this event.'),
'#default_value' => isset($settings['settings']['multiple_registrations']) ? $settings['settings']['multiple_registrations'] : -1,
);
$form['settings']['from_address'] = array(
'#type' => 'textfield',
'#title' => t('From Address'),
'#description' => t('From email address to use for confirmations, reminders, and broadcast emails.'),
'#required' => TRUE,
'#default_value' => isset($settings['settings']['from_address']) ? $settings['settings']['from_address'] : config_get('system.core', 'site_mail'),
);
$form['settings']['confirmation'] = array(
'#type' => 'textfield',
'#title' => t('Confirmation Message'),
'#description' => t('The message to display when someone registers. Leave blank for none.'),
'#size' => 60,
'#maxlength' => 120,
'#required' => FALSE,
'#default_value' => isset($settings['settings']['confirmation']) ? $settings['settings']['confirmation'] : 'Registration has been saved.',
);
$form['settings']['confirmation_redirect'] = array(
'#type' => 'textfield',
'#title' => t('Confirmation redirect path'),
'#description' => t('Optional path to redirect to when someone registers. Leave blank to redirect to the registration itself if the user has permission or the host entity if they do not.'),
'#size' => 60,
'#maxlength' => 120,
'#required' => FALSE,
'#default_value' => isset($settings['settings']['confirmation_redirect']) ? $settings['settings']['confirmation_redirect'] : '',
);
// Allow other modules to add their own custom settings:
$form['settings'] += module_invoke_all('registration_entity_settings', $settings);
// Only show save if we're not on the field instance defaults:
if ($entity_id) {
$form['save'] = array(
'#type' => 'submit',
'#value' => t('Save Settings'),
);
}
return $form;
}
/**
* Submit handler for registration_entity_settings_form().
*
* @array $form
* @array $form_state
*/
function registration_entity_settings_form_validate($form, &$form_state) {
// Ensure capacity is a non-negative integer.
$capacity = $form_state['values']['capacity'];
if (!is_numeric($capacity) || ((int) $capacity != $capacity) || ($capacity < 0)) {
form_set_error('capacity', t('"Capacity" must be a positive integer or 0 for unlimited.'));
}
// Ensure slot limit is a non-negative integer.
$slot_limit = $form_state['values']['settings']['maximum_spaces'];
if (!is_numeric($slot_limit) || ((int) $slot_limit != $slot_limit) || ($slot_limit < 0)) {
form_set_error('maximum_spaces', t('"Spaces allowed" must be a positive integer or 0 for unlimited.'));
}
// Validate from address:
if (!valid_email_address($form_state['values']['settings']['from_address'])) {
form_set_error('settings][from_address', t('From email address is invalid.'));
}
// Validate open date:
if (!empty($form_state['values']['scheduling']['open']) && is_string($form_state['values']['scheduling']['open']) && strtotime($form_state['values']['scheduling']['open']) === FALSE) {
form_set_error('scheduling][open', t('Date is invalid.'));
}
// Validate close date:
if (!empty($form_state['values']['scheduling']['close']) && is_string($form_state['values']['scheduling']['close']) && strtotime($form_state['values']['scheduling']['close']) === FALSE) {
form_set_error('scheduling][close', t('Date is invalid.'));
}
// If sending a confirmation, ensure template is set.
$confirmation = $form_state['values']['confirmation'];
if ($confirmation['send_confirmation']
&& (empty($confirmation['confirmation_settings']['confirmation_template']))) {
form_set_error('confirmation][send_confirmation', t('If sending a confirmation, provide a template.'));
}
// If confirmation template is set, ensure we're sending a confirmation.
if (empty($reminder['send_confirmation'])) {
if (!empty($reminder['confirmation_settings']['confirmation_template'])) {
form_set_error('confirmation][confirmation_settings][confirmation_template', t('You have provided a confirmation template, but not opted to send a confirmation. Either check the box to send a confirmation, or do not provide a confirmation template.'));
}
}
// If sending a reminder, ensure date and template are set.
$reminder = $form_state['values']['reminder'];
if ($reminder['send_reminder']
&& (empty($reminder['reminder_settings']['reminder_date']) ||
empty($reminder['reminder_settings']['reminder_template']))
) {
form_set_error('reminder][send_reminder', t('If sending a reminder, provide a date and template.'));
}
// If reminder date or template are set, ensure we're sending a reminder.
if (empty($reminder['send_reminder'])) {
if (!empty($reminder['reminder_settings']['reminder_template'])) {
form_set_error('reminder][reminder_settings][reminder_template', t('You have provided a reminder template, but not opted to send a reminder. Either check the box to send a reminder, or do not provide a reminder template.'));
}
if (!empty($reminder['reminder_settings']['reminder_date'])) {
form_set_error('reminder][reminder_settings][reminder_date', t('You have provided a reminder date, but not opted to send a reminder. Either check the box to send a reminder, or do not provide a reminder date.'));
}
}
// Validate reminder date:
if (!empty($reminder['reminder_settings']['reminder_date'])
&& strtotime($reminder['reminder_settings']['reminder_date']) === FALSE
) {
form_set_error('reminder][reminder_settings][reminder_date', t('Reminder date is invalid.'));
}
// Ensure reminder date is not in the past when "send_reminder" is TRUE:
if ($reminder['send_reminder'] && !empty($reminder['reminder_settings']['reminder_date'])) {
if (strtotime($reminder['reminder_settings']['reminder_date']) <= time()) {
form_set_error('reminder][reminder_settings][reminder_date', t('Reminder must be in the future.'));
}
}
}
/**
* Submit handler for registration_entity_settings_form().
*
* @array $form
* @array $form_state
*/
function registration_entity_settings_form_submit($form, &$form_state) {
$entity = $form_state['entity'];
$fields = registration_convert_form_settings($form_state['values']);
registration_update_entity_settings($entity['entity_type'], $entity['entity_id'], $fields);
$uri = entity_uri($entity['entity_type'], entity_load($entity['entity_type'], $entity['entity_id']));
$form_state['redirect'] = $uri['path'];
}
/**
* Helper to convert registration entity settings form values into a fields.
*
* @param $values
*
* @return array
*/
function registration_convert_form_settings($values) {
$fields = array(
'status' => $values['status'],
'capacity' => $values['capacity'],
'send_confirmation' => isset($values['confirmation']['send_confirmation']) ? $values['confirmation']['send_confirmation'] : 0,
'confirmation_template' => isset($values['confirmation']['confirmation_settings']['confirmation_template']) ? $values['confirmation']['confirmation_settings']['confirmation_template'] : NULL,
'send_reminder' => isset($values['reminder']['send_reminder']) ? $values['reminder']['send_reminder'] : 0,
'reminder_template' => isset($values['reminder']['reminder_settings']['reminder_template']) ? $values['reminder']['reminder_settings']['reminder_template'] : NULL,
'reminder_date' => NULL,
'settings' => serialize($values['settings']),
'open' => NULL,
'close' => NULL,
);
// Reminder: Reminder Date:
if (!empty($values['reminder']['reminder_settings']['reminder_date'])) {
$fields['reminder_date'] = date('Y-m-d H:i:s', strtotime($values['reminder']['reminder_settings']['reminder_date']));
}
// Scheduling: open date:
if (!empty($values['scheduling']['open'])) {
$fields['open'] = date('Y-m-d H:i:s', strtotime($values['scheduling']['open']));
}
// Scheduling: close date:
if (!empty($values['scheduling']['close'])) {
$fields['close'] = date('Y-m-d H:i:s', strtotime($values['scheduling']['close']));
}
return $fields;
}
/**
* Generates the Registration state editing form.
*/
function registration_state_form($form, &$form_state, $registration_state, $op = 'edit') {
$form['label'] = array(
'#title' => t('Label'),
'#type' => 'textfield',
'#default_value' => $registration_state->label,
'#description' => t('The human-readable name of this registration state.'),
'#required' => TRUE,
'#size' => 30,
);
// Machine-readable type name.
$form['name'] = array(
'#type' => 'machine_name',
'#default_value' => $registration_state->id(),
'#maxlength' => 32,
// '#disabled' => $registration_state->locked && $op != 'clone',
'#machine_name' => array(
'exists' => 'registration_get_states',
'source' => array('label'),
),
'#description' => t('A unique machine-readable name for
this registration state. It must only contain lowercase letters,
numbers, and underscores.'),
);
$form['default_state'] = array(
'#title' => t('Default'),
'#type' => 'checkbox',
'#default_value' => isset($registration_state->default_state) ? $registration_state->default_state : 0,
'#attributes' => array('class' => array('reg-default')),
);
$form['weight'] = array(
'#title' => t('Weight'),
'#type' => 'weight',
'#default_value' => isset($registration_state->weight) ? $registration_state->weight : 0,
'#delta' => 15,
'#attributes' => array('class' => array('registration-state-weight')),
);
$form['description'] = array(
'#title' => t('Description'),
'#type' => 'textfield',
'#default_value' => isset($registration_state->description) ? $registration_state->description : '',
'#maxlength' => 128,
'#size' => 50,
);
$form['active'] = array(
'#title' => t('Active'),
'#type' => 'checkbox',
'#default_value' => isset($registration_state->active) ? $registration_state->active : 0,
);
$form['held'] = array(
'#title' => t('Held'),
'#type' => 'checkbox',
'#default_value' => isset($registration_state->held) ? $registration_state->held : 0,
);
$form['show_on_form'] = array(
'#title' => t('Show on form'),
'#type' => 'checkbox',
'#default_value' => isset($registration_state->show_on_form) ? $registration_state->show_on_form : 0,
);
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save Registration state'),
'#weight' => 40,
);
return $form;
}
/**
* Form API submit callback for the type form.
*/