You are here

function avatar_selection_upload_form_submit in Avatar Selection 6

Same name and namespace in other branches
  1. 5.2 avatar_selection.module \avatar_selection_upload_form_submit()
  2. 7 avatar_selection.admin.inc \avatar_selection_upload_form_submit()

Submit the image upload form in the Avatar Selection administration page.

Parameters

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

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

File

./avatar_selection.admin.inc, line 591
Administrative page callbacks for the avatar_selection module.

Code

function avatar_selection_upload_form_submit($form, &$form_state) {
  $op = $form_state['values']['op'];
  if ($op == t('Upload')) {

    // Get access settings.
    $access = array_keys(array_filter($form_state['values']['access']));
    $og = array();
    if (module_exists('og')) {
      $og = array_keys(array_filter($form_state['values']['og_access']));
    }
    $name = $form_state['values']['avatar_name'];
    $weight = $form_state['values']['avatar_weight'];

    // Scan for new files.
    if ($form_state['values']['scan_avatars'] == 1) {
      $count = _avatar_selection_scan_images($name, $access, $og, $weight);
      drupal_set_message(t('Scan complete: %added new avatars found. %deleted avatars removed.', array(
        '%added' => $count['add'],
        '%deleted' => $count['delete'],
      )));
    }

    // Save uploaded files.
    $dir = file_create_path('avatar_selection');
    $is_writable = file_check_directory($dir, 1);
    if ($is_writable) {

      // 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,
        ),
      );
      if ($file = file_save_upload('picture_upload', $validators, $dir, FILE_EXISTS_RENAME)) {
        if (image_get_info($file->filepath)) {
          file_set_status($file, FILE_STATUS_PERMANENT);
          _avatar_selection_save_avatar_info(0, $file->filename, $name, $access, $og, $weight);
          drupal_set_message(t('New image saved.'));
        }
        else {
          file_delete($file->filepath);
          drupal_set_message(t('Uploaded file does not appear to be a valid image file. Please try again.'));
        }
      }
    }
    else {
      form_set_error('picture_upload', t('Directory not writable: !dir', array(
        '!dir' => $dir,
      )));
    }
  }
}