You are here

public function LingotekApi::downloadDocument in Lingotek Translation 7.3

Same name and namespace in other branches
  1. 7.7 lib/Drupal/lingotek/LingotekApi.php \LingotekApi::downloadDocument()
  2. 7.2 lib/Drupal/lingotek/LingotekApi.php \LingotekApi::downloadDocument()
  3. 7.4 lib/Drupal/lingotek/LingotekApi.php \LingotekApi::downloadDocument()
  4. 7.5 lib/Drupal/lingotek/LingotekApi.php \LingotekApi::downloadDocument()
  5. 7.6 lib/Drupal/lingotek/LingotekApi.php \LingotekApi::downloadDocument()

Downloads the translated document for the specified document and language.

Parameters

int $document_id: The Lingotek document ID that should be downloaded.

string $language_lingotek: A Lingotek language/locale code.

Return value

mixed On success, a SimpleXMLElement object representing the translated document. FALSE on failure.

File

lib/Drupal/lingotek/LingotekApi.php, line 434
Defines Drupal\lingotek\LingotekApi

Class

LingotekApi
@file Defines Drupal\lingotek\LingotekApi

Code

public function downloadDocument($document_id, $language_lingotek) {
  $document = FALSE;
  $params = array(
    'documentId' => $document_id,
    'targetLanguage' => $language_lingotek,
  );
  if ($results = $this
    ->request('downloadDocument', $params)) {
    try {

      // TODO: This is borrowed from the now-deprecated LingotekSession::download()
      // and could use refactoring.
      $tmpFile = tempnam(file_directory_temp(), 'lingotek');
      $fp = fopen($tmpFile, 'w');
      fwrite($fp, $results);
      fclose($fp);
      $text = '';
      $file = FALSE;

      // downloadDocument returns zip-encoded data.
      $zip = new ZipArchive();
      $zip
        ->open($tmpFile);
      $name = $zip
        ->getNameIndex(0);
      $file = $zip
        ->getStream($name);
      if ($file) {
        while (!feof($file)) {
          $text .= fread($file, 2);
        }
        fclose($file);
      }
      unlink($tmpFile);
      $document = new SimpleXMLElement($text);
    } catch (Exception $e) {
      LingotekLog::error('Unable to parse downloaded document. Error: @error. Text: !xml.', array(
        '!xml' => $text,
        '@error' => $e
          ->getMessage(),
      ));
    }
  }
  return $document;
}