You are here

public function LingotekSession::download in Lingotek Translation 7.2

Same name and namespace in other branches
  1. 6 lingotek.session.inc \LingotekSession::download()
  2. 7.3 lingotek.session.inc \LingotekSession::download()
  3. 7.4 lingotek.session.inc \LingotekSession::download()

Downloads a document from Lingotek.

Parameters

$api: Name of the api to call

$params: Key/value pairs to call as parameters in the api

Return value

Location of the file, or FALSE on error.

File

./lingotek.session.inc, line 155
Handles api calls, logging in and logging out of LingoTek

Class

LingotekSession

Code

public function download($api, $params) {
  $result = LingotekApi::instance()
    ->request($api, $params);
  if (!$result) {
    watchdog('lingotek', 'Unable to download Lingotek Document.', array(), WATCHDOG_ERROR);
    return FALSE;
  }
  $tmpFile = tempnam(file_directory_temp(), "lingotek");
  $fp = fopen($tmpFile, 'w');
  fwrite($fp, $result);
  fclose($fp);
  $text = "";
  $file = FALSE;

  //downloadDocument zips the file up
  if ($api == 'downloadDocument') {
    $zip = new ZipArchive();
    $zip
      ->open($tmpFile);
    $name = $zip
      ->getNameIndex(0);
    $file = $zip
      ->getStream($name);
  }
  else {
    $file = fopen($tmpFile, "r");
  }
  if ($file) {
    while (!feof($file)) {
      $text .= fread($file, 2);
    }
  }
  fclose($file);
  unlink($tmpFile);
  return $text;
}