profile2_page.inc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * @file
  4. * Adds separate pages for viewing and editing profiles.
  5. */
  6. /**
  7. * Shows the profile page for the current user.
  8. *
  9. * @see user_page()
  10. */
  11. function profile2_page_own($base_path) {
  12. global $user;
  13. if ($user->uid) {
  14. menu_set_active_item($base_path . '/' . $user->uid);
  15. return menu_execute_active_handler(NULL, FALSE);
  16. }
  17. else {
  18. return drupal_get_form('user_login');
  19. }
  20. }
  21. /**
  22. * Profile view page.
  23. */
  24. function profile2_page_view($profile) {
  25. return $profile->view('page', NULL, TRUE);
  26. }
  27. /**
  28. * The profile edit form.
  29. */
  30. function profile2_form($form, &$form_state, $profile) {
  31. global $user;
  32. if (empty($form_state['profiles'])) {
  33. $form_state['profiles'][$profile->type] = $profile;
  34. }
  35. // Prevent invoking the same hooks twice, so tell profile2_attach_form() to
  36. // skip invoking the hooks.
  37. $form_state['profile2_skip_hook'] = TRUE;
  38. profile2_attach_form($form, $form_state);
  39. $form['actions'] = array('#type' => 'actions');
  40. $form['actions']['submit'] = array(
  41. '#type' => 'submit',
  42. '#value' => t('Save'),
  43. '#weight' => 40,
  44. );
  45. if (user_access('administer profiles') && $user->uid != $profile->uid) {
  46. $delete_button_label = t('Delete profile');
  47. }
  48. elseif (user_access("delete own $profile->type profile") && $user->uid === $profile->uid) {
  49. $delete_button_label = t('Delete this profile');
  50. }
  51. if (empty($profile->is_new) && !empty($delete_button_label)) {
  52. $form['actions']['delete'] = array(
  53. '#type' => 'submit',
  54. '#value' => $delete_button_label,
  55. '#weight' => 45,
  56. '#limit_validation_errors' => array(),
  57. '#submit' => array('profile2_form_submit_delete')
  58. );
  59. }
  60. $form['#submit'][] = 'profile2_form_submit';
  61. return $form;
  62. }
  63. /**
  64. * Profile form submit handler.
  65. */
  66. function profile2_form_submit($form, &$form_state) {
  67. // The profile is being saved by profile2_form_submit_handler().
  68. drupal_set_message(t('The changes have been saved.'));
  69. $form_state['redirect'] = $form_state['profile2']->path();
  70. }
  71. /**
  72. * Profile form submit handler for the delete button.
  73. */
  74. function profile2_form_submit_delete($form, &$form_state) {
  75. $form_state['redirect'] = $form_state['profile2']->path() . '/delete';
  76. }
  77. /**
  78. * Confirm form for deleting a profile.
  79. */
  80. function profile2_page_delete_confirm_form($form, &$form_state, $profile) {
  81. global $user;
  82. $form_state += array('profile2' => $profile);
  83. $type = profile2_get_types($profile->type);
  84. if (isset($profile->uid) && $profile->uid === $user->uid) {
  85. $confirm_question = t('Are you sure you want to delete your own %label profile ?',
  86. array('%label' => $type->getTranslation('label')));
  87. }
  88. elseif (user_access('administer profiles')) {
  89. $user_account = user_load($profile->uid);
  90. if (!empty($user_account)) {
  91. $confirm_question = t("Are you sure you want to delete profile %label of user %user?",
  92. array('%label' => $type->getTranslation('label'), '%user' => $user_account->name));
  93. }
  94. }
  95. return confirm_form($form, $confirm_question, $profile->path());
  96. }
  97. function profile2_page_delete_confirm_form_submit($form, &$form_state) {
  98. $profile = $form_state['profile2'];
  99. $type = profile2_get_types($profile->type);
  100. $profile->delete();
  101. drupal_set_message(t('Deleted %label.', array('%label' => $type->getTranslation('label'))));
  102. $form_state['redirect'] = 'user/' . $profile->uid;
  103. }