<?php

/**
 * @file
 */

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

  // Note: admin/commerce/customer-profiles is defined by a default View.

  // Add a customer profile.
  $items['admin/commerce/customer-profiles/add'] = array(
    'title' => 'Add a customer profile',
    'description' => 'Add a new customer profile.',
    'page callback' => 'commerce_customer_ui_customer_profile_add_page',
    'access callback' => 'commerce_customer_ui_customer_profile_add_any_access',
    'weight' => 10,
    'file' => 'includes/commerce_customer_ui.profiles.inc',
  );
  foreach (commerce_customer_profile_types() as $type => $profile_type) {
    $items['admin/commerce/customer-profiles/add/' . strtr($type, array('_' => '-'))] = array(
      'title' => 'Create @name',
      'title arguments' => array('@name' => $profile_type['name']),
      'description' => $profile_type['description'],
      'page callback' => 'commerce_customer_ui_customer_profile_form_wrapper',
      'page arguments' => array(commerce_customer_profile_new($type)),
      'access callback' => 'commerce_customer_profile_access',
      'access arguments' => array('create', commerce_customer_profile_new($type)),
      'file' => 'includes/commerce_customer_ui.profiles.inc',
    );
  }

  $items['admin/commerce/customer-profiles/%commerce_customer_profile'] = array(
    'title callback' => 'commerce_customer_ui_customer_profile_title',
    'title arguments' => array(3),
    'page callback' => 'commerce_customer_ui_customer_profile_form_wrapper',
    'page arguments' => array(3),
    'access callback' => 'commerce_customer_profile_access',
    'access arguments' => array('update', 3),
    'weight' => 0,
    'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
    'file' => 'includes/commerce_customer_ui.profiles.inc',
  );
  $items['admin/commerce/customer-profiles/%commerce_customer_profile/edit'] = array(
    'title' => 'Edit',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -10,
    'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
  );
  $items['admin/commerce/customer-profiles/%commerce_customer_profile/delete'] = array(
    'title' => 'Delete a customer profile',
    'page callback' => 'commerce_customer_ui_customer_profile_delete_form_wrapper',
    'page arguments' => array(3),
    'access callback' => 'commerce_customer_profile_access',
    'access arguments' => array('delete', 3),
    'type' => MENU_LOCAL_TASK,
    'weight' => 10,
    'context' => MENU_CONTEXT_INLINE,
    'file' => 'includes/commerce_customer_ui.profiles.inc',
  );

  $items['admin/commerce/customer-profiles/types'] = array(
    'title' => 'Profile types',
    'description' => 'Manage customer profile types for your store.',
    'page callback' => 'commerce_customer_ui_customer_profile_types_overview',
    'access arguments' => array('administer customer profile types'),
    'type' => MENU_LOCAL_TASK,
    'weight' => 0,
    'file' => 'includes/commerce_customer_ui.profile_types.inc',
  );

  foreach (commerce_customer_profile_types() as $type => $profile_type) {
    // Convert underscores to hyphens for the menu item argument.
    $type_arg = strtr($type, '_', '-');

    $items['admin/commerce/customer-profiles/types/' . $type_arg] = array(
      'title' => $profile_type['name'],
      'page callback' => 'commerce_customer_ui_profile_type_redirect',
      'page arguments' => array($type),
      'access arguments' => array('administer customer profile types'),
    );
  }

  return $items;
}

/**
 * Menu item title callback: returns the ID of a customer profile for its pages.
 *
 * @param $profile
 *   The customer profile object as loaded via the URL wildcard.
 * @return
 *   A page title of the format "Profile [profile-id]".
 */
function commerce_customer_ui_customer_profile_title($profile) {
  return t('Customer profile @profile_id', array('@profile_id' => $profile->profile_id));
}

/**
 * Access callback: determine if the user can create any type of profile.
 */
function commerce_customer_ui_customer_profile_add_any_access() {
  // Grant automatic access to users with administer customer profiles permission.
  if (user_access('administer commerce_customer_profile entities')) {
    return TRUE;
  }

  // Check the user's access on a profile type basis.
  foreach (commerce_customer_profile_types() as $type => $profile_type) {
    if (commerce_customer_profile_access('create', commerce_customer_profile_new($type))) {
      return TRUE;
    }
  }

  return FALSE;
}

/**
 * Redirects a customer profile type URL to its fields management page.
 */
function commerce_customer_ui_profile_type_redirect($type) {
  drupal_goto('admin/commerce/customer-profiles/types/' . strtr($type, '_', '-') . '/fields');
}

/**
 * Implements hook_menu_alter().
 */
function commerce_customer_ui_menu_alter(&$items) {
  // Transform the field UI tabs into contextual links.
  foreach (commerce_customer_profile_types() as $type => $profile_type) {
    // Convert underscores to hyphens for the menu item argument.
    $type_arg = strtr($type, '_', '-');

    // Transform the field UI tabs into contextual links.
    $items['admin/commerce/customer-profiles/types/' . $type_arg . '/fields']['context'] = MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE;
    $items['admin/commerce/customer-profiles/types/' . $type_arg . '/display']['context'] = MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE;
  }
}

/**
 * Implements hook_menu_local_tasks_alter().
 */
function commerce_customer_ui_menu_local_tasks_alter(&$data, $router_item, $root_path) {
  // Add action link 'admin/commerce/customer-profiles/add' on
  // 'admin/commerce/customer-profiles'.
  if ($root_path == 'admin/commerce/customer-profiles') {
    $item = menu_get_item('admin/commerce/customer-profiles/add');
    if ($item['access']) {
      $data['actions']['output'][] = array(
        '#theme' => 'menu_local_action',
        '#link' => $item,
      );
    }
  }
}

/**
 * Implements hook_admin_menu_map().
 */
function commerce_customer_ui_admin_menu_map() {
  // Add awareness to the administration menu of the various profile types so
  // they are included in the dropdown menu.
  $map['admin/commerce/customer-profiles/types/%'] = array(
    'parent' => 'admin/commerce/customer-profiles/types',
    'arguments' => array(
      array('%' => array_keys(commerce_customer_profile_types())),
    ),
  );

  return $map;
}

/**
 * Implements hook_help().
 */
function commerce_customer_ui_help($path, $arg) {
  switch ($path) {
    case 'admin/commerce/customer-profiles/types/add':
      return '<p>' . t('Individual customer profile types can have different fields assigned to them.') . '</p>';
  }

  // Return the user defined help text per customer profile type when adding profiles.
  if ($arg[1] == 'commerce' && $arg[2] == 'customer-profiles' && $arg[3] == 'add' && $arg[4]) {
    $profile_type = commerce_customer_profile_type_load($arg[5]);
    return (!empty($profile_type['help']) ? '<p>' . filter_xss_admin($profile_type['help']) . '</p>' : '');
  }
}

/**
 * Implements hook_theme().
 */
function commerce_customer_ui_theme() {
  return array(
    'customer_profile_add_list' => array(
      'variables' => array('content' => array()),
      'file' => 'includes/commerce_customer_ui.profiles.inc',
    ),
    'customer_profile_type_admin_overview' => array(
      'variables' => array('type' => NULL),
      'file' => 'includes/commerce_customer_ui.profile_types.inc',
    ),
    'commerce_customer_profile_status' => array(
      'variables' => array('status' => NULL, 'label' => NULL, 'profile' => NULL),
      'path' => drupal_get_path('module', 'commerce_customer_ui') . '/theme',
      'template' => 'commerce-customer-profile-status',
    ),
  );
}

/**
 * Implements hook_entity_info_alter().
 */
function commerce_customer_ui_entity_info_alter(&$entity_info) {
  // Add a URI callback to the profile entity.
  $entity_info['commerce_customer_profile']['uri callback'] = 'commerce_customer_ui_customer_profile_uri';

  // Expose the admin UI for profile fields.
  foreach ($entity_info['commerce_customer_profile']['bundles'] as $type => &$bundle) {
    $bundle['admin'] = array(
      'path' => 'admin/commerce/customer-profiles/types/' . strtr($type, '_', '-'),
      'real path' => 'admin/commerce/customer-profiles/types/' . strtr($type, '_', '-'),
      'access arguments' => array('administer customer profile types'),
    );
  }
}

/**
 * Entity uri callback: points to the edit form of the given profile.
 */
function commerce_customer_ui_customer_profile_uri($profile) {
  // First look for a return value in the default entity uri callback.
  $uri = commerce_customer_profile_uri($profile);

  // If a value was found, return it now.
  if (!empty($uri)) {
    return $uri;
  }

  // Only return a value if the user has permission to view the profile.
  if (commerce_customer_profile_access('view', $profile)) {
    return array(
      'path' => 'admin/commerce/customer-profiles/' . $profile->profile_id,
    );
  }

  return NULL;
}

/**
 * Implements hook_forms().
 */
function commerce_customer_ui_forms($form_id, $args) {
  $forms = array();

  // Define a wrapper ID for the customer profile add / edit form.
  $forms['commerce_customer_ui_customer_profile_form'] = array(
    'callback' => 'commerce_customer_customer_profile_form',
  );

  // Define a wrapper ID for the customer profile delete confirmation form.
  $forms['commerce_customer_ui_customer_profile_delete_form'] = array(
    'callback' => 'commerce_customer_customer_profile_delete_form',
  );

  return $forms;
}

/**
 * Implements hook_form_alter().
 */
function commerce_customer_ui_form_alter(&$form, &$form_state, $form_id) {
  // On field administration forms for customer profile types add a breadcrumb.
  if (in_array($form_id, array('field_ui_field_overview_form', 'field_ui_display_overview_form'))) {
    if ($form['#entity_type'] == 'commerce_customer_profile') {
      // Load the customer profile type being modified for this form.
      $profile_type = commerce_customer_profile_type_load($form['#bundle']);
      drupal_set_title($profile_type['name']);
    }
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * The Customer UI module instantiates the Profile add/edit form at particular
 * paths in the Commerce IA. It uses its own form ID to do so and alters the
 * form here to add in appropriate redirection and an additional button.
 *
 * @see commerce_customer_ui_customer_profile_form()
 */
function commerce_customer_ui_form_commerce_customer_ui_customer_profile_form_alter(&$form, &$form_state) {
  // Add a submit handler to the save button to add a redirect.
  $form['actions']['submit']['#submit'][] = 'commerce_customer_ui_customer_profile_form_submit';

  // Add the save and continue button for new profiles.
  if (empty($form_state['customer_profile']->profile_id)) {
    $form['actions']['save_continue'] = array(
      '#type' => 'submit',
      '#value' => t('Save and add another'),
      '#submit' => $form['actions']['submit']['#submit'],
      '#suffix' => l(t('Cancel'), 'admin/commerce/customer-profiles'),
      '#weight' => 45,
    );
  }
  else {
    $form['actions']['submit']['#suffix'] = l(t('Cancel'), 'admin/commerce/customer-profiles');
  }
}

/**
 * Submit callback for commerce_customer_ui_customer_profile_form().
 *
 * @see commerce_customer_ui_form_commerce_customer_ui_customer_profile_form_alter()
 */
function commerce_customer_ui_customer_profile_form_submit($form, &$form_state) {
  // Set the redirect based on the button clicked.
  if ($form_state['triggering_element']['#parents'][0] == 'save_continue') {
    $form_state['redirect'] = 'admin/commerce/customer-profiles/add/' . strtr($form_state['customer_profile']->type, array('_' => '-'));
  }
  elseif (arg(2) == 'customer-profiles' && arg(3) == 'add') {
    $form_state['redirect'] = 'admin/commerce/customer-profiles';
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * The Customer UI module instantiates the Profile delete form at a particular
 * path in the Commerce IA. It uses its own form ID to do so and alters the
 * form here to add in appropriate redirection.
 *
 * @see commerce_customer_ui_customer_profile_delete_form()
 */
function commerce_customer_ui_form_commerce_customer_ui_customer_profile_delete_form_alter(&$form, &$form_state) {
  $form['actions']['cancel']['#href'] = 'admin/commerce/customer-profiles';
  $form['#submit'][] = 'commerce_customer_ui_customer_profile_delete_form_submit';
}

/**
 * Submit callback for commerce_customer_ui_customer_profile_delete_form().
 *
 * @see commerce_customer_ui_form_commerce_customer_ui_customer_profile_delete_form_alter()
 */
function commerce_customer_ui_customer_profile_delete_form_submit($form, &$form_state) {
  $form_state['redirect'] = 'admin/commerce/customer-profiles';
}

/**
 * Implements hook_views_api().
 */
function commerce_customer_ui_views_api() {
  return array(
    'api' => 3,
    'path' => drupal_get_path('module', 'commerce_customer_ui') . '/includes/views',
  );
}

/**
 * Sets the breadcrumb for administrative customer pages.
 *
 * @param $profiles
 *   TRUE or FALSE indicating whether or not the breadcrumb should include the
 *     profiles overview page.
 * @param $profile_types
 *   TRUE or FALSE indicating whether or not the breadcrumb should include the
 *     profile types administrative page.
 *
 * @deprecated since 7.x-1.4
 */
function commerce_customer_ui_set_breadcrumb($profile_types = FALSE) {
  // This function used to manually set a breadcrumb that is now properly
  // generated by Drupal itself.
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function commerce_customer_ui_form_entity_translation_admin_form_alter(&$form, &$form_state, $form_id) {
  // Hide the commerce_customer_profile option from entity translation.
  unset($form['entity_translation_entity_types']['#options']['commerce_customer_profile']);
}
