<?php

/**
 * @file
 * Adds separate pages for viewing and editing profiles.
 */

/**
 * Implements hook_menu().
 */
function profile2_page_menu() {
  $items = array();

  // Bugfix for uninstalling the module, see http://drupal.org/node/1008346.
  if (!module_exists('profile2')) {
    return;
  }

  foreach (profile2_get_types() as $type_name => $type) {
    if (!empty($type->data['use_page'])) {
      $path = profile2_page_get_base_path($type);
      $count = count(explode('/', $path));

      $items[$path] = array(
        'title callback' => 'profile2_page_title',
        'title arguments' => array($type_name),
        'page callback' => 'profile2_page_own',
        'page arguments' => array($path),
        'access callback' => 'user_access',
        'access arguments' => array("edit own $type_name profile"),
        'file' => 'profile2_page.inc',
        'menu_name' => 'user-menu',
      );

      $items[$path . '/%profile2_by_uid'] = array(
        'title callback' => 'profile2_page_title',
        'title arguments' => array($type_name, $count),
        'page callback' => 'profile2_page_view',
        'page arguments' => array($count),
        'load arguments' => array($type_name),
        'access callback' => 'profile2_access',
        'access arguments' => array('view', $count),
        'file' => 'profile2_page.inc',
        // Copied over the following hack from user_menu() to avoid $path
        // appearing in the breadcrumb:
        //
        // By assigning a different menu name, this item (and all registered
        // child paths) are no longer considered as children of 'user'. When
        // accessing the user account pages, the preferred menu link that is
        // used to build the active trail (breadcrumb) will be found in this
        // menu (unless there is more specific link), so the link to 'user' will
        // not be in the breadcrumb.
        'menu_name' => 'navigation',
      );
      $items[$path . '/%profile2_by_uid/view'] = array(
        'title' => 'View',
        'type' => MENU_DEFAULT_LOCAL_TASK,
        'load arguments' => array($type_name),
        'weight' => -10,
      );
      $items[$path . '/%profile2_by_uid/edit'] = array(
        'page callback' => 'entity_ui_get_form',
        'page arguments' => array('profile2', $count),
        'load arguments' => array($type_name),
        'access callback' => 'profile2_access',
        'access arguments' => array('edit', $count),
        'title' => 'Edit',
        'type' => MENU_LOCAL_TASK,
        'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
        'file' => 'profile2_page.inc',
      );
      $items[$path . '/%profile2_by_uid/delete'] = array(
        'page callback' => 'drupal_get_form',
        'page arguments' => array('profile2_page_delete_confirm_form', $count),
        'load arguments' => array($type_name),
        'access callback' => 'profile2_access',
        'access arguments' => array('delete', $count),
        'title' => 'Delete',
        'type' => MENU_LOCAL_TASK,
        'context' => MENU_CONTEXT_INLINE,
        'file' => 'profile2_page.inc',
      );
      // Devel integration.
      if (module_exists('devel')) {
        $devel_path = drupal_get_path('module', 'devel');
        $items[$path . '/%profile2_by_uid/devel'] = array(
          'title' => 'Devel',
          'page callback' => 'devel_load_object',
          'file' => 'devel.pages.inc',
          'file path' => $devel_path,
          'page arguments' => array('profile2', $count),
          'access arguments' => array('access devel information'),
          'type' => MENU_LOCAL_TASK,
          'weight' => 100,
        );
        $items[$path . '/%profile2_by_uid/devel/load'] = array(
          'title' => 'Load',
          'type' => MENU_DEFAULT_LOCAL_TASK,
        );
        $items[$path . '/%profile2_by_uid/devel/render'] = array(
          'title' => 'Render',
          'page callback' => 'devel_render_object',
          'page arguments' => array('profile2', $count),
          'access arguments' => array('access devel information'),
          'file' => 'devel.pages.inc',
          'file path' => $devel_path,
          'type' => MENU_LOCAL_TASK,
          'weight' => 100,
        );
      }
    }
  }
  return $items;
}

/**
 * Menu load callback.
 *
 * Returns the profile object for the given user. If there is none yet, a new
 * object is created.
 */
function profile2_by_uid_load($uid, $type_name) {
  if ($uid && is_numeric($uid) && ($account = user_load($uid))) {
    $profile = profile2_load_by_user($account, $type_name);
    if (!$profile) {
      $profile = profile2_create(array('type' => $type_name));
      $profile->setUser($account);
      $profile->is_new = TRUE;
    }
    return $profile;
  }
  return FALSE;
}

/**
 * Returns the base path to use as profile page.
 */
function profile2_page_get_base_path($profile_type) {
  // Allow for an easy customization of the page's base path.
  if (!empty($profile_type->data['page_path'])) {
    return $profile_type->data['page_path'];
  }
  return 'profile-' . $profile_type->type;
}

/**
 * Implements hook_forms().
 */
function profile2_page_forms($form_id, $args) {
  // For efficiency, only act if the third argument is 'profile2'.
  if (isset($args[2]) && is_string($args[2]) && $args[2] == 'profile2') {
    $info = entity_get_info('profile2');
    // Translate bundle form ids to the base form id 'profile2_form'.
    foreach ($info['bundles'] as $bundle => $bundle_info) {
      $forms['profile2_edit_' . $bundle . '_form']['callback'] = 'profile2_form';
      $forms['profile2_edit_' . $bundle . '_form']['wrapper callback'] = 'entity_ui_form_defaults';
    }
    return $forms;
  }
}

/**
 * Implements hook_profile2_type_load().
 */
function profile2_page_profile2_type_load($types) {
  foreach ($types as $type) {
    if (!empty($type->data['use_page'])) {
      // Disable user categories and the user account view.
      $type->userCategory = FALSE;
      $type->userView = FALSE;
    }
  }
}

/**
 * Implements hook_entity_info_alter().
 */
function profile2_page_entity_info_alter(&$entity_info) {
  // Add new view modes for the page.
  $entity_info['profile2']['view modes']['page'] = array(
    'label' => t('Profile page'),
     'custom settings' => FALSE,
  );
  $entity_info['profile2']['view modes']['teaser'] = array(
    'label' => t('Teaser'),
    'custom settings' => FALSE,
  );
  $entity_info['profile2']['uri callback'] = 'profile2_page_uri_callback';
  $entity_info['profile2']['form callback'] = 'profile2_page_form_callback';
}

/**
 * URI callback pointing to the profile page.
 *
 * @see profile2_pages_entity_info_alter()
 */
function profile2_page_uri_callback($profile) {
  $type = $profile->type();
  if (!empty($type->data['use_page'])) {
    return array('path' => profile2_page_get_base_path($type) . '/' . $profile->uid);
  }
  // Fall back to the default callback.
  return $profile->defaultUri();
}

/**
 * Form callback for entity_form().
 */
function profile2_page_form_callback($profile) {
  // Pre-populate the form-state with the right form include.
  $form_state = form_state_defaults();
  form_load_include($form_state, 'inc', 'profile2_page');
  return entity_ui_get_form('profile2', $profile, 'edit', $form_state);
}

/**
 * Menu title callback.
 */
function profile2_page_title($type_name, $profile2 = NULL) {
  $type = profile2_get_types($type_name);
  // If no profile is given, we are at the general path pointing to the own
  // profile.
  if (!isset($profile2)) {
    return t('My @profile-label', array('@profile-label' => drupal_strtolower($type->getTranslation('label'))));
  }
  return drupal_ucfirst($profile2->label());
}

/**
 * Implements hook_form_FORM_ID_alter() for the profile2 type form..
 */
function profile2_page_form_profile2_type_form_alter(&$form, &$form_state) {
  $type = $form_state['profile2_type'];
  $form['data']['use_page'] = array(
    '#type' => 'checkbox',
    '#title' => t('Provide a separate page for editing profiles.'),
    '#description' => t('If enabled, a separate menu item for editing the profile is generated and the profile is hidden from the user account page.'),
    '#default_value' => !empty($type->is_new) || !empty($type->data['use_page']),
  );
  $form['data']['#tree'] = TRUE;
}

/**
 * Implements hook_profile2_type_insert().
 */
function profile2_page_profile2_type_insert(ProfileType $type) {
  // Do not directly issue menu rebuilds here to avoid potentially multiple
  // rebuilds. Instead, let menu_get_item() issue the rebuild on the next page.
  if (!empty($type->data['use_page'])) {
    variable_set('menu_rebuild_needed', TRUE);
  }
}

/**
 * Implements hook_profile2_type_update().
 */
function profile2_page_profile2_type_update(ProfileType $type) {
  // Rebuild the menu if use_page or the type name has been changed.
  // @see profile2_page_profile2_type_insert()
  if (empty($type->data['use_page']) != empty($type->original->data['use_page']) || ($type->data['use_page'] &&  $type->type != $type->original->type)) {
    variable_set('menu_rebuild_needed', TRUE);
  }
}

/**
 * Implements hook_profile2_type_delete()
 */
function profile2_page_profile2_type_delete($type) {
  // Do not directly issue menu rebuilds here to avoid potentially multiple
  // rebuilds. Instead, let menu_get_item() issue the rebuild on the next page.
  if (!empty($type->data['use_page'])) {
    variable_set('menu_rebuild_needed', TRUE);
  }
}
