You are here

function amazon_settings_form_validate in Amazon Product Advertisement API 6

Same name and namespace in other branches
  1. 7.2 amazon.admin.inc \amazon_settings_form_validate()
  2. 7 amazon.admin.inc \amazon_settings_form_validate()

Validation handler for amazon_settings_form(). Handles the file upload and making sure it happens correctly.

File

./amazon.admin.inc, line 126

Code

function amazon_settings_form_validate($form, &$form_state) {
  $form_state['values']['amazon_aws_access_key'] = trim($form_state['values']['amazon_aws_access_key']);
  $form_state['values']['amazon_aws_secret_access_key'] = trim($form_state['values']['amazon_aws_secret_access_key']);
  if ($file = file_save_upload('amazon_default_image')) {
    $orig_picture = variable_get('amazon_default_image', '');
    if (!empty($orig_picture) && file_exists($orig_picture)) {
      file_delete($orig_picture);
    }

    // The image was saved using file_save_upload() and was added to the
    // files table as a temporary file. We'll make a copy and let the garbage
    // collector delete the original upload.
    $info = image_get_info($file->filepath);
    if (file_copy($file, 0, FILE_EXISTS_REPLACE)) {
      $form_state['values']['amazon_default_image'] = $file->filepath;
    }
    else {
      form_set_error('amazon_default_image', t("Failed to upload the picture image."));
    }
  }
}