You are here

function tmgmt_file_import_form_submit in Translation Management Tool 7

Same name and namespace in other branches
  1. 8 translators/tmgmt_file/tmgmt_file.module \tmgmt_file_import_form_submit()

Import form submit callback.

1 string reference to 'tmgmt_file_import_form_submit'
TMGMTFileTranslatorUIController::checkoutInfo in translators/file/tmgmt_file.ui.inc
Retrieves information about a translation job.

File

translators/file/tmgmt_file.module, line 37
Module file of the translation management test module.

Code

function tmgmt_file_import_form_submit($form, &$form_state) {

  // Ensure we have the file uploaded.
  $job = $form_state['tmgmt_job'];
  $supported_formats = array_keys(tmgmt_file_format_plugin_info());
  if ($file = file_save_upload('file', array(
    'file_validate_extensions' => array(
      implode(' ', $supported_formats),
    ),
  ))) {
    $extension = pathinfo($file->uri, PATHINFO_EXTENSION);
    $controller = tmgmt_file_format_controller($extension);
    if ($controller) {

      // Validate the file on job.
      $validated_job = $controller
        ->validateImport($file->uri, $job);
      if (!$validated_job) {
        $job
          ->addMessage('Failed to validate file, import aborted.', array(), 'error');
      }
      elseif ($validated_job->tjid != $job->tjid) {
        $job
          ->addMessage('The imported file job id @file_tjid does not match the job id @job_tjid.', array(
          '@file_tjid' => $validated_job->tjid,
          '@job_tjid' => $job->tjid,
        ), 'error');
      }
      else {
        try {

          // Validation successful, start import.
          $job
            ->addTranslatedData($controller
            ->import($file->uri));
          $job
            ->addMessage('Successfully imported file.');
        } catch (Exception $e) {
          $job
            ->addMessage('File import failed with the following message: @message', array(
            '@message' => $e
              ->getMessage(),
          ), 'error');
        }
      }
    }
  }
  foreach ($job
    ->getMessagesSince() as $message) {

    // Ignore debug messages.
    if ($message->type == 'debug') {
      continue;
    }
    if ($text = $message
      ->getMessage()) {
      drupal_set_message(filter_xss($text), $message->type);
    }
  }
}