You are here

function transliteration_retroactive_submit in Transliteration 6.3

Same name and namespace in other branches
  1. 7.3 transliteration.admin.inc \transliteration_retroactive_submit()

Form submit function; retroactively transliterate existing file names.

See also

transliteration_retroactive()

File

./transliteration.admin.inc, line 59
Retroactive transliteration and admin settings UI.

Code

function transliteration_retroactive_submit($form, &$form_state) {
  $count = 0;
  $errors = array();
  $result = db_query(transliteration_file_query());
  while ($file = db_fetch_object($result)) {
    if (!file_exists('./' . $file->filepath)) {

      // Missing file.
      $errors[] = $file->filepath;
      continue;
    }

    // Sanitize file name.
    $filename = transliteration_clean_filename(basename($file->filepath));

    // Build destination path.
    $destination = dirname($file->filepath) . '/' . $filename;

    // Store original source path.
    $source = $file->filepath;

    // Rename and update the file record accordingly.
    if (file_move($file->filepath, $destination, FILE_EXISTS_RENAME)) {
      db_query("UPDATE {files} SET filepath = '%s' WHERE fid = %d", $file->filepath, $file->fid);

      // If possible, add a url redirect to handle old URL references.
      if (module_exists('path_redirect')) {
        $redirect = array(
          'source' => $source,
          'redirect' => $file->filepath,
        );
        path_redirect_save($redirect);
      }
      $count++;
    }
    else {
      $errors[] = $file->filepath;
    }
  }
  if ($errors) {
    $message = t('Not all file names could be converted. The following files could not be accessed and have been ignored:');
    $message .= theme('item_list', $errors);
    drupal_set_message($message, 'error');
  }
  else {
    drupal_set_message(t('@filenames have been successfully transliterated.', array(
      '@filenames' => format_plural($count, '1 file name', '@count file names'),
    )));
  }

  // Flush page cache.
  cache_clear_all();
}