profile2_page.inc 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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');
  26. }
  27. /**
  28. * The profile edit form.
  29. */
  30. function profile2_form($form, &$form_state, $profile) {
  31. if (empty($form_state['profiles'])) {
  32. $form_state['profiles'][$profile->type] = $profile;
  33. }
  34. // Prevent invoking the same hooks twice, so tell profile2_attach_form() to
  35. // skip invoking the hooks.
  36. $form_state['profile2_skip_hook'] = TRUE;
  37. profile2_attach_form($form, $form_state);
  38. $form['actions'] = array('#type' => 'actions');
  39. $form['actions']['submit'] = array(
  40. '#type' => 'submit',
  41. '#value' => t('Save'),
  42. '#weight' => 40,
  43. );
  44. if (empty($profile->is_new) && user_access('administer profiles')) {
  45. $form['actions']['delete'] = array(
  46. '#type' => 'submit',
  47. '#value' => t('Delete profile'),
  48. '#weight' => 45,
  49. '#limit_validation_errors' => array(),
  50. '#submit' => array('profile2_form_submit_delete')
  51. );
  52. }
  53. $form['#submit'][] = 'profile2_form_submit';
  54. return $form;
  55. }
  56. /**
  57. * Profile form submit handler.
  58. */
  59. function profile2_form_submit($form, &$form_state) {
  60. // The profile is being saved by profile2_form_submit_handler().
  61. drupal_set_message(t('The changes have been saved.'));
  62. $form_state['redirect'] = $form_state['profile2']->path();
  63. }
  64. /**
  65. * Profile form submit handler for the delete button.
  66. */
  67. function profile2_form_submit_delete($form, &$form_state) {
  68. $form_state['redirect'] = $form_state['profile2']->path() . '/delete';
  69. }
  70. /**
  71. * Confirm form for deleting a profile.
  72. */
  73. function profile2_page_delete_confirm_form($form, &$form_state, $profile) {
  74. $form_state += array('profile2' => $profile);
  75. $confirm_question = t('Are you sure you want to delete profile %label?', array('%label' => $profile->label()));
  76. return confirm_form($form, $confirm_question, $profile->path());
  77. }
  78. function profile2_page_delete_confirm_form_submit($form, &$form_state) {
  79. $profile = $form_state['profile2'];
  80. $profile->delete();
  81. drupal_set_message(t('Deleted %label.', array('%label' => $profile->label)));
  82. $form_state['redirect'] = 'user/' . $profile->uid;
  83. }