<?php
/**
 * @file
 * Code for the Groups User Profile feature.
 */

include_once 'groups_user_profile.features.inc';

/**
 * Implements hook_field_default_field_instances_alter()
 *
 * Add profile teaser settings to user profile's twitter url
 * field.
 */
function groups_user_profile_field_default_field_instances_alter(&$fields) {
  if (isset($fields['user-user-field_twitter_url'])) {
    $field = &$fields['user-user-field_twitter_url'];
    $field['display']['profile_teaser'] = array(
      'label' => 'hidden',
      'module' => 'groups_social_link',
      'settings' => array(
        'provider' => 'twitter',
      ),
      'type' => 'groups_social_link_formatter',
      'weight' => 2,
    );
  }
}

/**
 * Implements hook_form_user_profile_form_alter()
 *
 * Override ambassador-region field visibility.
 */
function groups_user_profile_form_user_profile_form_alter(&$form, &$form_state,
  $form_id) {
  $current_user = user_uid_optional_load();
  $roles = array('ambassador', 'community_manager', 'administrator');
  $check = array_intersect($roles, array_values($current_user->roles));
  if (empty($check)) {
    $form['field_ambassador_region']['#access'] = FALSE;
  }
}

/**
 * Implements hook_field_default_field_bases_alter().
 *
 * Alter the default fields right before they are cached into the database.
 *
 * @param &$fields
 *   By reference. The fields that have been declared by another feature.
 */
function groups_user_profile_field_default_field_bases_alter(&$fields) {
  $source_dir = drupal_get_path('module', 'groups_user_profile') . '/images';
  $field_name = 'field_alternate_user_picture';
  $filename = 'picture-default.jpg';
  $source = $source_dir . '/' . $filename;
  $destination = 'default_images/' . $filename;
  if (isset($fields[$field_name])) {
    _groups_user_profile_set_default_image($fields[$field_name], $filename,
      $source, $destination);
  }
}

/**
 * Custom function to set the default image.
 *
 * @param  array $field
 * @param  string $filename Filename of the source image.
 * @param  string $source Filepath of the source image.
 * @param  string $destination Filepath you want to save to.
 */
function _groups_user_profile_set_default_image(&$field, $filename, $source,
  $destination) {
  // See if a default image hasn't been set for this field yet
  if (empty($field['settings']['default_image'])) {
    // Dynamically set the user default image on the field
    $destination = file_default_scheme() . '://' . $destination;
    // Check to see if it exists already
    $result = db_select('file_managed', 'f')
      ->fields('f', array('fid'))
      ->condition('f.uri', $destination)
      ->execute();
    $fid = $result->fetchField();
    // Upload the default user image
    if (!$fid && file_exists($source)) {
      $file = new stdClass;
      $file->filename = $filename;
      $file->timestamp = REQUEST_TIME;
      $file->uri = $source;
      $file->filemime = file_get_mimetype($source);
      $file->uid = 1;
      $file->status = 1;
      $file = file_copy($file, 'public://', FILE_EXISTS_REPLACE);
      $fid = $file->fid;
    }
    $field['settings']['default_image'] = (string) $fid;
  }
}