You are here

function avatar_selection_validate_user_avatar in Avatar Selection 7

Same name and namespace in other branches
  1. 6 avatar_selection.module \avatar_selection_validate_user_avatar()

Validate and upload the user's picture.

Parameters

&$form: General reference used in drupal, defining the structure & the fields of a form.

&$form_state: General reference, used to control the processing of the form.

2 string references to 'avatar_selection_validate_user_avatar'
avatar_selection_form_user_profile_form_alter in ./avatar_selection.module
Implements hook_form_alter().
avatar_selection_form_user_register_form_alter in ./avatar_selection.module
Implements hook_form_alter().

File

./avatar_selection.module, line 324
The Avatar Selection module allows the user to pick an avatar image from a list already loaded by an administrative user, and to the administrator to disable uploading other avatar files by the user.

Code

function avatar_selection_validate_user_avatar(&$form, &$form_state) {

  // If required, validate the uploaded picture.
  $validators = array(
    'file_validate_is_image' => array(),
    'file_validate_image_resolution' => array(
      variable_get('user_picture_dimensions', '85x85'),
    ),
    'file_validate_size' => array(
      variable_get('user_picture_file_size', 30) * 1024,
    ),
  );
  $file = file_save_upload('picture_upload', $validators);

  // No file upload, but avatar selection set.
  if (!$file && !empty($form_state['values']['select_avatar'])) {
    $path = file_build_uri('avatar_selection') . '/';

    // Load file.
    $file = file_load($form_state['values']['select_avatar']);
    $file->status = 0;
    $file = file_copy($file, 'temporary://' . $file->filename);
    $form_state['values']['picture_upload'] = $file;
  }
  elseif (!$file && variable_get('avatar_selection_set_random_default', FALSE)) {
    if (!empty($form_state['user']->uid)) {
      $account = $form_state['user'];
    }
    else {
      $account = drupal_anonymous_user();
    }
    $account->original = entity_load_unchanged('user', $account->uid);
    if (empty($account->original->picture) || isset($form_state['values']['picture_delete']) && $form_state['values']['picture_delete'] == 1) {
      $random_file = avatar_selection_get_random_image($account->original);
      if ($random_file) {
        $random_file->status = 0;
        $random_file = file_copy($random_file, 'temporary://' . $random_file->filename);
        $form_state['values']['picture_upload'] = $random_file;
      }
    }
  }
}