You are here

function media_browser_plus_move_file in Media Browser Plus 7.3

Same name and namespace in other branches
  1. 7 media_browser_plus.module \media_browser_plus_move_file()
  2. 7.2 media_browser_plus.module \media_browser_plus_move_file()

Moves and saves a file.

Every managed file that is saved or updated, should pass through this to ensure the filesystem location matches the folder term.

Parameters

int $tid: The folder's term id.

object $file: The file object.

int $replace: Replace behavior when the destination file already exists.

bool $save: Enables or disables saving the file object. Handy for cases in which the file object is saved anyway.

Return value

bool TRUE on success.

2 calls to media_browser_plus_move_file()
media_browser_plus_file_presave in ./media_browser_plus.file.inc
Implements hook_file_presave().
media_browser_plus_move_file_callback in ./media_browser_plus.module
Move the file to another folder.

File

./media_browser_plus.module, line 462
Media Browser Plus - enhanced file management functions.

Code

function media_browser_plus_move_file($tid, $file, $replace = NULL, $save = TRUE) {

  // See which file replace handling has to be used.
  if (is_null($replace)) {
    $replace = FILE_EXISTS_RENAME;

    // See if the file entity provides the file_replace property to indicate how
    // this has to be handled.
    if (isset($file->file_replace) && in_array($file->file_replace, array(
      FILE_EXISTS_REPLACE,
      FILE_EXISTS_RENAME,
      FILE_EXISTS_ERROR,
    ))) {
      $replace = $file->file_replace;
    }
  }

  // Ensure the new location is stored in the file entity.
  $file->field_folder[LANGUAGE_NONE] = array(
    array(
      'tid' => $tid,
    ),
  );

  // No need to process the file path if mpb doesn't mirror to filesystem.
  if (!variable_get('media_browser_plus_filesystem_folders', TRUE)) {
    if ($save) {
      file_save($file);
    }
    return TRUE;
  }
  $local_stream_wrappers = media_get_local_stream_wrappers();
  $scheme = file_uri_scheme($file->uri);
  if (function_exists('transliteration_clean_filename')) {
    $file->filename = transliteration_clean_filename($file->filename);
  }

  // Don't change the uri for non-local files.
  if (!isset($local_stream_wrappers[$scheme])) {
    if ($save) {
      file_save($file);
    }
  }
  else {

    // Media translation module does need this since it allows the creation of
    // file references which shouldn't move the referenced file itself when
    // moved. See http://drupal.org/node/1331818 for details.
    if (module_exists('media_translation') && media_translation_is_virtual_file($file->fid)) {
      file_save($file);
      return TRUE;
    }

    // If folder sync is enable move the file and then update the file entity.
    if (variable_get('media_browser_plus_filesystem_folders', TRUE)) {
      $folder = taxonomy_term_load($tid);
      $path = file_stream_wrapper_uri_normalize(file_uri_scheme($file->uri) . '://' . media_browser_plus_construct_dir_path($folder));
      file_prepare_directory($path, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
      if ($save) {
        return file_move($file, $path, $replace);
      }
      else {
        if ($uri = file_unmanaged_move($file->uri, $path, $replace)) {
          $file->uri = $uri;
          return TRUE;
        }
        return FALSE;
      }
    }
    else {

      // If folder sync is disabled just the file entity is updated.
      file_save($file);
    }
  }
  return TRUE;
}