You are here

function user_badges_images_form_submit in User Badges 7.2

Same name and namespace in other branches
  1. 5 user_badges.module \user_badges_images_form_submit()
  2. 6.2 user_badges.admin.inc \user_badges_images_form_submit()
  3. 6 user_badges.admin.inc \user_badges_images_form_submit()
  4. 7 user_badges.admin.inc \user_badges_images_form_submit()
  5. 7.3 user_badges.admin.inc \user_badges_images_form_submit()

Submission action for user_badges_images_form

Save the uploaded file or delete the selected one

File

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

Code

function user_badges_images_form_submit($form, &$form_state) {
  $files_to_delete = array_keys(array_filter($form_state['values']['images']));
  if (!empty($files_to_delete)) {
    $dir = 'public://badges';
    if (file_prepare_directory($dir)) {
      foreach ($files_to_delete as $path) {
        $fid = db_select('file_managed', 'f')
          ->fields('f', array(
          'fid',
        ))
          ->condition('f.uri', $path)
          ->execute()
          ->fetchField();

        // If the file is managed, verify that it is not used
        if ($fid) {
          $file = file_load($fid);
          $num_rows = db_select('user_badges_badges', 'ubb')
            ->condition('ubb.image', $path)
            ->countQuery()
            ->execute()
            ->fetchField();
          if (!$file || $num_rows || !file_delete($file)) {
            drupal_set_message(t('The file %file is still being used for some badges.', array(
              '%file' => $path,
            )), 'error');
          }
        }
        else {

          // If it not managed, delete it
          file_unmanaged_delete($path);
        }
      }
    }
  }
}