-
Notifications
You must be signed in to change notification settings - Fork 7
/
profile.module
1181 lines (1078 loc) · 36.3 KB
/
profile.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
* Support for configurable user profiles.
*/
/**
* Profile constant for user-defined profiles.
*/
define('PROFILE_STATUS_DISABLED', 0);
/**
* Profile constant for module-defined profiles.
*/
define('PROFILE_STATUS_ENABLED', 1);
/**
* Profile constant for user-defined profiles.
*/
define('PROFILE_STORAGE_NORMAL', 1);
/**
* Profile constant for profiles that override module-defined presets.
*/
define('PROFILE_STORAGE_OVERRIDE', 2);
/**
* Profile constant for module-defined profiles.
*/
define('PROFILE_STORAGE_DEFAULT', 4);
/**
* Implements hook_entity_info().
*/
function profile_entity_info() {
$return = array(
'profile' => array(
'label' => t('Profile'),
'description' => t('Profile user profiles.'),
'entity class' => 'Profile',
'controller class' => 'EntityDatabaseStorageController',
'base table' => 'profile',
'fieldable' => TRUE,
'view modes' => array(
'account' => array(
'label' => t('User account'),
'custom settings' => FALSE,
),
),
'entity keys' => array(
'id' => 'pid',
'bundle' => 'type',
'label' => 'label',
),
'bundles' => array(),
'bundle keys' => array(
'bundle' => 'type',
),
'label callback' => 'entity_class_label',
'uri callback' => 'entity_class_uri',
'access callback' => 'profile_access',
'module' => 'profile',
'metadata controller class' => 'ProfileMetadataController',
'token type' => 'profile',
),
);
$types = profile_get_types();
foreach ($types as $type => $info) {
$return['profile']['bundles'][$type] = array(
'label' => $info->label,
'admin' => array(
'path' => 'admin/structure/profiles/manage/%profile_type',
'real path' => 'admin/structure/profiles/manage/' . $type,
'bundle argument' => 4,
'access arguments' => array('administer profiles'),
),
);
}
return $return;
}
/**
* Implements hook_entity_info_alter().
*/
function profile_entity_info_alter(&$entity_info) {
// Switch to the controller provided by Entity Plus if available.
if (module_exists('entity_plus')) {
$entity_info['profile']['controller class'] = 'EntityPlusController';
}
}
/**
* Menu argument loader; Load a profile type by string.
*
* @param $type
* The machine-readable name of a profile type to load.
* @return
* A profile type array or FALSE if $type does not exist.
*/
function profile_type_load($type) {
return profile_get_types($type);
}
/**
* Implements hook_forms().
*/
function profile_forms() {
$forms['profile_user_profile_form'] = array(
'callback' => 'user_profile_form',
);
return $forms;
}
/**
* Implements hook_menu().
*/
function profile_menu() {
$items = array();
if (config_get('profile.settings', 'profile_display') == 'tabs') {
foreach (profile_get_types() as $type_name => $profile_type) {
$items['user/%user/edit/' . $type_name] = array(
'title' => $profile_type->label,
'weight' => $profile_type->weight,
'page callback' => 'backdrop_get_form',
'page arguments' => array('profile_user_profile_form', 1, 3),
'access callback' => 'profile_user_edit_access',
'access arguments' => array(1, $type_name),
'type' => MENU_LOCAL_TASK,
'file path' => backdrop_get_path('module', 'user'),
'file' => 'user.pages.inc',
);
}
}
$items['user/%user/edit/account'] = array(
'title' => 'Edit account',
'page callback' => 'backdrop_get_form',
'page arguments' => array('user_profile_form', 1),
'weight' => -30,
'type' => MENU_DEFAULT_LOCAL_TASK,
'file path' => backdrop_get_path('module', 'user'),
'file' => 'user.pages.inc',
);
$items['admin/config/people/profiles'] = array(
'title' => 'Profile display',
'page callback' => 'backdrop_get_form',
'page arguments' => array('profile_display_settings_form'),
'description' => 'Configure how profile edit forms are displayed when editing accounts.',
'access arguments' => array('administer profiles'),
'file' => 'profile.admin.inc',
);
$items['admin/structure/profiles'] = array(
'title' => 'Profiles',
'page callback' => 'backdrop_get_form',
'page arguments' => array('profile_type_overview_form', 'profile_type'),
'description' => 'Manage profiles.',
'access callback' => 'profile_access',
'access arguments' => array('view'),
'file' => 'profile.admin.inc',
);
$items['admin/structure/profiles/list'] = array(
'title' => 'List',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items['admin/structure/profiles/add'] = array(
'title' => 'Add profile',
'page callback' => 'backdrop_get_form',
'page arguments' => array('profile_type_form', NULL, 'add'),
'access callback' => 'profile_access',
'access arguments' => array('create'),
'type' => MENU_LOCAL_ACTION,
'file' => 'profile.admin.inc',
);
$items['admin/structure/profiles/manage/%profile_type'] = array(
'title callback' => 'profile_type_title',
'title arguments' => array(4, 'Edit'),
'page callback' => 'backdrop_get_form',
'page arguments' => array('profile_type_form', 4),
'access callback' => 'profile_access',
'access arguments' => array('update', 4),
'file' => 'profile.admin.inc',
);
$items['admin/structure/profiles/manage/%profile_type/edit'] = array(
'title' => 'Edit',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
// Clone form, a special case for the edit form.
$items['admin/structure/profiles/manage/%profile_type/clone'] = array(
'title callback' => 'profile_type_title',
'title arguments' => array(4, 'Clone'),
'page callback' => 'backdrop_get_form',
'page arguments' => array('profile_type_form', 4, 'clone'),
'access callback' => 'profile_access',
'access arguments' => array('create', 4),
'file' => 'profile.admin.inc',
);
// Delete form.
$items['admin/structure/profiles/manage/%profile_type/delete'] = array(
'page callback' => 'backdrop_get_form',
'page arguments' => array('profile_type_delete_form', 4),
'access callback' => 'profile_access',
'access arguments' => array('delete', 4),
'file' => 'profile.admin.inc',
);
// Define page which provides form to generate profiles using
// Devel generate module.
if (module_exists('devel')) {
$items['admin/devel/generate/profile'] = array(
'title' => 'Generate profiles',
'description' => 'Generate a given number of profiles for users. Optionally override current user profiles.',
'access arguments' => array('administer profiles'),
'page callback' => 'backdrop_get_form',
'page arguments' => array('profile_generate_form'),
'file' => 'profile.devel.inc',
);
}
return $items;
}
/**
* Menu callback. Build page title.
*/
function profile_type_title($type, $op = NULL) {
return $op . ' ' . check_plain($type->label);
}
/**
* Access callback for editing profiles.
*/
function profile_user_edit_access($account, $type_name) {
if (!user_edit_access($account)) {
return FALSE;
}
$profile = profile_load_by_user($account, $type_name);
if (empty($profile)) {
$profile = profile_create(array('type' => $type_name, 'uid' => $account->uid));
}
if (profile_access('edit', $profile, $account)) {
return TRUE;
}
return FALSE;
}
/**
* Implements hook_permission().
*/
function profile_permission() {
$permissions = array(
'administer profile types' => array(
'title' => t('Administer profile types'),
'description' => t('Create and delete fields on user profiles, and set their permissions.'),
),
'administer profiles' => array(
'title' => t('Administer profiles'),
'description' => t('Edit and view all user profiles.'),
),
);
// Generate per profile type permissions.
foreach (profile_get_types() as $type) {
$type_name = check_plain($type->type);
$permissions += array(
"edit own $type_name profile" => array(
'title' => t('%type_name: Edit own profile', array('%type_name' => $type->label)),
),
"edit any $type_name profile" => array(
'title' => t('%type_name: Edit any profile', array('%type_name' => $type->label)),
),
"view own $type_name profile" => array(
'title' => t('%type_name: View own profile', array('%type_name' => $type->label)),
),
"view any $type_name profile" => array(
'title' => t('%type_name: View any profile', array('%type_name' => $type->label)),
),
);
}
return $permissions;
}
/**
* Read all configuration files from disk of a particular profile type.
*
* @param $type
* A specific profile type.
* @return array
* An array of all configs for the specified type.
*/
function profile_get_types($type = NULL) {
$configs = &backdrop_static(__FUNCTION__, array());
if (empty($configs)) {
$config_names = config_get_names_with_prefix('profile.type');
foreach ($config_names as $config_file) {
$config = config($config_file);
$data = $config->get();
$data += array(
'weight' => 0,
);
$data = (object) $data;
$configs[$data->type] = $data;
}
// Sort the configs by weight.
backdrop_sort($configs, array(
'weight' => SORT_NUMERIC,
));
}
if ($type) {
if (isset($configs[$type])) {
return $configs[$type];
}
return array();
}
return $configs;
}
/**
* Fetch a profile object.
*
* @param $pid
* Integer specifying the profile id.
* @param $reset
* A boolean indicating that the internal cache should be reset.
* @return
* A fully-loaded $profile object or FALSE if it cannot be loaded.
*
* @see profile_load_multiple()
*/
function profile_load($pid, $reset = FALSE) {
$profiles = profile_load_multiple(array($pid), array(), $reset);
return reset($profiles);
}
/**
* Load multiple profiles based on certain conditions.
*
* @param $pids
* An array of profile IDs.
* @param $conditions
* An array of conditions to match against the {profile} table.
* @param $reset
* A boolean indicating that the internal cache should be reset.
* @return
* An array of profile objects, indexed by pid.
*
* @see entity_load()
* @see profile_load()
* @see profile_load_by_user()
*/
function profile_load_multiple($pids = array(), $conditions = array(), $reset = FALSE) {
return entity_load('profile', $pids, $conditions, $reset);
}
/**
* Fetch profiles by account.
*
* @param $account
* The user account to load profiles for, or its uid.
* @param $type_name
* To load a single profile, pass the type name of the profile to load.
* @return
* Either a single profile or an array of profiles keyed by profile type.
*
* @see profile_load_multiple()
* @see profile_profile_delete()
* @see Profile::save()
*/
function profile_load_by_user($account, $type_name = NULL) {
// Use a separate query to determine all profile ids per user and cache them.
// That way we can look up profiles by id and benefit from the static cache
// of the entity loader.
$cache = &backdrop_static(__FUNCTION__, array());
$uid = is_object($account) ? $account->uid : $account;
if (!isset($cache[$uid])) {
if (empty($type_name)) {
$profiles = profile_load_multiple(FALSE, array('uid' => $uid));
// Cache ids for further lookups.
$cache[$uid] = array();
foreach ($profiles as $pid => $profile) {
$cache[$uid][$profile->type] = $pid;
}
return $profiles ? array_combine(array_keys($cache[$uid]), $profiles) : array();
}
$cache[$uid] = db_select('profile', 'p')
->fields('p', array('type', 'pid'))
->condition('uid', $uid)
->execute()
->fetchAllKeyed();
}
if (isset($type_name)) {
return isset($cache[$uid][$type_name]) ? profile_load($cache[$uid][$type_name]) : FALSE;
}
// Return an array containing profiles keyed by profile type.
return $cache[$uid] ? array_combine(array_keys($cache[$uid]), profile_load_multiple($cache[$uid])) : $cache[$uid];
}
/**
* Implements hook_profile_delete().
*/
function profile_profile_delete($profile) {
// Clear the static cache from profile_load_by_user().
$cache = &backdrop_static('profile_load_by_user', array());
unset($cache[$profile->uid][$profile->type]);
}
/**
* Deletes a profile.
*/
function profile_delete(Profile $profile) {
$profile->delete();
}
/**
* Delete multiple profiles.
*
* @param $pids
* An array of profile IDs.
*/
function profile_delete_multiple(array $pids) {
entity_get_controller('profile')->delete($pids);
}
/**
* Implements hook_user_delete().
*/
function profile_user_delete($account) {
foreach (profile_load_by_user($account) as $profile) {
profile_delete($profile);
}
}
/**
* Create a new profile object.
*/
function profile_create(array $values) {
return new Profile($values);
}
/**
* Saves a profile to the database.
*
* @param $profile
* The profile object.
*/
function profile_save(Profile $profile) {
return $profile->save();
}
/**
* Saves a profile type to config.
*/
function profile_type_save($type) {
if (empty($type->type)) {
throw new Exception(t('Profile type key cannot be empty.'));
}
if ($type->storage === PROFILE_STORAGE_DEFAULT) {
$type->storage = PROFILE_STORAGE_NORMAL;
}
if (empty($type->label)) {
$type->label = $type->type;
}
$data = (array) $type;
$data += array(
'userview' => TRUE,
'registration' => 0,
'roles' => array(
'authenticated' => 'authenticated',
),
'module' => NULL,
'weight' => '0',
'storage' => 1,
'status' => 1,
'_config_translatables' => array(
'label',
),
);
// Allow modules to act on profile data before saving.
foreach (module_implements('profile_presave') as $module) {
$function = $module . '_profile_presave';
$function($type->name, $data);
}
config('profile.type.' . $data['type'])
->setData($data)
->save();
}
/**
* Deletes a profile type from config.
*/
function profile_type_delete($type) {
$config = config('profile.type.' . $type->type);
$config->delete();
field_attach_delete_bundle('profile', $type->type);
module_invoke_all('profile_type_delete', $type);
backdrop_static_reset('profile_get_types');
}
/**
* Implements hook_profile_type_delete().
*/
function profile_profile_type_delete($type) {
$pids = array_keys(profile_load_multiple(FALSE, array('type' => $type->type)));
if ($pids) {
profile_delete_multiple($pids);
}
}
/**
* Implements hook_user_view().
*/
function profile_user_view($account, $view_mode, $langcode) {
foreach (profile_get_types() as $type => $profile_type) {
if ($profile_type->userview && $profile = profile_load_by_user($account, $type)) {
if (profile_access('view', $profile)) {
$label = config_get_translated("profile.type.$type", 'label', array(), array('context' => "profile_type:$type:label"));
$account->content['profile_' . $type] = array(
'#type' => 'item',
'#title' => check_plain($label),
'#prefix' => '<a id="profile-' . $profile->type . '"></a>',
);
$render = $profile->view('account');
$account->content['profile_' . $type]['#markup'] = render($render);
}
}
}
}
/**
* Implements hook_form_FORM_ID_alter() for the user edit form.
*
* @see profile_form_validate_handler()
* @see profile_form_submit_handler()
*/
function profile_form_user_profile_form_alter(&$form, &$form_state, $form_id) {
$profile_display = config_get('profile.settings', 'profile_display');
$profile_types = profile_get_types();
$type_name = NULL;
if (isset($form_state['build_info']['args'][1])) {
$type_name = $form_state['build_info']['args'][1];
}
if ($profile_display == 'tabs') {
if ($form_id == 'profile_user_profile_form' && $type_name && isset($profile_types[$type_name])) {
$form_fields_to_keep = array('actions', 'form_build_id', 'form_token', 'form_id');
foreach (element_children($form) as $key) {
if (!in_array($key, $form_fields_to_keep)) {
$form[$key]['#access'] = FALSE;
}
}
// Only show the 'submit' button on this form, and hide any others (such as
// 'cancel account').
foreach (element_children($form['actions']) as $key) {
if ($key != 'submit') {
$form['actions'][$key]['#access'] = FALSE;
}
}
$profile = profile_load_by_user($form_state['user'], $type_name);
if (empty($profile)) {
$profile = profile_create(array('type' => $type_name, 'uid' => $form_state['user']->uid));
}
$form_state['profiles'][$profile->type] = $profile;
profile_attach_form($form, $form_state);
}
}
else {
foreach (profile_get_types() as $type_name => $profile_type) {
$profile = profile_load_by_user($form_state['user'], $type_name);
if (empty($profile)) {
$profile = profile_create(array('type' => $type_name, 'uid' => $form_state['user']->uid));
}
// Add the profile edit form if (a) the user has edit access to the
// profile, and (b) the account being edited has a role that gives them
// this profile.
$account_roles = $form_state['user']->roles;
$profile_roles = array_keys($profile_type->roles);
$matches = array_intersect($account_roles, $profile_roles);
$account_has_role = !empty($matches);
if (profile_access('edit', $profile) && $account_has_role) {
$form_state['profiles'][$profile->type] = $profile;
profile_attach_form($form, $form_state);
// Wrap each profile form in a fieldset.
$label = config_get_translated("profile.type.$type_name", 'label', array(), array('context' => "profile_type:$type_name:label"));
$form['profile_' . $type_name] += array(
'#type' => 'fieldset',
'#title' => check_plain($label),
);
}
}
}
return;
}
/**
* Implements hook_form_FORM_ID_alter() for the registration form.
*/
function profile_form_user_register_form_alter(&$form, &$form_state) {
foreach (profile_get_types() as $type_name => $profile_type) {
if (!empty($profile_type->registration)) {
if (empty($form_state['profiles'][$type_name])) {
$form_state['profiles'][$type_name] = profile_create(array('type' => $type_name));
}
}
}
// If we have profiles to attach to the registration form - then do it.
if (!empty($form_state['profiles'])) {
profile_attach_form($form, $form_state);
// Wrap each profile form in a fieldset.
foreach ($form_state['profiles'] as $type_name => $profile_type) {
$label = config_get_translated("profile.type.$type_name", 'label', array(), array('context' => "profile_type:$type_name:label"));
$form['profile_' . $type_name] += array(
'#type' => 'fieldset',
'#title' => check_plain($label),
);
}
}
}
/**
* Implements hook_module_implements_alter().
*
* Ensure that execution of profile_form_user_register_form_alter()
* is happens after all other implementations of hook_form_FORM_ID_alter().
*/
function profile_module_implements_alter(&$implementations, $hook) {
if ($hook == 'form_alter' && isset($implementations['profile'])) {
$group = $implementations['profile'];
unset($implementations['profile']);
$implementations['profile'] = $group;
}
}
/**
* Attaches the profile forms of the profiles set in
* $form_state['profiles'].
*
* Modules may alter the profile entity form regardless to which form it is
* attached by making use of hook_form_profile_form_alter().
*
* @param $form
* The form to which to attach the profile form. For each profile the form
* is added to @code $form['profile_' . $profile->type] @endcode. This helper
* also adds in a validation and a submit handler caring for the attached
* profile forms.
*
* @see hook_form_profile_form_alter()
* @see profile_form_validate_handler()
* @see profile_form_submit_handler()
*/
function profile_attach_form(&$form, &$form_state) {
foreach ($form_state['profiles'] as $type => $profile) {
$form['profile_' . $profile->type]['#tree'] = TRUE;
$form['profile_' . $profile->type]['#parents'] = array('profile_' . $profile->type);
field_attach_form('profile', $profile, $form['profile_' . $profile->type], $form_state);
if (user_access('administer profile types')) {
if (count(field_info_instances('profile', $profile->type)) == 0) {
$form['profile_' . $profile->type]['message'] = array(
'#markup' => t('No fields have been associated with this profile type. Go to the <a href="!url">Profile types</a> page to add some fields.', array('!url' => url('admin/structure/profiles'))),
);
}
}
// Provide a central place for modules to alter the profile forms, but
// skip that in case the caller cares about invoking the hooks.
// @see profile_form()
if (!isset($form_state['profile_skip_hook'])) {
$hooks[] = 'form_profile_edit_' . $type . '_form';
$hooks[] = 'form_profile_form';
backdrop_alter($hooks, $form, $form_state);
}
}
$form['#validate'][] = 'profile_form_validate_handler';
// Default name of user registry form callback.
$register_submit_callback = 'user_register_submit';
// LoginToBoggan module replaces default user_register_submit() callback
// with his own. So if this module enabled we need to track his callback
// instead one that comes from the User module.
if (module_exists('logintoboggan')) {
$register_submit_callback = 'logintoboggan_user_register_submit';
}
// Search for key of user register submit callback.
if (!empty($form['#submit']) && is_array($form['#submit'])) {
$submit_key = array_search($register_submit_callback, $form['#submit']);
}
if (isset($submit_key) && $submit_key !== FALSE) {
// Insert submit callback right before the user register submit callback.
// Needs for disabling email notification during user registration.
array_splice($form['#submit'], $submit_key, 0, array('profile_form_before_user_register_submit_handler'));
// Add a submit callback right after the user register submit callback.
// This is needed for creation of a new user profile.
array_splice($form['#submit'], $submit_key + 2, 0, array('profile_form_submit_handler'));
// Insert submit handler right after the creation of new user profile.
// This is needed for sending email which was blocked during registration.
array_splice($form['#submit'], $submit_key + 3, 0, array('profile_form_after_user_register_submit_handler'));
}
else {
// Fallback if some contrib module removes user register submit callback
// from form submit functions.
$form['#submit'][] = 'profile_form_submit_handler';
}
}
/**
* Validation handler for the profile form.
*
* @see profile_attach_form()
*/
function profile_form_validate_handler(&$form, &$form_state) {
foreach ($form_state['profiles'] as $type => $profile) {
if (isset($form_state['values']['profile_' . $profile->type])) {
// @see entity_form_field_validate()
$pseudo_entity = (object) $form_state['values']['profile_' . $profile->type];
$pseudo_entity->type = $type;
field_attach_form_validate('profile', $pseudo_entity, $form['profile_' . $profile->type], $form_state);
}
}
}
/**
* User registration form submit handler that executes right before
* user_register_submit().
*
* This callback disables the notification emails during the execution of the
* user_register_submit() callback. The reason for this is that we want to
* support profile tokens in emails during registration, and there is no other
* proper way to do this. See https://drupal.org/node/1097684.
*
* @see profile_form_after_user_register_submit_handler()
* @see user_register_submit()
* @see profile_attach_form()
*/
function profile_form_before_user_register_submit_handler(&$form, &$form_state) {
$config = config('system.core');
// List of available operations during the registration.
$register_ops = array(
'register_admin_created',
'register_no_approval_required',
'register_pending_approval',
);
// We also have to track if we change a variables, because later we have to
// restore them.
$changed_ops = &backdrop_static('profile_register_changed_operations', array());
foreach ($register_ops as $op) {
// Save variable value.
$register_op = 'user_mail_' . $op . '_notify';
if ($config->get($register_op)) {
$changed_ops[$register_op] = $config->get($register_op);
}
// Temporary disable the notification about registration.
$config->set($register_op, FALSE);
}
$config->save();
}
/**
* User registration form submit handler that executes right after
* user_register_submit().
*
* This callback sends delayed email notification to a user
* about their registration. See https://drupal.org/node/1097684.
*
* @see profile_form_prepare_user_register_submit_handler()
* @see user_register_submit()
* @see profile_attach_form()
*/
function profile_form_after_user_register_submit_handler(&$form, &$form_state) {
$config = config('system.core');
// List of registration operations where notification values were changed.
$changed_ops = &backdrop_static('profile_register_changed_operations', array());
// List of available operations during the registration.
$register_ops = array(
'register_admin_created',
'register_no_approval_required',
'register_pending_approval',
);
foreach ($register_ops as $op) {
// If we changed the notification value in
// profile_form_before_user_register_submit_handler() then change it back.
$register_op = 'user_mail_' . $op . '_notify';
if (isset($changed_ops[$register_op])) {
$config->set($register_op, $changed_ops[$register_op]);
}
// Otherwise, if we didn't change it, then it was originally TRUE, so make
// it so.
else {
$config->set($register_op, TRUE);
}
}
$config->save();
// Get the values that we need to define which notification
// should be sent to the user. Generally this is a trimmed version
// of user_register_submit() callback.
$admin = !empty($form_state['values']['administer_users']);
$account = $form_state['user'];
$notify = !empty($form_state['values']['notify']);
if ($admin && !$notify) {
// If admin has created a new account and decided to don't notify a user -
// then just do nothing.
}
elseif (!$admin && !config_get('system.core', 'user_email_verification') && $account->status) {
_user_mail_notify('register_no_approval_required', $account);
}
// No administrator approval required.
elseif ($account->status || $notify) {
$op = $notify ? 'register_admin_created' : 'register_no_approval_required';
_user_mail_notify($op, $account);
}
// Administrator approval required.
elseif (!$admin) {
_user_mail_notify('register_pending_approval', $account);
}
}
/**
* Submit handler that builds and saves all profiles in the form.
*
* @see profile_attach_form()
*/
function profile_form_submit_handler(&$form, &$form_state) {
profile_form_submit_build_profile($form, $form_state);
// This is needed as some submit callbacks like user_register_submit() rely on
// clean form values.
profile_form_submit_cleanup($form, $form_state);
foreach ($form_state['profiles'] as $type => $profile) {
// During registration set the uid field of the newly created user.
if (empty($profile->uid) && isset($form_state['user']->uid)) {
$profile->uid = $form_state['user']->uid;
}
profile_save($profile);
}
}
/**
* Submit builder. Extracts the form values and updates the profile entities.
*
* @see profile_attach_form()
*/
function profile_form_submit_build_profile(&$form, &$form_state) {
foreach ($form_state['profiles'] as $type => $profile) {
// @see entity_form_submit_build_entity()
if (isset($form['profile_' . $type]['#entity_builders'])) {
foreach ($form['profile_' . $type]['#entity_builders'] as $function) {
$function('profile', $profile, $form['profile_' . $type], $form_state);
}
}
field_attach_submit('profile', $profile, $form['profile_' . $type], $form_state);
}
}
/**
* Cleans up the form values as the user modules relies on clean values.
*
* @see profile_attach_form()
*/
function profile_form_submit_cleanup(&$form, &$form_state) {
foreach ($form_state['profiles'] as $type => $profile) {
unset($form_state['values']['profile_' . $type]);
}
}
/**
* Determines whether the given user has access to a profile.
*
* @param $op
* The operation being performed. One of 'view', 'update', 'create', 'delete'
* or just 'edit' (being the same as 'create' or 'update').
* @param $profile
* (optional) A profile to check access for. If nothing is given, access for
* all profiles is determined.
* @param $account
* The user to check for. Leave it to NULL to check for the global user.
* @return boolean
* Whether access is allowed or not.
*
* @see hook_profile_access()
* @see profile_profile_access()
*/
function profile_access($op, $profile = NULL, $account = NULL) {
// With access to all profiles there is no need to check further.
if (user_access('administer profiles', $account)) {
return TRUE;
}
// Check if profile user has current profile available by role.
if (isset($profile->type)) {
$profile_type = profile_type_load($profile->type);
if (!empty($profile_type) && !empty($profile_type->roles) && isset($profile->uid)) {
$profile_user = user_load($profile->uid);
$profile_roles = array_keys($profile_type->roles);
$user_roles = $profile_user->roles;
$matches = array_intersect($profile_roles, $user_roles);
if (empty($matches)) {
return FALSE;
}
}
}
if ($op == 'create' || $op == 'update') {
$op = 'edit';
}
// Allow modules to grant / deny access.
$access = module_invoke_all('profile_access', $op, $profile, $account);
// Only grant access if at least one module granted access and no one denied
// access.
if (in_array(FALSE, $access, TRUE)) {
return FALSE;
}
elseif (in_array(TRUE, $access, TRUE)) {
return TRUE;
}
return FALSE;
}
/**
* Implements hook_profile_access().
*/
function profile_profile_access($op, $profile = NULL, $account = NULL) {
// Don't grant access for users to delete their profile.
if (isset($profile) && ($type_name = $profile->type) && $op != 'delete') {
if (user_access("$op any $type_name profile", $account)) {
return TRUE;
}
$account = isset($account) ? $account : $GLOBALS['user'];
if (isset($profile->uid) && $profile->uid == $account->uid && user_access("$op own $type_name profile", $account)) {
return TRUE;
}
}
// Do not explicitly deny access so others may still grant access.
}
/**
* Access callback for the entity API.
*/
function profile_type_access($op, $type = NULL, $account = NULL) {
return user_access('administer profile types', $account);
}
/**
* Implements hook_theme().
*/
function profile_theme() {
return array(
'profile' => array(
'render element' => 'elements',
'template' => 'templates/profile',
'file' => 'profile.theme.inc',
),
);
}
/**
* View a profile.
*
* @see Profile::view()
*/
function profile_view($profile, $view_mode = 'full', $langcode = NULL, $page = NULL) {
return $profile->view($view_mode, $langcode, $page);
}
/**
* Implements hook_form_FORMID_alter().