You are here

function user_badges_images_form_validate in User Badges 6.2

Same name and namespace in other branches
  1. 5 user_badges.module \user_badges_images_form_validate()
  2. 6 user_badges.admin.inc \user_badges_images_form_validate()

Validate the uploaded image

Check whether: Delete has been chosen AND a checkbox has been selected OR Upload has been chosen AND the file upload form is not empty.

File

./user_badges.admin.inc, line 391
@brief User Badges admin functions

Code

function user_badges_images_form_validate($form, &$form_state) {
  $op = $form_state['clicked_button']['#value'];
  if ($op == t('Upload')) {
    $dir = file_create_path('badges');
    $is_writable = file_check_directory($dir, 1);
    if ($is_writable) {
      $validators = array(
        'file_validate_extensions' => array(
          'png jpg jpeg gif',
        ),
      );
      if ($file = file_save_upload('upload', $validators, $dir)) {
        if (!image_get_info($file->filepath)) {
          file_delete($file->filepath);
          form_set_error('upload', t('Uploaded image file does not appear to be a valid image file.  Please try again'));
        }
        else {
          user_badges_hold_temporary_file($file);
          $form_state['values']['file_image'] = $file;
        }
      }
      else {
        form_set_error('upload', t('Cannot save image.  Enter the path to an image and try again.'));
      }
    }
    else {
      form_set_error('upload', t('Cannot save image - directory not writable'));
    }
  }
  elseif ($op == t('Delete')) {
    if (count(array_filter($form_state['values']['images'])) == 0) {
      form_set_error('images', t('Please select images to delete.'));
    }
  }
}