You are here

function _user_badges_managed_file in User Badges 7.3

Convert a uri into a managed file object.

Loads existing file object for uri or creates new managed file entry.

Return value

object|false A file object on success or FALSE in the event of an error.

4 calls to _user_badges_managed_file()
user_badges_delete_form_submit in ./user_badges.admin.inc
Submit handler for user badge deletion.
user_badges_edit_form_submit in ./user_badges.admin.inc
Process user_badges_edit_form form submissions.
user_badges_images_form_submit in ./user_badges.admin.inc
Submission action for user_badges_images_form
_user_badges_images_from_library in ./user_badges.admin.inc
Scan the badges image directory.

File

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

Code

function _user_badges_managed_file($file_uri) {
  global $user;
  if (file_valid_uri($file_uri)) {

    // Check if there's an entry in the database for this file.
    $fid = db_select('file_managed', 'f')
      ->fields('f', array(
      'fid',
    ))
      ->condition('f.uri', $file_uri)
      ->execute()
      ->fetchField();
    if ($fid) {
      return file_load($fid);
    }
    else {

      // If no entry found, we need to save this to the database.
      $fileob = new stdClass();
      $fileob->fid = NULL;
      $fileob->uri = $file_uri;
      $fileob->filename = drupal_basename($fileob->uri);
      $fileob->filemime = file_get_mimetype($fileob->uri);
      $fileob->uid = $user->uid;
      $fileob->status = FILE_STATUS_PERMANENT;
      return file_save($fileob);
    }
  }
  return FALSE;
}