You are here

function fe_paths_move_file in File Entity Paths 7.2

Build new filename and path based on settings, and move file, if necessary.

Parameters

$file: The original file object.

$replace_path: Path pattern will be used as replacement pattern.

$replace_filename: Filename pattern will be used as replacement pattern.

$token_type: Originally the parent entity type, determine, which tokens is used in transformation.

$config_data: The additional data of actaul File Entity Paths configuration.

null $entity: The parent entity.

Return value

mixed The file, if transformation and file moving was success, otherwise FALSE.

1 call to fe_paths_move_file()
fe_paths_file_process in ./fe_paths.module
Actually pre processes the files. Set a $file->fe_paths_processed flag on the file entity, then add to process queue. The file will be processed on drupal_register_shutdown.

File

./fe_paths.module, line 351
Contains functions for the File Entity Paths module.

Code

function fe_paths_move_file($file, $replace_path, $replace_filename, $token_type, $config_data, $entity = NULL) {
  $scheme = file_uri_scheme($file->uri);
  $old_dir_uri = str_replace('/' . $file->filename, '', $file->uri);
  $path = token_replace($replace_path, array(
    $token_type => $entity,
    $token_type,
    'file' => $file,
    'file',
  ), array(
    'clear' => TRUE,
  ));
  $filename = token_replace($replace_filename, array(
    $token_type => $entity,
    $token_type,
    'file' => $file,
    'file',
  ), array(
    'clear' => TRUE,
  ));

  // Modify text by transliteration.
  if (isset($config_data['transliteration']) && $config_data['transliteration'] && module_exists('transliteration')) {

    // The path need some vudu to clean, because transliteration remove the
    // slashes.
    $parts = array();
    foreach (explode('/', $path) as $part) {
      $parts[] = transliteration_clean_filename($part);
    }
    $path = implode('/', $parts);
    $filename = transliteration_clean_filename($filename);
  }

  // Modify text by pathauto.
  if (isset($config_data['pathauto']) && $config_data['pathauto'] && module_exists('pathauto')) {
    module_load_include('inc', 'pathauto');
    $parts = array();
    foreach (explode('/', $path) as $part) {
      $parts[] = pathauto_cleanstring($part);
    }
    $path = implode('/', $parts);

    // The same vudu, as in the transliteration, but here is because of
    // punctuation.
    $parts = array();
    foreach (explode('.', $filename) as $part) {
      $parts[] = pathauto_cleanstring($part);
    }
    $filename = implode('.', $parts);
  }
  $new_uri = $scheme . '://' . $path . '/' . $filename;
  $directory = $scheme . '://' . $path;

  // Only do file_move, if the new uri is different than old. But need
  // to return with the file, because without this, no config match happen
  // in fe_paths_file_process.
  if ($new_uri == $file->uri) {
    return $file;
  }

  // Create directory, if necessary.
  file_prepare_directory($directory, FILE_CREATE_DIRECTORY);
  if (file_move($file, $new_uri)) {

    // Check, if directory is empty, and delete it, if yes, remove it.
    $files = file_scan_directory($old_dir_uri, '/.*/');
    if (file_exists($old_dir_uri) && empty($files)) {
      drupal_rmdir($old_dir_uri);
    }

    // Because of file_move works with a cloned $file object, need to update
    // this $file object.
    $file->filename = $filename;
    $file->uri = $new_uri;
    return $file;
  }
  else {
    return FALSE;
  }
}