public function Lingotek::uploadDocument in Lingotek Translation 3.0.x
Same name and namespace in other branches
- 8 src/Lingotek.php \Drupal\lingotek\Lingotek::uploadDocument()
- 8.2 src/Lingotek.php \Drupal\lingotek\Lingotek::uploadDocument()
- 4.0.x src/Lingotek.php \Drupal\lingotek\Lingotek::uploadDocument()
- 3.1.x src/Lingotek.php \Drupal\lingotek\Lingotek::uploadDocument()
- 3.2.x src/Lingotek.php \Drupal\lingotek\Lingotek::uploadDocument()
- 3.3.x src/Lingotek.php \Drupal\lingotek\Lingotek::uploadDocument()
- 3.4.x src/Lingotek.php \Drupal\lingotek\Lingotek::uploadDocument()
- 3.5.x src/Lingotek.php \Drupal\lingotek\Lingotek::uploadDocument()
- 3.6.x src/Lingotek.php \Drupal\lingotek\Lingotek::uploadDocument()
- 3.7.x src/Lingotek.php \Drupal\lingotek\Lingotek::uploadDocument()
- 3.8.x src/Lingotek.php \Drupal\lingotek\Lingotek::uploadDocument()
Uploads a document to the Lingotek service.
Parameters
string $title: The title of the document as it will be seen in the TMS.
string|array $content: The content of the document. It can be a json string or an array that will be json encoded.
string $locale: The Lingotek locale.
string $url: (optional) The document url in the site if any. This allows support for in-context review.
\Drupal\lingotek\LingotekProfileInterface $profile: (optional) The profile being used.
string $job_id: (optional) The job ID that will be associated.
Return value
string The document ID assigned to the uploaded document.
Throws
\Drupal\lingotek\Exception\LingotekPaymentRequiredException
\Drupal\lingotek\Exception\LingotekApiException
Overrides LingotekInterface::uploadDocument
File
- src/
Lingotek.php, line 220
Class
- Lingotek
- The connecting class between Drupal and Lingotek
Namespace
Drupal\lingotekCode
public function uploadDocument($title, $content, $locale, $url = NULL, LingotekProfileInterface $profile = NULL, $job_id = NULL) {
if (!is_array($content)) {
$data = json_decode($content, TRUE);
// This is the quickest way if $content is not a valid json object.
$content = $data === NULL ? $content : $data;
}
// Handle adding site defaults to the upload here, and leave
// the handling of the upload call itself to the API.
$defaults = [
'format' => 'JSON',
'project_id' => $this->configFactory
->get(static::SETTINGS)
->get('default.project'),
'fprm_id' => $this->lingotekFilterManager
->getFilterId($profile),
'fprm_subfilter_id' => $this->lingotekFilterManager
->getSubfilterId($profile),
'external_application_id' => 'e39e24c7-6c69-4126-946d-cf8fbff38ef0',
];
// Remove filters set to NULL
$defaults = array_filter($defaults);
$metadata = $this
->getIntelligenceMetadata($content);
if ($profile !== NULL && ($project = $profile
->getProject())) {
if ($project !== 'default') {
$defaults['project_id'] = $project;
}
}
if ($profile !== NULL && ($vault = $profile
->getVault())) {
if ($vault === 'default') {
$vault = $this->configFactory
->get(static::SETTINGS)
->get('default.vault');
}
$defaults['vault_id'] = $vault;
// If we use the project workflow template default vault, we omit the
// vault parameter and the TMS will decide.
if ($vault === 'project_workflow_vault') {
unset($defaults['vault_id']);
}
}
$args = array_merge($metadata, $defaults);
$args = array_merge([
'content' => json_encode($content),
'title' => $title,
'locale_code' => $locale,
], $args);
if ($url !== NULL) {
$args['external_url'] = $url;
}
if ($job_id !== NULL) {
$args['job_id'] = $job_id;
}
$response = $this->api
->addDocument($args);
$statusCode = $response
->getStatusCode();
if ($statusCode == Response::HTTP_ACCEPTED) {
$responseBody = Json::decode($response
->getBody(), TRUE);
if (!empty($responseBody) && !empty($responseBody['properties']['id'])) {
return $responseBody['properties']['id'];
}
else {
return FALSE;
}
}
elseif ($statusCode == Response::HTTP_PAYMENT_REQUIRED) {
// This is only applicable to subscription-based connectors, but the
// recommended action is to present the user with a message letting them
// know their Lingotek account has been disabled, and to please contact
// support to re-enable their account.
$responseBody = Json::decode($response
->getBody());
$message = '';
if (!empty($responseBody) && isset($responseBody['messages'])) {
$message = $responseBody['messages'][0];
}
throw new LingotekPaymentRequiredException($message);
}
else {
return FALSE;
}
return FALSE;
}