You are here

public function LingotekSession::downloadTriggered in Lingotek Translation 7.6

Same name and namespace in other branches
  1. 7.7 lingotek.session.inc \LingotekSession::downloadTriggered()
  2. 7.5 lingotek.session.inc \LingotekSession::downloadTriggered()

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 178
Handles api calls, logging in and logging out of LingoTek

Class

LingotekSession

Code

public function downloadTriggered($api, $params) {
  $result = LingotekApi::instance()
    ->request($api, $params);
  $result_decoded = json_decode($result);

  // non-zipped will be decoded
  if (!$result) {
    LingotekLog::error('Unable to download Lingotek Document.', array());
    return FALSE;
  }
  elseif (isset($result_decoded->results)) {
    LingotekLog::error('Download Lingotek Document Failed. !error ', array(
      '!error' => $result,
    ));
    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;
}