<?php

include_once('panopoly_users.features.inc');

/**
 * Implementation of hook_apps_app_info()
 */
function panopoly_users_apps_app_info() {
  return array(
    'configure form' => 'panopoly_users_configure_form',
  );
}

/**
 * Configuration Form for Panopoly Magic
 */
function panopoly_users_configure_form($form, &$form_state) {
  $form = array();

  $form['panopoly_users_login_destination'] = array(
    '#title' => t('Login Destination'),
    '#type' => 'textfield',
    '#required' => TRUE,
    '#default_value' => variable_get('panopoly_users_login_destination', '<front>'),
    '#description' => t('Where do you want to redirect users when the login using the standard login link? Use <front> for the Drupal frontpage.'),
  );

  $form['panopoly_users_remove_tabs'] = array(
     '#title' => t('Remove User Tabs'),
     '#type' => 'select',
     '#required' => TRUE,
     '#options' => array(
       0 => t('Do Not Remove'),
       1 => t('Remove'),
     ),
     '#default_value' => variable_get('panopoly_users_remove_tabs', 1),
     '#description' => t('Do you want to remove the login, reset password, and register tabs on the user page? These links will be transfered into the relevant field descriptions instead.'),
  );

  return system_settings_form($form);
}

/**
 * Implementation of hook_ctools_plugin_directory()
 */
function panopoly_users_ctools_plugin_directory($module, $plugin) {
  return 'plugins/' . $plugin;
} 

/**
 * Implementation of hook_menu_alter()
 */
function panopoly_users_menu_alter(&$items) {

  // Remove tabs from user login page
  if (variable_get('panopoly_users_remove_tabs', TRUE)) {
    $items['user/login']['type'] = MENU_CALLBACK;
    $items['user/register']['type'] = MENU_CALLBACK;
    $items['user/password']['type'] = MENU_CALLBACK;
  }
}

/**
 * Implementation of hook_form_alter()
 */
function panopoly_users_form_alter(&$form, $form_state, $form_id) {

  if (variable_get('panopoly_users_remove_tabs', TRUE)) {

    // Add a create account link below the username
    if ($form_id == 'user_login' && variable_get('user_register')) {
      $form['name']['#description'] .= '<br>' . t('If you don\'t have an username, !create.', array('!create' => l(t('create an account'), 'user/register')));
    }

    // Add the request password link below the password
    if ($form_id == 'user_login') {
      $form['pass']['#description'] .= '<br>' . t('If you forgot your password, !request.', array('!request' => l(t('request a new password'), 'user/password')));
    }
  }
}

/**
 * Implementation of hook_user_login()
 *
 * This will redirect the user to a specified page when they log in.
 */
function panopoly_users_user_login(&$edit, $account) {
  $edit['redirect'] = variable_get('panopoly_users_login_destination', '<front>');
}

/**
 * Prepares variables for user_picture templates.
 *
 * Sets the 'user_picture' variable based on field_user_picture. 
 */
function panopoly_users_preprocess_user_picture(&$variables) {
  $account = $variables['account'];

  // Sometimes we get a partially loaded user object that doesn't have fields.
  if (!isset($account->field_user_picture)) {
    $account = user_load($account->uid);
  }

  $filepath = !empty($account->field_user_picture[LANGUAGE_NONE][0]['uri']) ?
    $account->field_user_picture[LANGUAGE_NONE][0]['uri'] : '';

  // Copied graciously from user.module...
  if (isset($filepath)) {
    $alt = t("@user's picture", array('@user' => format_username($account)));
    // If the image does not have a valid Drupal scheme (for eg. HTTP),
    // don't load image styles.
    if (module_exists('image') && file_valid_uri($filepath) && $style = variable_get('user_picture_style', '')) {
      $variables['user_picture'] = theme('image_style', array('style_name' => $style, 'path' => $filepath, 'alt' => $alt, 'title' => $alt));
    }
    else {
      $variables['user_picture'] = theme('image', array('path' => $filepath, 'alt' => $alt, 'title' => $alt));
    }
    if (!empty($account->uid) && user_access('access user profiles')) {
      $attributes = array('attributes' => array('title' => t('View user profile.')), 'html' => TRUE);
      $variables['user_picture'] = l($variables['user_picture'], "user/$account->uid", $attributes);
    }
  }
}

