You are here

public function TMGMTFileFormatHTML::validateImport in Translation Management Tool 7

Validates that the given file is valid and can be imported.

@todo this function should NOT return a job. We need a import processor instance instead to deal with the import context.

Parameters

string $imported_file: File path to the file to be imported.

bool $is_file: (optional) Whether $imported_file is the path to a file or not.

Return value

TMGMTJob Returns the corresponding translation job entity if the import file is valid, FALSE otherwise.

Overrides TMGMTFileFormatInterface::validateImport

File

translators/file/tmgmt_file.format.html.inc, line 64

Class

TMGMTFileFormatHTML
Export into HTML.

Code

public function validateImport($imported_file, $is_file = TRUE) {
  $dom = new DOMDocument();
  if (!$dom
    ->loadHTMLFile($imported_file)) {
    return FALSE;
  }
  $xml = simplexml_import_dom($dom);

  // Collect meta information.
  $meta_tags = $xml
    ->xpath('//meta');
  $meta = array();
  foreach ($meta_tags as $meta_tag) {
    $meta[(string) $meta_tag['name']] = (string) $meta_tag['content'];
  }

  // Check required meta tags.
  foreach (array(
    'JobID',
    'languageSource',
    'languageTarget',
  ) as $name) {
    if (!isset($meta[$name])) {
      return FALSE;
    }
  }

  // Attempt to load the job.
  if (!($job = tmgmt_job_load($meta['JobID']))) {
    drupal_set_message(t('The imported file job id @file_tjid is not available.', array(
      '@file_tjid' => $job->tjid,
    )), 'error');
    return FALSE;
  }

  // Check language.
  if ($meta['languageSource'] != $job
    ->getTranslator()
    ->mapToRemoteLanguage($job->source_language) || $meta['languageTarget'] != $job
    ->getTranslator()
    ->mapToRemoteLanguage($job->target_language)) {
    return FALSE;
  }

  // Validation successful.
  return $job;
}