You are here

function spaces_design_upload_validate in Spaces 6.2

Element validator for logo upload. Actually handles file creation and value setting tasks all at once.

1 string reference to 'spaces_design_upload_validate'
space_setting_logo::form in spaces_design/spaces_design.spaces.inc

File

spaces_design/spaces_design.spaces.inc, line 121

Code

function spaces_design_upload_validate($element, &$form_state) {

  // If required, validate the uploaded picture.
  $validators = array(
    'file_validate_is_image' => array(),
    'file_validate_image_resolution' => array(
      '600x600',
    ),
    'file_validate_size' => array(
      1000 * 1024,
    ),
  );

  // @TODO: the first argument is troublesome -- the upload is registered in the
  // global $_FILES array under the 'settings' key -- probably because this
  // is the root level key in the form. Look into changing this...
  if ($file = file_save_upload('settings', $validators, file_directory_path())) {

    // Remove the old picture.
    if (isset($form_state['values']['space']->settings['logo']['fid'])) {
      $old_file = $form_state['values']['space']->settings['logo']['file'];
      if (file_exists($old_file->filepath)) {
        file_delete($old_file->filepath);
        db_query('DELETE FROM {files} WHERE fid = %d', $old_file->fid);
      }
    }

    // Uploaded file was not an image, set an error
    if ($error = file_validate_is_image($file)) {
      form_set_error($error);
      file_delete($file->filepath);
      db_query('DELETE FROM {files} WHERE fid = %d', $file->fid);
      $form_state['values']['settings']['logo']['fid'] = 0;
    }
    else {
      file_set_status($file, 1);
      $form_state['values']['settings']['logo']['fid'] = $file->fid;

      // Retrieve autocolor for logo and see if it makes sense to populate the color field.
      if ($autocolor = _spaces_design_image_autocolor($file->filepath)) {
        $form_state['values']['settings']['color'] = $autocolor;
      }
    }
  }
}