You are here

function dynamic_background_admin_images_validate in Dynamic Background 6

Same name and namespace in other branches
  1. 7.2 includes/backgrounds.admin.inc \dynamic_background_admin_images_validate()
  2. 7 includes/backgrounds.admin.inc \dynamic_background_admin_images_validate()

The administration images form validator form. It handles uploading the image and deletion if the checkbox is flaged.

1 string reference to 'dynamic_background_admin_images_validate'
dynamic_background_admin_images in includes/backgrounds.admin.inc
The image administation form.

File

includes/backgrounds.admin.inc, line 50
Implementation of the administration image upload and selecton form.

Code

function dynamic_background_admin_images_validate($form, &$form_state) {

  // Load settings.
  $settings = variable_get('dynamic_background_setting', array());

  // Validate the file extension (move to config, with upload path).
  $validators = array(
    'file_validate_extensions' => array(
      $settings['extensions'],
    ),
  );

  // Loop through all uploaded files.
  if (!empty($_FILES)) {
    foreach ($_FILES['files']['name'] as $file_field => $val) {
      if ($val == '') {
        continue;
      }
      if ($file = file_save_upload($file_field, $validators)) {
        $info = image_get_info($file->filepath);
        $destination = file_directory_path() . '/' . $settings['path'] . '/' . $file->filename;

        // Check if file exists.
        if (file_exists($destination)) {
          form_set_error('default', t('Failded to upload image, as one with that name exists.'));
          return;
        }

        // Copy file.
        if (file_copy($file, $destination, FILE_EXISTS_REPLACE)) {
          $form_state['values'][$file_field]['default'] = $file->filepath;
        }
        else {
          form_set_error('default', t("Failed to upload the image; the %directory directory doesn't exist or is not writable.", array(
            '%directory' => file_directory_path() . '/' . $settings['path'],
          )));
        }
      }
      else {
        form_set_error('default', t("Failed to upload the image; the image type should be jpeg, jpg or png."));
      }
    }
  }

  // Check for image deletion.
  foreach ($form_state['values'] as $field => $data) {
    if (strstr($field, 'dynamic_background_picture_') && $data['picture_delete']) {

      // Found one flaged for deletion.
      if (!file_delete($data['default'])) {
        form_set_error($field, t('Could not delete %file from the system.', array(
          '%file' => $data['default'],
        )));
      }
      else {

        // Remove the delete images informations from the form.
        unset($form_state['values'][$field]['default']);
        $form_state['values'][$field]['picture_delete'] = 0;
      }
    }
  }
}