You are here

function filefield_paths_filefield_paths_process_file in File (Field) Paths 5

Same name and namespace in other branches
  1. 8 filefield_paths.inc \filefield_paths_filefield_paths_process_file()
  2. 6 filefield_paths.module \filefield_paths_filefield_paths_process_file()
  3. 7 modules/filefield_paths.inc \filefield_paths_filefield_paths_process_file()

Implementation of hook_filefield_paths_process_file().

File

./filefield_paths.module, line 425
Adds extra functionality to FileFields Path settings.

Code

function filefield_paths_filefield_paths_process_file($new, &$file, $settings, &$node, &$update) {
  if ($new) {

    // Process filename
    $file['filename']['old'] = $file['field']['filename'];
    if (($file['filename']['new'] = $settings['filename']['value']) != '') {
      $file['filename']['new'] = filefield_paths_process_string($file['filename']['new'], 'node', $node, $settings['filename']);
      $file['filename']['new'] = filefield_paths_process_string($file['filename']['new'], 'field', array(
        0 => $file['field'],
      ), $settings['filename']);
    }
    else {
      $file['filename']['new'] = $file['field']['filename'];
    }

    // Process filepath
    $file['filepath']['old'] = $file['field']['filepath'];
    $file['filepath']['new'] = filefield_paths_process_string(file_directory_path() . '/' . $settings['filepath']['value'] . '/' . $file['filename']['new'], 'node', $node, $settings['filepath']);
    $file['filepath']['new'] = filefield_paths_process_string($file['filepath']['new'], 'field', array(
      0 => $file['field'],
    ), $settings['filepath']);

    // Finalize files if necessary
    if (dirname($file['filepath']['new']) != dirname($file['field']['filepath']) || $file['filename']['new'] != $file['field']['filename']) {
      if (filefield_paths_file_move($file)) {

        // Fix reference to old paths in Body and Teaser
        // TODO: allow for CCK fields
        if (isset($node->body)) {
          $file['filepath']['new'] = str_replace($file['filename']['old'], $file['filename']['new'], $file['filepath']['new']);
          $pattern = array(
            'regex' => str_replace('/', '\\/', file_directory_path()) . '(.*?)' . str_replace('/', '\\/', str_replace(file_directory_path(), '', $file['filepath']['old'])),
            'regex_enc' => str_replace('/', '\\/', drupal_urlencode(file_directory_path())) . '(.*?)' . str_replace('/', '\\/', str_replace(drupal_urlencode(file_directory_path()), '', drupal_urlencode($file['filepath']['old']))),
            'replace' => file_directory_path() . '$1' . str_replace(file_directory_path(), '', $file['filepath']['new']),
          );
          $body = $node->body;
          $body = preg_replace('/' . $pattern['regex'] . '/s', $pattern['replace'], $body);
          $body = preg_replace('/' . $pattern['regex_enc'] . '/s', $pattern['replace'], $body);
          $teaser = $node->teaser;
          $teaser = preg_replace('/' . $pattern['regex'] . '/s', $pattern['replace'], $teaser);
          $teaser = preg_replace('/' . $pattern['regex_enc'] . '/s', $pattern['replace'], $teaser);
          if ($body != $node->body || $teaser != $node->teaser) {
            $node->body = $body;
            $node->teaser = $teaser;
            $update->node = TRUE;
          }
        }

        // Store new filename in file Array
        $file['field']['filename'] = $file['filename']['new'];
      }
    }
  }
}